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