@matheusbsbs / matheus-bagliano / commits / 1c40c36b9a
Fork 0

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 <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Martín Nieto committed Apr 14, 2011 at 16:28 UTC 1c40c36b9acbb55726ea0fcf98714a85e159e45f
1 file changed +43 -30
builtin/log.c
+43 -30
@@ -25,12 +25,15 @@ static const char *default_date_mode = NULL;
25
26 static int default_show_root = 1;
27 static int decoration_style;
28 +static int decoration_given;
29 static const char *fmt_patch_subject_prefix = "PATCH";
30 static const char *fmt_pretty;
31
31 -static const char * const builtin_log_usage =
32 +static const char * const builtin_log_usage[] = {
33 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
33 - " or: git show [options] <object>...";
34 + " or: git show [options] <object>...",
35 + NULL
36 +};
37
38 static int parse_decoration_style(const char *var, const char *value)
39 {
@@ -49,12 +52,41 @@ static int parse_decoration_style(const char *var, const char *value)
52 return -1;
53 }
54
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 +
72 static void cmd_log_init(int argc, const char **argv, const char *prefix,
73 struct rev_info *rev, struct setup_revision_opt *opt)
74 {
55 - int i;
56 - int decoration_given = 0;
75 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);
90
91 rev->abbrev = DEFAULT_ABBREV;
92 rev->commit_format = CMIT_FMT_DEFAULT;
@@ -69,14 +101,12 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
101 if (default_date_mode)
102 rev->date_mode = parse_date_format(default_date_mode);
103
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);
104 argc = setup_revisions(argc, argv, rev, opt);
105
106 + /* Any arguments at this point are not recognized */
107 + if (argc > 1)
108 + die("unrecognized argument: %s", argv[1]);
109 +
110 memset(&w, 0, sizeof(w));
111 userformat_find_requirements(NULL, &w);
112
@@ -92,26 +122,9 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
122 if (rev->diffopt.nr_paths != 1)
123 usage("git logs can only follow renames on one pathname at a time");
124 }
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;
128
129 /*
130 * defeat log.decorate configuration interacting with --pretty=raw