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

[PATCH 2/3] parse-opt: migrate git-merge-base.

From
Pierre Habouzit <madcoder@debian.org>
Date
Oct 1, 2008, 06:05 UTC
Message-ID
<1222841106-26148-2-git-send-email-madcoder@debian.org>
In-Reply-To
<1222841106-26148-1-git-send-email-madcoder@debian.org>
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 builtin-merge-base.c |   37 ++++++++++++++++---------------------
 1 files changed, 16 insertions(+), 21 deletions(-)
diff --git a/builtin-merge-base.c b/builtin-merge-base.c
index b08da51..03fc1c2 100644
--- a/builtin-merge-base.c
+++ b/builtin-merge-base.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "commit.h"
+#include "parse-options.h"
 
 static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
 {
@@ -21,8 +22,10 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
 	return 0;
 }
 
-static const char merge_base_usage[] =
-"git merge-base [--all] <commit-id> <commit-id>...";
+static const char * const merge_base_usage[] = {
+	"git merge-base [--all] <commit-id> <commit-id>...",
+	NULL
+};
 
 static struct commit *get_commit_reference(const char *arg)
 {
@@ -44,25 +47,17 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
 	int rev_nr = 0;
 	int show_all = 0;
 
-	git_config(git_default_config, NULL);
-
-	while (1 < argc && argv[1][0] == '-') {
-		const char *arg = argv[1];
-		if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
-			show_all = 1;
-		else
-			usage(merge_base_usage);
-		argc--; argv++;
-	}
-	if (argc < 3)
-		usage(merge_base_usage);
-
-	rev = xmalloc((argc - 1) * sizeof(*rev));
-
-	do {
-		rev[rev_nr++] = get_commit_reference(argv[1]);
-		argc--; argv++;
-	} while (argc > 1);
+	struct option options[] = {
+		OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"),
+		OPT_END()
+	};
 
+	git_config(git_default_config, NULL);
+	argc = parse_options(argc, argv, options, merge_base_usage, 0);
+	if (argc < 2)
+		usage_with_options(merge_base_usage, options);
+	rev = xmalloc(argc * sizeof(*rev));
+	while (argc-- > 0)
+		rev[rev_nr++] = get_commit_reference(*argv++);
 	return show_merge_base(rev, rev_nr, show_all);
 }
-- 
1.6.0.2.415.g9800c0.dirty
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 11 of 19 in “small warm-up: easy parse-opt migrations.”
  1. Pierre HabouzitSep 28, 2008
  2. parse-opt: migrate fmt-merge-msg.Pierre Habouzit, Sep 28, 2008
  3. parse-opt: migrate git-merge-base.Pierre Habouzit, Sep 28, 2008
  4. parse-opt: migrate builtin-merge-file.Pierre Habouzit, Sep 28, 2008
  5. Shawn O. PearceSep 29, 2008
  6. Pierre HabouzitSep 30, 2008
  7. Shawn O. PearceSep 30, 2008
  8. Pierre HabouzitSep 30, 2008
  9. Shawn O. PearceSep 30, 2008
  10. 1/3 parse-opt: migrate fmt-merge-msg.Pierre Habouzit, Oct 1, 2008
  11. 2/3 parse-opt: migrate git-merge-base.Pierre Habouzit, Oct 1, 2008
  12. 3/3 parse-opt: migrate builtin-merge-file.Pierre Habouzit, Oct 1, 2008
  13. Shawn O. PearceOct 1, 2008
  14. Shawn O. PearceOct 1, 2008
  15. Pierre HabouzitOct 2, 2008
  16. parse-opt: migrate fmt-merge-msg.Pierre Habouzit, Oct 2, 2008
  17. parse-opt: migrate git-merge-base.Pierre Habouzit, Oct 2, 2008
  18. parse-opt: migrate builtin-merge-file.Pierre Habouzit, Oct 2, 2008
  19. Sverre RabbelierSep 28, 2008

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.