Skip to content

Commit 287ab28

Browse files
peffgitster
authored andcommitted
diff: reuse diff setup for --no-index case
When "--no-index" is in effect (or implied by the arguments), git-diff jumps early to a special code path to perform that diff. This means we miss out on some settings like enabling --ext-diff and --textconv by default. Let's jump to the no-index path _after_ we've done more setup on rev.diffopt. Since some of the options don't affect us (e.g., items related to the index), let's re-order the setup into two blocks (see the in-code comments). Note that we also need to stop re-initializing the diffopt struct in diff_no_index(). This should not be necessary, as it will already have been initialized by cmd_diff() (and there are no other callers). That in turn lets us drop the "repository" argument from diff_no_index (which never made much sense, since the whole point is that you don't need a repository). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0d0ac38 commit 287ab28

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

builtin/diff.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,21 +337,23 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
337337
"--no-index" : "[--no-index]");
338338

339339
}
340-
if (no_index)
341-
/* If this is a no-index diff, just run it and exit there. */
342-
diff_no_index(the_repository, &rev, argc, argv);
343-
344-
/* Otherwise, we are doing the usual "git" diff */
345-
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
346340

347-
/* Scale to real terminal size and respect statGraphWidth config */
341+
/* Set up defaults that will apply to both no-index and regular diffs. */
348342
rev.diffopt.stat_width = -1;
349343
rev.diffopt.stat_graph_width = -1;
350-
351-
/* Default to let external and textconv be used */
352344
rev.diffopt.flags.allow_external = 1;
353345
rev.diffopt.flags.allow_textconv = 1;
354346

347+
/* If this is a no-index diff, just run it and exit there. */
348+
if (no_index)
349+
diff_no_index(&rev, argc, argv);
350+
351+
/*
352+
* Otherwise, we are doing the usual "git" diff; set up any
353+
* further defaults that apply to regular diffs.
354+
*/
355+
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
356+
355357
/*
356358
* Default to intent-to-add entries invisible in the
357359
* index. This makes them show up as new files in diff-files

diff-no-index.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,14 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
233233
}
234234
}
235235

236-
void diff_no_index(struct repository *r,
237-
struct rev_info *revs,
236+
void diff_no_index(struct rev_info *revs,
238237
int argc, const char **argv)
239238
{
240239
int i;
241240
const char *paths[2];
242241
struct strbuf replacement = STRBUF_INIT;
243242
const char *prefix = revs->prefix;
244243

245-
/*
246-
* FIXME: --no-index should not look at index and we should be
247-
* able to pass NULL repo. Maybe later.
248-
*/
249-
repo_diff_setup(r, &revs->diffopt);
250244
for (i = 1; i < argc - 2; ) {
251245
int j;
252246
if (!strcmp(argv[i], "--no-index"))

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ int diff_flush_patch_id(struct diff_options *, struct object_id *, int);
434434

435435
int diff_result_code(struct diff_options *, int);
436436

437-
void diff_no_index(struct repository *, struct rev_info *, int, const char **);
437+
void diff_no_index(struct rev_info *, int, const char **);
438438

439439
int index_differs_from(const char *def, const struct diff_flags *flags,
440440
int ita_invisible_in_index);

t/t4053-diff-no-index.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,12 @@ test_expect_success 'diff --no-index from repo subdir with absolute paths' '
137137
test_cmp expect actual
138138
'
139139

140+
test_expect_success 'diff --no-index allows external diff' '
141+
test_expect_code 1 \
142+
env GIT_EXTERNAL_DIFF="echo external ;:" \
143+
git diff --no-index non/git/a non/git/b >actual &&
144+
echo external >expect &&
145+
test_cmp expect actual
146+
'
147+
140148
test_done

0 commit comments

Comments
 (0)