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