Skip to content

Commit 42264bc

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b214ab5 commit 42264bc

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

builtin/init-db.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "builtin.h"
1010
#include "exec-cmd.h"
1111
#include "parse-options.h"
12+
#include "worktree.h"
1213

1314
#ifndef DEFAULT_GIT_TEMPLATE_DIR
1415
#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)
364365

365366
if (rename(src, git_dir))
366367
die_errno(_("unable to move %s to %s"), src, git_dir);
368+
repair_worktrees(NULL, NULL);
367369
}
368370

369371
write_file(git_link, "gitdir: %s", git_dir);

t/t0001-init.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
405405
test_path_is_dir realgitdir/refs
406406
'
407407

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+
408419
test_expect_success MINGW '.git hidden' '
409420
rm -rf newdir &&
410421
(

0 commit comments

Comments
 (0)