Skip to content

Commit dcd6a8c

Browse files
committed
Merge branch 'nd/diff-parseopt-4'
Fourth batch to teach the diff machinery to use the parse-options API. * nd/diff-parseopt-4: am: avoid diff_opt_parse() diff --no-index: use parse_options() instead of diff_opt_parse() range-diff: use parse_options() instead of diff_opt_parse() diff.c: allow --no-color-moved-ws diff-parseopt: convert --color-moved-ws diff-parseopt: convert --[no-]color-moved diff-parseopt: convert --inter-hunk-context diff-parseopt: convert --no-prefix diff-parseopt: convert --line-prefix diff-parseopt: convert --[src|dst]-prefix diff-parseopt: convert --[no-]abbrev diff-parseopt: convert --diff-filter diff-parseopt: convert --find-object diff-parseopt: convert -O diff-parseopt: convert --pickaxe-all|--pickaxe-regex diff-parseopt: convert -S|-G diff-parseopt: convert -l diff-parseopt: convert -z diff-parseopt: convert --ita-[in]visible-in-index diff-parseopt: convert --ws-error-highlight
2 parents 4284497 + cdb5330 commit dcd6a8c

File tree

9 files changed

+219
-239
lines changed

9 files changed

+219
-239
lines changed

builtin/am.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,11 +1501,11 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
15011501
* review them with extra care to spot mismerges.
15021502
*/
15031503
struct rev_info rev_info;
1504-
const char *diff_filter_str = "--diff-filter=AM";
15051504

15061505
repo_init_revisions(the_repository, &rev_info, NULL);
15071506
rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
1508-
diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1, rev_info.prefix);
1507+
rev_info.diffopt.filter |= diff_filter_bit('A');
1508+
rev_info.diffopt.filter |= diff_filter_bit('M');
15091509
add_pending_oid(&rev_info, "HEAD", &our_tree, 0);
15101510
diff_setup_done(&rev_info.diffopt);
15111511
run_diff_index(&rev_info, 1);

builtin/diff.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
321321

322322
repo_init_revisions(the_repository, &rev, prefix);
323323

324-
if (no_index && argc != i + 2) {
325-
if (no_index == DIFF_NO_INDEX_IMPLICIT) {
326-
/*
327-
* There was no --no-index and there were not two
328-
* paths. It is possible that the user intended
329-
* to do an inside-repository operation.
330-
*/
331-
fprintf(stderr, "Not a git repository\n");
332-
fprintf(stderr,
333-
"To compare two paths outside a working tree:\n");
334-
}
335-
/* Give the usage message for non-repository usage and exit. */
336-
usagef("git diff %s <path> <path>",
337-
no_index == DIFF_NO_INDEX_EXPLICIT ?
338-
"--no-index" : "[--no-index]");
339-
340-
}
341-
342324
/* Set up defaults that will apply to both no-index and regular diffs. */
343325
rev.diffopt.stat_width = -1;
344326
rev.diffopt.stat_graph_width = -1;
@@ -347,7 +329,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
347329

348330
/* If this is a no-index diff, just run it and exit there. */
349331
if (no_index)
350-
diff_no_index(&rev, argc, argv);
332+
exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
333+
argc, argv));
334+
351335

352336
/*
353337
* Otherwise, we are doing the usual "git" diff; set up any

builtin/range-diff.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,27 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
1616
int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1717
struct diff_options diffopt = { NULL };
1818
int simple_color = -1;
19-
struct option options[] = {
19+
struct option range_diff_options[] = {
2020
OPT_INTEGER(0, "creation-factor", &creation_factor,
2121
N_("Percentage by which creation is weighted")),
2222
OPT_BOOL(0, "no-dual-color", &simple_color,
2323
N_("use simple diff colors")),
2424
OPT_END()
2525
};
26-
int i, j, res = 0;
26+
struct option *options;
27+
int res = 0;
2728
struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
2829

2930
git_config(git_diff_ui_config, NULL);
3031

3132
repo_diff_setup(the_repository, &diffopt);
3233

34+
options = parse_options_concat(range_diff_options, diffopt.parseopts);
3335
argc = parse_options(argc, argv, NULL, options,
34-
builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
35-
PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
36-
37-
for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
38-
int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
36+
builtin_range_diff_usage, 0);
3937

40-
if (!c)
41-
argv[j++] = argv[i++];
42-
else
43-
i += c;
44-
}
45-
while (i < argc)
46-
argv[j++] = argv[i++];
47-
argc = j;
4838
diff_setup_done(&diffopt);
4939

50-
/* Make sure that there are no unparsed options */
51-
argc = parse_options(argc, argv, NULL,
52-
options + ARRAY_SIZE(options) - 1, /* OPT_END */
53-
builtin_range_diff_usage, 0);
54-
5540
/* force color when --dual-color was used */
5641
if (!simple_color)
5742
diffopt.use_color = 1;
@@ -90,6 +75,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
9075
error(_("need two commit ranges"));
9176
usage_with_options(builtin_range_diff_usage, options);
9277
}
78+
FREE_AND_NULL(options);
9379

9480
res = show_range_diff(range1.buf, range2.buf, creation_factor,
9581
simple_color < 1, &diffopt);

diff-no-index.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "revision.h"
1515
#include "log-tree.h"
1616
#include "builtin.h"
17+
#include "parse-options.h"
1718
#include "string-list.h"
1819
#include "dir.h"
1920

@@ -233,29 +234,37 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
233234
}
234235
}
235236

236-
void diff_no_index(struct rev_info *revs,
237-
int argc, const char **argv)
237+
static const char * const diff_no_index_usage[] = {
238+
N_("git diff --no-index [<options>] <path> <path>"),
239+
NULL
240+
};
241+
242+
int diff_no_index(struct rev_info *revs,
243+
int implicit_no_index,
244+
int argc, const char **argv)
238245
{
239-
int i;
246+
int i, no_index;
240247
const char *paths[2];
241248
struct strbuf replacement = STRBUF_INIT;
242249
const char *prefix = revs->prefix;
243-
244-
for (i = 1; i < argc - 2; ) {
245-
int j;
246-
if (!strcmp(argv[i], "--no-index"))
247-
i++;
248-
else if (!strcmp(argv[i], "--"))
249-
i++;
250-
else {
251-
j = diff_opt_parse(&revs->diffopt, argv + i, argc - i,
252-
revs->prefix);
253-
if (j <= 0)
254-
die("invalid diff option/value: %s", argv[i]);
255-
i += j;
256-
}
250+
struct option no_index_options[] = {
251+
OPT_BOOL_F(0, "no-index", &no_index, "",
252+
PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
253+
OPT_END(),
254+
};
255+
struct option *options;
256+
257+
options = parse_options_concat(no_index_options,
258+
revs->diffopt.parseopts);
259+
argc = parse_options(argc, argv, revs->prefix, options,
260+
diff_no_index_usage, 0);
261+
if (argc != 2) {
262+
if (implicit_no_index)
263+
warning(_("Not a git repository. Use --no-index to "
264+
"compare two paths outside a working tree"));
265+
usage_with_options(diff_no_index_usage, options);
257266
}
258-
267+
FREE_AND_NULL(options);
259268
for (i = 0; i < 2; i++) {
260269
const char *p = argv[argc - 2 + i];
261270
if (!strcmp(p, "-"))
@@ -287,7 +296,7 @@ void diff_no_index(struct rev_info *revs,
287296
revs->diffopt.flags.exit_with_status = 1;
288297

289298
if (queue_diff(&revs->diffopt, paths[0], paths[1]))
290-
exit(1);
299+
return 1;
291300
diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
292301
diffcore_std(&revs->diffopt);
293302
diff_flush(&revs->diffopt);
@@ -298,5 +307,5 @@ void diff_no_index(struct rev_info *revs,
298307
* The return code for --no-index imitates diff(1):
299308
* 0 = no changes, 1 = changes, else error
300309
*/
301-
exit(diff_result_code(&revs->diffopt, 0));
310+
return diff_result_code(&revs->diffopt, 0);
302311
}

0 commit comments

Comments
 (0)