branch: convert delete_branches() to a flags argument

delete_branches() takes separate force and quiet parameters, while check_branch_commit() takes force. The next commits would grow this collection further. Replace them with a single unsigned flags argument and an enum. Test the FORCE and QUIET bits directly from flags at each use site so that mutating or forwarding flags cannot leave cached values stale. No change in behavior. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Harald Nordgren committed Jul 30, 2026 at 13:58 UTC 7cd45661ad29fc90dbf0718ec60a9b3211701c08
1 file changed +24 -16
builtin/branch.c
+24 -16
@@ -189,16 +189,22 @@ static int branch_merged(int kind, const char *name,
189 return merged;
190 }
191
192 +enum delete_branch_flags {
193 + DELETE_BRANCH_FORCE = (1 << 0),
194 + DELETE_BRANCH_QUIET = (1 << 1),
195 +};
196 +
197 static int check_branch_commit(const char *branchname, const char *refname,
198 const struct object_id *oid, struct commit *head_rev,
194 - int kinds, int force)
199 + int kinds, unsigned int flags)
200 {
201 struct commit *rev = lookup_commit_reference(the_repository, oid);
197 - if (!force && !rev) {
202 + if (!(flags & DELETE_BRANCH_FORCE) && !rev) {
203 error(_("couldn't look up commit object for '%s'"), refname);
204 return -1;
205 }
201 - if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
206 + if (!(flags & DELETE_BRANCH_FORCE) &&
207 + !branch_merged(kinds, branchname, rev, head_rev)) {
208 error(_("the branch '%s' is not fully merged"), branchname);
209 advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH,
210 _("If you are sure you want to delete it, "
@@ -217,8 +223,8 @@ static void delete_branch_config(const char *branchname)
223 strbuf_release(&buf);
224 }
225
220 -static int delete_branches(int argc, const char **argv, int force, int kinds,
221 - int quiet)
226 +static int delete_branches(int argc, const char **argv, int kinds,
227 + unsigned int flags)
228 {
229 struct commit *head_rev = NULL;
230 struct object_id oid;
@@ -241,7 +247,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
247 remote_branch = 1;
248 allowed_interpret = INTERPRET_BRANCH_REMOTE;
249
244 - force = 1;
250 + flags |= DELETE_BRANCH_FORCE;
251 break;
252 case FILTER_REFS_BRANCHES:
253 fmt = "refs/heads/%s";
@@ -252,12 +258,12 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
258 }
259 branch_name_pos = strcspn(fmt, "%");
260
255 - if (!force)
261 + if (!(flags & DELETE_BRANCH_FORCE))
262 head_rev = lookup_commit_reference(the_repository, &head_oid);
263
264 for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
265 char *target = NULL;
260 - int flags = 0;
266 + int ref_flags = 0;
267
268 copy_branchname(the_repository, &bname,
269 argv[i], allowed_interpret);
@@ -280,7 +286,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
286 RESOLVE_REF_READING
287 | RESOLVE_REF_NO_RECURSE
288 | RESOLVE_REF_ALLOW_BAD_NAME,
283 - &oid, &flags);
289 + &oid, &ref_flags);
290 if (!target) {
291 if (remote_branch) {
292 error(_("remote-tracking branch '%s' not found"), bname.buf);
@@ -292,7 +298,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
298 | RESOLVE_REF_NO_RECURSE
299 | RESOLVE_REF_ALLOW_BAD_NAME,
300 &oid,
295 - &flags);
301 + &ref_flags);
302 FREE_AND_NULL(virtual_name);
303
304 if (virtual_target)
@@ -307,16 +313,16 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
313 continue;
314 }
315
310 - if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
316 + if (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
317 check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
312 - force)) {
318 + flags)) {
319 ret = 1;
320 goto next;
321 }
322
323 item = string_list_append(&refs_to_delete, name);
318 - item->util = xstrdup((flags & REF_ISBROKEN) ? "broken"
319 - : (flags & REF_ISSYMREF) ? target
324 + item->util = xstrdup((ref_flags & REF_ISBROKEN) ? "broken"
325 + : (ref_flags & REF_ISSYMREF) ? target
326 : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV));
327
328 next:
@@ -331,7 +337,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
337 char *name = item->string;
338 if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
339 char *refname = name + branch_name_pos;
334 - if (!quiet)
340 + if (!(flags & DELETE_BRANCH_QUIET))
341 printf(remote_branch
342 ? _("Deleted remote-tracking branch %s (was %s).\n")
343 : _("Deleted branch %s (was %s).\n"),
@@ -896,7 +902,9 @@ int cmd_branch(int argc,
902 if (delete) {
903 if (!argc)
904 die(_("branch name required"));
899 - ret = delete_branches(argc, argv, delete > 1, filter.kind, quiet);
905 + ret = delete_branches(argc, argv, filter.kind,
906 + (delete > 1 ? DELETE_BRANCH_FORCE : 0) |
907 + (quiet ? DELETE_BRANCH_QUIET : 0));
908 goto out;
909 } else if (show_current) {
910 print_current_branch_name();