mv: prevent mismatched data when ignoring errors.
authorbrian m. carlson <sandals@crustytoothpaste.net>
Sat, 15 Mar 2014 18:56:52 +0000 (18:56 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 Mar 2014 18:38:41 +0000 (11:38 -0700)
We shrink the source and destination arrays, but not the modes or
submodule_gitfile arrays, resulting in potentially mismatched data. Shrink
all the arrays at the same time to prevent this. Add tests to ensure the
problem does not recur.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/mv.c
t/t7001-mv.sh
index 525807722417ddf561423bd798dc15c50f2b50f9..45e57f307b799490a949d60b37f8cff1a1e5d07f 100644 (file)
@@ -231,6 +231,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                                        memmove(destination + i,
                                                destination + i + 1,
                                                (argc - i) * sizeof(char *));
+                                       memmove(modes + i, modes + i + 1,
+                                               (argc - i) * sizeof(enum update_mode));
+                                       memmove(submodule_gitfile + i,
+                                               submodule_gitfile + i + 1,
+                                               (argc - i) * sizeof(char *));
                                        i--;
                                }
                        } else
index 3bfdfed1f7774edb8a87864bdb6a021dc6a185ed..4023b6ec48729d1cd2c9e9809fd322852166d9f0 100755 (executable)
@@ -294,7 +294,8 @@ test_expect_success 'setup submodule' '
        git submodule add ./. sub &&
        echo content >file &&
        git add file &&
-       git commit -m "added sub and file"
+       git commit -m "added sub and file" &&
+       git branch submodule
 '
 
 test_expect_success 'git mv cannot move a submodule in a file' '
@@ -442,4 +443,14 @@ test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
        git diff-files --quiet -- sub .gitmodules
 '
 
+test_expect_success 'mv -k does not accidentally destroy submodules' '
+       git checkout submodule &&
+       mkdir dummy dest &&
+       git mv -k dummy sub dest &&
+       git status --porcelain >actual &&
+       grep "^R  sub -> dest/sub" actual &&
+       git reset --hard &&
+       git checkout .
+'
+
 test_done