mv: move submodules together with their work trees

Currently the attempt to use "git mv" on a submodule errors out with:

  fatal: source directory is empty, source=<src>, destination=<dest>

The reason is that mv searches for the submodule with a trailing slash in
the index, which it doesn't find (because it is stored without a trailing
slash). As it doesn't find any index entries inside the submodule it
claims the directory would be empty even though it isn't.

Fix that by searching for the name without a trailing slash and continue
if it is a submodule. Then rename() will move the submodule work tree just
like it moves a file.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jens Lehmann 2013-07-30 21:49:25 +02:00 committed by Junio C Hamano
parent 4838c81fab
commit 1150246828
2 changed files with 81 additions and 42 deletions

View File

@ -118,9 +118,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
&& lstat(dst, &st) == 0) && lstat(dst, &st) == 0)
bad = _("cannot move directory over file"); bad = _("cannot move directory over file");
else if (src_is_dir) { else if (src_is_dir) {
int first = cache_name_pos(src, length);
if (first >= 0) {
if (!S_ISGITLINK(active_cache[first]->ce_mode))
die (_("Huh? Directory %s is in index and no submodule?"), src);
} else {
const char *src_w_slash = add_slash(src); const char *src_w_slash = add_slash(src);
int len_w_slash = length + 1; int last, len_w_slash = length + 1;
int first, last;
modes[i] = WORKING_DIRECTORY; modes[i] = WORKING_DIRECTORY;
@ -168,6 +172,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
} }
argc += last - first; argc += last - first;
} }
}
} else if (cache_name_pos(src, length) < 0) } else if (cache_name_pos(src, length) < 0)
bad = _("not under version control"); bad = _("not under version control");
else if (lstat(dst, &st) == 0) { else if (lstat(dst, &st) == 0) {

View File

@ -259,4 +259,38 @@ test_expect_success SYMLINKS 'check moved symlink' '
rm -f moved symlink rm -f moved symlink
test_expect_success 'setup submodule' '
git commit -m initial &&
git reset --hard &&
git submodule add ./. sub &&
echo content >file &&
git add file &&
git commit -m "added sub and file"
'
test_expect_success 'git mv cannot move a submodule in a file' '
test_must_fail git mv sub file
'
test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
entry="$(git ls-files --stage sub | cut -f 1)" &&
git rm .gitmodules &&
(
cd sub &&
rm -f .git &&
cp -a ../.git/modules/sub .git &&
GIT_WORK_TREE=. git config --unset core.worktree
) &&
mkdir mod &&
git mv sub mod/sub &&
! test -e sub &&
[ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
(
cd mod/sub &&
git status
) &&
git update-index --refresh &&
git diff-files --quiet
'
test_done test_done