coverity: detect and report when the token or project is incorrect
[git.git] / builtin / diff.c
blobb19530c996c7cd43be9baa3649c93247de888ebd
1 /*
2 * Builtin "git diff"
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "builtin.h"
8 #include "config.h"
9 #include "ewah/ewok.h"
10 #include "lockfile.h"
11 #include "color.h"
12 #include "commit.h"
13 #include "blob.h"
14 #include "gettext.h"
15 #include "tag.h"
16 #include "diff.h"
17 #include "diff-merges.h"
18 #include "diffcore.h"
19 #include "preload-index.h"
20 #include "read-cache-ll.h"
21 #include "revision.h"
22 #include "log-tree.h"
23 #include "setup.h"
24 #include "submodule.h"
25 #include "oid-array.h"
26 #include "tree.h"
28 #define DIFF_NO_INDEX_EXPLICIT 1
29 #define DIFF_NO_INDEX_IMPLICIT 2
31 static const char builtin_diff_usage[] =
32 "git diff [<options>] [<commit>] [--] [<path>...]\n"
33 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
34 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
35 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
36 " or: git diff [<options>] <blob> <blob>\n"
37 " or: git diff [<options>] --no-index [--] <path> <path>"
38 "\n"
39 COMMON_DIFF_OPTIONS_HELP;
41 static const char *blob_path(struct object_array_entry *entry)
43 return entry->path ? entry->path : entry->name;
46 static void stuff_change(struct diff_options *opt,
47 unsigned old_mode, unsigned new_mode,
48 const struct object_id *old_oid,
49 const struct object_id *new_oid,
50 int old_oid_valid,
51 int new_oid_valid,
52 const char *old_path,
53 const char *new_path)
55 struct diff_filespec *one, *two;
57 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
58 oideq(old_oid, new_oid) && (old_mode == new_mode))
59 return;
61 if (opt->flags.reverse_diff) {
62 SWAP(old_mode, new_mode);
63 SWAP(old_oid, new_oid);
64 SWAP(old_path, new_path);
67 if (opt->prefix &&
68 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
69 strncmp(new_path, opt->prefix, opt->prefix_length)))
70 return;
72 one = alloc_filespec(old_path);
73 two = alloc_filespec(new_path);
74 fill_filespec(one, old_oid, old_oid_valid, old_mode);
75 fill_filespec(two, new_oid, new_oid_valid, new_mode);
77 diff_queue(&diff_queued_diff, one, two);
80 static int builtin_diff_b_f(struct rev_info *revs,
81 int argc, const char **argv UNUSED,
82 struct object_array_entry **blob)
84 /* Blob vs file in the working tree*/
85 struct stat st;
86 const char *path;
88 if (argc > 1)
89 usage(builtin_diff_usage);
91 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
92 path = revs->prune_data.items[0].match;
94 if (lstat(path, &st))
95 die_errno(_("failed to stat '%s'"), path);
96 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
97 die(_("'%s': not a regular file or symlink"), path);
99 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
101 if (blob[0]->mode == S_IFINVALID)
102 blob[0]->mode = canon_mode(st.st_mode);
104 stuff_change(&revs->diffopt,
105 blob[0]->mode, canon_mode(st.st_mode),
106 &blob[0]->item->oid, null_oid(),
107 1, 0,
108 blob[0]->path ? blob[0]->path : path,
109 path);
110 diffcore_std(&revs->diffopt);
111 diff_flush(&revs->diffopt);
112 return 0;
115 static int builtin_diff_blobs(struct rev_info *revs,
116 int argc, const char **argv UNUSED,
117 struct object_array_entry **blob)
119 const unsigned mode = canon_mode(S_IFREG | 0644);
121 if (argc > 1)
122 usage(builtin_diff_usage);
124 if (blob[0]->mode == S_IFINVALID)
125 blob[0]->mode = mode;
127 if (blob[1]->mode == S_IFINVALID)
128 blob[1]->mode = mode;
130 stuff_change(&revs->diffopt,
131 blob[0]->mode, blob[1]->mode,
132 &blob[0]->item->oid, &blob[1]->item->oid,
133 1, 1,
134 blob_path(blob[0]), blob_path(blob[1]));
135 diffcore_std(&revs->diffopt);
136 diff_flush(&revs->diffopt);
137 return 0;
140 static int builtin_diff_index(struct rev_info *revs,
141 int argc, const char **argv)
143 unsigned int option = 0;
144 while (1 < argc) {
145 const char *arg = argv[1];
146 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
147 option |= DIFF_INDEX_CACHED;
148 else if (!strcmp(arg, "--merge-base"))
149 option |= DIFF_INDEX_MERGE_BASE;
150 else
151 usage(builtin_diff_usage);
152 argv++; argc--;
155 * Make sure there is one revision (i.e. pending object),
156 * and there is no revision filtering parameters.
158 if (revs->pending.nr != 1 ||
159 revs->max_count != -1 || revs->min_age != -1 ||
160 revs->max_age != -1)
161 usage(builtin_diff_usage);
162 if (!(option & DIFF_INDEX_CACHED)) {
163 setup_work_tree();
164 if (repo_read_index_preload(the_repository,
165 &revs->diffopt.pathspec, 0) < 0) {
166 perror("repo_read_index_preload");
167 return -1;
169 } else if (repo_read_index(the_repository) < 0) {
170 perror("repo_read_cache");
171 return -1;
173 return run_diff_index(revs, option);
176 static int builtin_diff_tree(struct rev_info *revs,
177 int argc, const char **argv,
178 struct object_array_entry *ent0,
179 struct object_array_entry *ent1)
181 const struct object_id *(oid[2]);
182 struct object_id mb_oid;
183 int merge_base = 0;
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--;
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;
202 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
203 * swap them.
205 if (ent1->item->flags & UNINTERESTING)
206 swap = 1;
207 oid[swap] = &ent0->item->oid;
208 oid[1 - swap] = &ent1->item->oid;
210 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
211 log_tree_diff_flush(revs);
212 return 0;
215 static int builtin_diff_combined(struct rev_info *revs,
216 int argc, const char **argv UNUSED,
217 struct object_array_entry *ent,
218 int ents, int first_non_parent)
220 struct oid_array parents = OID_ARRAY_INIT;
221 int i;
223 if (argc > 1)
224 usage(builtin_diff_usage);
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);
231 diff_merges_set_dense_combined_if_unset(revs);
233 for (i = 0; i < ents; i++) {
234 if (i != first_non_parent)
235 oid_array_append(&parents, &ent[i].item->oid);
237 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
238 oid_array_clear(&parents);
239 return 0;
242 static void refresh_index_quietly(void)
244 struct lock_file lock_file = LOCK_INIT;
245 int fd;
247 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
248 if (fd < 0)
249 return;
250 discard_index(&the_index);
251 repo_read_index(the_repository);
252 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
253 NULL);
254 repo_update_index_if_able(the_repository, &lock_file);
257 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
259 unsigned int options = 0;
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;
270 else if (!strcmp(argv[1], "-h"))
271 usage(builtin_diff_usage);
272 else
273 return error(_("invalid option: %s"), argv[1]);
274 argv++; argc--;
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).
283 if (revs->max_count == -1 &&
284 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
285 diff_merges_set_dense_combined_if_unset(revs);
287 setup_work_tree();
288 if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
289 0) < 0) {
290 perror("repo_read_index_preload");
291 return -1;
293 return run_diff_files(revs, options);
296 struct symdiff {
297 struct bitmap *skip;
298 int warn;
299 const char *base, *left, *right;
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.
308 * For an actual symmetric diff, *symdiff is set this way:
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.
318 * If there is no symmetric diff argument, sym->skip is NULL and
319 * sym->warn is cleared. The remaining fields are not set.
321 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
323 int i, is_symdiff = 0, basecount = 0, othercount = 0;
324 int lpos = -1, rpos = -1, basepos = -1;
325 struct bitmap *map = NULL;
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.
335 * If we don't have just one merge base, we pick one
336 * at random.
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.
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 */
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;
370 if (!map)
371 map = bitmap_new();
372 bitmap_set(map, i);
376 * Forbid any additional revs for both A...B and A..B.
378 if (lpos >= 0 && othercount > 0)
379 usage(builtin_diff_usage);
381 if (!is_symdiff) {
382 bitmap_free(map);
383 sym->warn = 0;
384 sym->skip = NULL;
385 return;
388 sym->left = rev->pending.objects[lpos].name;
389 sym->right = rev->pending.objects[rpos].name;
390 if (basecount == 0)
391 die(_("%s...%s: no merge base"), sym->left, sym->right);
392 sym->base = rev->pending.objects[basepos].name;
393 bitmap_unset(map, basepos); /* unmark the base we want */
394 sym->warn = basecount > 1;
395 sym->skip = map;
398 int cmd_diff(int argc, const char **argv, const char *prefix)
400 int i;
401 struct rev_info rev;
402 struct object_array ent = OBJECT_ARRAY_INIT;
403 int first_non_parent = -1;
404 int blobs = 0, paths = 0;
405 struct object_array_entry *blob[2];
406 int nongit = 0, no_index = 0;
407 int result = 0;
408 struct symdiff sdiff;
411 * We could get N tree-ish in the rev.pending_objects list.
412 * Also there could be M blobs there, and P pathspecs. --cached may
413 * also be present.
415 * N=0, M=0:
416 * cache vs files (diff-files)
418 * N=0, M=0, --cached:
419 * HEAD vs cache (diff-index --cached)
421 * N=0, M=2:
422 * compare two random blobs. P must be zero.
424 * N=0, M=1, P=1:
425 * compare a blob with a working tree file.
427 * N=1, M=0:
428 * tree vs files (diff-index)
430 * N=1, M=0, --cached:
431 * tree vs cache (diff-index --cached)
433 * N=2, M=0:
434 * tree vs tree (diff-tree)
436 * N=0, M=0, P=2:
437 * compare two filesystem entities (aka --no-index).
439 * Other cases are errors.
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;
448 if (!strcmp(argv[i], "--no-index"))
449 no_index = DIFF_NO_INDEX_EXPLICIT;
450 if (argv[i][0] != '-')
451 break;
454 prefix = setup_git_directory_gently(&nongit);
456 if (!nongit) {
457 prepare_repo_settings(the_repository);
458 the_repository->settings.command_requires_full_index = 0;
461 if (!no_index) {
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.
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;
475 init_diff_ui_defaults();
476 git_config(git_diff_ui_config, NULL);
477 prefix = precompose_argv_prefix(argc, argv, prefix);
479 repo_init_revisions(the_repository, &rev, prefix);
481 /* Set up defaults that will apply to both no-index and regular diffs. */
482 rev.diffopt.stat_width = -1;
483 rev.diffopt.stat_graph_width = -1;
484 rev.diffopt.flags.allow_external = 1;
485 rev.diffopt.flags.allow_textconv = 1;
487 /* If this is a no-index diff, just run it and exit there. */
488 if (no_index)
489 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
490 argc, argv));
494 * Otherwise, we are doing the usual "git" diff; set up any
495 * further defaults that apply to regular diffs.
497 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
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.
504 rev.diffopt.ita_invisible_in_index = 1;
506 if (nongit)
507 die(_("Not a git repository"));
508 argc = setup_revisions(argc, argv, &rev, NULL);
509 if (!rev.diffopt.output_format) {
510 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
511 diff_setup_done(&rev.diffopt);
514 rev.diffopt.flags.recursive = 1;
515 rev.diffopt.rotate_to_strict = 1;
517 setup_diff_pager(&rev.diffopt);
520 * Do we have --cached and not have a pending object, then
521 * default to HEAD by hand. Eek.
523 if (!rev.pending.nr) {
524 int i;
525 for (i = 1; i < argc; i++) {
526 const char *arg = argv[i];
527 if (!strcmp(arg, "--"))
528 break;
529 else if (!strcmp(arg, "--cached") ||
530 !strcmp(arg, "--staged")) {
531 add_head_to_pending(&rev);
532 if (!rev.pending.nr) {
533 struct tree *tree;
534 tree = lookup_tree(the_repository,
535 the_repository->hash_algo->empty_tree);
536 add_pending_object(&rev, &tree->object, "HEAD");
538 break;
543 symdiff_prepare(&rev, &sdiff);
544 for (i = 0; i < rev.pending.nr; i++) {
545 struct object_array_entry *entry = &rev.pending.objects[i];
546 struct object *obj = entry->item;
547 const char *name = entry->name;
548 int flags = (obj->flags & UNINTERESTING);
549 if (!obj->parsed)
550 obj = parse_object(the_repository, &obj->oid);
551 obj = deref_tag(the_repository, obj, NULL, 0);
552 if (!obj)
553 die(_("invalid object '%s' given."), name);
554 if (obj->type == OBJ_COMMIT)
555 obj = &repo_get_commit_tree(the_repository,
556 ((struct commit *)obj))->object;
558 if (obj->type == OBJ_TREE) {
559 if (sdiff.skip && bitmap_get(sdiff.skip, i))
560 continue;
561 obj->flags |= flags;
562 add_object_array(obj, name, &ent);
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;
567 } else if (obj->type == OBJ_BLOB) {
568 if (2 <= blobs)
569 die(_("more than two blobs given: '%s'"), name);
570 blob[blobs] = entry;
571 blobs++;
573 } else {
574 die(_("unhandled object '%s' given."), name);
577 if (rev.prune_data.nr)
578 paths += rev.prune_data.nr;
581 * Now, do the arguments look reasonable?
583 if (!ent.nr) {
584 switch (blobs) {
585 case 0:
586 result = builtin_diff_files(&rev, argc, argv);
587 break;
588 case 1:
589 if (paths != 1)
590 usage(builtin_diff_usage);
591 result = builtin_diff_b_f(&rev, argc, argv, blob);
592 break;
593 case 2:
594 if (paths)
595 usage(builtin_diff_usage);
596 result = builtin_diff_blobs(&rev, argc, argv, blob);
597 break;
598 default:
599 usage(builtin_diff_usage);
602 else if (blobs)
603 usage(builtin_diff_usage);
604 else if (ent.nr == 1)
605 result = builtin_diff_index(&rev, argc, argv);
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);
610 result = builtin_diff_tree(&rev, argc, argv,
611 &ent.objects[0], &ent.objects[1]);
612 } else
613 result = builtin_diff_combined(&rev, argc, argv,
614 ent.objects, ent.nr,
615 first_non_parent);
616 result = diff_result_code(&rev.diffopt, result);
617 if (1 < rev.diffopt.skip_stat_unmatch)
618 refresh_index_quietly();
619 release_revisions(&rev);
620 object_array_clear(&ent);
621 UNLEAK(blob);
622 return result;