Skip to content

Commit 298327f

Browse files
calebdwttaylorr
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). This also adds a test case for reinitializing a repository that has relative worktrees. Signed-off-by: Caleb White <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 30e9e41 commit 298327f

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+
git -C mainwt config worktree.useRelativePaths true
440+
else
441+
git -C mainwt config 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
@@ -653,45 +653,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat
653653

654654
void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)
655655
{
656-
struct strbuf path = STRBUF_INIT;
657-
struct strbuf repo = STRBUF_INIT;
658656
struct strbuf gitdir = STRBUF_INIT;
659657
struct strbuf dotgit = STRBUF_INIT;
660-
struct strbuf olddotgit = STRBUF_INIT;
661-
struct strbuf tmp = STRBUF_INIT;
658+
int is_relative_path;
662659

663660
if (is_main_worktree(wt))
664661
goto done;
665662

666-
strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
667-
strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
663+
strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
668664

669-
if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
665+
if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)
670666
goto done;
671667

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

680675
if (!file_exists(dotgit.buf))
681676
goto done;
682677

683-
strbuf_addbuf(&path, &dotgit);
684-
strbuf_strip_suffix(&path, "/.git");
685-
686-
write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
687-
write_file(gitdir.buf, "%s", relative_path(dotgit.buf, repo.buf, &tmp));
678+
write_worktree_linking_files(dotgit, gitdir, is_relative_path);
688679
done:
689-
strbuf_release(&path);
690-
strbuf_release(&repo);
691680
strbuf_release(&gitdir);
692681
strbuf_release(&dotgit);
693-
strbuf_release(&olddotgit);
694-
strbuf_release(&tmp);
695682
}
696683

697684
void repair_worktrees_after_gitdir_move(const char *old_path)

0 commit comments

Comments
 (0)