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

[PATCH] parse-opt: migrate fmt-merge-msg.

From
Pierre Habouzit <madcoder@debian.org>
Date
Sep 28, 2008, 09:45 UTC
Message-ID
<1222595139-32087-2-git-send-email-madcoder@debian.org>
In-Reply-To
<1222595139-32087-1-git-send-email-madcoder@debian.org>
Also fix an inefficient printf("%s", ...) where we can use write_in_full.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 builtin-fmt-merge-msg.c |   50 +++++++++++++++++++++-------------------------
 1 files changed, 23 insertions(+), 27 deletions(-)
diff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c
index a1879f1..654d996 100644
--- a/builtin-fmt-merge-msg.c
+++ b/builtin-fmt-merge-msg.c
@@ -5,8 +5,10 @@
 #include "revision.h"
 #include "tag.h"
 
-static const char *fmt_merge_msg_usage =
-	"git fmt-merge-msg [--log] [--no-log] [--file <file>]";
+static const char * const fmt_merge_msg_usage[] = {
+	"git fmt-merge-msg [--log|--no-log] [--file <file>]",
+	NULL
+};
 
 static int merge_log;
 
@@ -342,37 +344,31 @@ int fmt_merge_msg(int merge_log, struct strbuf *in, struct strbuf *out) {
 
 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 {
+	const char *inpath = NULL;
+	struct option options[] = {
+		OPT_BOOLEAN(0, "log", &merge_log, "populate log with the shortlog"),
+		OPT_STRING('F', "file", &inpath, "file", "file to read from"),
+		OPT_END()
+	};
+
 	FILE *in = stdin;
 	struct strbuf input, output;
 	int ret;
 
 	git_config(fmt_merge_msg_config, NULL);
-
-	while (argc > 1) {
-		if (!strcmp(argv[1], "--log"))
-			merge_log = 1;
-		else if (!strcmp(argv[1], "--no-log"))
-			merge_log = 0;
-		else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
-			if (argc < 3)
-				die ("Which file?");
-			if (!strcmp(argv[2], "-"))
-				in = stdin;
-			else {
-				fclose(in);
-				in = fopen(argv[2], "r");
-				if (!in)
-					die("cannot open %s", argv[2]);
-			}
-			argc--; argv++;
-		} else
-			break;
-		argc--; argv++;
+	argc = parse_options(argc, argv, options, fmt_merge_msg_usage, 0);
+	if (argc > 0)
+		usage_with_options(fmt_merge_msg_usage, options);
+
+	if (!inpath || strcmp(inpath, "-"))
+		in = stdin;
+	else {
+		fclose(in);
+		in = fopen(argv[2], "r");
+		if (!in)
+			die("cannot open %s", argv[2]);
 	}
 
-	if (argc > 1)
-		usage(fmt_merge_msg_usage);
-
 	strbuf_init(&input, 0);
 	if (strbuf_read(&input, fileno(in), 0) < 0)
 		die("could not read input file %s", strerror(errno));
@@ -381,6 +377,6 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 	ret = fmt_merge_msg(merge_log, &input, &output);
 	if (ret)
 		return ret;
-	printf("%s", output.buf);
+	write_in_full(STDOUT_FILENO, output.buf, output.len);
 	return 0;
 }
-- 
1.6.0.2.516.g12936
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 2 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.