diff-merges: move specific diff-index "-m" handling to diff-index

Move specific handling of "-m" for diff-index to diff-index.c, so diff-merges is left to handle only diff for merges options. Being a better design by itself, this is especially essential in preparation for letting -m imply -p, as "diff-index -m" obviously should not imply -p, as it's entirely unrelated. To handle this, in addition to moving specific diff-index "-m" code out of diff-merges, we introduce new diff_merges_suppress_options_parsing() and call it before generic options processing in cmd_diff_index(). This new diff_merges_suppress_options_parsing() could then be reused and called before invocations of setup_revisions() for other commands that don't need --diff-merges options, but that's outside of the scope of these patch series. Signed-off-by: Sergey Organov <sorganov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sergey Organov committed May 21, 2021 at 00:46 UTC 19b2517f95a0a908a8ada7417cf0717299e7e1aa
3 files changed +24 -12
builtin/diff-index.c
+9
@@ -2,6 +2,7 @@
2 #include "cache.h"
3 #include "config.h"
4 #include "diff.h"
5 +#include "diff-merges.h"
6 #include "commit.h"
7 #include "revision.h"
8 #include "builtin.h"
@@ -27,6 +28,12 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
28 rev.abbrev = 0;
29 prefix = precompose_argv_prefix(argc, argv, prefix);
30
31 + /*
32 + * We need no diff for merges options, and we need to avoid conflict
33 + * with our own meaning of "-m".
34 + */
35 + diff_merges_suppress_options_parsing();
36 +
37 argc = setup_revisions(argc, argv, &rev, NULL);
38 for (i = 1; i < argc; i++) {
39 const char *arg = argv[i];
@@ -35,6 +42,8 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
42 option |= DIFF_INDEX_CACHED;
43 else if (!strcmp(arg, "--merge-base"))
44 option |= DIFF_INDEX_MERGE_BASE;
45 + else if (!strcmp(arg, "-m"))
46 + rev.match_missing = 1;
47 else
48 usage(diff_cache_usage);
49 }
diff-merges.c
+13 -12
@@ -6,6 +6,7 @@ typedef void (*diff_merges_setup_func_t)(struct rev_info *);
6 static void set_separate(struct rev_info *revs);
7
8 static diff_merges_setup_func_t set_to_default = set_separate;
9 +static int suppress_parsing;
10
11 static void suppress(struct rev_info *revs)
12 {
@@ -30,17 +31,6 @@ static void set_first_parent(struct rev_info *revs)
31 revs->first_parent_merges = 1;
32 }
33
33 -static void set_m(struct rev_info *revs)
34 -{
35 - /*
36 - * To "diff-index", "-m" means "match missing", and to the "log"
37 - * family of commands, it means "show default diff for merges". Set
38 - * both fields appropriately.
39 - */
40 - set_to_default(revs);
41 - revs->match_missing = 1;
42 -}
43 -
34 static void set_combined(struct rev_info *revs)
35 {
36 suppress(revs);
@@ -101,14 +91,22 @@ int diff_merges_config(const char *value)
91 return 0;
92 }
93
94 +void diff_merges_suppress_options_parsing(void)
95 +{
96 + suppress_parsing = 1;
97 +}
98 +
99 int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
100 {
101 int argcount = 1;
102 const char *optarg;
103 const char *arg = argv[0];
104
105 + if (suppress_parsing)
106 + return 0;
107 +
108 if (!strcmp(arg, "-m")) {
111 - set_m(revs);
109 + set_to_default(revs);
110 } else if (!strcmp(arg, "-c")) {
111 set_combined(revs);
112 revs->combined_imply_patch = 1;
@@ -155,6 +153,9 @@ void diff_merges_set_dense_combined_if_unset(struct rev_info *revs)
153
154 void diff_merges_setup_revs(struct rev_info *revs)
155 {
156 + if (suppress_parsing)
157 + return;
158 +
159 if (revs->combine_merges == 0)
160 revs->dense_combined_merges = 0;
161 if (revs->separate_merges == 0)
diff-merges.h
+2
@@ -11,6 +11,8 @@ struct rev_info;
11
12 int diff_merges_config(const char *value);
13
14 +void diff_merges_suppress_options_parsing(void);
15 +
16 int diff_merges_parse_opts(struct rev_info *revs, const char **argv);
17
18 void diff_merges_suppress(struct rev_info *revs);