]> git.proxmox.com Git - libgit2.git/commitdiff
worktree: split off function opening working directory
authorPatrick Steinhardt <ps@pks.im>
Wed, 15 Mar 2017 12:54:14 +0000 (13:54 +0100)
committerPatrick Steinhardt <ps@pks.im>
Fri, 17 Mar 2017 07:56:18 +0000 (08:56 +0100)
Separate the logic of finding the worktree directory of a repository and
actually opening the working tree's directory. This is a preparatory
step for opening the worktree structure of a repository itself.

src/worktree.c

index 7977d8e7bed7858190b3aadba02ac18d6bb2f12f..ad1a6e597c0e1dafefab35ebaaa8cb48301eb454 100644 (file)
@@ -121,40 +121,61 @@ out:
        return err;
 }
 
-int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name)
+static int open_worktree_dir(git_worktree **out, const char *parent, const char *dir, const char *name)
 {
-       git_buf path = GIT_BUF_INIT;
+       git_buf gitdir = GIT_BUF_INIT;
        git_worktree *wt = NULL;
-       int error;
-
-       assert(repo && name);
-
-       *out = NULL;
-
-       if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0)
-               goto out;
+       int error = 0;
 
-       if (!is_worktree_dir(path.ptr)) {
+       if (!is_worktree_dir(dir)) {
                error = -1;
                goto out;
        }
 
-       if ((wt = git__malloc(sizeof(struct git_repository))) == NULL) {
+       if ((wt = git__calloc(1, sizeof(struct git_repository))) == NULL) {
                error = -1;
                goto out;
        }
 
        if ((wt->name = git__strdup(name)) == NULL
-           || (wt->commondir_path = git_worktree__read_link(path.ptr, "commondir")) == NULL
-           || (wt->gitlink_path = git_worktree__read_link(path.ptr, "gitdir")) == NULL
-           || (wt->parent_path = git__strdup(git_repository_path(repo))) == NULL) {
+           || (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL
+           || (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL
+           || (wt->parent_path = git__strdup(parent)) == NULL) {
                error = -1;
                goto out;
        }
-       wt->gitdir_path = git_buf_detach(&path);
+
+       if ((error = git_path_prettify_dir(&gitdir, dir, NULL)) < 0)
+               goto out;
+       wt->gitdir_path = git_buf_detach(&gitdir);
+
        wt->locked = !!git_worktree_is_locked(NULL, wt);
 
-       (*out) = wt;
+       *out = wt;
+
+out:
+       if (error)
+               git_worktree_free(wt);
+       git_buf_free(&gitdir);
+
+       return error;
+}
+
+int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name)
+{
+       git_buf path = GIT_BUF_INIT;
+       git_worktree *wt = NULL;
+       int error;
+
+       assert(repo && name);
+
+       *out = NULL;
+
+       if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0)
+               goto out;
+
+       if ((error = (open_worktree_dir(out, git_repository_path(repo), path.ptr, name))) < 0)
+               goto out;
 
 out:
        git_buf_free(&path);