Commit 8f154be3 by Patrick Steinhardt

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.
parent 7cf7a407
......@@ -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);
......
......@@ -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/"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment