Skip to content

Commit 2037ca8

Browse files
calebdwgitster
authored andcommitted
worktree: refactor repair_worktree_after_gitdir_move()
This refactors `repair_worktree_after_gitdir_move()` to use the new `write_worktree_linking_files` function. It also preserves the relativity of the linking files; e.g., if an existing worktree used absolute paths then the repaired paths will be absolute (and visa-versa). `repair_worktree_after_gitdir_move()` is used to repair both sets of worktree linking files if the `.git` directory is moved during a re-initialization using `git init`. This also adds a test case for reinitializing a repository that has relative worktrees. Signed-off-by: Caleb White <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e6df1ee commit 2037ca8

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

t/t0001-init.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
434434
sep_git_dir_worktree () {
435435
test_when_finished "rm -rf mainwt linkwt seprepo" &&
436436
git init mainwt &&
437+
if test "relative" = $2
438+
then
439+
test_config -C mainwt worktree.useRelativePaths true
440+
else
441+
test_config -C mainwt worktree.useRelativePaths false
442+
fi
437443
test_commit -C mainwt gumby &&
438444
git -C mainwt worktree add --detach ../linkwt &&
439445
git -C "$1" init --separate-git-dir ../seprepo &&
@@ -442,12 +448,20 @@ sep_git_dir_worktree () {
442448
test_cmp expect actual
443449
}
444450

445-
test_expect_success 're-init to move gitdir with linked worktrees' '
446-
sep_git_dir_worktree mainwt
451+
test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '
452+
sep_git_dir_worktree mainwt absolute
453+
'
454+
455+
test_expect_success 're-init to move gitdir within linked worktree (absolute)' '
456+
sep_git_dir_worktree linkwt absolute
457+
'
458+
459+
test_expect_success 're-init to move gitdir with linked worktrees (relative)' '
460+
sep_git_dir_worktree mainwt relative
447461
'
448462

449-
test_expect_success 're-init to move gitdir within linked worktree' '
450-
sep_git_dir_worktree linkwt
463+
test_expect_success 're-init to move gitdir within linked worktree (relative)' '
464+
sep_git_dir_worktree linkwt relative
451465
'
452466

453467
test_expect_success MINGW '.git hidden' '

worktree.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -651,45 +651,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat
651651

652652
void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)
653653
{
654-
struct strbuf path = STRBUF_INIT;
655-
struct strbuf repo = STRBUF_INIT;
656654
struct strbuf gitdir = STRBUF_INIT;
657655
struct strbuf dotgit = STRBUF_INIT;
658-
struct strbuf olddotgit = STRBUF_INIT;
659-
struct strbuf tmp = STRBUF_INIT;
656+
int is_relative_path;
660657

661658
if (is_main_worktree(wt))
662659
goto done;
663660

664-
strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
665-
strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
661+
strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
666662

667-
if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
663+
if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)
668664
goto done;
669665

670-
strbuf_rtrim(&olddotgit);
671-
if (is_absolute_path(olddotgit.buf)) {
672-
strbuf_addbuf(&dotgit, &olddotgit);
673-
} else {
674-
strbuf_addf(&dotgit, "%s/worktrees/%s/%s", old_path, wt->id, olddotgit.buf);
666+
strbuf_rtrim(&dotgit);
667+
is_relative_path = ! is_absolute_path(dotgit.buf);
668+
if (is_relative_path) {
669+
strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id);
675670
strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);
676671
}
677672

678673
if (!file_exists(dotgit.buf))
679674
goto done;
680675

681-
strbuf_addbuf(&path, &dotgit);
682-
strbuf_strip_suffix(&path, "/.git");
683-
684-
write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
685-
write_file(gitdir.buf, "%s", relative_path(dotgit.buf, repo.buf, &tmp));
676+
write_worktree_linking_files(dotgit, gitdir, is_relative_path);
686677
done:
687-
strbuf_release(&path);
688-
strbuf_release(&repo);
689678
strbuf_release(&gitdir);
690679
strbuf_release(&dotgit);
691-
strbuf_release(&olddotgit);
692-
strbuf_release(&tmp);
693680
}
694681

695682
void repair_worktrees_after_gitdir_move(const char *old_path)

0 commit comments

Comments
 (0)