mv: prevent mismatched data when ignoring errors.

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>

brian m. carlson committed Mar 15, 2014 at 18:56 UTC fb8a4e8079ab8fc37e9cde32957c35637280ab8f
2 files changed +17 -1
builtin/mv.c
+5
@@ -231,6 +231,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
231 memmove(destination + i,
232 destination + i + 1,
233 (argc - i) * sizeof(char *));
234 + memmove(modes + i, modes + i + 1,
235 + (argc - i) * sizeof(enum update_mode));
236 + memmove(submodule_gitfile + i,
237 + submodule_gitfile + i + 1,
238 + (argc - i) * sizeof(char *));
239 i--;
240 }
241 } else
t/t7001-mv.sh
+12 -1
@@ -294,7 +294,8 @@ test_expect_success 'setup submodule' '
294 git submodule add ./. sub &&
295 echo content >file &&
296 git add file &&
297 - git commit -m "added sub and file"
297 + git commit -m "added sub and file" &&
298 + git branch submodule
299 '
300
301 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' '
443 git diff-files --quiet -- sub .gitmodules
444 '
445
446 +test_expect_success 'mv -k does not accidentally destroy submodules' '
447 + git checkout submodule &&
448 + mkdir dummy dest &&
449 + git mv -k dummy sub dest &&
450 + git status --porcelain >actual &&
451 + grep "^R sub -> dest/sub" actual &&
452 + git reset --hard &&
453 + git checkout .
454 +'
455 +
456 test_done