Skip to content

Commit 73b0631

Browse files
committed
Merge branch 'tg/diff-no-index-refactor'
"git diff ../else/where/A ../else/where/B" when ../else/where is clearly outside the repository, and "git diff --no-index A B", do not have to look at the index at all, but we used to read the index unconditionally. * tg/diff-no-index-refactor: diff: avoid some nesting diff: add test for --no-index executed outside repo diff: don't read index when --no-index is given diff: move no-index detection to builtin/diff.c
2 parents 6904f9a + aad90e8 commit 73b0631

File tree

5 files changed

+103
-49
lines changed

5 files changed

+103
-49
lines changed

builtin/diff.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include "submodule.h"
1717
#include "sha1-array.h"
1818

19+
#define DIFF_NO_INDEX_EXPLICIT 1
20+
#define DIFF_NO_INDEX_IMPLICIT 2
21+
1922
struct blobinfo {
2023
unsigned char sha1[20];
2124
const char *name;
@@ -259,7 +262,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
259262
struct object_array ent = OBJECT_ARRAY_INIT;
260263
int blobs = 0, paths = 0;
261264
struct blobinfo blob[2];
262-
int nongit;
265+
int nongit = 0, no_index = 0;
263266
int result = 0;
264267

265268
/*
@@ -285,14 +288,59 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
285288
* Other cases are errors.
286289
*/
287290

288-
prefix = setup_git_directory_gently(&nongit);
289-
gitmodules_config();
291+
/* Were we asked to do --no-index explicitly? */
292+
for (i = 1; i < argc; i++) {
293+
if (!strcmp(argv[i], "--")) {
294+
i++;
295+
break;
296+
}
297+
if (!strcmp(argv[i], "--no-index"))
298+
no_index = DIFF_NO_INDEX_EXPLICIT;
299+
if (argv[i][0] != '-')
300+
break;
301+
}
302+
303+
if (!no_index)
304+
prefix = setup_git_directory_gently(&nongit);
305+
306+
/*
307+
* Treat git diff with at least one path outside of the
308+
* repo the same as if the command would have been executed
309+
* outside of a git repository. In this case it behaves
310+
* the same way as "git diff --no-index <a> <b>", which acts
311+
* as a colourful "diff" replacement.
312+
*/
313+
if (nongit || ((argc == i + 2) &&
314+
(!path_inside_repo(prefix, argv[i]) ||
315+
!path_inside_repo(prefix, argv[i + 1]))))
316+
no_index = DIFF_NO_INDEX_IMPLICIT;
317+
318+
if (!no_index)
319+
gitmodules_config();
290320
git_config(git_diff_ui_config, NULL);
291321

292322
init_revisions(&rev, prefix);
293323

294-
/* If this is a no-index diff, just run it and exit there. */
295-
diff_no_index(&rev, argc, argv, nongit, prefix);
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+
if (no_index)
342+
/* If this is a no-index diff, just run it and exit there. */
343+
diff_no_index(&rev, argc, argv, prefix);
296344

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

diff-no-index.c

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -183,54 +183,12 @@ static int queue_diff(struct diff_options *o,
183183

184184
void diff_no_index(struct rev_info *revs,
185185
int argc, const char **argv,
186-
int nongit, const char *prefix)
186+
const char *prefix)
187187
{
188188
int i, prefixlen;
189-
int no_index = 0;
190189
unsigned deprecated_show_diff_q_option_used = 0;
191190
const char *paths[2];
192191

193-
/* Were we asked to do --no-index explicitly? */
194-
for (i = 1; i < argc; i++) {
195-
if (!strcmp(argv[i], "--")) {
196-
i++;
197-
break;
198-
}
199-
if (!strcmp(argv[i], "--no-index"))
200-
no_index = 1;
201-
if (argv[i][0] != '-')
202-
break;
203-
}
204-
205-
if (!no_index && !nongit) {
206-
/*
207-
* Inside a git repository, without --no-index. Only
208-
* when a path outside the repository is given,
209-
* e.g. "git diff /var/tmp/[12]", or "git diff
210-
* Makefile /var/tmp/Makefile", allow it to be used as
211-
* a colourful "diff" replacement.
212-
*/
213-
if ((argc != i + 2) ||
214-
(path_inside_repo(prefix, argv[i]) &&
215-
path_inside_repo(prefix, argv[i+1])))
216-
return;
217-
}
218-
if (argc != i + 2) {
219-
if (!no_index) {
220-
/*
221-
* There was no --no-index and there were not two
222-
* paths. It is possible that the user intended
223-
* to do an inside-repository operation.
224-
*/
225-
fprintf(stderr, "Not a git repository\n");
226-
fprintf(stderr,
227-
"To compare two paths outside a working tree:\n");
228-
}
229-
/* Give the usage message for non-repository usage and exit. */
230-
usagef("git diff %s <path> <path>",
231-
no_index ? "--no-index" : "[--no-index]");
232-
}
233-
234192
diff_setup(&revs->diffopt);
235193
for (i = 1; i < argc - 2; ) {
236194
int j;

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
332332

333333
extern int diff_result_code(struct diff_options *, int);
334334

335-
extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
335+
extern void diff_no_index(struct rev_info *, int, const char **, const char *);
336336

337337
extern int index_differs_from(const char *def, int diff_flags);
338338

t/perf/p4001-diff-no-index.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
test_description="Test diff --no-index performance"
4+
5+
. ./perf-lib.sh
6+
7+
test_perf_large_repo
8+
test_checkout_worktree
9+
10+
file1=$(git ls-files | tail -n 2 | head -1)
11+
file2=$(git ls-files | tail -n 1 | head -1)
12+
13+
test_expect_success "empty files, so they take no time to diff" "
14+
echo >$file1 &&
15+
echo >$file2
16+
"
17+
18+
test_perf "diff --no-index" "
19+
git diff --no-index $file1 $file2 >/dev/null
20+
"
21+
22+
test_done

t/t4053-diff-no-index.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,30 @@ test_expect_success 'git diff --no-index relative path outside repo' '
2929
)
3030
'
3131

32+
test_expect_success 'git diff --no-index with broken index' '
33+
(
34+
cd repo &&
35+
echo broken >.git/index &&
36+
git diff --no-index a ../non/git/a
37+
)
38+
'
39+
40+
test_expect_success 'git diff outside repo with broken index' '
41+
(
42+
cd repo &&
43+
git diff ../non/git/a ../non/git/b
44+
)
45+
'
46+
47+
test_expect_success 'git diff --no-index executed outside repo gives correct error message' '
48+
(
49+
GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
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
55+
)
56+
'
57+
3258
test_done

0 commit comments

Comments
 (0)