Skip to content

Commit 78d986b

Browse files
pcloudsgitster
authored andcommitted
worktree move: refuse to move worktrees with submodules
Submodules contains .git files with relative paths. After a worktree move, these files need to be updated or they may point to nowhere. This is a bandage patch to make sure "worktree move" don't break people's worktrees by accident. When .git file update code is in place, this validate_no_submodules() could be removed. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c64a8d2 commit 78d986b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Documentation/git-worktree.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ with `--reason`.
7979
move::
8080

8181
Move a working tree to a new location. Note that the main working tree
82-
cannot be moved.
82+
or linked working trees containing submodules cannot be moved.
8383

8484
prune::
8585

builtin/worktree.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,27 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
606606
return ret;
607607
}
608608

609+
static void validate_no_submodules(const struct worktree *wt)
610+
{
611+
struct index_state istate = { NULL };
612+
int i, found_submodules = 0;
613+
614+
if (read_index_from(&istate, worktree_git_path(wt, "index")) > 0) {
615+
for (i = 0; i < istate.cache_nr; i++) {
616+
struct cache_entry *ce = istate.cache[i];
617+
618+
if (S_ISGITLINK(ce->ce_mode)) {
619+
found_submodules = 1;
620+
break;
621+
}
622+
}
623+
}
624+
discard_index(&istate);
625+
626+
if (found_submodules)
627+
die(_("working trees containing submodules cannot be moved"));
628+
}
629+
609630
static int move_worktree(int ac, const char **av, const char *prefix)
610631
{
611632
struct option options[] = {
@@ -643,6 +664,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
643664
if (file_exists(dst.buf))
644665
die(_("target '%s' already exists"), dst.buf);
645666

667+
validate_no_submodules(wt);
668+
646669
reason = is_worktree_locked(wt);
647670
if (reason) {
648671
if (*reason)

0 commit comments

Comments
 (0)