Skip to content

Commit 3beb56b

Browse files
committed
Merge branch 'jc/diff-no-no-index'
* jc/diff-no-no-index: git diff --no-index: default to page like other diff frontends git-diff: allow --no-index semantics a bit more "git diff": do not ignore index without --no-index diff-files: do not play --no-index games tests: do not use implicit "git diff --no-index"
2 parents 2fe1839 + b3fde6c commit 3beb56b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+740
-738
lines changed

Documentation/git-diff-files.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-diff-files - Compares files in the working tree and the index
88

99
SYNOPSIS
1010
--------
11-
'git-diff-files' [-q] [-0|-1|-2|-3|-c|--cc|--no-index] [<common diff options>] [<path>...]
11+
'git-diff-files' [-q] [-0|-1|-2|-3|-c|--cc] [<common diff options>] [<path>...]
1212

1313
DESCRIPTION
1414
-----------
@@ -36,9 +36,6 @@ omit diff output for unmerged entries and just show "Unmerged".
3636
diff, similar to the way 'diff-tree' shows a merge
3737
commit with these flags.
3838

39-
--no-index::
40-
Compare the two given files / directories.
41-
4239
-q::
4340
Remain silent even on nonexistent files
4441

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ LIB_OBJS += diffcore-order.o
405405
LIB_OBJS += diffcore-pickaxe.o
406406
LIB_OBJS += diffcore-rename.o
407407
LIB_OBJS += diff-delta.o
408+
LIB_OBJS += diff-no-index.o
408409
LIB_OBJS += diff-lib.o
409410
LIB_OBJS += diff.o
410411
LIB_OBJS += dir.o

builtin-diff-files.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,54 @@
1010
#include "builtin.h"
1111

1212
static const char diff_files_usage[] =
13-
"git-diff-files [-q] [-0/-1/2/3 |-c|--cc|--no-index] [<common diff options>] [<path>...]"
13+
"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
1414
COMMON_DIFF_OPTIONS_HELP;
1515

1616
int cmd_diff_files(int argc, const char **argv, const char *prefix)
1717
{
1818
struct rev_info rev;
19-
int nongit;
2019
int result;
20+
unsigned options = 0;
2121

22-
prefix = setup_git_directory_gently(&nongit);
2322
init_revisions(&rev, prefix);
2423
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
2524
rev.abbrev = 0;
2625

27-
if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
28-
argc = 0;
29-
else
30-
argc = setup_revisions(argc, argv, &rev, NULL);
26+
argc = setup_revisions(argc, argv, &rev, NULL);
27+
while (1 < argc && argv[1][0] == '-') {
28+
if (!strcmp(argv[1], "--base"))
29+
rev.max_count = 1;
30+
else if (!strcmp(argv[1], "--ours"))
31+
rev.max_count = 2;
32+
else if (!strcmp(argv[1], "--theirs"))
33+
rev.max_count = 3;
34+
else if (!strcmp(argv[1], "-q"))
35+
options |= DIFF_SILENT_ON_REMOVED;
36+
else
37+
usage(diff_files_usage);
38+
argv++; argc--;
39+
}
3140
if (!rev.diffopt.output_format)
3241
rev.diffopt.output_format = DIFF_FORMAT_RAW;
33-
result = run_diff_files_cmd(&rev, argc, argv);
42+
43+
/*
44+
* Make sure there are NO revision (i.e. pending object) parameter,
45+
* rev.max_count is reasonable (0 <= n <= 3), and
46+
* there is no other revision filtering parameters.
47+
*/
48+
if (rev.pending.nr ||
49+
rev.min_age != -1 || rev.max_age != -1 ||
50+
3 < rev.max_count)
51+
usage(diff_files_usage);
52+
53+
if (rev.max_count == -1 &&
54+
(rev.diffopt.output_format & DIFF_FORMAT_PATCH))
55+
rev.combine_merges = rev.dense_combined_merges = 1;
56+
57+
if (read_cache() < 0) {
58+
perror("read_cache");
59+
return -1;
60+
}
61+
result = run_diff_files(&rev, options);
3462
return diff_result_code(&rev.diffopt, result);
3563
}

builtin-diff.c

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,37 @@ static void refresh_index_quietly(void)
202202
rollback_lock_file(lock_file);
203203
}
204204

205+
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
206+
{
207+
int result;
208+
unsigned int options = 0;
209+
210+
while (1 < argc && argv[1][0] == '-') {
211+
if (!strcmp(argv[1], "--base"))
212+
revs->max_count = 1;
213+
else if (!strcmp(argv[1], "--ours"))
214+
revs->max_count = 2;
215+
else if (!strcmp(argv[1], "--theirs"))
216+
revs->max_count = 3;
217+
else if (!strcmp(argv[1], "-q"))
218+
options |= DIFF_SILENT_ON_REMOVED;
219+
else
220+
return error("invalid option: %s", argv[1]);
221+
argv++; argc--;
222+
}
223+
224+
if (revs->max_count == -1 &&
225+
(revs->diffopt.output_format & DIFF_FORMAT_PATCH))
226+
revs->combine_merges = revs->dense_combined_merges = 1;
227+
228+
if (read_cache() < 0) {
229+
perror("read_cache");
230+
return -1;
231+
}
232+
result = run_diff_files(revs, options);
233+
return diff_result_code(&revs->diffopt, result);
234+
}
235+
205236
int cmd_diff(int argc, const char **argv, const char *prefix)
206237
{
207238
int i;
@@ -230,6 +261,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
230261
* N=2, M=0:
231262
* tree vs tree (diff-tree)
232263
*
264+
* N=0, M=0, P=2:
265+
* compare two filesystem entities (aka --no-index).
266+
*
233267
* Other cases are errors.
234268
*/
235269

@@ -240,21 +274,21 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
240274
diff_use_color_default = git_use_color_default;
241275

242276
init_revisions(&rev, prefix);
277+
278+
/* If this is a no-index diff, just run it and exit there. */
279+
diff_no_index(&rev, argc, argv, nongit, prefix);
280+
281+
/* Otherwise, we are doing the usual "git" diff */
243282
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
244283

245-
if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
246-
argc = 0;
247-
else
248-
argc = setup_revisions(argc, argv, &rev, NULL);
284+
if (nongit)
285+
die("Not a git repository");
286+
argc = setup_revisions(argc, argv, &rev, NULL);
249287
if (!rev.diffopt.output_format) {
250288
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
251289
if (diff_setup_done(&rev.diffopt) < 0)
252290
die("diff_setup_done failed");
253291
}
254-
if (rev.diffopt.prefix && nongit) {
255-
rev.diffopt.prefix = NULL;
256-
rev.diffopt.prefix_length = 0;
257-
}
258292
DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
259293
DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
260294

@@ -265,7 +299,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
265299
if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
266300
setup_pager();
267301

268-
/* Do we have --cached and not have a pending object, then
302+
/*
303+
* Do we have --cached and not have a pending object, then
269304
* default to HEAD by hand. Eek.
270305
*/
271306
if (!rev.pending.nr) {
@@ -333,7 +368,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
333368
if (!ents) {
334369
switch (blobs) {
335370
case 0:
336-
result = run_diff_files_cmd(&rev, argc, argv);
371+
result = builtin_diff_files(&rev, argc, argv);
337372
break;
338373
case 1:
339374
if (paths != 1)

0 commit comments

Comments
 (0)