Code

Merge branch 'cn/log-parse-opt'
authorJunio C Hamano <gitster@pobox.com>
Fri, 13 May 2011 17:59:57 +0000 (10:59 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 13 May 2011 17:59:57 +0000 (10:59 -0700)
* cn/log-parse-opt:
  log: convert to parse-options

1  2 
builtin/log.c

diff --cc builtin/log.c
index d43ad3a6172b4e083a6c066ab15dfce5bdb0ad3c,ca353402a6984b6fc122c712173f4f061dd5bf20..f6219909a787ae355a43e39974140a36848ab1d6
@@@ -49,36 -52,61 +52,64 @@@ static int parse_decoration_style(cons
        return -1;
  }
  
 -static void cmd_log_init(int argc, const char **argv, const char *prefix,
+ static int decorate_callback(const struct option *opt, const char *arg, int unset)
+ {
+       if (unset)
+               decoration_style = 0;
+       else if (arg)
+               decoration_style = parse_decoration_style("command line", arg);
+       else
+               decoration_style = DECORATE_SHORT_REFS;
+       if (decoration_style < 0)
+               die("invalid --decorate option: %s", arg);
+       decoration_given = 1;
+       return 0;
+ }
 +static void cmd_log_init_defaults(struct rev_info *rev)
 +{
 +      rev->abbrev = DEFAULT_ABBREV;
 +      rev->commit_format = CMIT_FMT_DEFAULT;
 +      if (fmt_pretty)
 +              get_commit_format(fmt_pretty, rev);
 +      rev->verbose_header = 1;
 +      DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
 +      rev->show_root_diff = default_show_root;
 +      rev->subject_prefix = fmt_patch_subject_prefix;
 +      DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
 +
 +      if (default_date_mode)
 +              rev->date_mode = parse_date_format(default_date_mode);
 +}
 +
 +static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
                         struct rev_info *rev, struct setup_revision_opt *opt)
  {
-       int i;
-       int decoration_given = 0;
        struct userformat_want w;
-       /*
-        * Check for -h before setup_revisions(), or "git log -h" will
-        * fail when run without a git directory.
-        */
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(builtin_log_usage);
+       int quiet = 0, source = 0;
+       const struct option builtin_log_options[] = {
+               OPT_BOOLEAN(0, "quiet", &quiet, "supress diff output"),
+               OPT_BOOLEAN(0, "source", &source, "show source"),
+               { OPTION_CALLBACK, 0, "decorate", NULL, NULL, "decorate options",
+                 PARSE_OPT_OPTARG, decorate_callback},
+               OPT_END()
+       };
+       argc = parse_options(argc, argv, prefix,
+                            builtin_log_options, builtin_log_usage,
+                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
+                            PARSE_OPT_KEEP_DASHDASH);
 -      rev->abbrev = DEFAULT_ABBREV;
 -      rev->commit_format = CMIT_FMT_DEFAULT;
 -      if (fmt_pretty)
 -              get_commit_format(fmt_pretty, rev);
 -      rev->verbose_header = 1;
 -      DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
 -      rev->show_root_diff = default_show_root;
 -      rev->subject_prefix = fmt_patch_subject_prefix;
 -      DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
 -
 -      if (default_date_mode)
 -              rev->date_mode = parse_date_format(default_date_mode);
 -
        argc = setup_revisions(argc, argv, rev, opt);
  
+       /* Any arguments at this point are not recognized */
+       if (argc > 1)
+               die("unrecognized argument: %s", argv[1]);
        memset(&w, 0, sizeof(w));
        userformat_find_requirements(NULL, &w);
  
                rev->always_show_header = 0;
        if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
                rev->always_show_header = 0;
 -              if (rev->diffopt.nr_paths != 1)
 +              if (rev->diffopt.pathspec.nr != 1)
                        usage("git logs can only follow renames on one pathname at a time");
        }
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-               if (!strcmp(arg, "--decorate")) {
-                       decoration_style = DECORATE_SHORT_REFS;
-                       decoration_given = 1;
-               } else if (!prefixcmp(arg, "--decorate=")) {
-                       const char *v = skip_prefix(arg, "--decorate=");
-                       decoration_style = parse_decoration_style(arg, v);
-                       if (decoration_style < 0)
-                               die(_("invalid --decorate option: %s"), arg);
-                       decoration_given = 1;
-               } else if (!strcmp(arg, "--no-decorate")) {
-                       decoration_style = 0;
-               } else if (!strcmp(arg, "--source")) {
-                       rev->show_source = 1;
-               } else if (!strcmp(arg, "-h")) {
-                       usage(builtin_log_usage);
-               } else
-                       die(_("unrecognized argument: %s"), arg);
-       }
+       if (source)
+               rev->show_source = 1;
  
        /*
         * defeat log.decorate configuration interacting with --pretty=raw