Merge branch 'mh/use-wincred-from-system'
[alt-git.git] / builtin / diff.c
blob7b64659fe79301fcf5d6c34e12e09cc8b6089f1c
1 /*
2 * Builtin "git diff"
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #define USE_THE_INDEX_VARIABLE
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 "gettext.h"
15 #include "tag.h"
16 #include "diff.h"
17 #include "diff-merges.h"
18 #include "diffcore.h"
19 #include "revision.h"
20 #include "log-tree.h"
21 #include "builtin.h"
22 #include "setup.h"
23 #include "submodule.h"
24 #include "oid-array.h"
25 #include "tree.h"
27 #define DIFF_NO_INDEX_EXPLICIT 1
28 #define DIFF_NO_INDEX_IMPLICIT 2
30 static const char builtin_diff_usage[] =
31 "git diff [<options>] [<commit>] [--] [<path>...]\n"
32 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
33 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
34 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
35 " or: git diff [<options>] <blob> <blob>\n"
36 " or: git diff [<options>] --no-index [--] <path> <path>"
37 "\n"
38 COMMON_DIFF_OPTIONS_HELP;
40 static const char *blob_path(struct object_array_entry *entry)
42 return entry->path ? entry->path : entry->name;
45 static void stuff_change(struct diff_options *opt,
46 unsigned old_mode, unsigned new_mode,
47 const struct object_id *old_oid,
48 const struct object_id *new_oid,
49 int old_oid_valid,
50 int new_oid_valid,
51 const char *old_path,
52 const char *new_path)
54 struct diff_filespec *one, *two;
56 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
57 oideq(old_oid, new_oid) && (old_mode == new_mode))
58 return;
60 if (opt->flags.reverse_diff) {
61 SWAP(old_mode, new_mode);
62 SWAP(old_oid, new_oid);
63 SWAP(old_path, new_path);
66 if (opt->prefix &&
67 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
68 strncmp(new_path, opt->prefix, opt->prefix_length)))
69 return;
71 one = alloc_filespec(old_path);
72 two = alloc_filespec(new_path);
73 fill_filespec(one, old_oid, old_oid_valid, old_mode);
74 fill_filespec(two, new_oid, new_oid_valid, new_mode);
76 diff_queue(&diff_queued_diff, one, two);
79 static int builtin_diff_b_f(struct rev_info *revs,
80 int argc, const char **argv UNUSED,
81 struct object_array_entry **blob)
83 /* Blob vs file in the working tree*/
84 struct stat st;
85 const char *path;
87 if (argc > 1)
88 usage(builtin_diff_usage);
90 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
91 path = revs->prune_data.items[0].match;
93 if (lstat(path, &st))
94 die_errno(_("failed to stat '%s'"), path);
95 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
96 die(_("'%s': not a regular file or symlink"), path);
98 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
100 if (blob[0]->mode == S_IFINVALID)
101 blob[0]->mode = canon_mode(st.st_mode);
103 stuff_change(&revs->diffopt,
104 blob[0]->mode, canon_mode(st.st_mode),
105 &blob[0]->item->oid, null_oid(),
106 1, 0,
107 blob[0]->path ? blob[0]->path : path,
108 path);
109 diffcore_std(&revs->diffopt);
110 diff_flush(&revs->diffopt);
111 return 0;
114 static int 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);
136 return 0;
139 static int builtin_diff_index(struct rev_info *revs,
140 int argc, const char **argv)
142 unsigned int option = 0;
143 while (1 < argc) {
144 const char *arg = argv[1];
145 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
146 option |= DIFF_INDEX_CACHED;
147 else if (!strcmp(arg, "--merge-base"))
148 option |= DIFF_INDEX_MERGE_BASE;
149 else
150 usage(builtin_diff_usage);
151 argv++; argc--;
154 * Make sure there is one revision (i.e. pending object),
155 * and there is no revision filtering parameters.
157 if (revs->pending.nr != 1 ||
158 revs->max_count != -1 || revs->min_age != -1 ||
159 revs->max_age != -1)
160 usage(builtin_diff_usage);
161 if (!(option & DIFF_INDEX_CACHED)) {
162 setup_work_tree();
163 if (repo_read_index_preload(the_repository,
164 &revs->diffopt.pathspec, 0) < 0) {
165 perror("repo_read_index_preload");
166 return -1;
168 } else if (repo_read_index(the_repository) < 0) {
169 perror("repo_read_cache");
170 return -1;
172 return run_diff_index(revs, option);
175 static int builtin_diff_tree(struct rev_info *revs,
176 int argc, const char **argv,
177 struct object_array_entry *ent0,
178 struct object_array_entry *ent1)
180 const struct object_id *(oid[2]);
181 struct object_id mb_oid;
182 int merge_base = 0;
184 while (1 < argc) {
185 const char *arg = argv[1];
186 if (!strcmp(arg, "--merge-base"))
187 merge_base = 1;
188 else
189 usage(builtin_diff_usage);
190 argv++; argc--;
193 if (merge_base) {
194 diff_get_merge_base(revs, &mb_oid);
195 oid[0] = &mb_oid;
196 oid[1] = &revs->pending.objects[1].item->oid;
197 } else {
198 int swap = 0;
201 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
202 * swap them.
204 if (ent1->item->flags & UNINTERESTING)
205 swap = 1;
206 oid[swap] = &ent0->item->oid;
207 oid[1 - swap] = &ent1->item->oid;
209 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
210 log_tree_diff_flush(revs);
211 return 0;
214 static int builtin_diff_combined(struct rev_info *revs,
215 int argc, const char **argv UNUSED,
216 struct object_array_entry *ent,
217 int ents, int first_non_parent)
219 struct oid_array parents = OID_ARRAY_INIT;
220 int i;
222 if (argc > 1)
223 usage(builtin_diff_usage);
225 if (first_non_parent < 0)
226 die(_("no merge given, only parents."));
227 if (first_non_parent >= ents)
228 BUG("first_non_parent out of range: %d", first_non_parent);
230 diff_merges_set_dense_combined_if_unset(revs);
232 for (i = 0; i < ents; i++) {
233 if (i != first_non_parent)
234 oid_array_append(&parents, &ent[i].item->oid);
236 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
237 oid_array_clear(&parents);
238 return 0;
241 static void refresh_index_quietly(void)
243 struct lock_file lock_file = LOCK_INIT;
244 int fd;
246 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
247 if (fd < 0)
248 return;
249 discard_index(&the_index);
250 repo_read_index(the_repository);
251 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
252 NULL);
253 repo_update_index_if_able(the_repository, &lock_file);
256 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
258 unsigned int options = 0;
260 while (1 < argc && argv[1][0] == '-') {
261 if (!strcmp(argv[1], "--base"))
262 revs->max_count = 1;
263 else if (!strcmp(argv[1], "--ours"))
264 revs->max_count = 2;
265 else if (!strcmp(argv[1], "--theirs"))
266 revs->max_count = 3;
267 else if (!strcmp(argv[1], "-q"))
268 options |= DIFF_SILENT_ON_REMOVED;
269 else if (!strcmp(argv[1], "-h"))
270 usage(builtin_diff_usage);
271 else
272 return error(_("invalid option: %s"), argv[1]);
273 argv++; argc--;
277 * "diff --base" should not combine merges because it was not
278 * asked to. "diff -c" should not densify (if the user wants
279 * dense one, --cc can be explicitly asked for, or just rely
280 * on the default).
282 if (revs->max_count == -1 &&
283 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
284 diff_merges_set_dense_combined_if_unset(revs);
286 setup_work_tree();
287 if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
288 0) < 0) {
289 perror("repo_read_index_preload");
290 return -1;
292 return run_diff_files(revs, options);
295 struct symdiff {
296 struct bitmap *skip;
297 int warn;
298 const char *base, *left, *right;
302 * Check for symmetric-difference arguments, and if present, arrange
303 * everything we need to know to handle them correctly. As a bonus,
304 * weed out all bogus range-based revision specifications, e.g.,
305 * "git diff A..B C..D" or "git diff A..B C" get rejected.
307 * For an actual symmetric diff, *symdiff is set this way:
309 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
310 * indices that the caller should ignore (extra merge bases, of
311 * which there might be many, and A in A...B). Note that the
312 * chosen merge base and right side are NOT marked.
313 * - warn is set if there are multiple merge bases.
314 * - base, left, and right point to the names to use in a
315 * warning about multiple merge bases.
317 * If there is no symmetric diff argument, sym->skip is NULL and
318 * sym->warn is cleared. The remaining fields are not set.
320 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
322 int i, is_symdiff = 0, basecount = 0, othercount = 0;
323 int lpos = -1, rpos = -1, basepos = -1;
324 struct bitmap *map = NULL;
327 * Use the whence fields to find merge bases and left and
328 * right parts of symmetric difference, so that we do not
329 * depend on the order that revisions are parsed. If there
330 * are any revs that aren't from these sources, we have a
331 * "git diff C A...B" or "git diff A...B C" case. Or we
332 * could even get "git diff A...B C...E", for instance.
334 * If we don't have just one merge base, we pick one
335 * at random.
337 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
338 * so we must check for SYMMETRIC_LEFT too. The two arrays
339 * rev->pending.objects and rev->cmdline.rev are parallel.
341 for (i = 0; i < rev->cmdline.nr; i++) {
342 struct object *obj = rev->pending.objects[i].item;
343 switch (rev->cmdline.rev[i].whence) {
344 case REV_CMD_MERGE_BASE:
345 if (basepos < 0)
346 basepos = i;
347 basecount++;
348 break; /* do mark all bases */
349 case REV_CMD_LEFT:
350 if (lpos >= 0)
351 usage(builtin_diff_usage);
352 lpos = i;
353 if (obj->flags & SYMMETRIC_LEFT) {
354 is_symdiff = 1;
355 break; /* do mark A */
357 continue;
358 case REV_CMD_RIGHT:
359 if (rpos >= 0)
360 usage(builtin_diff_usage);
361 rpos = i;
362 continue; /* don't mark B */
363 case REV_CMD_PARENTS_ONLY:
364 case REV_CMD_REF:
365 case REV_CMD_REV:
366 othercount++;
367 continue;
369 if (!map)
370 map = bitmap_new();
371 bitmap_set(map, i);
375 * Forbid any additional revs for both A...B and A..B.
377 if (lpos >= 0 && othercount > 0)
378 usage(builtin_diff_usage);
380 if (!is_symdiff) {
381 bitmap_free(map);
382 sym->warn = 0;
383 sym->skip = NULL;
384 return;
387 sym->left = rev->pending.objects[lpos].name;
388 sym->right = rev->pending.objects[rpos].name;
389 if (basecount == 0)
390 die(_("%s...%s: no merge base"), sym->left, sym->right);
391 sym->base = rev->pending.objects[basepos].name;
392 bitmap_unset(map, basepos); /* unmark the base we want */
393 sym->warn = basecount > 1;
394 sym->skip = map;
397 int cmd_diff(int argc, const char **argv, const char *prefix)
399 int i;
400 struct rev_info rev;
401 struct object_array ent = OBJECT_ARRAY_INIT;
402 int first_non_parent = -1;
403 int blobs = 0, paths = 0;
404 struct object_array_entry *blob[2];
405 int nongit = 0, no_index = 0;
406 int result = 0;
407 struct symdiff sdiff;
410 * We could get N tree-ish in the rev.pending_objects list.
411 * Also there could be M blobs there, and P pathspecs. --cached may
412 * also be present.
414 * N=0, M=0:
415 * cache vs files (diff-files)
417 * N=0, M=0, --cached:
418 * HEAD vs cache (diff-index --cached)
420 * N=0, M=2:
421 * compare two random blobs. P must be zero.
423 * N=0, M=1, P=1:
424 * compare a blob with a working tree file.
426 * N=1, M=0:
427 * tree vs files (diff-index)
429 * N=1, M=0, --cached:
430 * tree vs cache (diff-index --cached)
432 * N=2, M=0:
433 * tree vs tree (diff-tree)
435 * N=0, M=0, P=2:
436 * compare two filesystem entities (aka --no-index).
438 * Other cases are errors.
441 /* Were we asked to do --no-index explicitly? */
442 for (i = 1; i < argc; i++) {
443 if (!strcmp(argv[i], "--")) {
444 i++;
445 break;
447 if (!strcmp(argv[i], "--no-index"))
448 no_index = DIFF_NO_INDEX_EXPLICIT;
449 if (argv[i][0] != '-')
450 break;
453 prefix = setup_git_directory_gently(&nongit);
455 if (!nongit) {
456 prepare_repo_settings(the_repository);
457 the_repository->settings.command_requires_full_index = 0;
460 if (!no_index) {
462 * Treat git diff with at least one path outside of the
463 * repo the same as if the command would have been executed
464 * outside of a git repository. In this case it behaves
465 * the same way as "git diff --no-index <a> <b>", which acts
466 * as a colourful "diff" replacement.
468 if (nongit || ((argc == i + 2) &&
469 (!path_inside_repo(prefix, argv[i]) ||
470 !path_inside_repo(prefix, argv[i + 1]))))
471 no_index = DIFF_NO_INDEX_IMPLICIT;
474 init_diff_ui_defaults();
475 git_config(git_diff_ui_config, NULL);
476 prefix = precompose_argv_prefix(argc, argv, prefix);
478 repo_init_revisions(the_repository, &rev, prefix);
480 /* Set up defaults that will apply to both no-index and regular diffs. */
481 rev.diffopt.stat_width = -1;
482 rev.diffopt.stat_graph_width = -1;
483 rev.diffopt.flags.allow_external = 1;
484 rev.diffopt.flags.allow_textconv = 1;
486 /* If this is a no-index diff, just run it and exit there. */
487 if (no_index)
488 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
489 argc, argv));
493 * Otherwise, we are doing the usual "git" diff; set up any
494 * further defaults that apply to regular diffs.
496 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
499 * Default to intent-to-add entries invisible in the
500 * index. This makes them show up as new files in diff-files
501 * and not at all in diff-cached.
503 rev.diffopt.ita_invisible_in_index = 1;
505 if (nongit)
506 die(_("Not a git repository"));
507 argc = setup_revisions(argc, argv, &rev, NULL);
508 if (!rev.diffopt.output_format) {
509 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
510 diff_setup_done(&rev.diffopt);
513 rev.diffopt.flags.recursive = 1;
514 rev.diffopt.rotate_to_strict = 1;
516 setup_diff_pager(&rev.diffopt);
519 * Do we have --cached and not have a pending object, then
520 * default to HEAD by hand. Eek.
522 if (!rev.pending.nr) {
523 int i;
524 for (i = 1; i < argc; i++) {
525 const char *arg = argv[i];
526 if (!strcmp(arg, "--"))
527 break;
528 else if (!strcmp(arg, "--cached") ||
529 !strcmp(arg, "--staged")) {
530 add_head_to_pending(&rev);
531 if (!rev.pending.nr) {
532 struct tree *tree;
533 tree = lookup_tree(the_repository,
534 the_repository->hash_algo->empty_tree);
535 add_pending_object(&rev, &tree->object, "HEAD");
537 break;
542 symdiff_prepare(&rev, &sdiff);
543 for (i = 0; i < rev.pending.nr; i++) {
544 struct object_array_entry *entry = &rev.pending.objects[i];
545 struct object *obj = entry->item;
546 const char *name = entry->name;
547 int flags = (obj->flags & UNINTERESTING);
548 if (!obj->parsed)
549 obj = parse_object(the_repository, &obj->oid);
550 obj = deref_tag(the_repository, obj, NULL, 0);
551 if (!obj)
552 die(_("invalid object '%s' given."), name);
553 if (obj->type == OBJ_COMMIT)
554 obj = &repo_get_commit_tree(the_repository,
555 ((struct commit *)obj))->object;
557 if (obj->type == OBJ_TREE) {
558 if (sdiff.skip && bitmap_get(sdiff.skip, i))
559 continue;
560 obj->flags |= flags;
561 add_object_array(obj, name, &ent);
562 if (first_non_parent < 0 &&
563 (i >= rev.cmdline.nr || /* HEAD by hand. */
564 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
565 first_non_parent = ent.nr - 1;
566 } else if (obj->type == OBJ_BLOB) {
567 if (2 <= blobs)
568 die(_("more than two blobs given: '%s'"), name);
569 blob[blobs] = entry;
570 blobs++;
572 } else {
573 die(_("unhandled object '%s' given."), name);
576 if (rev.prune_data.nr)
577 paths += rev.prune_data.nr;
580 * Now, do the arguments look reasonable?
582 if (!ent.nr) {
583 switch (blobs) {
584 case 0:
585 result = builtin_diff_files(&rev, argc, argv);
586 break;
587 case 1:
588 if (paths != 1)
589 usage(builtin_diff_usage);
590 result = builtin_diff_b_f(&rev, argc, argv, blob);
591 break;
592 case 2:
593 if (paths)
594 usage(builtin_diff_usage);
595 result = builtin_diff_blobs(&rev, argc, argv, blob);
596 break;
597 default:
598 usage(builtin_diff_usage);
601 else if (blobs)
602 usage(builtin_diff_usage);
603 else if (ent.nr == 1)
604 result = builtin_diff_index(&rev, argc, argv);
605 else if (ent.nr == 2) {
606 if (sdiff.warn)
607 warning(_("%s...%s: multiple merge bases, using %s"),
608 sdiff.left, sdiff.right, sdiff.base);
609 result = builtin_diff_tree(&rev, argc, argv,
610 &ent.objects[0], &ent.objects[1]);
611 } else
612 result = builtin_diff_combined(&rev, argc, argv,
613 ent.objects, ent.nr,
614 first_non_parent);
615 result = diff_result_code(&rev.diffopt, result);
616 if (1 < rev.diffopt.skip_stat_unmatch)
617 refresh_index_quietly();
618 release_revisions(&rev);
619 object_array_clear(&ent);
620 UNLEAK(blob);
621 return result;