worktree: split off function opening working directory 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.
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 76 77 78 79 80 81 82 83
diff --git a/src/worktree.c b/src/worktree.c
index 7977d8e..ad1a6e5 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -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);