Skip to content

Commit 2f3e3f5

Browse files
committed
Merge branch 'cn/log-parse-opt'
* cn/log-parse-opt: log: convert to parse-options
2 parents 32341b9 + 1c40c36 commit 2f3e3f5

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

builtin/log.c

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ static const char *default_date_mode = NULL;
2525

2626
static int default_show_root = 1;
2727
static int decoration_style;
28+
static int decoration_given;
2829
static const char *fmt_patch_subject_prefix = "PATCH";
2930
static const char *fmt_pretty;
3031

31-
static const char * const builtin_log_usage =
32+
static const char * const builtin_log_usage[] = {
3233
"git log [<options>] [<since>..<until>] [[--] <path>...]\n"
33-
" or: git show [options] <object>...";
34+
" or: git show [options] <object>...",
35+
NULL
36+
};
3437

3538
static int parse_decoration_style(const char *var, const char *value)
3639
{
@@ -49,6 +52,23 @@ static int parse_decoration_style(const char *var, const char *value)
4952
return -1;
5053
}
5154

55+
static int decorate_callback(const struct option *opt, const char *arg, int unset)
56+
{
57+
if (unset)
58+
decoration_style = 0;
59+
else if (arg)
60+
decoration_style = parse_decoration_style("command line", arg);
61+
else
62+
decoration_style = DECORATE_SHORT_REFS;
63+
64+
if (decoration_style < 0)
65+
die("invalid --decorate option: %s", arg);
66+
67+
decoration_given = 1;
68+
69+
return 0;
70+
}
71+
5272
static void cmd_log_init_defaults(struct rev_info *rev)
5373
{
5474
rev->abbrev = DEFAULT_ABBREV;
@@ -68,17 +88,28 @@ static void cmd_log_init_defaults(struct rev_info *rev)
6888
static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
6989
struct rev_info *rev, struct setup_revision_opt *opt)
7090
{
71-
int i;
72-
int decoration_given = 0;
7391
struct userformat_want w;
74-
/*
75-
* Check for -h before setup_revisions(), or "git log -h" will
76-
* fail when run without a git directory.
77-
*/
78-
if (argc == 2 && !strcmp(argv[1], "-h"))
79-
usage(builtin_log_usage);
92+
int quiet = 0, source = 0;
93+
94+
const struct option builtin_log_options[] = {
95+
OPT_BOOLEAN(0, "quiet", &quiet, "supress diff output"),
96+
OPT_BOOLEAN(0, "source", &source, "show source"),
97+
{ OPTION_CALLBACK, 0, "decorate", NULL, NULL, "decorate options",
98+
PARSE_OPT_OPTARG, decorate_callback},
99+
OPT_END()
100+
};
101+
102+
argc = parse_options(argc, argv, prefix,
103+
builtin_log_options, builtin_log_usage,
104+
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
105+
PARSE_OPT_KEEP_DASHDASH);
106+
80107
argc = setup_revisions(argc, argv, rev, opt);
81108

109+
/* Any arguments at this point are not recognized */
110+
if (argc > 1)
111+
die("unrecognized argument: %s", argv[1]);
112+
82113
memset(&w, 0, sizeof(w));
83114
userformat_find_requirements(NULL, &w);
84115

@@ -94,26 +125,9 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
94125
if (rev->diffopt.pathspec.nr != 1)
95126
usage("git logs can only follow renames on one pathname at a time");
96127
}
97-
for (i = 1; i < argc; i++) {
98-
const char *arg = argv[i];
99-
if (!strcmp(arg, "--decorate")) {
100-
decoration_style = DECORATE_SHORT_REFS;
101-
decoration_given = 1;
102-
} else if (!prefixcmp(arg, "--decorate=")) {
103-
const char *v = skip_prefix(arg, "--decorate=");
104-
decoration_style = parse_decoration_style(arg, v);
105-
if (decoration_style < 0)
106-
die(_("invalid --decorate option: %s"), arg);
107-
decoration_given = 1;
108-
} else if (!strcmp(arg, "--no-decorate")) {
109-
decoration_style = 0;
110-
} else if (!strcmp(arg, "--source")) {
111-
rev->show_source = 1;
112-
} else if (!strcmp(arg, "-h")) {
113-
usage(builtin_log_usage);
114-
} else
115-
die(_("unrecognized argument: %s"), arg);
116-
}
128+
129+
if (source)
130+
rev->show_source = 1;
117131

118132
/*
119133
* defeat log.decorate configuration interacting with --pretty=raw

0 commit comments

Comments
 (0)