]> git.ipfire.org Git - thirdparty/git.git/blobdiff - diff.c
diff-parseopt: convert --dirstat and friends
[thirdparty/git.git] / diff.c
diff --git a/diff.c b/diff.c
index 1b5f27636061feb0ecf273f257c5e080c5ff7481..1cdbe8e6884eb2d329330c78ea0a9bdf53a4ec4f 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -23,6 +23,7 @@
 #include "argv-array.h"
 #include "graph.h"
 #include "packfile.h"
+#include "parse-options.h"
 #include "help.h"
 
 #ifdef NO_FAST_WORKING_DIRECTORY
@@ -4425,6 +4426,8 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
        builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
 }
 
+static void prep_parse_options(struct diff_options *options);
+
 void repo_diff_setup(struct repository *r, struct diff_options *options)
 {
        memcpy(options, &default_diff_options, sizeof(*options));
@@ -4466,6 +4469,8 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
 
        options->color_moved = diff_color_moved_default;
        options->color_moved_ws_handling = diff_color_moved_ws_default;
+
+       prep_parse_options(options);
 }
 
 void diff_setup_done(struct diff_options *options)
@@ -4569,6 +4574,8 @@ void diff_setup_done(struct diff_options *options)
 
        if (!options->use_color || external_diff())
                options->color_moved = 0;
+
+       FREE_AND_NULL(options->parseopts);
 }
 
 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
@@ -4860,6 +4867,85 @@ static int parse_objfind_opt(struct diff_options *opt, const char *arg)
        return 1;
 }
 
+static int diff_opt_dirstat(const struct option *opt,
+                           const char *arg, int unset)
+{
+       struct diff_options *options = opt->value;
+
+       BUG_ON_OPT_NEG(unset);
+       if (!strcmp(opt->long_name, "cumulative")) {
+               if (arg)
+                       BUG("how come --cumulative take a value?");
+               arg = "cumulative";
+       } else if (!strcmp(opt->long_name, "dirstat-by-file"))
+               parse_dirstat_opt(options, "files");
+       parse_dirstat_opt(options, arg ? arg : "");
+       return 0;
+}
+
+static int diff_opt_unified(const struct option *opt,
+                           const char *arg, int unset)
+{
+       struct diff_options *options = opt->value;
+       char *s;
+
+       BUG_ON_OPT_NEG(unset);
+
+       options->context = strtol(arg, &s, 10);
+       if (*s)
+               return error(_("%s expects a numerical value"), "--unified");
+       enable_patch_output(&options->output_format);
+
+       return 0;
+}
+
+static void prep_parse_options(struct diff_options *options)
+{
+       struct option parseopts[] = {
+               OPT_GROUP(N_("Diff output format options")),
+               OPT_BITOP('p', "patch", &options->output_format,
+                         N_("generate patch"),
+                         DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
+               OPT_BITOP('u', NULL, &options->output_format,
+                         N_("generate patch"),
+                         DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
+               OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
+                              N_("generate diffs with <n> lines context"),
+                              PARSE_OPT_NONEG, diff_opt_unified),
+               OPT_BOOL('W', "function-context", &options->flags.funccontext,
+                        N_("generate diffs with <n> lines context")),
+               OPT_BIT_F(0, "raw", &options->output_format,
+                         N_("generate the diff in raw format"),
+                         DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
+               OPT_BITOP(0, "patch-with-raw", &options->output_format,
+                         N_("synonym for '-p --raw'"),
+                         DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
+                         DIFF_FORMAT_NO_OUTPUT),
+               OPT_BIT_F(0, "numstat", &options->output_format,
+                         N_("machine friendly --stat"),
+                         DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
+               OPT_BIT_F(0, "shortstat", &options->output_format,
+                         N_("output only the last line of --stat"),
+                         DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
+               OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
+                              N_("output the distribution of relative amount of changes for each sub-directory"),
+                              PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
+                              diff_opt_dirstat),
+               OPT_CALLBACK_F(0, "cumulative", options, NULL,
+                              N_("synonym for --dirstat=cumulative"),
+                              PARSE_OPT_NONEG | PARSE_OPT_NOARG,
+                              diff_opt_dirstat),
+               OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."),
+                              N_("synonym for --dirstat=files,param1,param2..."),
+                              PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
+                              diff_opt_dirstat),
+               OPT_END()
+       };
+
+       ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
+       memcpy(options->parseopts, parseopts, sizeof(parseopts));
+}
+
 int diff_opt_parse(struct diff_options *options,
                   const char **av, int ac, const char *prefix)
 {
@@ -4870,29 +4956,18 @@ int diff_opt_parse(struct diff_options *options,
        if (!prefix)
                prefix = "";
 
+       ac = parse_options(ac, av, prefix, options->parseopts, NULL,
+                          PARSE_OPT_KEEP_DASHDASH |
+                          PARSE_OPT_KEEP_UNKNOWN |
+                          PARSE_OPT_NO_INTERNAL_HELP |
+                          PARSE_OPT_ONE_SHOT |
+                          PARSE_OPT_STOP_AT_NON_OPTION);
+
+       if (ac)
+               return ac;
+
        /* Output format options */
-       if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
-           || opt_arg(arg, 'U', "unified", &options->context))
-               enable_patch_output(&options->output_format);
-       else if (!strcmp(arg, "--raw"))
-               options->output_format |= DIFF_FORMAT_RAW;
-       else if (!strcmp(arg, "--patch-with-raw")) {
-               enable_patch_output(&options->output_format);
-               options->output_format |= DIFF_FORMAT_RAW;
-       } else if (!strcmp(arg, "--numstat"))
-               options->output_format |= DIFF_FORMAT_NUMSTAT;
-       else if (!strcmp(arg, "--shortstat"))
-               options->output_format |= DIFF_FORMAT_SHORTSTAT;
-       else if (skip_prefix(arg, "-X", &arg) ||
-                skip_to_optional_arg(arg, "--dirstat", &arg))
-               return parse_dirstat_opt(options, arg);
-       else if (!strcmp(arg, "--cumulative"))
-               return parse_dirstat_opt(options, "cumulative");
-       else if (skip_to_optional_arg(arg, "--dirstat-by-file", &arg)) {
-               parse_dirstat_opt(options, "files");
-               return parse_dirstat_opt(options, arg);
-       }
-       else if (!strcmp(arg, "--check"))
+       if (!strcmp(arg, "--check"))
                options->output_format |= DIFF_FORMAT_CHECKDIFF;
        else if (!strcmp(arg, "--summary"))
                options->output_format |= DIFF_FORMAT_SUMMARY;
@@ -5162,12 +5237,6 @@ int diff_opt_parse(struct diff_options *options,
        else if (opt_arg(arg, '\0', "inter-hunk-context",
                         &options->interhunkcontext))
                ;
-       else if (!strcmp(arg, "-W"))
-               options->flags.funccontext = 1;
-       else if (!strcmp(arg, "--function-context"))
-               options->flags.funccontext = 1;
-       else if (!strcmp(arg, "--no-function-context"))
-               options->flags.funccontext = 0;
        else if ((argcount = parse_long_opt("output", av, &optarg))) {
                char *path = prefix_filename(prefix, optarg);
                options->file = xfopen(path, "w");