Sync with 'maint'
[git/gitster.git] / builtin / diff.c
blob5fb8a5545e03295f94f5ae1638cbb1f5a303c207
1 /*
2 * Builtin "git diff"
4 * Copyright (c) 2006 Junio C Hamano
5 */
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 "gettext.h"
14 #include "tag.h"
15 #include "diff.h"
16 #include "diff-merges.h"
17 #include "diffcore.h"
18 #include "preload-index.h"
19 #include "read-cache-ll.h"
20 #include "revision.h"
21 #include "log-tree.h"
22 #include "setup.h"
23 #include "oid-array.h"
24 #include "tree.h"
26 #define DIFF_NO_INDEX_EXPLICIT 1
27 #define DIFF_NO_INDEX_IMPLICIT 2
29 static const char builtin_diff_usage[] =
30 "git diff [<options>] [<commit>] [--] [<path>...]\n"
31 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
32 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
33 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
34 " or: git diff [<options>] <blob> <blob>\n"
35 " or: git diff [<options>] --no-index [--] <path> <path>"
36 "\n"
37 COMMON_DIFF_OPTIONS_HELP;
39 static const char *blob_path(struct object_array_entry *entry)
41 return entry->path ? entry->path : entry->name;
44 static void stuff_change(struct diff_options *opt,
45 unsigned old_mode, unsigned new_mode,
46 const struct object_id *old_oid,
47 const struct object_id *new_oid,
48 int old_oid_valid,
49 int new_oid_valid,
50 const char *old_path,
51 const char *new_path)
53 struct diff_filespec *one, *two;
55 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
56 oideq(old_oid, new_oid) && (old_mode == new_mode))
57 return;
59 if (opt->flags.reverse_diff) {
60 SWAP(old_mode, new_mode);
61 SWAP(old_oid, new_oid);
62 SWAP(old_path, new_path);
65 if (opt->prefix &&
66 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
67 strncmp(new_path, opt->prefix, opt->prefix_length)))
68 return;
70 one = alloc_filespec(old_path);
71 two = alloc_filespec(new_path);
72 fill_filespec(one, old_oid, old_oid_valid, old_mode);
73 fill_filespec(two, new_oid, new_oid_valid, new_mode);
75 diff_queue(&diff_queued_diff, one, two);
78 static void builtin_diff_b_f(struct rev_info *revs,
79 int argc, const char **argv UNUSED,
80 struct object_array_entry **blob)
82 /* Blob vs file in the working tree*/
83 struct stat st;
84 const char *path;
86 if (argc > 1)
87 usage(builtin_diff_usage);
89 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
90 path = revs->prune_data.items[0].match;
92 if (lstat(path, &st))
93 die_errno(_("failed to stat '%s'"), path);
94 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
95 die(_("'%s': not a regular file or symlink"), path);
97 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
99 if (blob[0]->mode == S_IFINVALID)
100 blob[0]->mode = canon_mode(st.st_mode);
102 stuff_change(&revs->diffopt,
103 blob[0]->mode, canon_mode(st.st_mode),
104 &blob[0]->item->oid, null_oid(),
105 1, 0,
106 blob[0]->path ? blob[0]->path : path,
107 path);
108 diffcore_std(&revs->diffopt);
109 diff_flush(&revs->diffopt);
112 static void builtin_diff_blobs(struct rev_info *revs,
113 int argc, const char **argv UNUSED,
114 struct object_array_entry **blob)
116 const unsigned mode = canon_mode(S_IFREG | 0644);
118 if (argc > 1)
119 usage(builtin_diff_usage);
121 if (blob[0]->mode == S_IFINVALID)
122 blob[0]->mode = mode;
124 if (blob[1]->mode == S_IFINVALID)
125 blob[1]->mode = mode;
127 stuff_change(&revs->diffopt,
128 blob[0]->mode, blob[1]->mode,
129 &blob[0]->item->oid, &blob[1]->item->oid,
130 1, 1,
131 blob_path(blob[0]), blob_path(blob[1]));
132 diffcore_std(&revs->diffopt);
133 diff_flush(&revs->diffopt);
136 static void builtin_diff_index(struct rev_info *revs,
137 int argc, const char **argv)
139 unsigned int option = 0;
140 while (1 < argc) {
141 const char *arg = argv[1];
142 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
143 option |= DIFF_INDEX_CACHED;
144 else if (!strcmp(arg, "--merge-base"))
145 option |= DIFF_INDEX_MERGE_BASE;
146 else
147 usage(builtin_diff_usage);
148 argv++; argc--;
151 * Make sure there is one revision (i.e. pending object),
152 * and there is no revision filtering parameters.
154 if (revs->pending.nr != 1 ||
155 revs->max_count != -1 || revs->min_age != -1 ||
156 revs->max_age != -1)
157 usage(builtin_diff_usage);
158 if (!(option & DIFF_INDEX_CACHED)) {
159 setup_work_tree();
160 if (repo_read_index_preload(the_repository,
161 &revs->diffopt.pathspec, 0) < 0) {
162 die_errno("repo_read_index_preload");
164 } else if (repo_read_index(the_repository) < 0) {
165 die_errno("repo_read_cache");
167 run_diff_index(revs, option);
170 static void builtin_diff_tree(struct rev_info *revs,
171 int argc, const char **argv,
172 struct object_array_entry *ent0,
173 struct object_array_entry *ent1)
175 const struct object_id *(oid[2]);
176 struct object_id mb_oid;
177 int merge_base = 0;
179 while (1 < argc) {
180 const char *arg = argv[1];
181 if (!strcmp(arg, "--merge-base"))
182 merge_base = 1;
183 else
184 usage(builtin_diff_usage);
185 argv++; argc--;
188 if (merge_base) {
189 diff_get_merge_base(revs, &mb_oid);
190 oid[0] = &mb_oid;
191 oid[1] = &revs->pending.objects[1].item->oid;
192 } else {
193 int swap = 0;
196 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
197 * swap them.
199 if (ent1->item->flags & UNINTERESTING)
200 swap = 1;
201 oid[swap] = &ent0->item->oid;
202 oid[1 - swap] = &ent1->item->oid;
204 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
205 log_tree_diff_flush(revs);
208 static void builtin_diff_combined(struct rev_info *revs,
209 int argc, const char **argv UNUSED,
210 struct object_array_entry *ent,
211 int ents, int first_non_parent)
213 struct oid_array parents = OID_ARRAY_INIT;
214 int i;
216 if (argc > 1)
217 usage(builtin_diff_usage);
219 if (first_non_parent < 0)
220 die(_("no merge given, only parents."));
221 if (first_non_parent >= ents)
222 BUG("first_non_parent out of range: %d", first_non_parent);
224 diff_merges_set_dense_combined_if_unset(revs);
226 for (i = 0; i < ents; i++) {
227 if (i != first_non_parent)
228 oid_array_append(&parents, &ent[i].item->oid);
230 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
231 oid_array_clear(&parents);
234 static void refresh_index_quietly(void)
236 struct lock_file lock_file = LOCK_INIT;
237 int fd;
239 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
240 if (fd < 0)
241 return;
242 discard_index(the_repository->index);
243 repo_read_index(the_repository);
244 refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
245 NULL);
246 repo_update_index_if_able(the_repository, &lock_file);
249 static void builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
251 unsigned int options = 0;
253 while (1 < argc && argv[1][0] == '-') {
254 if (!strcmp(argv[1], "--base"))
255 revs->max_count = 1;
256 else if (!strcmp(argv[1], "--ours"))
257 revs->max_count = 2;
258 else if (!strcmp(argv[1], "--theirs"))
259 revs->max_count = 3;
260 else if (!strcmp(argv[1], "-q"))
261 options |= DIFF_SILENT_ON_REMOVED;
262 else if (!strcmp(argv[1], "-h"))
263 usage(builtin_diff_usage);
264 else {
265 error(_("invalid option: %s"), argv[1]);
266 usage(builtin_diff_usage);
268 argv++; argc--;
272 * "diff --base" should not combine merges because it was not
273 * asked to. "diff -c" should not densify (if the user wants
274 * dense one, --cc can be explicitly asked for, or just rely
275 * on the default).
277 if (revs->max_count == -1 &&
278 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
279 diff_merges_set_dense_combined_if_unset(revs);
281 setup_work_tree();
282 if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
283 0) < 0) {
284 die_errno("repo_read_index_preload");
286 run_diff_files(revs, options);
289 struct symdiff {
290 struct bitmap *skip;
291 int warn;
292 const char *base, *left, *right;
296 * Check for symmetric-difference arguments, and if present, arrange
297 * everything we need to know to handle them correctly. As a bonus,
298 * weed out all bogus range-based revision specifications, e.g.,
299 * "git diff A..B C..D" or "git diff A..B C" get rejected.
301 * For an actual symmetric diff, *symdiff is set this way:
303 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
304 * indices that the caller should ignore (extra merge bases, of
305 * which there might be many, and A in A...B). Note that the
306 * chosen merge base and right side are NOT marked.
307 * - warn is set if there are multiple merge bases.
308 * - base, left, and right point to the names to use in a
309 * warning about multiple merge bases.
311 * If there is no symmetric diff argument, sym->skip is NULL and
312 * sym->warn is cleared. The remaining fields are not set.
314 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
316 int i, is_symdiff = 0, basecount = 0, othercount = 0;
317 int lpos = -1, rpos = -1, basepos = -1;
318 struct bitmap *map = NULL;
321 * Use the whence fields to find merge bases and left and
322 * right parts of symmetric difference, so that we do not
323 * depend on the order that revisions are parsed. If there
324 * are any revs that aren't from these sources, we have a
325 * "git diff C A...B" or "git diff A...B C" case. Or we
326 * could even get "git diff A...B C...E", for instance.
328 * If we don't have just one merge base, we pick one
329 * at random.
331 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
332 * so we must check for SYMMETRIC_LEFT too. The two arrays
333 * rev->pending.objects and rev->cmdline.rev are parallel.
335 for (i = 0; i < rev->cmdline.nr; i++) {
336 struct object *obj = rev->pending.objects[i].item;
337 switch (rev->cmdline.rev[i].whence) {
338 case REV_CMD_MERGE_BASE:
339 if (basepos < 0)
340 basepos = i;
341 basecount++;
342 break; /* do mark all bases */
343 case REV_CMD_LEFT:
344 if (lpos >= 0)
345 usage(builtin_diff_usage);
346 lpos = i;
347 if (obj->flags & SYMMETRIC_LEFT) {
348 is_symdiff = 1;
349 break; /* do mark A */
351 continue;
352 case REV_CMD_RIGHT:
353 if (rpos >= 0)
354 usage(builtin_diff_usage);
355 rpos = i;
356 continue; /* don't mark B */
357 case REV_CMD_PARENTS_ONLY:
358 case REV_CMD_REF:
359 case REV_CMD_REV:
360 othercount++;
361 continue;
363 if (!map)
364 map = bitmap_new();
365 bitmap_set(map, i);
369 * Forbid any additional revs for both A...B and A..B.
371 if (lpos >= 0 && othercount > 0)
372 usage(builtin_diff_usage);
374 if (!is_symdiff) {
375 bitmap_free(map);
376 sym->warn = 0;
377 sym->skip = NULL;
378 return;
381 sym->left = rev->pending.objects[lpos].name;
382 sym->right = rev->pending.objects[rpos].name;
383 if (basecount == 0)
384 die(_("%s...%s: no merge base"), sym->left, sym->right);
385 sym->base = rev->pending.objects[basepos].name;
386 bitmap_unset(map, basepos); /* unmark the base we want */
387 sym->warn = basecount > 1;
388 sym->skip = map;
391 static void symdiff_release(struct symdiff *sdiff)
393 bitmap_free(sdiff->skip);
396 int cmd_diff(int argc, const char **argv, const char *prefix)
398 int i;
399 struct rev_info rev;
400 struct object_array ent = OBJECT_ARRAY_INIT;
401 int first_non_parent = -1;
402 int blobs = 0, paths = 0;
403 struct object_array_entry *blob[2];
404 int nongit = 0, no_index = 0;
405 int result;
406 struct symdiff sdiff;
409 * We could get N tree-ish in the rev.pending_objects list.
410 * Also there could be M blobs there, and P pathspecs. --cached may
411 * also be present.
413 * N=0, M=0:
414 * cache vs files (diff-files)
416 * N=0, M=0, --cached:
417 * HEAD vs cache (diff-index --cached)
419 * N=0, M=2:
420 * compare two random blobs. P must be zero.
422 * N=0, M=1, P=1:
423 * compare a blob with a working tree file.
425 * N=1, M=0:
426 * tree vs files (diff-index)
428 * N=1, M=0, --cached:
429 * tree vs cache (diff-index --cached)
431 * N=2, M=0:
432 * tree vs tree (diff-tree)
434 * N=0, M=0, P=2:
435 * compare two filesystem entities (aka --no-index).
437 * Other cases are errors.
440 /* Were we asked to do --no-index explicitly? */
441 for (i = 1; i < argc; i++) {
442 if (!strcmp(argv[i], "--")) {
443 i++;
444 break;
446 if (!strcmp(argv[i], "--no-index"))
447 no_index = DIFF_NO_INDEX_EXPLICIT;
448 if (argv[i][0] != '-')
449 break;
452 prefix = setup_git_directory_gently(&nongit);
454 if (!nongit) {
455 prepare_repo_settings(the_repository);
456 the_repository->settings.command_requires_full_index = 0;
459 if (!no_index) {
461 * Treat git diff with at least one path outside of the
462 * repo the same as if the command would have been executed
463 * outside of a git repository. In this case it behaves
464 * the same way as "git diff --no-index <a> <b>", which acts
465 * as a colourful "diff" replacement.
467 if (nongit || ((argc == i + 2) &&
468 (!path_inside_repo(prefix, argv[i]) ||
469 !path_inside_repo(prefix, argv[i + 1]))))
470 no_index = DIFF_NO_INDEX_IMPLICIT;
474 * When operating outside of a Git repository we need to have a hash
475 * algorithm at hand so that we can generate the blob hashes. We
476 * default to SHA1 here, but may eventually want to change this to be
477 * configurable via a command line option.
479 if (nongit)
480 repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
482 init_diff_ui_defaults();
483 git_config(git_diff_ui_config, NULL);
484 prefix = precompose_argv_prefix(argc, argv, prefix);
486 repo_init_revisions(the_repository, &rev, prefix);
488 /* Set up defaults that will apply to both no-index and regular diffs. */
489 init_diffstat_widths(&rev.diffopt);
490 rev.diffopt.flags.allow_external = 1;
491 rev.diffopt.flags.allow_textconv = 1;
493 /* If this is a no-index diff, just run it and exit there. */
494 if (no_index)
495 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
496 argc, argv));
500 * Otherwise, we are doing the usual "git" diff; set up any
501 * further defaults that apply to regular diffs.
503 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
506 * Default to intent-to-add entries invisible in the
507 * index. This makes them show up as new files in diff-files
508 * and not at all in diff-cached.
510 rev.diffopt.ita_invisible_in_index = 1;
512 if (nongit)
513 die(_("Not a git repository"));
514 argc = setup_revisions(argc, argv, &rev, NULL);
515 if (!rev.diffopt.output_format) {
516 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
517 diff_setup_done(&rev.diffopt);
520 rev.diffopt.flags.recursive = 1;
521 rev.diffopt.rotate_to_strict = 1;
523 setup_diff_pager(&rev.diffopt);
526 * Do we have --cached and not have a pending object, then
527 * default to HEAD by hand. Eek.
529 if (!rev.pending.nr) {
530 int i;
531 for (i = 1; i < argc; i++) {
532 const char *arg = argv[i];
533 if (!strcmp(arg, "--"))
534 break;
535 else if (!strcmp(arg, "--cached") ||
536 !strcmp(arg, "--staged")) {
537 add_head_to_pending(&rev);
538 if (!rev.pending.nr) {
539 struct tree *tree;
540 tree = lookup_tree(the_repository,
541 the_repository->hash_algo->empty_tree);
542 add_pending_object(&rev, &tree->object, "HEAD");
544 break;
549 symdiff_prepare(&rev, &sdiff);
550 for (i = 0; i < rev.pending.nr; i++) {
551 struct object_array_entry *entry = &rev.pending.objects[i];
552 struct object *obj = entry->item;
553 const char *name = entry->name;
554 int flags = (obj->flags & UNINTERESTING);
555 if (!obj->parsed)
556 obj = parse_object(the_repository, &obj->oid);
557 obj = deref_tag(the_repository, obj, NULL, 0);
558 if (!obj)
559 die(_("invalid object '%s' given."), name);
560 if (obj->type == OBJ_COMMIT)
561 obj = &repo_get_commit_tree(the_repository,
562 ((struct commit *)obj))->object;
564 if (obj->type == OBJ_TREE) {
565 if (sdiff.skip && bitmap_get(sdiff.skip, i))
566 continue;
567 obj->flags |= flags;
568 add_object_array(obj, name, &ent);
569 if (first_non_parent < 0 &&
570 (i >= rev.cmdline.nr || /* HEAD by hand. */
571 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
572 first_non_parent = ent.nr - 1;
573 } else if (obj->type == OBJ_BLOB) {
574 if (2 <= blobs)
575 die(_("more than two blobs given: '%s'"), name);
576 blob[blobs] = entry;
577 blobs++;
579 } else {
580 die(_("unhandled object '%s' given."), name);
583 if (rev.prune_data.nr)
584 paths += rev.prune_data.nr;
587 * Now, do the arguments look reasonable?
589 if (!ent.nr) {
590 switch (blobs) {
591 case 0:
592 builtin_diff_files(&rev, argc, argv);
593 break;
594 case 1:
595 if (paths != 1)
596 usage(builtin_diff_usage);
597 builtin_diff_b_f(&rev, argc, argv, blob);
598 break;
599 case 2:
600 if (paths)
601 usage(builtin_diff_usage);
602 builtin_diff_blobs(&rev, argc, argv, blob);
603 break;
604 default:
605 usage(builtin_diff_usage);
608 else if (blobs)
609 usage(builtin_diff_usage);
610 else if (ent.nr == 1)
611 builtin_diff_index(&rev, argc, argv);
612 else if (ent.nr == 2) {
613 if (sdiff.warn)
614 warning(_("%s...%s: multiple merge bases, using %s"),
615 sdiff.left, sdiff.right, sdiff.base);
616 builtin_diff_tree(&rev, argc, argv,
617 &ent.objects[0], &ent.objects[1]);
618 } else
619 builtin_diff_combined(&rev, argc, argv,
620 ent.objects, ent.nr,
621 first_non_parent);
622 result = diff_result_code(&rev);
623 if (1 < rev.diffopt.skip_stat_unmatch)
624 refresh_index_quietly();
625 release_revisions(&rev);
626 object_array_clear(&ent);
627 symdiff_release(&sdiff);
628 UNLEAK(blob);
629 return result;