@matheusbsbs / matheus-bagliano / commits / 42264bc841
Fork 0

init: teach --separate-git-dir to repair linked worktrees

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>

Eric Sunshine committed Aug 31, 2020 at 02:57 UTC 42264bc841d416aab5a980cb4eeedf687f1190a5
2 files changed +13
builtin/init-db.c
+2
@@ -9,6 +9,7 @@
9 #include "builtin.h"
10 #include "exec-cmd.h"
11 #include "parse-options.h"
12 +#include "worktree.h"
13
14 #ifndef DEFAULT_GIT_TEMPLATE_DIR
15 #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)
365
366 if (rename(src, git_dir))
367 die_errno(_("unable to move %s to %s"), src, git_dir);
368 + repair_worktrees(NULL, NULL);
369 }
370
371 write_file(git_link, "gitdir: %s", git_dir);
t/t0001-init.sh
+11
@@ -405,6 +405,17 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
405 test_path_is_dir realgitdir/refs
406 '
407
408 +test_expect_success 're-init to move gitdir with linked worktrees' '
409 + test_when_finished "rm -rf mainwt linkwt seprepo" &&
410 + git init mainwt &&
411 + test_commit -C mainwt gumby &&
412 + git -C mainwt worktree add --detach ../linkwt &&
413 + git -C mainwt init --separate-git-dir ../seprepo &&
414 + git -C mainwt rev-parse --git-common-dir >expect &&
415 + git -C linkwt rev-parse --git-common-dir >actual &&
416 + test_cmp expect actual
417 +'
418 +
419 test_expect_success MINGW '.git hidden' '
420 rm -rf newdir &&
421 (