@matheusbsbs / matheus-bagliano / commits / c8ef0383ac
Fork 0

parse-opt: migrate fmt-merge-msg.

Also fix an inefficient printf("%s", ...) where we can use write_in_full. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Pierre Habouzit committed Oct 2, 2008 at 14:59 UTC c8ef0383aca38bfedf85a311bbdd30008fcbebcf
1 file changed +21 -28
builtin-fmt-merge-msg.c
+21 -28
@@ -5,8 +5,10 @@
5 #include "revision.h"
6 #include "tag.h"
7
8 -static const char *fmt_merge_msg_usage =
9 - "git fmt-merge-msg [--log] [--no-log] [--file <file>]";
8 +static const char * const fmt_merge_msg_usage[] = {
9 + "git fmt-merge-msg [--log|--no-log] [--file <file>]",
10 + NULL
11 +};
12
13 static int merge_summary;
14
@@ -347,38 +349,29 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
349
350 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
351 {
352 + const char *inpath = NULL;
353 + struct option options[] = {
354 + OPT_BOOLEAN(0, "log", &merge_summary, "populate log with the shortlog"),
355 + OPT_BOOLEAN(0, "summary", &merge_summary, "alias for --log"),
356 + OPT_STRING('F', "file", &inpath, "file", "file to read from"),
357 + OPT_END()
358 + };
359 +
360 FILE *in = stdin;
361 struct strbuf input, output;
362 int ret;
363
364 git_config(fmt_merge_msg_config, NULL);
355 -
356 - while (argc > 1) {
357 - if (!strcmp(argv[1], "--log") || !strcmp(argv[1], "--summary"))
358 - merge_summary = 1;
359 - else if (!strcmp(argv[1], "--no-log")
360 - || !strcmp(argv[1], "--no-summary"))
361 - merge_summary = 0;
362 - else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
363 - if (argc < 3)
364 - die ("Which file?");
365 - if (!strcmp(argv[2], "-"))
366 - in = stdin;
367 - else {
368 - fclose(in);
369 - in = fopen(argv[2], "r");
370 - if (!in)
371 - die("cannot open %s", argv[2]);
372 - }
373 - argc--; argv++;
374 - } else
375 - break;
376 - argc--; argv++;
365 + argc = parse_options(argc, argv, options, fmt_merge_msg_usage, 0);
366 + if (argc > 0)
367 + usage_with_options(fmt_merge_msg_usage, options);
368 +
369 + if (inpath && strcmp(inpath, "-")) {
370 + in = fopen(inpath, "r");
371 + if (!in)
372 + die("cannot open %s", inpath);
373 }
374
379 - if (argc > 1)
380 - usage(fmt_merge_msg_usage);
381 -
375 strbuf_init(&input, 0);
376 if (strbuf_read(&input, fileno(in), 0) < 0)
377 die("could not read input file %s", strerror(errno));
@@ -387,6 +380,6 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
380 ret = fmt_merge_msg(merge_summary, &input, &output);
381 if (ret)
382 return ret;
390 - printf("%s", output.buf);
383 + write_in_full(STDOUT_FILENO, output.buf, output.len);
384 return 0;
385 }