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>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2019-01-27 07:35:31 +07:00 committed by Junio C Hamano
parent 2b393ef357
commit 4a28847839
2 changed files with 29 additions and 0 deletions

27
diff.c
View File

@ -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,16 @@ static int parse_objfind_opt(struct diff_options *opt, const char *arg)
return 1;
}
static void prep_parse_options(struct diff_options *options)
{
struct option parseopts[] = {
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,6 +4887,16 @@ 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))

2
diff.h
View File

@ -15,6 +15,7 @@ struct diff_filespec;
struct diff_options;
struct diff_queue_struct;
struct oid_array;
struct option;
struct repository;
struct rev_info;
struct strbuf;
@ -229,6 +230,7 @@ struct diff_options {
unsigned color_moved_ws_handling;
struct repository *repo;
struct option *parseopts;
};
void diff_emit_submodule_del(struct diff_options *o, const char *line);