Skip to content

Commit

Permalink
mv: prevent mismatched data when ignoring errors.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
brian m. carlson authored and Junio C Hamano committed Mar 17, 2014
1 parent 89ccc1b commit fb8a4e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions builtin/mv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion t/t7001-mv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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' '
Expand Down Expand Up @@ -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

0 comments on commit fb8a4e8

Please sign in to comment.