diff options
author | Patrick Steinhardt <ps@pks.im> | 2019-02-14 13:05:49 +0100 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2019-02-14 14:01:27 +0100 |
commit | efb20825e16f1b924986be4c6bc0410ce64aaa81 (patch) | |
tree | 507fd0cf970f1ceb97c0ebf8bd32f80803c98d31 /src | |
parent | 788cd2d5f070c496e5f66467d92bfd714f2878f4 (diff) | |
download | libgit2-efb20825e16f1b924986be4c6bc0410ce64aaa81.tar.gz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/branch.c | 2 | ||||
-rw-r--r-- | src/refs.c | 2 | ||||
-rw-r--r-- | src/repository.c | 35 | ||||
-rw-r--r-- | src/repository.h | 11 |
4 files changed, 33 insertions, 17 deletions
diff --git a/src/branch.c b/src/branch.c index 516a87f31..30d29a60d 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 644bc2e68..a031842bf 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 26936a82f..2bfa57736 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 1edcc842b..450a8bf04 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. |