Merge branch 'ml/doc-merge-updates' into maint-2.43
[alt-git.git] / builtin / diff.c
blob55e7d21755a09c9754d45fea8689cbcbd32939dd
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 void 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);
114 static void builtin_diff_blobs(struct rev_info *revs,
115 int argc, const char **argv UNUSED,
116 struct object_array_entry **blob)
118 const unsigned mode = canon_mode(S_IFREG | 0644);
120 if (argc > 1)
121 usage(builtin_diff_usage);
123 if (blob[0]->mode == S_IFINVALID)
124 blob[0]->mode = mode;
126 if (blob[1]->mode == S_IFINVALID)
127 blob[1]->mode = mode;
129 stuff_change(&revs->diffopt,
130 blob[0]->mode, blob[1]->mode,
131 &blob[0]->item->oid, &blob[1]->item->oid,
132 1, 1,
133 blob_path(blob[0]), blob_path(blob[1]));
134 diffcore_std(&revs->diffopt);
135 diff_flush(&revs->diffopt);
138 static void builtin_diff_index(struct rev_info *revs,
139 int argc, const char **argv)
141 unsigned int option = 0;
142 while (1 < argc) {
143 const char *arg = argv[1];
144 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
145 option |= DIFF_INDEX_CACHED;
146 else if (!strcmp(arg, "--merge-base"))
147 option |= DIFF_INDEX_MERGE_BASE;
148 else
149 usage(builtin_diff_usage);
150 argv++; argc--;
153 * Make sure there is one revision (i.e. pending object),
154 * and there is no revision filtering parameters.
156 if (revs->pending.nr != 1 ||
157 revs->max_count != -1 || revs->min_age != -1 ||
158 revs->max_age != -1)
159 usage(builtin_diff_usage);
160 if (!(option & DIFF_INDEX_CACHED)) {
161 setup_work_tree();
162 if (repo_read_index_preload(the_repository,
163 &revs->diffopt.pathspec, 0) < 0) {
164 die_errno("repo_read_index_preload");
166 } else if (repo_read_index(the_repository) < 0) {
167 die_errno("repo_read_cache");
169 run_diff_index(revs, option);
172 static void builtin_diff_tree(struct rev_info *revs,
173 int argc, const char **argv,
174 struct object_array_entry *ent0,
175 struct object_array_entry *ent1)
177 const struct object_id *(oid[2]);
178 struct object_id mb_oid;
179 int merge_base = 0;
181 while (1 < argc) {
182 const char *arg = argv[1];
183 if (!strcmp(arg, "--merge-base"))
184 merge_base = 1;
185 else
186 usage(builtin_diff_usage);
187 argv++; argc--;
190 if (merge_base) {
191 diff_get_merge_base(revs, &mb_oid);
192 oid[0] = &mb_oid;
193 oid[1] = &revs->pending.objects[1].item->oid;
194 } else {
195 int swap = 0;
198 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
199 * swap them.
201 if (ent1->item->flags & UNINTERESTING)
202 swap = 1;
203 oid[swap] = &ent0->item->oid;
204 oid[1 - swap] = &ent1->item->oid;
206 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
207 log_tree_diff_flush(revs);
210 static void builtin_diff_combined(struct rev_info *revs,
211 int argc, const char **argv UNUSED,
212 struct object_array_entry *ent,
213 int ents, int first_non_parent)
215 struct oid_array parents = OID_ARRAY_INIT;
216 int i;
218 if (argc > 1)
219 usage(builtin_diff_usage);
221 if (first_non_parent < 0)
222 die(_("no merge given, only parents."));
223 if (first_non_parent >= ents)
224 BUG("first_non_parent out of range: %d", first_non_parent);
226 diff_merges_set_dense_combined_if_unset(revs);
228 for (i = 0; i < ents; i++) {
229 if (i != first_non_parent)
230 oid_array_append(&parents, &ent[i].item->oid);
232 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
233 oid_array_clear(&parents);
236 static void refresh_index_quietly(void)
238 struct lock_file lock_file = LOCK_INIT;
239 int fd;
241 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
242 if (fd < 0)
243 return;
244 discard_index(&the_index);
245 repo_read_index(the_repository);
246 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
247 NULL);
248 repo_update_index_if_able(the_repository, &lock_file);
251 static void builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
253 unsigned int options = 0;
255 while (1 < argc && argv[1][0] == '-') {
256 if (!strcmp(argv[1], "--base"))
257 revs->max_count = 1;
258 else if (!strcmp(argv[1], "--ours"))
259 revs->max_count = 2;
260 else if (!strcmp(argv[1], "--theirs"))
261 revs->max_count = 3;
262 else if (!strcmp(argv[1], "-q"))
263 options |= DIFF_SILENT_ON_REMOVED;
264 else if (!strcmp(argv[1], "-h"))
265 usage(builtin_diff_usage);
266 else {
267 error(_("invalid option: %s"), argv[1]);
268 usage(builtin_diff_usage);
270 argv++; argc--;
274 * "diff --base" should not combine merges because it was not
275 * asked to. "diff -c" should not densify (if the user wants
276 * dense one, --cc can be explicitly asked for, or just rely
277 * on the default).
279 if (revs->max_count == -1 &&
280 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
281 diff_merges_set_dense_combined_if_unset(revs);
283 setup_work_tree();
284 if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
285 0) < 0) {
286 die_errno("repo_read_index_preload");
288 run_diff_files(revs, options);
291 struct symdiff {
292 struct bitmap *skip;
293 int warn;
294 const char *base, *left, *right;
298 * Check for symmetric-difference arguments, and if present, arrange
299 * everything we need to know to handle them correctly. As a bonus,
300 * weed out all bogus range-based revision specifications, e.g.,
301 * "git diff A..B C..D" or "git diff A..B C" get rejected.
303 * For an actual symmetric diff, *symdiff is set this way:
305 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
306 * indices that the caller should ignore (extra merge bases, of
307 * which there might be many, and A in A...B). Note that the
308 * chosen merge base and right side are NOT marked.
309 * - warn is set if there are multiple merge bases.
310 * - base, left, and right point to the names to use in a
311 * warning about multiple merge bases.
313 * If there is no symmetric diff argument, sym->skip is NULL and
314 * sym->warn is cleared. The remaining fields are not set.
316 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
318 int i, is_symdiff = 0, basecount = 0, othercount = 0;
319 int lpos = -1, rpos = -1, basepos = -1;
320 struct bitmap *map = NULL;
323 * Use the whence fields to find merge bases and left and
324 * right parts of symmetric difference, so that we do not
325 * depend on the order that revisions are parsed. If there
326 * are any revs that aren't from these sources, we have a
327 * "git diff C A...B" or "git diff A...B C" case. Or we
328 * could even get "git diff A...B C...E", for instance.
330 * If we don't have just one merge base, we pick one
331 * at random.
333 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
334 * so we must check for SYMMETRIC_LEFT too. The two arrays
335 * rev->pending.objects and rev->cmdline.rev are parallel.
337 for (i = 0; i < rev->cmdline.nr; i++) {
338 struct object *obj = rev->pending.objects[i].item;
339 switch (rev->cmdline.rev[i].whence) {
340 case REV_CMD_MERGE_BASE:
341 if (basepos < 0)
342 basepos = i;
343 basecount++;
344 break; /* do mark all bases */
345 case REV_CMD_LEFT:
346 if (lpos >= 0)
347 usage(builtin_diff_usage);
348 lpos = i;
349 if (obj->flags & SYMMETRIC_LEFT) {
350 is_symdiff = 1;
351 break; /* do mark A */
353 continue;
354 case REV_CMD_RIGHT:
355 if (rpos >= 0)
356 usage(builtin_diff_usage);
357 rpos = i;
358 continue; /* don't mark B */
359 case REV_CMD_PARENTS_ONLY:
360 case REV_CMD_REF:
361 case REV_CMD_REV:
362 othercount++;
363 continue;
365 if (!map)
366 map = bitmap_new();
367 bitmap_set(map, i);
371 * Forbid any additional revs for both A...B and A..B.
373 if (lpos >= 0 && othercount > 0)
374 usage(builtin_diff_usage);
376 if (!is_symdiff) {
377 bitmap_free(map);
378 sym->warn = 0;
379 sym->skip = NULL;
380 return;
383 sym->left = rev->pending.objects[lpos].name;
384 sym->right = rev->pending.objects[rpos].name;
385 if (basecount == 0)
386 die(_("%s...%s: no merge base"), sym->left, sym->right);
387 sym->base = rev->pending.objects[basepos].name;
388 bitmap_unset(map, basepos); /* unmark the base we want */
389 sym->warn = basecount > 1;
390 sym->skip = map;
393 int cmd_diff(int argc, const char **argv, const char *prefix)
395 int i;
396 struct rev_info rev;
397 struct object_array ent = OBJECT_ARRAY_INIT;
398 int first_non_parent = -1;
399 int blobs = 0, paths = 0;
400 struct object_array_entry *blob[2];
401 int nongit = 0, no_index = 0;
402 int result;
403 struct symdiff sdiff;
406 * We could get N tree-ish in the rev.pending_objects list.
407 * Also there could be M blobs there, and P pathspecs. --cached may
408 * also be present.
410 * N=0, M=0:
411 * cache vs files (diff-files)
413 * N=0, M=0, --cached:
414 * HEAD vs cache (diff-index --cached)
416 * N=0, M=2:
417 * compare two random blobs. P must be zero.
419 * N=0, M=1, P=1:
420 * compare a blob with a working tree file.
422 * N=1, M=0:
423 * tree vs files (diff-index)
425 * N=1, M=0, --cached:
426 * tree vs cache (diff-index --cached)
428 * N=2, M=0:
429 * tree vs tree (diff-tree)
431 * N=0, M=0, P=2:
432 * compare two filesystem entities (aka --no-index).
434 * Other cases are errors.
437 /* Were we asked to do --no-index explicitly? */
438 for (i = 1; i < argc; i++) {
439 if (!strcmp(argv[i], "--")) {
440 i++;
441 break;
443 if (!strcmp(argv[i], "--no-index"))
444 no_index = DIFF_NO_INDEX_EXPLICIT;
445 if (argv[i][0] != '-')
446 break;
449 prefix = setup_git_directory_gently(&nongit);
451 if (!nongit) {
452 prepare_repo_settings(the_repository);
453 the_repository->settings.command_requires_full_index = 0;
456 if (!no_index) {
458 * Treat git diff with at least one path outside of the
459 * repo the same as if the command would have been executed
460 * outside of a git repository. In this case it behaves
461 * the same way as "git diff --no-index <a> <b>", which acts
462 * as a colourful "diff" replacement.
464 if (nongit || ((argc == i + 2) &&
465 (!path_inside_repo(prefix, argv[i]) ||
466 !path_inside_repo(prefix, argv[i + 1]))))
467 no_index = DIFF_NO_INDEX_IMPLICIT;
470 init_diff_ui_defaults();
471 git_config(git_diff_ui_config, NULL);
472 prefix = precompose_argv_prefix(argc, argv, prefix);
474 repo_init_revisions(the_repository, &rev, prefix);
476 /* Set up defaults that will apply to both no-index and regular diffs. */
477 init_diffstat_widths(&rev.diffopt);
478 rev.diffopt.flags.allow_external = 1;
479 rev.diffopt.flags.allow_textconv = 1;
481 /* If this is a no-index diff, just run it and exit there. */
482 if (no_index)
483 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
484 argc, argv));
488 * Otherwise, we are doing the usual "git" diff; set up any
489 * further defaults that apply to regular diffs.
491 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
494 * Default to intent-to-add entries invisible in the
495 * index. This makes them show up as new files in diff-files
496 * and not at all in diff-cached.
498 rev.diffopt.ita_invisible_in_index = 1;
500 if (nongit)
501 die(_("Not a git repository"));
502 argc = setup_revisions(argc, argv, &rev, NULL);
503 if (!rev.diffopt.output_format) {
504 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
505 diff_setup_done(&rev.diffopt);
508 rev.diffopt.flags.recursive = 1;
509 rev.diffopt.rotate_to_strict = 1;
511 setup_diff_pager(&rev.diffopt);
514 * Do we have --cached and not have a pending object, then
515 * default to HEAD by hand. Eek.
517 if (!rev.pending.nr) {
518 int i;
519 for (i = 1; i < argc; i++) {
520 const char *arg = argv[i];
521 if (!strcmp(arg, "--"))
522 break;
523 else if (!strcmp(arg, "--cached") ||
524 !strcmp(arg, "--staged")) {
525 add_head_to_pending(&rev);
526 if (!rev.pending.nr) {
527 struct tree *tree;
528 tree = lookup_tree(the_repository,
529 the_repository->hash_algo->empty_tree);
530 add_pending_object(&rev, &tree->object, "HEAD");
532 break;
537 symdiff_prepare(&rev, &sdiff);
538 for (i = 0; i < rev.pending.nr; i++) {
539 struct object_array_entry *entry = &rev.pending.objects[i];
540 struct object *obj = entry->item;
541 const char *name = entry->name;
542 int flags = (obj->flags & UNINTERESTING);
543 if (!obj->parsed)
544 obj = parse_object(the_repository, &obj->oid);
545 obj = deref_tag(the_repository, obj, NULL, 0);
546 if (!obj)
547 die(_("invalid object '%s' given."), name);
548 if (obj->type == OBJ_COMMIT)
549 obj = &repo_get_commit_tree(the_repository,
550 ((struct commit *)obj))->object;
552 if (obj->type == OBJ_TREE) {
553 if (sdiff.skip && bitmap_get(sdiff.skip, i))
554 continue;
555 obj->flags |= flags;
556 add_object_array(obj, name, &ent);
557 if (first_non_parent < 0 &&
558 (i >= rev.cmdline.nr || /* HEAD by hand. */
559 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
560 first_non_parent = ent.nr - 1;
561 } else if (obj->type == OBJ_BLOB) {
562 if (2 <= blobs)
563 die(_("more than two blobs given: '%s'"), name);
564 blob[blobs] = entry;
565 blobs++;
567 } else {
568 die(_("unhandled object '%s' given."), name);
571 if (rev.prune_data.nr)
572 paths += rev.prune_data.nr;
575 * Now, do the arguments look reasonable?
577 if (!ent.nr) {
578 switch (blobs) {
579 case 0:
580 builtin_diff_files(&rev, argc, argv);
581 break;
582 case 1:
583 if (paths != 1)
584 usage(builtin_diff_usage);
585 builtin_diff_b_f(&rev, argc, argv, blob);
586 break;
587 case 2:
588 if (paths)
589 usage(builtin_diff_usage);
590 builtin_diff_blobs(&rev, argc, argv, blob);
591 break;
592 default:
593 usage(builtin_diff_usage);
596 else if (blobs)
597 usage(builtin_diff_usage);
598 else if (ent.nr == 1)
599 builtin_diff_index(&rev, argc, argv);
600 else if (ent.nr == 2) {
601 if (sdiff.warn)
602 warning(_("%s...%s: multiple merge bases, using %s"),
603 sdiff.left, sdiff.right, sdiff.base);
604 builtin_diff_tree(&rev, argc, argv,
605 &ent.objects[0], &ent.objects[1]);
606 } else
607 builtin_diff_combined(&rev, argc, argv,
608 ent.objects, ent.nr,
609 first_non_parent);
610 result = diff_result_code(&rev.diffopt);
611 if (1 < rev.diffopt.skip_stat_unmatch)
612 refresh_index_quietly();
613 release_revisions(&rev);
614 object_array_clear(&ent);
615 UNLEAK(blob);
616 return result;