Skip to content

Commit 16bb3d7

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent c380a48 commit 16bb3d7

File tree

4 files changed

+35
-41
lines changed

4 files changed

+35
-41
lines changed

builtin/diff.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -320,26 +320,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
320320

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

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-
}
340323
if (no_index)
341324
/* 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));
343328

344329
/* Otherwise, we are doing the usual "git" diff */
345330
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;

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,35 +234,43 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
233234
}
234235
}
235236

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)
239246
{
240-
int i;
247+
int i, no_index;
241248
const char *paths[2];
242249
struct strbuf replacement = STRBUF_INIT;
243250
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;
244257

245258
/*
246259
* FIXME: --no-index should not look at index and we should be
247260
* able to pass NULL repo. Maybe later.
248261
*/
249262
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);
263272
}
264-
273+
FREE_AND_NULL(options);
265274
for (i = 0; i < 2; i++) {
266275
const char *p = argv[argc - 2 + i];
267276
if (!strcmp(p, "-"))
@@ -293,7 +302,7 @@ void diff_no_index(struct repository *r,
293302
revs->diffopt.flags.exit_with_status = 1;
294303

295304
if (queue_diff(&revs->diffopt, paths[0], paths[1]))
296-
exit(1);
305+
return 1;
297306
diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
298307
diffcore_std(&revs->diffopt);
299308
diff_flush(&revs->diffopt);
@@ -304,5 +313,5 @@ void diff_no_index(struct repository *r,
304313
* The return code for --no-index imitates diff(1):
305314
* 0 = no changes, 1 = changes, else error
306315
*/
307-
exit(diff_result_code(&revs->diffopt, 0));
316+
return diff_result_code(&revs->diffopt, 0);
308317
}

diff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ int diff_flush_patch_id(struct diff_options *, struct object_id *, int);
437437

438438
int diff_result_code(struct diff_options *, int);
439439

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 **);
441442

442443
int index_differs_from(struct repository *r, const char *def,
443444
const struct diff_flags *flags,

t/t4053-diff-no-index.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ test_expect_success 'git diff --no-index executed outside repo gives correct err
5050
export GIT_CEILING_DIRECTORIES &&
5151
cd non/git &&
5252
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
5554
)
5655
'
5756

0 commit comments

Comments
 (0)