Merge branch 'ph/parseopt'

* ph/parseopt:
  parse-opt: migrate builtin-merge-file.
  parse-opt: migrate git-merge-base.
  parse-opt: migrate fmt-merge-msg.
This commit is contained in:
Shawn O. Pearce 2008-10-09 10:19:08 -07:00
commit c9618ff939
3 changed files with 73 additions and 79 deletions

View File

@ -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,37 +349,28 @@ 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);
argc = parse_options(argc, argv, options, fmt_merge_msg_usage, 0);
if (argc > 0)
usage_with_options(fmt_merge_msg_usage, options);
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 (inpath && strcmp(inpath, "-")) {
in = fopen(inpath, "r");
if (!in)
die("cannot open %s", argv[2]);
die("cannot open %s", inpath);
}
argc--; argv++;
} else
break;
argc--; argv++;
}
if (argc > 1)
usage(fmt_merge_msg_usage);
strbuf_init(&input, 0);
if (strbuf_read(&input, fileno(in), 0) < 0)
@ -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;
}

View File

@ -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;
struct option options[] = {
OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"),
OPT_END()
};
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);
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);
}

View File

@ -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"))
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);
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];
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)