[PATCH 1/2] branch: move stacked branch helpers
- From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Aug 25, 2026, 21:25 UTC
- Message-ID
- <f7856e7a5b7b996be9380eb78d24c92b682a1fae.1787693117.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2365.git.git.1787693117.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>
Move the stacked branch helpers earlier so delete_branches() can use them without a forward declaration.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- builtin/branch.c | 104 +++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 52 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c index a613148fc7..87f0aa4051 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -203,6 +203,58 @@ enum delete_branch_flags { DELETE_BRANCH_DRY_RUN = (1 << 4), }; +struct stacked_branch_data { + struct strset *deletable_branch_names; + struct strset *protected_branch_names; +}; + +static int collect_stacked_branch_base(const struct reference *ref, + void *cb_data) +{ + struct stacked_branch_data *data = cb_data; + const char *branch_name; + struct branch *branch; + const char *upstream_refname; + const char *upstream_branch_name; + + if (!skip_prefix(ref->name, "refs/heads/", &branch_name)) + BUG("expected local branch ref, got '%s'", ref->name); + if (strset_contains(data->deletable_branch_names, branch_name)) + return 0; + + branch = branch_get(branch_name); + upstream_refname = branch_get_upstream(branch, NULL); + if (!upstream_refname || + !skip_prefix(upstream_refname, "refs/heads/", + &upstream_branch_name) || + !strset_contains(data->deletable_branch_names, + upstream_branch_name)) + return 0; + + strset_add(data->protected_branch_names, upstream_branch_name); + return 0; +} + +static void protect_stacked_branch_bases(struct ref_store *refs, + struct strset *deletable_branch_names, + struct strset *protected_branch_names) +{ + struct stacked_branch_data data = { + .deletable_branch_names = deletable_branch_names, + .protected_branch_names = protected_branch_names, + }; + struct refs_for_each_ref_options opts = { + .prefix = "refs/heads/", + }; + struct hashmap_iter iter; + struct strmap_entry *entry; + + refs_for_each_ref_ext(refs, collect_stacked_branch_base, &data, &opts); + + strset_for_each_entry(protected_branch_names, &iter, entry) + strset_remove(deletable_branch_names, entry->key); +} + static int check_branch_commit(const char *branchname, const char *refname, const struct object_id *oid, struct commit *head_rev, int kinds, unsigned int flags) @@ -718,58 +770,6 @@ static int parse_opt_forked(const struct option *opt, const char *arg, int unset return 0; } -struct stacked_branch_data { - struct strset *deletable_branch_names; - struct strset *protected_branch_names; -}; - -static int collect_stacked_branch_base(const struct reference *ref, - void *cb_data) -{ - struct stacked_branch_data *data = cb_data; - const char *branch_name; - struct branch *branch; - const char *upstream_refname; - const char *upstream_branch_name; - - if (!skip_prefix(ref->name, "refs/heads/", &branch_name)) - BUG("expected local branch ref, got '%s'", ref->name); - if (strset_contains(data->deletable_branch_names, branch_name)) - return 0; - - branch = branch_get(branch_name); - upstream_refname = branch_get_upstream(branch, NULL); - if (!upstream_refname || - !skip_prefix(upstream_refname, "refs/heads/", - &upstream_branch_name) || - !strset_contains(data->deletable_branch_names, - upstream_branch_name)) - return 0; - - strset_add(data->protected_branch_names, upstream_branch_name); - return 0; -} - -static void protect_stacked_branch_bases(struct ref_store *refs, - struct strset *deletable_branch_names, - struct strset *protected_branch_names) -{ - struct stacked_branch_data data = { - .deletable_branch_names = deletable_branch_names, - .protected_branch_names = protected_branch_names, - }; - struct refs_for_each_ref_options opts = { - .prefix = "refs/heads/", - }; - struct hashmap_iter iter; - struct strmap_entry *entry; - - refs_for_each_ref_ext(refs, collect_stacked_branch_base, &data, &opts); - - strset_for_each_entry(protected_branch_names, &iter, entry) - strset_remove(deletable_branch_names, entry->key); -} - static void clear_deleted_upstreams(struct strset *protected_branch_names, struct strset *deletable_branch_names) {
-- gitgitgadget