]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff.c
Merge branch 'mk/doc-gitfile-more' into maint-2.43
[thirdparty/git.git] / builtin / diff.c
CommitLineData
65056021
JH
1/*
2 * Builtin "git diff"
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
07047d68 6#define USE_THE_INDEX_VARIABLE
bc5c5ec0 7#include "builtin.h"
b2141fc1 8#include "config.h"
8bfcb3a6 9#include "ewah/ewok.h"
697cc8ef 10#include "lockfile.h"
6b2f2d98 11#include "color.h"
65056021
JH
12#include "commit.h"
13#include "blob.h"
f394e093 14#include "gettext.h"
65056021
JH
15#include "tag.h"
16#include "diff.h"
3b6c17b5 17#include "diff-merges.h"
65056021 18#include "diffcore.h"
fbffdfb1 19#include "preload-index.h"
08c46a49 20#include "read-cache-ll.h"
65056021
JH
21#include "revision.h"
22#include "log-tree.h"
e38da487 23#include "setup.h"
302ad7a9 24#include "submodule.h"
fe299ec5 25#include "oid-array.h"
d4a4f929 26#include "tree.h"
65056021 27
470faf96
TG
28#define DIFF_NO_INDEX_EXPLICIT 1
29#define DIFF_NO_INDEX_IMPLICIT 2
30
65056021 31static const char builtin_diff_usage[] =
b7e10b2c 32"git diff [<options>] [<commit>] [--] [<path>...]\n"
eb448631
DL
33" or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
34" or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
74f3390d
SY
35" or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
36" or: git diff [<options>] <blob> <blob>\n"
acf7828e
ÆAB
37" or: git diff [<options>] --no-index [--] <path> <path>"
38"\n"
b7e10b2c 39COMMON_DIFF_OPTIONS_HELP;
65056021 40
158b06ca
JK
41static const char *blob_path(struct object_array_entry *entry)
42{
43 return entry->path ? entry->path : entry->name;
44}
45
65056021
JH
46static void stuff_change(struct diff_options *opt,
47 unsigned old_mode, unsigned new_mode,
9c4b0f66 48 const struct object_id *old_oid,
49 const struct object_id *new_oid,
50 int old_oid_valid,
51 int new_oid_valid,
d04ec74b
JK
52 const char *old_path,
53 const char *new_path)
65056021
JH
54{
55 struct diff_filespec *one, *two;
56
9c4b0f66 57 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
4a7e27e9 58 oideq(old_oid, new_oid) && (old_mode == new_mode))
65056021
JH
59 return;
60
0d1e0e78 61 if (opt->flags.reverse_diff) {
35d803bc 62 SWAP(old_mode, new_mode);
9c4b0f66 63 SWAP(old_oid, new_oid);
d04ec74b 64 SWAP(old_path, new_path);
65056021 65 }
cd676a51
JH
66
67 if (opt->prefix &&
d04ec74b
JK
68 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
69 strncmp(new_path, opt->prefix, opt->prefix_length)))
cd676a51
JH
70 return;
71
d04ec74b
JK
72 one = alloc_filespec(old_path);
73 two = alloc_filespec(new_path);
f9704c2d
BW
74 fill_filespec(one, old_oid, old_oid_valid, old_mode);
75 fill_filespec(two, new_oid, new_oid_valid, new_mode);
65056021 76
65056021
JH
77 diff_queue(&diff_queued_diff, one, two);
78}
79
c0049ca0
JK
80static void builtin_diff_b_f(struct rev_info *revs,
81 int argc, const char **argv UNUSED,
82 struct object_array_entry **blob)
65056021
JH
83{
84 /* Blob vs file in the working tree*/
85 struct stat st;
887c6c18 86 const char *path;
65056021 87
a610786f
TH
88 if (argc > 1)
89 usage(builtin_diff_usage);
90
887c6c18
NTND
91 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
92 path = revs->prune_data.items[0].match;
93
65056021 94 if (lstat(path, &st))
54214529 95 die_errno(_("failed to stat '%s'"), path);
65056021 96 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
54214529 97 die(_("'%s': not a regular file or symlink"), path);
01618a3a 98
a5a818ee
JH
99 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
100
42f5ba5b
JK
101 if (blob[0]->mode == S_IFINVALID)
102 blob[0]->mode = canon_mode(st.st_mode);
01618a3a 103
65056021 104 stuff_change(&revs->diffopt,
42f5ba5b 105 blob[0]->mode, canon_mode(st.st_mode),
14228447 106 &blob[0]->item->oid, null_oid(),
e5450100 107 1, 0,
30d005c0
JK
108 blob[0]->path ? blob[0]->path : path,
109 path);
65056021
JH
110 diffcore_std(&revs->diffopt);
111 diff_flush(&revs->diffopt);
65056021
JH
112}
113
c0049ca0
JK
114static void builtin_diff_blobs(struct rev_info *revs,
115 int argc, const char **argv UNUSED,
116 struct object_array_entry **blob)
65056021 117{
33de80b1 118 const unsigned mode = canon_mode(S_IFREG | 0644);
65056021 119
a610786f
TH
120 if (argc > 1)
121 usage(builtin_diff_usage);
122
42f5ba5b
JK
123 if (blob[0]->mode == S_IFINVALID)
124 blob[0]->mode = mode;
01618a3a 125
42f5ba5b
JK
126 if (blob[1]->mode == S_IFINVALID)
127 blob[1]->mode = mode;
01618a3a 128
65056021 129 stuff_change(&revs->diffopt,
42f5ba5b
JK
130 blob[0]->mode, blob[1]->mode,
131 &blob[0]->item->oid, &blob[1]->item->oid,
e5450100 132 1, 1,
158b06ca 133 blob_path(blob[0]), blob_path(blob[1]));
65056021
JH
134 diffcore_std(&revs->diffopt);
135 diff_flush(&revs->diffopt);
65056021
JH
136}
137
c0049ca0
JK
138static void builtin_diff_index(struct rev_info *revs,
139 int argc, const char **argv)
65056021 140{
4c3fe82e 141 unsigned int option = 0;
65056021
JH
142 while (1 < argc) {
143 const char *arg = argv[1];
2baf1850 144 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
4c3fe82e 145 option |= DIFF_INDEX_CACHED;
0f5a1d44
DL
146 else if (!strcmp(arg, "--merge-base"))
147 option |= DIFF_INDEX_MERGE_BASE;
65056021
JH
148 else
149 usage(builtin_diff_usage);
150 argv++; argc--;
151 }
152 /*
153 * Make sure there is one revision (i.e. pending object),
154 * and there is no revision filtering parameters.
155 */
1f1e895f 156 if (revs->pending.nr != 1 ||
65056021
JH
157 revs->max_count != -1 || revs->min_age != -1 ||
158 revs->max_age != -1)
159 usage(builtin_diff_usage);
4c3fe82e 160 if (!(option & DIFF_INDEX_CACHED)) {
7349afd2 161 setup_work_tree();
07047d68
ÆAB
162 if (repo_read_index_preload(the_repository,
163 &revs->diffopt.pathspec, 0) < 0) {
3755077b 164 die_errno("repo_read_index_preload");
7349afd2 165 }
07047d68 166 } else if (repo_read_index(the_repository) < 0) {
3755077b 167 die_errno("repo_read_cache");
b4e1e4a7 168 }
25bd3acd 169 run_diff_index(revs, option);
65056021
JH
170}
171
c0049ca0
JK
172static void builtin_diff_tree(struct rev_info *revs,
173 int argc, const char **argv,
174 struct object_array_entry *ent0,
175 struct object_array_entry *ent1)
65056021 176{
9c4b0f66 177 const struct object_id *(oid[2]);
3d09c228
DL
178 struct object_id mb_oid;
179 int merge_base = 0;
a610786f 180
3d09c228
DL
181 while (1 < argc) {
182 const char *arg = argv[1];
183 if (!strcmp(arg, "--merge-base"))
184 merge_base = 1;
185 else
186 usage(builtin_diff_usage);
187 argv++; argc--;
188 }
0fe7c1de 189
3d09c228
DL
190 if (merge_base) {
191 diff_get_merge_base(revs, &mb_oid);
192 oid[0] = &mb_oid;
193 oid[1] = &revs->pending.objects[1].item->oid;
194 } else {
195 int swap = 0;
196
197 /*
198 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
199 * swap them.
200 */
201 if (ent1->item->flags & UNINTERESTING)
202 swap = 1;
203 oid[swap] = &ent0->item->oid;
204 oid[1 - swap] = &ent1->item->oid;
205 }
66f414f8 206 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
65056021 207 log_tree_diff_flush(revs);
65056021
JH
208}
209
c0049ca0
JK
210static void builtin_diff_combined(struct rev_info *revs,
211 int argc, const char **argv UNUSED,
212 struct object_array_entry *ent,
213 int ents, int first_non_parent)
0fe7c1de 214{
910650d2 215 struct oid_array parents = OID_ARRAY_INIT;
0fe7c1de
JH
216 int i;
217
a610786f
TH
218 if (argc > 1)
219 usage(builtin_diff_usage);
220
a79c6b60
RS
221 if (first_non_parent < 0)
222 die(_("no merge given, only parents."));
223 if (first_non_parent >= ents)
224 BUG("first_non_parent out of range: %d", first_non_parent);
225
3b6c17b5
SO
226 diff_merges_set_dense_combined_if_unset(revs);
227
a79c6b60
RS
228 for (i = 0; i < ents; i++) {
229 if (i != first_non_parent)
230 oid_array_append(&parents, &ent[i].item->oid);
231 }
232 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
910650d2 233 oid_array_clear(&parents);
0fe7c1de
JH
234}
235
aecbf914
JH
236static void refresh_index_quietly(void)
237{
837e34eb 238 struct lock_file lock_file = LOCK_INIT;
aecbf914
JH
239 int fd;
240
07047d68 241 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
aecbf914
JH
242 if (fd < 0)
243 return;
07047d68
ÆAB
244 discard_index(&the_index);
245 repo_read_index(the_repository);
246 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
247 NULL);
1b0d968b 248 repo_update_index_if_able(the_repository, &lock_file);
aecbf914
JH
249}
250
c0049ca0 251static void builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
0569e9b8 252{
0569e9b8
JH
253 unsigned int options = 0;
254
255 while (1 < argc && argv[1][0] == '-') {
256 if (!strcmp(argv[1], "--base"))
257 revs->max_count = 1;
258 else if (!strcmp(argv[1], "--ours"))
259 revs->max_count = 2;
260 else if (!strcmp(argv[1], "--theirs"))
261 revs->max_count = 3;
262 else if (!strcmp(argv[1], "-q"))
263 options |= DIFF_SILENT_ON_REMOVED;
1c370ea4
MM
264 else if (!strcmp(argv[1], "-h"))
265 usage(builtin_diff_usage);
5ad6e2b4
JK
266 else {
267 error(_("invalid option: %s"), argv[1]);
268 usage(builtin_diff_usage);
269 }
0569e9b8
JH
270 argv++; argc--;
271 }
272
903e09a3
JH
273 /*
274 * "diff --base" should not combine merges because it was not
275 * asked to. "diff -c" should not densify (if the user wants
276 * dense one, --cc can be explicitly asked for, or just rely
277 * on the default).
278 */
3b6c17b5 279 if (revs->max_count == -1 &&
0569e9b8 280 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
3b6c17b5 281 diff_merges_set_dense_combined_if_unset(revs);
0569e9b8 282
4f38f6b5 283 setup_work_tree();
07047d68
ÆAB
284 if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
285 0) < 0) {
3755077b 286 die_errno("repo_read_index_preload");
0569e9b8 287 }
25bd3acd 288 run_diff_files(revs, options);
0569e9b8
JH
289}
290
8bfcb3a6
CT
291struct symdiff {
292 struct bitmap *skip;
293 int warn;
294 const char *base, *left, *right;
295};
296
297/*
298 * Check for symmetric-difference arguments, and if present, arrange
299 * everything we need to know to handle them correctly. As a bonus,
300 * weed out all bogus range-based revision specifications, e.g.,
301 * "git diff A..B C..D" or "git diff A..B C" get rejected.
302 *
303 * For an actual symmetric diff, *symdiff is set this way:
304 *
305 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
306 * indices that the caller should ignore (extra merge bases, of
307 * which there might be many, and A in A...B). Note that the
308 * chosen merge base and right side are NOT marked.
309 * - warn is set if there are multiple merge bases.
310 * - base, left, and right point to the names to use in a
311 * warning about multiple merge bases.
312 *
313 * If there is no symmetric diff argument, sym->skip is NULL and
314 * sym->warn is cleared. The remaining fields are not set.
315 */
316static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
317{
318 int i, is_symdiff = 0, basecount = 0, othercount = 0;
319 int lpos = -1, rpos = -1, basepos = -1;
320 struct bitmap *map = NULL;
321
322 /*
323 * Use the whence fields to find merge bases and left and
324 * right parts of symmetric difference, so that we do not
325 * depend on the order that revisions are parsed. If there
326 * are any revs that aren't from these sources, we have a
327 * "git diff C A...B" or "git diff A...B C" case. Or we
328 * could even get "git diff A...B C...E", for instance.
329 *
330 * If we don't have just one merge base, we pick one
331 * at random.
332 *
333 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
334 * so we must check for SYMMETRIC_LEFT too. The two arrays
335 * rev->pending.objects and rev->cmdline.rev are parallel.
336 */
337 for (i = 0; i < rev->cmdline.nr; i++) {
338 struct object *obj = rev->pending.objects[i].item;
339 switch (rev->cmdline.rev[i].whence) {
340 case REV_CMD_MERGE_BASE:
341 if (basepos < 0)
342 basepos = i;
343 basecount++;
344 break; /* do mark all bases */
345 case REV_CMD_LEFT:
346 if (lpos >= 0)
347 usage(builtin_diff_usage);
348 lpos = i;
349 if (obj->flags & SYMMETRIC_LEFT) {
350 is_symdiff = 1;
351 break; /* do mark A */
352 }
353 continue;
354 case REV_CMD_RIGHT:
355 if (rpos >= 0)
356 usage(builtin_diff_usage);
357 rpos = i;
358 continue; /* don't mark B */
359 case REV_CMD_PARENTS_ONLY:
360 case REV_CMD_REF:
361 case REV_CMD_REV:
362 othercount++;
363 continue;
364 }
afe8a907 365 if (!map)
8bfcb3a6
CT
366 map = bitmap_new();
367 bitmap_set(map, i);
368 }
369
370 /*
371 * Forbid any additional revs for both A...B and A..B.
372 */
373 if (lpos >= 0 && othercount > 0)
374 usage(builtin_diff_usage);
375
376 if (!is_symdiff) {
377 bitmap_free(map);
378 sym->warn = 0;
379 sym->skip = NULL;
380 return;
381 }
382
383 sym->left = rev->pending.objects[lpos].name;
384 sym->right = rev->pending.objects[rpos].name;
8bfcb3a6
CT
385 if (basecount == 0)
386 die(_("%s...%s: no merge base"), sym->left, sym->right);
5f46e610 387 sym->base = rev->pending.objects[basepos].name;
8bfcb3a6
CT
388 bitmap_unset(map, basepos); /* unmark the base we want */
389 sym->warn = basecount > 1;
390 sym->skip = map;
391}
392
a633fca0 393int cmd_diff(int argc, const char **argv, const char *prefix)
65056021 394{
1f1e895f 395 int i;
65056021 396 struct rev_info rev;
33055fa8 397 struct object_array ent = OBJECT_ARRAY_INIT;
a79c6b60 398 int first_non_parent = -1;
33055fa8 399 int blobs = 0, paths = 0;
42f5ba5b 400 struct object_array_entry *blob[2];
470faf96 401 int nongit = 0, no_index = 0;
c0049ca0 402 int result;
8bfcb3a6 403 struct symdiff sdiff;
65056021
JH
404
405 /*
406 * We could get N tree-ish in the rev.pending_objects list.
a9d7689c
DL
407 * Also there could be M blobs there, and P pathspecs. --cached may
408 * also be present.
65056021
JH
409 *
410 * N=0, M=0:
a9d7689c
DL
411 * cache vs files (diff-files)
412 *
413 * N=0, M=0, --cached:
414 * HEAD vs cache (diff-index --cached)
415 *
65056021
JH
416 * N=0, M=2:
417 * compare two random blobs. P must be zero.
a9d7689c 418 *
65056021 419 * N=0, M=1, P=1:
a9d7689c 420 * compare a blob with a working tree file.
65056021
JH
421 *
422 * N=1, M=0:
a9d7689c 423 * tree vs files (diff-index)
65056021 424 *
a9d7689c 425 * N=1, M=0, --cached:
65056021
JH
426 * tree vs cache (diff-index --cached)
427 *
428 * N=2, M=0:
429 * tree vs tree (diff-tree)
430 *
0569e9b8
JH
431 * N=0, M=0, P=2:
432 * compare two filesystem entities (aka --no-index).
433 *
65056021
JH
434 * Other cases are errors.
435 */
230f544e 436
470faf96
TG
437 /* Were we asked to do --no-index explicitly? */
438 for (i = 1; i < argc; i++) {
439 if (!strcmp(argv[i], "--")) {
440 i++;
441 break;
442 }
443 if (!strcmp(argv[i], "--no-index"))
444 no_index = DIFF_NO_INDEX_EXPLICIT;
445 if (argv[i][0] != '-')
446 break;
447 }
448
28a4e580 449 prefix = setup_git_directory_gently(&nongit);
6df5762d 450
51ba65b5
LD
451 if (!nongit) {
452 prepare_repo_settings(the_repository);
453 the_repository->settings.command_requires_full_index = 0;
454 }
455
28a4e580 456 if (!no_index) {
475b362c
JK
457 /*
458 * Treat git diff with at least one path outside of the
459 * repo the same as if the command would have been executed
460 * outside of a git repository. In this case it behaves
461 * the same way as "git diff --no-index <a> <b>", which acts
462 * as a colourful "diff" replacement.
463 */
464 if (nongit || ((argc == i + 2) &&
465 (!path_inside_repo(prefix, argv[i]) ||
466 !path_inside_repo(prefix, argv[i + 1]))))
467 no_index = DIFF_NO_INDEX_IMPLICIT;
468 }
470faf96 469
5404c116 470 init_diff_ui_defaults();
ef90d6d4 471 git_config(git_diff_ui_config, NULL);
5c327502 472 prefix = precompose_argv_prefix(argc, argv, prefix);
6b2f2d98 473
2abf3503 474 repo_init_revisions(the_repository, &rev, prefix);
0569e9b8 475
287ab28b 476 /* Set up defaults that will apply to both no-index and regular diffs. */
4ca7a3fd 477 init_diffstat_widths(&rev.diffopt);
0d1e0e78
BW
478 rev.diffopt.flags.allow_external = 1;
479 rev.diffopt.flags.allow_textconv = 1;
61af494c 480
287ab28b
JK
481 /* If this is a no-index diff, just run it and exit there. */
482 if (no_index)
dcd6a8c0
JH
483 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
484 argc, argv));
485
287ab28b
JK
486
487 /*
488 * Otherwise, we are doing the usual "git" diff; set up any
489 * further defaults that apply to regular diffs.
490 */
491 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
492
0231ae71
NTND
493 /*
494 * Default to intent-to-add entries invisible in the
495 * index. This makes them show up as new files in diff-files
496 * and not at all in diff-cached.
497 */
498 rev.diffopt.ita_invisible_in_index = 1;
499
0569e9b8 500 if (nongit)
54214529 501 die(_("Not a git repository"));
0569e9b8 502 argc = setup_revisions(argc, argv, &rev, NULL);
047fbe90 503 if (!rev.diffopt.output_format) {
c9b5ef99 504 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
28452655 505 diff_setup_done(&rev.diffopt);
047fbe90 506 }
61af494c 507
0d1e0e78 508 rev.diffopt.flags.recursive = 1;
1eb4136a 509 rev.diffopt.rotate_to_strict = 1;
c9b5ef99 510
1af3d977 511 setup_diff_pager(&rev.diffopt);
89d07f75 512
0569e9b8
JH
513 /*
514 * Do we have --cached and not have a pending object, then
65056021
JH
515 * default to HEAD by hand. Eek.
516 */
1f1e895f 517 if (!rev.pending.nr) {
65056021
JH
518 int i;
519 for (i = 1; i < argc; i++) {
520 const char *arg = argv[i];
521 if (!strcmp(arg, "--"))
522 break;
2baf1850
DS
523 else if (!strcmp(arg, "--cached") ||
524 !strcmp(arg, "--staged")) {
3384a2df 525 add_head_to_pending(&rev);
a2b7a3b3
NTND
526 if (!rev.pending.nr) {
527 struct tree *tree;
f86bcc7b
SB
528 tree = lookup_tree(the_repository,
529 the_repository->hash_algo->empty_tree);
a2b7a3b3
NTND
530 add_pending_object(&rev, &tree->object, "HEAD");
531 }
65056021
JH
532 break;
533 }
534 }
535 }
536
8bfcb3a6 537 symdiff_prepare(&rev, &sdiff);
1f1e895f 538 for (i = 0; i < rev.pending.nr; i++) {
026f09e7
MH
539 struct object_array_entry *entry = &rev.pending.objects[i];
540 struct object *obj = entry->item;
541 const char *name = entry->name;
65056021
JH
542 int flags = (obj->flags & UNINTERESTING);
543 if (!obj->parsed)
109cd76d 544 obj = parse_object(the_repository, &obj->oid);
a74093da 545 obj = deref_tag(the_repository, obj, NULL, 0);
65056021 546 if (!obj)
54214529 547 die(_("invalid object '%s' given."), name);
1974632c 548 if (obj->type == OBJ_COMMIT)
ecb5091f
ÆAB
549 obj = &repo_get_commit_tree(the_repository,
550 ((struct commit *)obj))->object;
5b1e14ea 551
1974632c 552 if (obj->type == OBJ_TREE) {
8bfcb3a6
CT
553 if (sdiff.skip && bitmap_get(sdiff.skip, i))
554 continue;
65056021 555 obj->flags |= flags;
33055fa8 556 add_object_array(obj, name, &ent);
a79c6b60
RS
557 if (first_non_parent < 0 &&
558 (i >= rev.cmdline.nr || /* HEAD by hand. */
559 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
560 first_non_parent = ent.nr - 1;
5b1e14ea 561 } else if (obj->type == OBJ_BLOB) {
65056021 562 if (2 <= blobs)
54214529 563 die(_("more than two blobs given: '%s'"), name);
42f5ba5b 564 blob[blobs] = entry;
65056021 565 blobs++;
230f544e 566
5b1e14ea
MH
567 } else {
568 die(_("unhandled object '%s' given."), name);
65056021 569 }
65056021 570 }
887c6c18 571 if (rev.prune_data.nr)
afe069d1 572 paths += rev.prune_data.nr;
65056021
JH
573
574 /*
575 * Now, do the arguments look reasonable?
576 */
33055fa8 577 if (!ent.nr) {
65056021
JH
578 switch (blobs) {
579 case 0:
c0049ca0 580 builtin_diff_files(&rev, argc, argv);
65056021
JH
581 break;
582 case 1:
583 if (paths != 1)
584 usage(builtin_diff_usage);
c0049ca0 585 builtin_diff_b_f(&rev, argc, argv, blob);
65056021
JH
586 break;
587 case 2:
0fe7c1de
JH
588 if (paths)
589 usage(builtin_diff_usage);
c0049ca0 590 builtin_diff_blobs(&rev, argc, argv, blob);
65056021
JH
591 break;
592 default:
593 usage(builtin_diff_usage);
594 }
595 }
596 else if (blobs)
597 usage(builtin_diff_usage);
33055fa8 598 else if (ent.nr == 1)
c0049ca0 599 builtin_diff_index(&rev, argc, argv);
8bfcb3a6
CT
600 else if (ent.nr == 2) {
601 if (sdiff.warn)
602 warning(_("%s...%s: multiple merge bases, using %s"),
603 sdiff.left, sdiff.right, sdiff.base);
c0049ca0
JK
604 builtin_diff_tree(&rev, argc, argv,
605 &ent.objects[0], &ent.objects[1]);
c008c0ff 606 } else
c0049ca0
JK
607 builtin_diff_combined(&rev, argc, argv,
608 ent.objects, ent.nr,
609 first_non_parent);
5cc6b2d7 610 result = diff_result_code(&rev.diffopt);
aecbf914
JH
611 if (1 < rev.diffopt.skip_stat_unmatch)
612 refresh_index_quietly();
bf1b32d0 613 release_revisions(&rev);
ac95f5d3 614 object_array_clear(&ent);
886e1084 615 UNLEAK(blob);
41bbf9d5 616 return result;
65056021 617}