summaryrefslogtreecommitdiff
path: root/src/worktree.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2015-10-21 11:48:02 +0200
committerPatrick Steinhardt <ps@pks.im>2017-02-13 10:28:15 +0100
commit45f2b7a43ffe77bac3acbf21a041b56f03842ba8 (patch)
tree8bc78ba8af8f5f7608c4e8b18bf42282cbfb4152 /src/worktree.c
parent854b5c70e3f8f4701d005cbd0623f0bef8d00060 (diff)
downloadlibgit2-45f2b7a43ffe77bac3acbf21a041b56f03842ba8.tar.gz
worktree: implement `git_worktree_list`
Add new module for working trees with the `git_worktree_list` function. The function lists names for all working trees of a certain repository.
Diffstat (limited to 'src/worktree.c')
-rw-r--r--src/worktree.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/worktree.c b/src/worktree.c
new file mode 100644
index 000000000..28d895d5c
--- /dev/null
+++ b/src/worktree.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "git2/worktree.h"
+
+#include "common.h"
+#include "repository.h"
+
+static bool is_worktree_dir(git_buf *dir)
+{
+ return git_path_contains_file(dir, "commondir")
+ && git_path_contains_file(dir, "gitdir")
+ && git_path_contains_file(dir, "HEAD");
+}
+
+int git_worktree_list(git_strarray *wts, git_repository *repo)
+{
+ git_vector worktrees = GIT_VECTOR_INIT;
+ git_buf path = GIT_BUF_INIT;
+ char *worktree;
+ unsigned i, len;
+ int error;
+
+ assert(wts && repo);
+
+ wts->count = 0;
+ wts->strings = NULL;
+
+ if ((error = git_buf_printf(&path, "%s/worktrees/", repo->commondir)) < 0)
+ goto exit;
+ if (!git_path_exists(path.ptr) || git_path_is_empty_dir(path.ptr))
+ goto exit;
+ if ((error = git_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0)
+ goto exit;
+
+ len = path.size;
+
+ git_vector_foreach(&worktrees, i, worktree) {
+ git_buf_truncate(&path, len);
+ git_buf_puts(&path, worktree);
+
+ if (!is_worktree_dir(&path)) {
+ git_vector_remove(&worktrees, i);
+ git__free(worktree);
+ }
+ }
+
+ wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees);
+
+exit:
+ git_buf_free(&path);
+
+ return error;
+}