diff: support ^! for merges
[git/debian.git] / builtin / diff.c
blob0e49919735ffda44e5839d730dc389b09ed739e6
1 /*
2 * Builtin "git diff"
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.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 "tag.h"
15 #include "diff.h"
16 #include "diff-merges.h"
17 #include "diffcore.h"
18 #include "revision.h"
19 #include "log-tree.h"
20 #include "builtin.h"
21 #include "submodule.h"
22 #include "oid-array.h"
24 #define DIFF_NO_INDEX_EXPLICIT 1
25 #define DIFF_NO_INDEX_IMPLICIT 2
27 static const char builtin_diff_usage[] =
28 "git diff [<options>] [<commit>] [--] [<path>...]\n"
29 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
30 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
31 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
32 " or: git diff [<options>] <blob> <blob>\n"
33 " or: git diff [<options>] --no-index [--] <path> <path>\n"
34 COMMON_DIFF_OPTIONS_HELP;
36 static const char *blob_path(struct object_array_entry *entry)
38 return entry->path ? entry->path : entry->name;
41 static void stuff_change(struct diff_options *opt,
42 unsigned old_mode, unsigned new_mode,
43 const struct object_id *old_oid,
44 const struct object_id *new_oid,
45 int old_oid_valid,
46 int new_oid_valid,
47 const char *old_path,
48 const char *new_path)
50 struct diff_filespec *one, *two;
52 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
53 oideq(old_oid, new_oid) && (old_mode == new_mode))
54 return;
56 if (opt->flags.reverse_diff) {
57 SWAP(old_mode, new_mode);
58 SWAP(old_oid, new_oid);
59 SWAP(old_path, new_path);
62 if (opt->prefix &&
63 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
64 strncmp(new_path, opt->prefix, opt->prefix_length)))
65 return;
67 one = alloc_filespec(old_path);
68 two = alloc_filespec(new_path);
69 fill_filespec(one, old_oid, old_oid_valid, old_mode);
70 fill_filespec(two, new_oid, new_oid_valid, new_mode);
72 diff_queue(&diff_queued_diff, one, two);
75 static int builtin_diff_b_f(struct rev_info *revs,
76 int argc, const char **argv,
77 struct object_array_entry **blob)
79 /* Blob vs file in the working tree*/
80 struct stat st;
81 const char *path;
83 if (argc > 1)
84 usage(builtin_diff_usage);
86 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
87 path = revs->prune_data.items[0].match;
89 if (lstat(path, &st))
90 die_errno(_("failed to stat '%s'"), path);
91 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
92 die(_("'%s': not a regular file or symlink"), path);
94 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
96 if (blob[0]->mode == S_IFINVALID)
97 blob[0]->mode = canon_mode(st.st_mode);
99 stuff_change(&revs->diffopt,
100 blob[0]->mode, canon_mode(st.st_mode),
101 &blob[0]->item->oid, null_oid(),
102 1, 0,
103 blob[0]->path ? blob[0]->path : path,
104 path);
105 diffcore_std(&revs->diffopt);
106 diff_flush(&revs->diffopt);
107 return 0;
110 static int builtin_diff_blobs(struct rev_info *revs,
111 int argc, const char **argv,
112 struct object_array_entry **blob)
114 const unsigned mode = canon_mode(S_IFREG | 0644);
116 if (argc > 1)
117 usage(builtin_diff_usage);
119 if (blob[0]->mode == S_IFINVALID)
120 blob[0]->mode = mode;
122 if (blob[1]->mode == S_IFINVALID)
123 blob[1]->mode = mode;
125 stuff_change(&revs->diffopt,
126 blob[0]->mode, blob[1]->mode,
127 &blob[0]->item->oid, &blob[1]->item->oid,
128 1, 1,
129 blob_path(blob[0]), blob_path(blob[1]));
130 diffcore_std(&revs->diffopt);
131 diff_flush(&revs->diffopt);
132 return 0;
135 static int builtin_diff_index(struct rev_info *revs,
136 int argc, const char **argv)
138 unsigned int option = 0;
139 while (1 < argc) {
140 const char *arg = argv[1];
141 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
142 option |= DIFF_INDEX_CACHED;
143 else if (!strcmp(arg, "--merge-base"))
144 option |= DIFF_INDEX_MERGE_BASE;
145 else
146 usage(builtin_diff_usage);
147 argv++; argc--;
150 * Make sure there is one revision (i.e. pending object),
151 * and there is no revision filtering parameters.
153 if (revs->pending.nr != 1 ||
154 revs->max_count != -1 || revs->min_age != -1 ||
155 revs->max_age != -1)
156 usage(builtin_diff_usage);
157 if (!(option & DIFF_INDEX_CACHED)) {
158 setup_work_tree();
159 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
160 perror("read_cache_preload");
161 return -1;
163 } else if (read_cache() < 0) {
164 perror("read_cache");
165 return -1;
167 return run_diff_index(revs, option);
170 static int 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);
206 return 0;
209 static int builtin_diff_combined(struct rev_info *revs,
210 int argc, const char **argv,
211 struct object_array_entry *ent,
212 int ents, int first_non_parent)
214 struct oid_array parents = OID_ARRAY_INIT;
215 int i;
217 if (argc > 1)
218 usage(builtin_diff_usage);
220 if (first_non_parent < 0)
221 die(_("no merge given, only parents."));
222 if (first_non_parent >= ents)
223 BUG("first_non_parent out of range: %d", first_non_parent);
225 diff_merges_set_dense_combined_if_unset(revs);
227 for (i = 0; i < ents; i++) {
228 if (i != first_non_parent)
229 oid_array_append(&parents, &ent[i].item->oid);
231 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
232 oid_array_clear(&parents);
233 return 0;
236 static void refresh_index_quietly(void)
238 struct lock_file lock_file = LOCK_INIT;
239 int fd;
241 fd = hold_locked_index(&lock_file, 0);
242 if (fd < 0)
243 return;
244 discard_cache();
245 read_cache();
246 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
247 repo_update_index_if_able(the_repository, &lock_file);
250 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
252 unsigned int options = 0;
254 while (1 < argc && argv[1][0] == '-') {
255 if (!strcmp(argv[1], "--base"))
256 revs->max_count = 1;
257 else if (!strcmp(argv[1], "--ours"))
258 revs->max_count = 2;
259 else if (!strcmp(argv[1], "--theirs"))
260 revs->max_count = 3;
261 else if (!strcmp(argv[1], "-q"))
262 options |= DIFF_SILENT_ON_REMOVED;
263 else if (!strcmp(argv[1], "-h"))
264 usage(builtin_diff_usage);
265 else
266 return error(_("invalid option: %s"), argv[1]);
267 argv++; argc--;
271 * "diff --base" should not combine merges because it was not
272 * asked to. "diff -c" should not densify (if the user wants
273 * dense one, --cc can be explicitly asked for, or just rely
274 * on the default).
276 if (revs->max_count == -1 &&
277 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
278 diff_merges_set_dense_combined_if_unset(revs);
280 setup_work_tree();
281 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
282 perror("read_cache_preload");
283 return -1;
285 return run_diff_files(revs, options);
288 struct symdiff {
289 struct bitmap *skip;
290 int warn;
291 const char *base, *left, *right;
295 * Check for symmetric-difference arguments, and if present, arrange
296 * everything we need to know to handle them correctly. As a bonus,
297 * weed out all bogus range-based revision specifications, e.g.,
298 * "git diff A..B C..D" or "git diff A..B C" get rejected.
300 * For an actual symmetric diff, *symdiff is set this way:
302 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
303 * indices that the caller should ignore (extra merge bases, of
304 * which there might be many, and A in A...B). Note that the
305 * chosen merge base and right side are NOT marked.
306 * - warn is set if there are multiple merge bases.
307 * - base, left, and right point to the names to use in a
308 * warning about multiple merge bases.
310 * If there is no symmetric diff argument, sym->skip is NULL and
311 * sym->warn is cleared. The remaining fields are not set.
313 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
315 int i, is_symdiff = 0, basecount = 0, othercount = 0;
316 int lpos = -1, rpos = -1, basepos = -1;
317 struct bitmap *map = NULL;
320 * Use the whence fields to find merge bases and left and
321 * right parts of symmetric difference, so that we do not
322 * depend on the order that revisions are parsed. If there
323 * are any revs that aren't from these sources, we have a
324 * "git diff C A...B" or "git diff A...B C" case. Or we
325 * could even get "git diff A...B C...E", for instance.
327 * If we don't have just one merge base, we pick one
328 * at random.
330 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
331 * so we must check for SYMMETRIC_LEFT too. The two arrays
332 * rev->pending.objects and rev->cmdline.rev are parallel.
334 for (i = 0; i < rev->cmdline.nr; i++) {
335 struct object *obj = rev->pending.objects[i].item;
336 switch (rev->cmdline.rev[i].whence) {
337 case REV_CMD_MERGE_BASE:
338 if (basepos < 0)
339 basepos = i;
340 basecount++;
341 break; /* do mark all bases */
342 case REV_CMD_LEFT:
343 if (lpos >= 0)
344 usage(builtin_diff_usage);
345 lpos = i;
346 if (obj->flags & SYMMETRIC_LEFT) {
347 is_symdiff = 1;
348 break; /* do mark A */
350 continue;
351 case REV_CMD_RIGHT:
352 if (rpos >= 0)
353 usage(builtin_diff_usage);
354 rpos = i;
355 continue; /* don't mark B */
356 case REV_CMD_PARENTS_ONLY:
357 case REV_CMD_REF:
358 case REV_CMD_REV:
359 othercount++;
360 continue;
362 if (!map)
363 map = bitmap_new();
364 bitmap_set(map, i);
368 * Forbid any additional revs for both A...B and A..B.
370 if (lpos >= 0 && othercount > 0)
371 usage(builtin_diff_usage);
373 if (!is_symdiff) {
374 bitmap_free(map);
375 sym->warn = 0;
376 sym->skip = NULL;
377 return;
380 sym->left = rev->pending.objects[lpos].name;
381 sym->right = rev->pending.objects[rpos].name;
382 if (basecount == 0)
383 die(_("%s...%s: no merge base"), sym->left, sym->right);
384 sym->base = rev->pending.objects[basepos].name;
385 bitmap_unset(map, basepos); /* unmark the base we want */
386 sym->warn = basecount > 1;
387 sym->skip = map;
390 int cmd_diff(int argc, const char **argv, const char *prefix)
392 int i;
393 struct rev_info rev;
394 struct object_array ent = OBJECT_ARRAY_INIT;
395 int first_non_parent = -1;
396 int blobs = 0, paths = 0;
397 struct object_array_entry *blob[2];
398 int nongit = 0, no_index = 0;
399 int result = 0;
400 struct symdiff sdiff;
403 * We could get N tree-ish in the rev.pending_objects list.
404 * Also there could be M blobs there, and P pathspecs. --cached may
405 * also be present.
407 * N=0, M=0:
408 * cache vs files (diff-files)
410 * N=0, M=0, --cached:
411 * HEAD vs cache (diff-index --cached)
413 * N=0, M=2:
414 * compare two random blobs. P must be zero.
416 * N=0, M=1, P=1:
417 * compare a blob with a working tree file.
419 * N=1, M=0:
420 * tree vs files (diff-index)
422 * N=1, M=0, --cached:
423 * tree vs cache (diff-index --cached)
425 * N=2, M=0:
426 * tree vs tree (diff-tree)
428 * N=0, M=0, P=2:
429 * compare two filesystem entities (aka --no-index).
431 * Other cases are errors.
434 /* Were we asked to do --no-index explicitly? */
435 for (i = 1; i < argc; i++) {
436 if (!strcmp(argv[i], "--")) {
437 i++;
438 break;
440 if (!strcmp(argv[i], "--no-index"))
441 no_index = DIFF_NO_INDEX_EXPLICIT;
442 if (argv[i][0] != '-')
443 break;
446 prefix = setup_git_directory_gently(&nongit);
448 if (!nongit) {
449 prepare_repo_settings(the_repository);
450 the_repository->settings.command_requires_full_index = 0;
453 if (!no_index) {
455 * Treat git diff with at least one path outside of the
456 * repo the same as if the command would have been executed
457 * outside of a git repository. In this case it behaves
458 * the same way as "git diff --no-index <a> <b>", which acts
459 * as a colourful "diff" replacement.
461 if (nongit || ((argc == i + 2) &&
462 (!path_inside_repo(prefix, argv[i]) ||
463 !path_inside_repo(prefix, argv[i + 1]))))
464 no_index = DIFF_NO_INDEX_IMPLICIT;
467 init_diff_ui_defaults();
468 git_config(git_diff_ui_config, NULL);
469 prefix = precompose_argv_prefix(argc, argv, prefix);
471 repo_init_revisions(the_repository, &rev, prefix);
473 /* Set up defaults that will apply to both no-index and regular diffs. */
474 rev.diffopt.stat_width = -1;
475 rev.diffopt.stat_graph_width = -1;
476 rev.diffopt.flags.allow_external = 1;
477 rev.diffopt.flags.allow_textconv = 1;
479 /* If this is a no-index diff, just run it and exit there. */
480 if (no_index)
481 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
482 argc, argv));
486 * Otherwise, we are doing the usual "git" diff; set up any
487 * further defaults that apply to regular diffs.
489 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
492 * Default to intent-to-add entries invisible in the
493 * index. This makes them show up as new files in diff-files
494 * and not at all in diff-cached.
496 rev.diffopt.ita_invisible_in_index = 1;
498 if (nongit)
499 die(_("Not a git repository"));
500 argc = setup_revisions(argc, argv, &rev, NULL);
501 if (!rev.diffopt.output_format) {
502 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
503 diff_setup_done(&rev.diffopt);
506 rev.diffopt.flags.recursive = 1;
507 rev.diffopt.rotate_to_strict = 1;
509 setup_diff_pager(&rev.diffopt);
512 * Do we have --cached and not have a pending object, then
513 * default to HEAD by hand. Eek.
515 if (!rev.pending.nr) {
516 int i;
517 for (i = 1; i < argc; i++) {
518 const char *arg = argv[i];
519 if (!strcmp(arg, "--"))
520 break;
521 else if (!strcmp(arg, "--cached") ||
522 !strcmp(arg, "--staged")) {
523 add_head_to_pending(&rev);
524 if (!rev.pending.nr) {
525 struct tree *tree;
526 tree = lookup_tree(the_repository,
527 the_repository->hash_algo->empty_tree);
528 add_pending_object(&rev, &tree->object, "HEAD");
530 break;
535 symdiff_prepare(&rev, &sdiff);
536 for (i = 0; i < rev.pending.nr; i++) {
537 struct object_array_entry *entry = &rev.pending.objects[i];
538 struct object *obj = entry->item;
539 const char *name = entry->name;
540 int flags = (obj->flags & UNINTERESTING);
541 if (!obj->parsed)
542 obj = parse_object(the_repository, &obj->oid);
543 obj = deref_tag(the_repository, obj, NULL, 0);
544 if (!obj)
545 die(_("invalid object '%s' given."), name);
546 if (obj->type == OBJ_COMMIT)
547 obj = &get_commit_tree(((struct commit *)obj))->object;
549 if (obj->type == OBJ_TREE) {
550 if (sdiff.skip && bitmap_get(sdiff.skip, i))
551 continue;
552 obj->flags |= flags;
553 add_object_array(obj, name, &ent);
554 if (first_non_parent < 0 &&
555 (i >= rev.cmdline.nr || /* HEAD by hand. */
556 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
557 first_non_parent = ent.nr - 1;
558 } else if (obj->type == OBJ_BLOB) {
559 if (2 <= blobs)
560 die(_("more than two blobs given: '%s'"), name);
561 blob[blobs] = entry;
562 blobs++;
564 } else {
565 die(_("unhandled object '%s' given."), name);
568 if (rev.prune_data.nr)
569 paths += rev.prune_data.nr;
572 * Now, do the arguments look reasonable?
574 if (!ent.nr) {
575 switch (blobs) {
576 case 0:
577 result = builtin_diff_files(&rev, argc, argv);
578 break;
579 case 1:
580 if (paths != 1)
581 usage(builtin_diff_usage);
582 result = builtin_diff_b_f(&rev, argc, argv, blob);
583 break;
584 case 2:
585 if (paths)
586 usage(builtin_diff_usage);
587 result = builtin_diff_blobs(&rev, argc, argv, blob);
588 break;
589 default:
590 usage(builtin_diff_usage);
593 else if (blobs)
594 usage(builtin_diff_usage);
595 else if (ent.nr == 1)
596 result = builtin_diff_index(&rev, argc, argv);
597 else if (ent.nr == 2) {
598 if (sdiff.warn)
599 warning(_("%s...%s: multiple merge bases, using %s"),
600 sdiff.left, sdiff.right, sdiff.base);
601 result = builtin_diff_tree(&rev, argc, argv,
602 &ent.objects[0], &ent.objects[1]);
603 } else
604 result = builtin_diff_combined(&rev, argc, argv,
605 ent.objects, ent.nr,
606 first_non_parent);
607 result = diff_result_code(&rev.diffopt, result);
608 if (1 < rev.diffopt.skip_stat_unmatch)
609 refresh_index_quietly();
610 release_revisions(&rev);
611 UNLEAK(ent);
612 UNLEAK(blob);
613 return result;