Skip to content

Commit 4a28847

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2b393ef commit 4a28847

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

diff.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "argv-array.h"
2424
#include "graph.h"
2525
#include "packfile.h"
26+
#include "parse-options.h"
2627
#include "help.h"
2728

2829
#ifdef NO_FAST_WORKING_DIRECTORY
@@ -4425,6 +4426,8 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
44254426
builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
44264427
}
44274428

4429+
static void prep_parse_options(struct diff_options *options);
4430+
44284431
void repo_diff_setup(struct repository *r, struct diff_options *options)
44294432
{
44304433
memcpy(options, &default_diff_options, sizeof(*options));
@@ -4466,6 +4469,8 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
44664469

44674470
options->color_moved = diff_color_moved_default;
44684471
options->color_moved_ws_handling = diff_color_moved_ws_default;
4472+
4473+
prep_parse_options(options);
44694474
}
44704475

44714476
void diff_setup_done(struct diff_options *options)
@@ -4569,6 +4574,8 @@ void diff_setup_done(struct diff_options *options)
45694574

45704575
if (!options->use_color || external_diff())
45714576
options->color_moved = 0;
4577+
4578+
FREE_AND_NULL(options->parseopts);
45724579
}
45734580

45744581
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)
48604867
return 1;
48614868
}
48624869

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+
48634880
int diff_opt_parse(struct diff_options *options,
48644881
const char **av, int ac, const char *prefix)
48654882
{
@@ -4870,6 +4887,16 @@ int diff_opt_parse(struct diff_options *options,
48704887
if (!prefix)
48714888
prefix = "";
48724889

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+
48734900
/* Output format options */
48744901
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
48754902
|| opt_arg(arg, 'U', "unified", &options->context))

diff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct diff_filespec;
1515
struct diff_options;
1616
struct diff_queue_struct;
1717
struct oid_array;
18+
struct option;
1819
struct repository;
1920
struct rev_info;
2021
struct strbuf;
@@ -229,6 +230,7 @@ struct diff_options {
229230
unsigned color_moved_ws_handling;
230231

231232
struct repository *repo;
233+
struct option *parseopts;
232234
};
233235

234236
void diff_emit_submodule_del(struct diff_options *o, const char *line);

0 commit comments

Comments
 (0)