branch: prepare delete_branches for a bulk caller

Teach delete_branches() a new mode for the upcoming --delete-merged caller that checks whether a branch is merged into its upstream without falling back to HEAD when there is no upstream. Existing callers keep their current behavior. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Harald Nordgren committed Jul 25, 2026 at 11:32 UTC 01df07c9df79770da4c0ef966dc7a22d305ef11f
1 file changed +9 -4
builtin/branch.c
+9 -4
@@ -168,10 +168,13 @@ static int branch_merged(int kind, const char *name,
168 * upstream, if any, otherwise with HEAD", we should just
169 * return the result of the repo_in_merge_bases() above without
170 * any of the following code, but during the transition period,
171 - * a gentle reminder is in order.
171 + * a gentle reminder is in order. Callers that opt out of the
172 + * HEAD fallback by passing head_rev=NULL are not interested in
173 + * the reminder either: they have already established that the
174 + * branch has an upstream, so HEAD is irrelevant to the decision.
175 */
173 - if (head_rev != reference_rev) {
174 - int expect = head_rev ? repo_in_merge_bases(the_repository, rev, head_rev) : 0;
176 + if (head_rev && head_rev != reference_rev) {
177 + int expect = repo_in_merge_bases(the_repository, rev, head_rev);
178 if (expect < 0)
179 exit(128);
180 if (expect == merged)
@@ -193,6 +196,7 @@ enum delete_branch_flags {
196 DELETE_BRANCH_FORCE = (1 << 0),
197 DELETE_BRANCH_QUIET = (1 << 1),
198 DELETE_BRANCH_SKIP_UNMERGED = (1 << 2),
199 + DELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),
200 };
201
202 static int check_branch_commit(const char *branchname, const char *refname,
@@ -262,7 +266,8 @@ static int delete_branches(int argc, const char **argv, int kinds,
266 }
267 branch_name_pos = strcspn(fmt, "%");
268
265 - if (!(flags & DELETE_BRANCH_FORCE))
269 + if (!(flags & DELETE_BRANCH_FORCE) &&
270 + !(flags & DELETE_BRANCH_NO_HEAD_FALLBACK))
271 head_rev = lookup_commit_reference(the_repository, &head_oid);
272
273 for (i = 0; i < argc; i++, strbuf_reset(&bname)) {