]> git.ipfire.org Git - thirdparty/git.git/commitdiff
init: teach --separate-git-dir to repair linked worktrees
authorEric Sunshine <sunshine@sunshineco.com>
Mon, 31 Aug 2020 06:57:59 +0000 (02:57 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 31 Aug 2020 18:47:45 +0000 (11:47 -0700)
A linked worktree's .git file is a "gitfile" pointing at the
.git/worktrees/<id> directory within the repository. When `git init
--separate-git-dir=<path>` is used on an existing repository to relocate
the repository's .git/ directory to a different location, it neglects to
update the .git files of linked worktrees, thus breaking the worktrees
by making it impossible for them to locate the repository. Fix this by
teaching --separate-git-dir to repair the .git file of each linked
worktree to point at the new repository location.

Reported-by: Henré Botha <henrebotha@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/init-db.c
t/t0001-init.sh

index bbc9bc78f9d5a32ed9d167a79426ba0329bdea95..7b915d88aba7fc56e0eeafffcf869ac8c923a8fe 100644 (file)
@@ -9,6 +9,7 @@
 #include "builtin.h"
 #include "exec-cmd.h"
 #include "parse-options.h"
+#include "worktree.h"
 
 #ifndef DEFAULT_GIT_TEMPLATE_DIR
 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -364,6 +365,7 @@ static void separate_git_dir(const char *git_dir, const char *git_link)
 
                if (rename(src, git_dir))
                        die_errno(_("unable to move %s to %s"), src, git_dir);
+               repair_worktrees(NULL, NULL);
        }
 
        write_file(git_link, "gitdir: %s", git_dir);
index 50222a10c5b1c68d5509c7e4952bdde79bc1a86c..e489eb4ddb0fdf9e099cb7a0e2efecd954fd8b85 100755 (executable)
@@ -405,6 +405,17 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
        test_path_is_dir realgitdir/refs
 '
 
+test_expect_success 're-init to move gitdir with linked worktrees' '
+       test_when_finished "rm -rf mainwt linkwt seprepo" &&
+       git init mainwt &&
+       test_commit -C mainwt gumby &&
+       git -C mainwt worktree add --detach ../linkwt &&
+       git -C mainwt init --separate-git-dir ../seprepo &&
+       git -C mainwt rev-parse --git-common-dir >expect &&
+       git -C linkwt rev-parse --git-common-dir >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success MINGW '.git hidden' '
        rm -rf newdir &&
        (