worktree: write resolved paths into link files The three link files "worktree/.git", ".git/worktrees/<name>/commondir" and ".git/worktrees/<name>/gitdir" should always contain absolute and resolved paths. Adjust the logic creating new worktrees to first use `git_path_prettify_dir` before writing out these files, so that paths are resolved first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
diff --git a/src/worktree.c b/src/worktree.c
index 2b0ebfb..393a088 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -272,7 +272,7 @@ out:
int git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *worktree)
{
- git_buf gitdir = GIT_BUF_INIT, buf = GIT_BUF_INIT;
+ git_buf gitdir = GIT_BUF_INIT, wddir = GIT_BUF_INIT, buf = GIT_BUF_INIT;
git_reference *ref = NULL, *head = NULL;
git_commit *commit = NULL;
git_repository *wt = NULL;
@@ -293,23 +293,27 @@ int git_worktree_add(git_worktree **out, git_repository *repo, const char *name,
goto out;
if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
goto out;
+ if ((err = git_path_prettify_dir(&gitdir, gitdir.ptr, NULL)) < 0)
+ goto out;
/* Create worktree work dir */
if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0)
goto out;
+ if ((err = git_path_prettify_dir(&wddir, worktree, NULL)) < 0)
+ goto out;
/* Create worktree .git file */
if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0)
goto out;
- if ((err = write_wtfile(worktree, ".git", &buf)) < 0)
+ if ((err = write_wtfile(wddir.ptr, ".git", &buf)) < 0)
goto out;
/* Create gitdir files */
- if ((err = git_buf_sets(&buf, repo->commondir)) < 0
+ if ((err = git_path_prettify_dir(&buf, repo->commondir, NULL) < 0)
|| (err = git_buf_putc(&buf, '\n')) < 0
|| (err = write_wtfile(gitdir.ptr, "commondir", &buf)) < 0)
goto out;
- if ((err = git_buf_joinpath(&buf, worktree, ".git")) < 0
+ if ((err = git_buf_joinpath(&buf, wddir.ptr, ".git")) < 0
|| (err = git_buf_putc(&buf, '\n')) < 0
|| (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0)
goto out;
@@ -325,7 +329,7 @@ int git_worktree_add(git_worktree **out, git_repository *repo, const char *name,
/* Set worktree's HEAD */
if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0)
goto out;
- if ((err = git_repository_open(&wt, worktree)) < 0)
+ if ((err = git_repository_open(&wt, wddir.ptr)) < 0)
goto out;
/* Checkout worktree's HEAD */
@@ -339,6 +343,7 @@ int git_worktree_add(git_worktree **out, git_repository *repo, const char *name,
out:
git_buf_free(&gitdir);
+ git_buf_free(&wddir);
git_buf_free(&buf);
git_reference_free(ref);
git_reference_free(head);
diff --git a/tests/worktree/worktree.c b/tests/worktree/worktree.c
index e74647f..6e90e6a 100644
--- a/tests/worktree/worktree.c
+++ b/tests/worktree/worktree.c
@@ -306,7 +306,9 @@ void test_worktree_worktree__init_submodule(void)
cl_git_pass(git_worktree_add(&worktree, sm, "repo-worktree", path.ptr));
cl_git_pass(git_repository_open_from_worktree(&wt, worktree));
+ cl_git_pass(git_path_prettify_dir(&path, path.ptr, NULL));
cl_assert_equal_s(path.ptr, wt->workdir);
+ cl_git_pass(git_path_prettify_dir(&path, sm->commondir, NULL));
cl_assert_equal_s(sm->commondir, wt->commondir);
cl_git_pass(git_buf_joinpath(&path, sm->gitdir, "worktrees/repo-worktree/"));