]> git.proxmox.com Git - libgit2.git/commitdiff
repository: add function to iterate over all HEADs
authorPatrick Steinhardt <ps@pks.im>
Tue, 4 Apr 2017 16:44:29 +0000 (18:44 +0200)
committerPatrick Steinhardt <ps@pks.im>
Wed, 5 Apr 2017 11:50:38 +0000 (13:50 +0200)
While we already provide functions to get the current repository's HEAD,
it is quite involved to iterate over HEADs of both the repository and
all linked work trees. This commit implements a function
`git_repository_foreach_head`, which accepts a callback which is then
called for all HEAD files.

src/repository.c
src/repository.h

index 7eb5bed80f48ae376a52cb242d21ee41450c4658..707b7b7bd33337dd0f38073c814dc22cbde130e0 100644 (file)
@@ -2137,6 +2137,38 @@ out:
        return error;
 }
 
+int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload)
+{
+       git_strarray worktrees = GIT_VECTOR_INIT;
+       git_buf path = GIT_BUF_INIT;
+       int error;
+       size_t i;
+
+       /* Execute callback for HEAD of commondir */
+       if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
+           (error = cb(repo, path.ptr, payload) != 0))
+               goto out;
+
+       if ((error = git_worktree_list(&worktrees, repo)) < 0) {
+               error = 0;
+               goto out;
+       }
+
+       /* Execute callback for all worktree HEADs */
+       for (i = 0; i < worktrees.count; i++) {
+               if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
+                       continue;
+
+               if ((error = cb(repo, path.ptr, payload)) != 0)
+                       goto out;
+       }
+
+out:
+       git_buf_free(&path);
+       git_strarray_free(&worktrees);
+       return error;
+}
+
 int git_repository_head_unborn(git_repository *repo)
 {
        git_reference *ref = NULL;
index 33adfa60ac4bd02605f53e9d2f667ea856e75024..f922d00f23cc9b29a5191b1b8bf205db6f7e69ed 100644 (file)
@@ -159,6 +159,26 @@ GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo)
 int git_repository_head_tree(git_tree **tree, git_repository *repo);
 int git_repository_create_head(const char *git_dir, const char *ref_name);
 
+/*
+ * Called for each HEAD.
+ *
+ * Can return either 0, causing the iteration over HEADs to
+ * continue, or a non-0 value causing the iteration to abort. The
+ * return value is passed back to the caller of
+ * `git_repository_foreach_head`
+ */
+typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *path, void *payload);
+
+/*
+ * Iterate over repository and all worktree HEADs.
+ *
+ * This function will be called for the repository HEAD and for
+ * all HEADS of linked worktrees. For each HEAD, the callback is
+ * executed with the given payload. The return value equals the
+ * return value of the last executed callback function.
+ */
+int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload);
+
 /*
  * Weak pointers to repository internals.
  *