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 <cdwhite3@pm.me> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Caleb White committed
Nov 29, 2024 at 22:23 UTC
2037ca85ad93ec905b46543df6df4080f6ca258b
2 files changed
+26
-25
t/t0001-init.sh
+18
-4
@@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
434
sep_git_dir_worktree () {
435
test_when_finished "rm -rf mainwt linkwt seprepo" &&
436
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
443
test_commit -C mainwt gumby &&
444
git -C mainwt worktree add --detach ../linkwt &&
445
git -C "$1" init --separate-git-dir ../seprepo &&
@@ -442,12 +448,20 @@ sep_git_dir_worktree () {
448
test_cmp expect actual
449
}
450
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
461
'
462
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
465
'
466
467
test_expect_success MINGW '.git hidden' '
worktree.c
+8
-21
@@ -651,45 +651,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat
651
652
void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)
653
{
654
- struct strbuf path = STRBUF_INIT;
655
- struct strbuf repo = STRBUF_INIT;
654
struct strbuf gitdir = STRBUF_INIT;
655
struct strbuf dotgit = STRBUF_INIT;
658
- struct strbuf olddotgit = STRBUF_INIT;
659
- struct strbuf tmp = STRBUF_INIT;
656
+ int is_relative_path;
657
658
if (is_main_worktree(wt))
659
goto done;
660
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);
662
667
- if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
663
+ if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)
664
goto done;
665
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);
670
strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);
671
}
672
673
if (!file_exists(dotgit.buf))
674
goto done;
675
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);
677
done:
687
- strbuf_release(&path);
688
- strbuf_release(&repo);
678
strbuf_release(&gitdir);
679
strbuf_release(&dotgit);
691
- strbuf_release(&olddotgit);
692
- strbuf_release(&tmp);
680
}
681
682
void repair_worktrees_after_gitdir_move(const char *old_path)