diff.c: prepare to use parse_options() for parsing

This is a preparation step to start using parse_options() to parse diff/revision options instead of what we have now. There are a couple of good things from using parse_options(): - better help usage - easier to add new options - better completion support - help usage generation - better integration with main command option parser. We can just concat the main command's option array and diffopt's together and parse all in one go. - detect colidding options (e.g. --reverse is used by revision code, so diff code can't use it as long name for -R) - consistent syntax, e.g. option that takes mandatory argument will now accept both "--option=value" and "--option value". The plan is migrate all diff/rev options to parse_options(). Then we could get rid of diff_opt_parse() and expose parseopts[] directly to the caller. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 27, 2019 at 07:35 UTC 4a2884783949d1791ee5d720d8440d5536ebdc91
2 files changed +29
diff.c
+27
@@ -23,6 +23,7 @@
23 #include "argv-array.h"
24 #include "graph.h"
25 #include "packfile.h"
26 +#include "parse-options.h"
27 #include "help.h"
28
29 #ifdef NO_FAST_WORKING_DIRECTORY
@@ -4425,6 +4426,8 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4426 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
4427 }
4428
4429 +static void prep_parse_options(struct diff_options *options);
4430 +
4431 void repo_diff_setup(struct repository *r, struct diff_options *options)
4432 {
4433 memcpy(options, &default_diff_options, sizeof(*options));
@@ -4466,6 +4469,8 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
4469
4470 options->color_moved = diff_color_moved_default;
4471 options->color_moved_ws_handling = diff_color_moved_ws_default;
4472 +
4473 + prep_parse_options(options);
4474 }
4475
4476 void diff_setup_done(struct diff_options *options)
@@ -4569,6 +4574,8 @@ void diff_setup_done(struct diff_options *options)
4574
4575 if (!options->use_color || external_diff())
4576 options->color_moved = 0;
4577 +
4578 + FREE_AND_NULL(options->parseopts);
4579 }
4580
4581 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
@@ -4860,6 +4867,16 @@ static int parse_objfind_opt(struct diff_options *opt, const char *arg)
4867 return 1;
4868 }
4869
4870 +static void prep_parse_options(struct diff_options *options)
4871 +{
4872 + struct option parseopts[] = {
4873 + OPT_END()
4874 + };
4875 +
4876 + ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
4877 + memcpy(options->parseopts, parseopts, sizeof(parseopts));
4878 +}
4879 +
4880 int diff_opt_parse(struct diff_options *options,
4881 const char **av, int ac, const char *prefix)
4882 {
@@ -4870,6 +4887,16 @@ int diff_opt_parse(struct diff_options *options,
4887 if (!prefix)
4888 prefix = "";
4889
4890 + ac = parse_options(ac, av, prefix, options->parseopts, NULL,
4891 + PARSE_OPT_KEEP_DASHDASH |
4892 + PARSE_OPT_KEEP_UNKNOWN |
4893 + PARSE_OPT_NO_INTERNAL_HELP |
4894 + PARSE_OPT_ONE_SHOT |
4895 + PARSE_OPT_STOP_AT_NON_OPTION);
4896 +
4897 + if (ac)
4898 + return ac;
4899 +
4900 /* Output format options */
4901 if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
4902 || opt_arg(arg, 'U', "unified", &options->context))
diff.h
+2
@@ -15,6 +15,7 @@ struct diff_filespec;
15 struct diff_options;
16 struct diff_queue_struct;
17 struct oid_array;
18 +struct option;
19 struct repository;
20 struct rev_info;
21 struct strbuf;
@@ -229,6 +230,7 @@ struct diff_options {
230 unsigned color_moved_ws_handling;
231
232 struct repository *repo;
233 + struct option *parseopts;
234 };
235
236 void diff_emit_submodule_del(struct diff_options *o, const char *line);