Skip to content

Commit efb2082

Browse files
committed
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 parent 788cd2d commit efb2082

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

src/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ int git_branch_is_checked_out(const git_reference *branch)
159159
return 0;
160160

161161
return git_repository_foreach_head(git_reference_owner(branch),
162-
branch_equals, (void *) branch) == 1;
162+
branch_equals, 0, (void *) branch) == 1;
163163
}
164164

165165
int git_branch_delete(git_reference *branch)

src/refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ static int reference__rename(git_reference **out, git_reference *ref, const char
692692
payload.old_name = ref->name;
693693
memcpy(&payload.new_name, &normalized, sizeof(normalized));
694694

695-
error = git_repository_foreach_head(repo, update_wt_heads, &payload);
695+
error = git_repository_foreach_head(repo, update_wt_heads, 0, &payload);
696696
}
697697

698698
return error;

src/repository.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,30 +2210,37 @@ int git_repository_head_for_worktree(git_reference **out, git_repository *repo,
22102210
return error;
22112211
}
22122212

2213-
int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload)
2213+
int git_repository_foreach_head(git_repository *repo,
2214+
git_repository_foreach_head_cb cb,
2215+
int flags, void *payload)
22142216
{
22152217
git_strarray worktrees = GIT_VECTOR_INIT;
22162218
git_buf path = GIT_BUF_INIT;
22172219
int error;
22182220
size_t i;
22192221

2220-
/* Execute callback for HEAD of commondir */
2221-
if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
2222-
(error = cb(repo, path.ptr, payload) != 0))
2223-
goto out;
22242222

2225-
if ((error = git_worktree_list(&worktrees, repo)) < 0) {
2226-
error = 0;
2227-
goto out;
2223+
if (!(flags & GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO)) {
2224+
/* Gather HEAD of main repository */
2225+
if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
2226+
(error = cb(repo, path.ptr, payload) != 0))
2227+
goto out;
22282228
}
22292229

2230-
/* Execute callback for all worktree HEADs */
2231-
for (i = 0; i < worktrees.count; i++) {
2232-
if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
2233-
continue;
2234-
2235-
if ((error = cb(repo, path.ptr, payload)) != 0)
2230+
if (!(flags & GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES)) {
2231+
if ((error = git_worktree_list(&worktrees, repo)) < 0) {
2232+
error = 0;
22362233
goto out;
2234+
}
2235+
2236+
/* Gather HEADs of all worktrees */
2237+
for (i = 0; i < worktrees.count; i++) {
2238+
if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
2239+
continue;
2240+
2241+
if ((error = cb(repo, path.ptr, payload)) != 0)
2242+
goto out;
2243+
}
22372244
}
22382245

22392246
out:

src/repository.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ int git_repository_create_head(const char *git_dir, const char *ref_name);
176176
*/
177177
typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *path, void *payload);
178178

179+
enum {
180+
/* Skip enumeration of the main repository HEAD */
181+
GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO = (1u << 0),
182+
/* Skip enumeration of worktree HEADs */
183+
GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES = (1u << 1),
184+
};
185+
179186
/*
180187
* Iterate over repository and all worktree HEADs.
181188
*
@@ -184,7 +191,9 @@ typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *
184191
* executed with the given payload. The return value equals the
185192
* return value of the last executed callback function.
186193
*/
187-
int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload);
194+
int git_repository_foreach_head(git_repository *repo,
195+
git_repository_foreach_head_cb cb,
196+
int flags, void *payload);
188197

189198
/*
190199
* Weak pointers to repository internals.

tests/worktree/worktree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ void test_worktree_worktree__foreach_head_gives_same_results_in_wt_and_repo(void
604604
cl_git_pass(git_reference_lookup(&heads[0], fixture.repo, GIT_HEAD_FILE));
605605
cl_git_pass(git_reference_lookup(&heads[1], fixture.worktree, GIT_HEAD_FILE));
606606

607-
cl_git_pass(git_repository_foreach_head(fixture.repo, read_head_ref, &repo_refs));
608-
cl_git_pass(git_repository_foreach_head(fixture.worktree, read_head_ref, &worktree_refs));
607+
cl_git_pass(git_repository_foreach_head(fixture.repo, read_head_ref, 0, &repo_refs));
608+
cl_git_pass(git_repository_foreach_head(fixture.worktree, read_head_ref, 0, &worktree_refs));
609609

610610
cl_assert_equal_i(repo_refs.length, ARRAY_SIZE(heads));
611611
cl_assert_equal_i(worktree_refs.length, ARRAY_SIZE(heads));

0 commit comments

Comments
 (0)