Skip to content

Commit 1c40c36

Browse files
carlosmngitster
authored andcommitted
log: convert to parse-options
Use parse-options in cmd_log_init instead of manually iterating through them. This makes the code a bit cleaner but more importantly allows us to catch the "--quiet" option which causes some of the log-related commands to misbehave as it would otherwise get passed on to the diff. Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 06ff449 commit 1c40c36

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

builtin/log.c

Lines changed: 43 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,12 +52,41 @@ 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(int argc, const char **argv, const char *prefix,
5373
struct rev_info *rev, struct setup_revision_opt *opt)
5474
{
55-
int i;
56-
int decoration_given = 0;
5775
struct userformat_want w;
76+
int quiet = 0, source = 0;
77+
78+
const struct option builtin_log_options[] = {
79+
OPT_BOOLEAN(0, "quiet", &quiet, "supress diff output"),
80+
OPT_BOOLEAN(0, "source", &source, "show source"),
81+
{ OPTION_CALLBACK, 0, "decorate", NULL, NULL, "decorate options",
82+
PARSE_OPT_OPTARG, decorate_callback},
83+
OPT_END()
84+
};
85+
86+
argc = parse_options(argc, argv, prefix,
87+
builtin_log_options, builtin_log_usage,
88+
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
89+
PARSE_OPT_KEEP_DASHDASH);
5890

5991
rev->abbrev = DEFAULT_ABBREV;
6092
rev->commit_format = CMIT_FMT_DEFAULT;
@@ -69,14 +101,12 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
69101
if (default_date_mode)
70102
rev->date_mode = parse_date_format(default_date_mode);
71103

72-
/*
73-
* Check for -h before setup_revisions(), or "git log -h" will
74-
* fail when run without a git directory.
75-
*/
76-
if (argc == 2 && !strcmp(argv[1], "-h"))
77-
usage(builtin_log_usage);
78104
argc = setup_revisions(argc, argv, rev, opt);
79105

106+
/* Any arguments at this point are not recognized */
107+
if (argc > 1)
108+
die("unrecognized argument: %s", argv[1]);
109+
80110
memset(&w, 0, sizeof(w));
81111
userformat_find_requirements(NULL, &w);
82112

@@ -92,26 +122,9 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
92122
if (rev->diffopt.nr_paths != 1)
93123
usage("git logs can only follow renames on one pathname at a time");
94124
}
95-
for (i = 1; i < argc; i++) {
96-
const char *arg = argv[i];
97-
if (!strcmp(arg, "--decorate")) {
98-
decoration_style = DECORATE_SHORT_REFS;
99-
decoration_given = 1;
100-
} else if (!prefixcmp(arg, "--decorate=")) {
101-
const char *v = skip_prefix(arg, "--decorate=");
102-
decoration_style = parse_decoration_style(arg, v);
103-
if (decoration_style < 0)
104-
die("invalid --decorate option: %s", arg);
105-
decoration_given = 1;
106-
} else if (!strcmp(arg, "--no-decorate")) {
107-
decoration_style = 0;
108-
} else if (!strcmp(arg, "--source")) {
109-
rev->show_source = 1;
110-
} else if (!strcmp(arg, "-h")) {
111-
usage(builtin_log_usage);
112-
} else
113-
die("unrecognized argument: %s", arg);
114-
}
125+
126+
if (source)
127+
rev->show_source = 1;
115128

116129
/*
117130
* defeat log.decorate configuration interacting with --pretty=raw

0 commit comments

Comments
 (0)