git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH PARSEOPT 2/4] Use OPT_SET_INT and OPT_BIT in builtin-branch

From
Pierre Habouzit <madcoder@debian.org>
Date
Nov 7, 2007, 10:20 UTC
Message-ID
<1194430832-6224-4-git-send-email-madcoder@debian.org>
In-Reply-To
<1194430832-6224-3-git-send-email-madcoder@debian.org>
Also remove a spurious after-check on --abbrev (OPT__ABBREV already takes
care of that)
---
 builtin-branch.c |   44 ++++++++++++++++----------------------------
 1 files changed, 16 insertions(+), 28 deletions(-)
diff --git a/builtin-branch.c b/builtin-branch.c
index 3bf40f1..2694c9c 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -507,48 +507,36 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
-	int delete = 0, force_delete = 0, force_create = 0;
-	int rename = 0, force_rename = 0;
+	int delete = 0, rename = 0, force_create = 0;
 	int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
 	int reflog = 0, track;
-	int kinds = REF_LOCAL_BRANCH, kind_remote = 0, kind_any = 0;
+	int kinds = REF_LOCAL_BRANCH;
 
 	struct option options[] = {
 		OPT_GROUP("Generic options"),
 		OPT__VERBOSE(&verbose),
 		OPT_BOOLEAN( 0 , "track",  &track, "set up tracking mode (see git-pull(1))"),
 		OPT_BOOLEAN( 0 , "color",  &branch_use_color, "use colored output"),
-		OPT_BOOLEAN('r', NULL,     &kind_remote, "act on remote-tracking branches"),
+		OPT_SET_INT('r', NULL,     &kinds, "act on remote-tracking branches",
+			REF_REMOTE_BRANCH),
 		OPT__ABBREV(&abbrev),
 
 		OPT_GROUP("Specific git-branch actions:"),
-		OPT_BOOLEAN('a', NULL,     &kind_any, "list both remote-tracking and local branches"),
-		OPT_BOOLEAN('d', NULL,     &delete, "delete fully merged branch"),
-		OPT_BOOLEAN('D', NULL,     &force_delete, "delete branch (even if not merged)"),
-		OPT_BOOLEAN('l', NULL,     &reflog, "create the branch's reflog"),
-		OPT_BOOLEAN('f', NULL,     &force_create, "force creation (when already exists)"),
-		OPT_BOOLEAN('m', NULL,     &rename, "move/rename a branch and its reflog"),
-		OPT_BOOLEAN('M', NULL,     &force_rename, "move/rename a branch, even if target exists"),
+		OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches",
+			REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
+		OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1),
+		OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
+		OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1),
+		OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
+		OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"),
+		OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"),
 		OPT_END(),
 	};
 
 	git_config(git_branch_config);
 	track = branch_track;
 	argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
-
-	delete |= force_delete;
-	rename |= force_rename;
-	if (kind_remote)
-		kinds = REF_REMOTE_BRANCH;
-	if (kind_any)
-		kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
-	if (abbrev && abbrev < MINIMUM_ABBREV)
-		abbrev = MINIMUM_ABBREV;
-	else if (abbrev > 40)
-		abbrev = 40;
-
-	if ((delete && rename) || (delete && force_create) ||
-	    (rename && force_create))
+	if (!!delete + !!rename + !!force_create > 1)
 		usage_with_options(builtin_branch_usage, options);
 
 	head = resolve_ref("HEAD", head_sha1, 0, NULL);
@@ -564,13 +552,13 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	}
 
 	if (delete)
-		return delete_branches(argc, argv, force_delete, kinds);
+		return delete_branches(argc, argv, delete > 1, kinds);
 	else if (argc == 0)
 		print_ref_list(kinds, detached, verbose, abbrev);
 	else if (rename && (argc == 1))
-		rename_branch(head, argv[0], force_rename);
+		rename_branch(head, argv[0], rename > 1);
 	else if (rename && (argc == 2))
-		rename_branch(argv[0], argv[1], force_rename);
+		rename_branch(argv[0], argv[1], rename > 1);
 	else if (argc <= 2)
 		create_branch(argv[0], (argc == 2) ? argv[1] : head,
 			      force_create, reflog, track);
-- 
1.5.3.5.1598.gdef4e
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 4 of 19 in “Preliminary patches to the diff.[hc] parseoptification”
  1. Pierre HabouzitNov 7, 2007
  2. 1/1 Make gcc warning about empty if body go away.Pierre Habouzit, Nov 7, 2007
  3. 1/4 parse-options new features.Pierre Habouzit, Nov 7, 2007
  4. 2/4 Use OPT_SET_INT and OPT_BIT in builtin-branchPierre Habouzit, Nov 7, 2007
  5. 3/4 Use OPT_BIT in builtin-for-each-refPierre Habouzit, Nov 7, 2007
  6. 4/4 Use OPT_BIT in builtin-pack-refsPierre Habouzit, Nov 7, 2007
  7. 1/2 Make the diff_options bitfields be an unsigned with explicit masks.Pierre Habouzit, Nov 7, 2007
  8. 2/2 Reorder diff_opt_parse options more logically per topics.Pierre Habouzit, Nov 7, 2007
  9. Junio C HamanoNov 8, 2007
  10. Pierre HabouzitNov 8, 2007
  11. 1/2 Make the diff_options bitfields be an unsigned with explicit masks.Pierre Habouzit, Nov 10, 2007
  12. Junio C HamanoNov 8, 2007
  13. Pierre HabouzitNov 9, 2007
  14. Junio C HamanoNov 8, 2007
  15. Junio C HamanoNov 8, 2007
  16. Pierre HabouzitNov 8, 2007
  17. Andreas EricssonNov 8, 2007
  18. Jon LoeligerNov 8, 2007
  19. Andreas EricssonNov 8, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.