diff options
author | Shawn O. Pearce <spearce@spearce.org> | 2008-10-09 10:19:08 -0700 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2008-10-09 10:19:08 -0700 |
commit | c9618ff939aeb5e895d90c2478db989038eccc26 (patch) | |
tree | 189c0f633079ce7d5481a0835b1522309e6e10e3 | |
parent | Merge branch 'rz/grepz' (diff) | |
parent | parse-opt: migrate builtin-merge-file. (diff) | |
download | tgif-c9618ff939aeb5e895d90c2478db989038eccc26.tar.xz |
Merge branch 'ph/parseopt'
* ph/parseopt:
parse-opt: migrate builtin-merge-file.
parse-opt: migrate git-merge-base.
parse-opt: migrate fmt-merge-msg.
-rw-r--r-- | builtin-fmt-merge-msg.c | 49 | ||||
-rw-r--r-- | builtin-merge-base.c | 37 | ||||
-rw-r--r-- | builtin-merge-file.c | 68 |
3 files changed, 74 insertions, 80 deletions
diff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c index df02ba7afd..c6324dc795 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_summary; @@ -347,38 +349,29 @@ int fmt_merge_msg(int merge_summary, 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_summary, "populate log with the shortlog"), + OPT_BOOLEAN(0, "summary", &merge_summary, "alias for --log"), + 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") || !strcmp(argv[1], "--summary")) - merge_summary = 1; - else if (!strcmp(argv[1], "--no-log") - || !strcmp(argv[1], "--no-summary")) - merge_summary = 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 = fopen(inpath, "r"); + if (!in) + die("cannot open %s", inpath); } - 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)); @@ -387,6 +380,6 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) ret = fmt_merge_msg(merge_summary, &input, &output); if (ret) return ret; - printf("%s", output.buf); + write_in_full(STDOUT_FILENO, output.buf, output.len); return 0; } diff --git a/builtin-merge-base.c b/builtin-merge-base.c index b08da516e4..03fc1c2114 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); } diff --git a/builtin-merge-file.c b/builtin-merge-file.c index 45c98538cd..9d4e874809 100644 --- a/builtin-merge-file.c +++ b/builtin-merge-file.c @@ -2,21 +2,44 @@ #include "cache.h" #include "xdiff/xdiff.h" #include "xdiff-interface.h" +#include "parse-options.h" -static const char merge_file_usage[] = -"git merge-file [-p | --stdout] [--diff3] [-q | --quiet] [-L name1 [-L orig [-L name2]]] file1 orig_file file2"; +static const char *const merge_file_usage[] = { + "git merge-file [options] [-L name1 [-L orig [-L name2]]] file1 orig_file file2", + NULL +}; + +static int label_cb(const struct option *opt, const char *arg, int unset) +{ + static int label_count = 0; + const char **names = (const char **)opt->value; + + if (label_count >= 3) + return error("too many labels on the command line"); + names[label_count++] = arg; + return 0; +} int cmd_merge_file(int argc, const char **argv, const char *prefix) { - const char *names[3]; + const char *names[3] = { NULL, NULL, NULL }; mmfile_t mmfs[3]; mmbuffer_t result = {NULL, 0}; xpparam_t xpp = {XDF_NEED_MINIMAL}; int ret = 0, i = 0, to_stdout = 0; int merge_level = XDL_MERGE_ZEALOUS_ALNUM; - int merge_style = 0; + int merge_style = 0, quiet = 0; int nongit; + struct option options[] = { + OPT_BOOLEAN('p', "stdout", &to_stdout, "send results to standard output"), + OPT_SET_INT(0, "diff3", &merge_style, "use a diff3 based merge", XDL_MERGE_DIFF3), + OPT__QUIET(&quiet), + OPT_CALLBACK('L', NULL, names, "name", + "set labels for file1/orig_file/file2", &label_cb), + OPT_END(), + }; + prefix = setup_git_directory_gently(&nongit); if (!nongit) { /* Read the configuration file */ @@ -25,37 +48,20 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix) merge_style = git_xmerge_style; } - while (argc > 4) { - if (!strcmp(argv[1], "-L") && i < 3) { - names[i++] = argv[2]; - argc--; - argv++; - } else if (!strcmp(argv[1], "-p") || - !strcmp(argv[1], "--stdout")) - to_stdout = 1; - else if (!strcmp(argv[1], "-q") || - !strcmp(argv[1], "--quiet")) - freopen("/dev/null", "w", stderr); - else if (!strcmp(argv[1], "--diff3")) - merge_style = XDL_MERGE_DIFF3; - else - usage(merge_file_usage); - argc--; - argv++; - } - - if (argc != 4) - usage(merge_file_usage); - - for (; i < 3; i++) - names[i] = argv[i + 1]; + argc = parse_options(argc, argv, options, merge_file_usage, 0); + if (argc != 3) + usage_with_options(merge_file_usage, options); + if (quiet) + freopen("/dev/null", "w", stderr); for (i = 0; i < 3; i++) { - if (read_mmfile(mmfs + i, argv[i + 1])) + if (!names[i]) + names[i] = argv[i]; + if (read_mmfile(mmfs + i, argv[i])) return -1; if (buffer_is_binary(mmfs[i].ptr, mmfs[i].size)) return error("Cannot merge binary files: %s\n", - argv[i + 1]); + argv[i]); } ret = xdl_merge(mmfs + 1, mmfs + 0, names[0], mmfs + 2, names[2], @@ -65,7 +71,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix) free(mmfs[i].ptr); if (ret >= 0) { - const char *filename = argv[1]; + const char *filename = argv[0]; FILE *f = to_stdout ? stdout : fopen(filename, "wb"); if (!f) |