diff --no-index: use parse_options() instead of diff_opt_parse()

While at there, move exit() back to the caller. It's easier to see the flow that way than burying it in diff-no-index.c 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 Mar 24, 2019 at 15:20 UTC 16bb3d714dcb1bf50f7f96b957c4306dc6df137e
4 files changed +35 -41
builtin/diff.c
+3 -18
@@ -320,26 +320,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
320
321 repo_init_revisions(the_repository, &rev, prefix);
322
323 - if (no_index && argc != i + 2) {
324 - if (no_index == DIFF_NO_INDEX_IMPLICIT) {
325 - /*
326 - * There was no --no-index and there were not two
327 - * paths. It is possible that the user intended
328 - * to do an inside-repository operation.
329 - */
330 - fprintf(stderr, "Not a git repository\n");
331 - fprintf(stderr,
332 - "To compare two paths outside a working tree:\n");
333 - }
334 - /* Give the usage message for non-repository usage and exit. */
335 - usagef("git diff %s <path> <path>",
336 - no_index == DIFF_NO_INDEX_EXPLICIT ?
337 - "--no-index" : "[--no-index]");
338 -
339 - }
323 if (no_index)
324 /* If this is a no-index diff, just run it and exit there. */
342 - diff_no_index(the_repository, &rev, argc, argv);
325 + exit(diff_no_index(the_repository, &rev,
326 + no_index == DIFF_NO_INDEX_IMPLICIT,
327 + argc, argv));
328
329 /* Otherwise, we are doing the usual "git" diff */
330 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
diff-no-index.c
+29 -20
@@ -14,6 +14,7 @@
14 #include "revision.h"
15 #include "log-tree.h"
16 #include "builtin.h"
17 +#include "parse-options.h"
18 #include "string-list.h"
19 #include "dir.h"
20
@@ -233,35 +234,43 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
234 }
235 }
236
236 -void diff_no_index(struct repository *r,
237 - struct rev_info *revs,
238 - 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 repository *r,
243 + struct rev_info *revs,
244 + int implicit_no_index,
245 + int argc, const char **argv)
246 {
240 - int i;
247 + int i, no_index;
248 const char *paths[2];
249 struct strbuf replacement = STRBUF_INIT;
250 const char *prefix = revs->prefix;
251 + struct option no_index_options[] = {
252 + OPT_BOOL_F(0, "no-index", &no_index, "",
253 + PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
254 + OPT_END(),
255 + };
256 + struct option *options;
257
258 /*
259 * FIXME: --no-index should not look at index and we should be
260 * able to pass NULL repo. Maybe later.
261 */
262 repo_diff_setup(r, &revs->diffopt);
250 - for (i = 1; i < argc - 2; ) {
251 - int j;
252 - if (!strcmp(argv[i], "--no-index"))
253 - i++;
254 - else if (!strcmp(argv[i], "--"))
255 - i++;
256 - else {
257 - j = diff_opt_parse(&revs->diffopt, argv + i, argc - i,
258 - revs->prefix);
259 - if (j <= 0)
260 - die("invalid diff option/value: %s", argv[i]);
261 - i += j;
262 - }
263 + options = parse_options_concat(no_index_options,
264 + revs->diffopt.parseopts);
265 + argc = parse_options(argc, argv, revs->prefix, options,
266 + diff_no_index_usage, 0);
267 + if (argc != 2) {
268 + if (implicit_no_index)
269 + warning(_("Not a git repository. Use --no-index to "
270 + "compare two paths outside a working tree"));
271 + usage_with_options(diff_no_index_usage, options);
272 }
264 -
273 + FREE_AND_NULL(options);
274 for (i = 0; i < 2; i++) {
275 const char *p = argv[argc - 2 + i];
276 if (!strcmp(p, "-"))
@@ -293,7 +302,7 @@ void diff_no_index(struct repository *r,
302 revs->diffopt.flags.exit_with_status = 1;
303
304 if (queue_diff(&revs->diffopt, paths[0], paths[1]))
296 - exit(1);
305 + return 1;
306 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
307 diffcore_std(&revs->diffopt);
308 diff_flush(&revs->diffopt);
@@ -304,5 +313,5 @@ void diff_no_index(struct repository *r,
313 * The return code for --no-index imitates diff(1):
314 * 0 = no changes, 1 = changes, else error
315 */
307 - exit(diff_result_code(&revs->diffopt, 0));
316 + return diff_result_code(&revs->diffopt, 0);
317 }
diff.h
+2 -1
@@ -437,7 +437,8 @@ int diff_flush_patch_id(struct diff_options *, struct object_id *, int);
437
438 int diff_result_code(struct diff_options *, int);
439
440 -void diff_no_index(struct repository *, struct rev_info *, int, const char **);
440 +int diff_no_index(struct repository *, struct rev_info *,
441 + int implicit_no_index, int, const char **);
442
443 int index_differs_from(struct repository *r, const char *def,
444 const struct diff_flags *flags,
t/t4053-diff-no-index.sh
+1 -2
@@ -50,8 +50,7 @@ test_expect_success 'git diff --no-index executed outside repo gives correct err
50 export GIT_CEILING_DIRECTORIES &&
51 cd non/git &&
52 test_must_fail git diff --no-index a 2>actual.err &&
53 - echo "usage: git diff --no-index <path> <path>" >expect.err &&
54 - test_cmp expect.err actual.err
53 + test_i18ngrep "usage: git diff --no-index" actual.err
54 )
55 '
56