worktree move: refuse to move worktrees with submodules
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Mon, 12 Feb 2018 09:49:38 +0000 (16:49 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 12 Feb 2018 21:13:35 +0000 (13:13 -0800)
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 <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-worktree.txt
builtin/worktree.c
index 4fa1dd3a487fbf4f81eb001e5245bb71b8461771..e6764ee8e063720dae08a09ce6a373829575887e 100644 (file)
@@ -79,7 +79,7 @@ with `--reason`.
 move::
 
 Move a working tree to a new location. Note that the main working tree
-cannot be moved.
+or linked working trees containing submodules cannot be moved.
 
 prune::
 
index 6fe41313c9a78b57566c2499a1e65abea93ae4f0..4789cebe110112cd953a6beee31f678cafea4b77 100644 (file)
@@ -606,6 +606,27 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
        return ret;
 }
 
+static void validate_no_submodules(const struct worktree *wt)
+{
+       struct index_state istate = { NULL };
+       int i, found_submodules = 0;
+
+       if (read_index_from(&istate, worktree_git_path(wt, "index")) > 0) {
+               for (i = 0; i < istate.cache_nr; i++) {
+                       struct cache_entry *ce = istate.cache[i];
+
+                       if (S_ISGITLINK(ce->ce_mode)) {
+                               found_submodules = 1;
+                               break;
+                       }
+               }
+       }
+       discard_index(&istate);
+
+       if (found_submodules)
+               die(_("working trees containing submodules cannot be moved"));
+}
+
 static int move_worktree(int ac, const char **av, const char *prefix)
 {
        struct option options[] = {
@@ -643,6 +664,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
        if (file_exists(dst.buf))
                die(_("target '%s' already exists"), dst.buf);
 
+       validate_no_submodules(wt);
+
        reason = is_worktree_locked(wt);
        if (reason) {
                if (*reason)