]> git.proxmox.com Git - libgit2.git/commitdiff
worktree: write resolved paths into link files
authorPatrick Steinhardt <ps@pks.im>
Fri, 17 Mar 2017 07:13:59 +0000 (08:13 +0100)
committerPatrick Steinhardt <ps@pks.im>
Fri, 17 Mar 2017 08:27:56 +0000 (09:27 +0100)
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.

src/worktree.c
tests/worktree/worktree.c

index 2b0ebfb830e46c35418942a721d1af0d78cd08cc..393a088fe1a059bc4d551cbfd4ac9810eeded8da 100644 (file)
@@ -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);
index e74647f54d686159dfa533b2eff914bfd949dcc7..6e90e6ac04af1cbab9b7245efbd119e0c50455d1 100644 (file)
@@ -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/"));