branches: introduce flag to skip enumeration of certain HEADs Right now, the function `git_repository_foreach_head` will always iterate over all HEADs of the main repository and its worktrees. In some cases, it might be required to skip either of those, though. Add a flag in preparation for the following commit that enables this behaviour.
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
diff --git a/src/branch.c b/src/branch.c
index 516a87f..30d29a6 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -159,7 +159,7 @@ int git_branch_is_checked_out(const git_reference *branch)
return 0;
return git_repository_foreach_head(git_reference_owner(branch),
- branch_equals, (void *) branch) == 1;
+ branch_equals, 0, (void *) branch) == 1;
}
int git_branch_delete(git_reference *branch)
diff --git a/src/refs.c b/src/refs.c
index 644bc2e..a031842 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -692,7 +692,7 @@ static int reference__rename(git_reference **out, git_reference *ref, const char
payload.old_name = ref->name;
memcpy(&payload.new_name, &normalized, sizeof(normalized));
- error = git_repository_foreach_head(repo, update_wt_heads, &payload);
+ error = git_repository_foreach_head(repo, update_wt_heads, 0, &payload);
}
return error;
diff --git a/src/repository.c b/src/repository.c
index 26936a8..2bfa577 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -2210,30 +2210,37 @@ out:
return error;
}
-int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload)
+int git_repository_foreach_head(git_repository *repo,
+ git_repository_foreach_head_cb cb,
+ int flags, 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;
+ if (!(flags & GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO)) {
+ /* Gather HEAD of main repository */
+ if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
+ (error = cb(repo, path.ptr, payload) != 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)
+ if (!(flags & GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES)) {
+ if ((error = git_worktree_list(&worktrees, repo)) < 0) {
+ error = 0;
goto out;
+ }
+
+ /* Gather HEADs of all worktrees */
+ 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:
diff --git a/src/repository.h b/src/repository.h
index 1edcc84..450a8bf 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -176,6 +176,13 @@ int git_repository_create_head(const char *git_dir, const char *ref_name);
*/
typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *path, void *payload);
+enum {
+ /* Skip enumeration of the main repository HEAD */
+ GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO = (1u << 0),
+ /* Skip enumeration of worktree HEADs */
+ GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES = (1u << 1),
+};
+
/*
* Iterate over repository and all worktree HEADs.
*
@@ -184,7 +191,9 @@ typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *
* 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);
+int git_repository_foreach_head(git_repository *repo,
+ git_repository_foreach_head_cb cb,
+ int flags, void *payload);
/*
* Weak pointers to repository internals.
diff --git a/tests/worktree/worktree.c b/tests/worktree/worktree.c
index 73a6a01..d41aa2c 100644
--- a/tests/worktree/worktree.c
+++ b/tests/worktree/worktree.c
@@ -604,8 +604,8 @@ void test_worktree_worktree__foreach_head_gives_same_results_in_wt_and_repo(void
cl_git_pass(git_reference_lookup(&heads[0], fixture.repo, GIT_HEAD_FILE));
cl_git_pass(git_reference_lookup(&heads[1], fixture.worktree, GIT_HEAD_FILE));
- cl_git_pass(git_repository_foreach_head(fixture.repo, read_head_ref, &repo_refs));
- cl_git_pass(git_repository_foreach_head(fixture.worktree, read_head_ref, &worktree_refs));
+ cl_git_pass(git_repository_foreach_head(fixture.repo, read_head_ref, 0, &repo_refs));
+ cl_git_pass(git_repository_foreach_head(fixture.worktree, read_head_ref, 0, &worktree_refs));
cl_assert_equal_i(repo_refs.length, ARRAY_SIZE(heads));
cl_assert_equal_i(worktree_refs.length, ARRAY_SIZE(heads));