4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
16 #include "diff-merges.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 [<commit>] [--] [<path>...]\n"
30 " or: git diff [<options>] <commit> [--merge-base] [<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,
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))
56 if (opt->flags.reverse_diff) {
57 SWAP(old_mode, new_mode);
58 SWAP(old_oid, new_oid);
59 SWAP(old_path, new_path);
63 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
64 strncmp(new_path, opt->prefix, opt->prefix_length)))
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*/
84 usage(builtin_diff_usage);
86 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
87 path = revs->prune_data.items[0].match;
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,
103 blob[0]->path ? blob[0]->path : path,
105 diffcore_std(&revs->diffopt);
106 diff_flush(&revs->diffopt);
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);
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,
129 blob_path(blob[0]), blob_path(blob[1]));
130 diffcore_std(&revs->diffopt);
131 diff_flush(&revs->diffopt);
135 static int builtin_diff_index(struct rev_info *revs,
136 int argc, const char **argv)
138 unsigned int option = 0;
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;
146 usage(builtin_diff_usage);
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 ||
156 usage(builtin_diff_usage);
157 if (!(option & DIFF_INDEX_CACHED)) {
159 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
160 perror("read_cache_preload");
163 } else if (read_cache() < 0) {
164 perror("read_cache");
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;
180 const char *arg = argv[1];
181 if (!strcmp(arg, "--merge-base"))
184 usage(builtin_diff_usage);
189 diff_get_merge_base(revs, &mb_oid);
191 oid[1] = &revs->pending.objects[1].item->oid;
196 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
199 if (ent1->item->flags & UNINTERESTING)
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);
209 static int builtin_diff_combined(struct rev_info *revs,
210 int argc, const char **argv,
211 struct object_array_entry *ent,
214 struct oid_array parents = OID_ARRAY_INIT;
218 usage(builtin_diff_usage);
220 diff_merges_set_dense_combined_if_unset(revs);
222 for (i = 1; i < ents; i++)
223 oid_array_append(&parents, &ent[i].item->oid);
224 diff_tree_combined(&ent[0].item->oid, &parents, revs);
225 oid_array_clear(&parents);
229 static void refresh_index_quietly(void)
231 struct lock_file lock_file = LOCK_INIT;
234 fd = hold_locked_index(&lock_file, 0);
239 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
240 repo_update_index_if_able(the_repository, &lock_file);
243 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
245 unsigned int options = 0;
247 while (1 < argc && argv[1][0] == '-') {
248 if (!strcmp(argv[1], "--base"))
250 else if (!strcmp(argv[1], "--ours"))
252 else if (!strcmp(argv[1], "--theirs"))
254 else if (!strcmp(argv[1], "-q"))
255 options |= DIFF_SILENT_ON_REMOVED;
256 else if (!strcmp(argv[1], "-h"))
257 usage(builtin_diff_usage);
259 return error(_("invalid option: %s"), argv[1]);
264 * "diff --base" should not combine merges because it was not
265 * asked to. "diff -c" should not densify (if the user wants
266 * dense one, --cc can be explicitly asked for, or just rely
269 if (revs->max_count == -1 &&
270 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
271 diff_merges_set_dense_combined_if_unset(revs);
274 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
275 perror("read_cache_preload");
278 return run_diff_files(revs, options);
284 const char *base, *left, *right;
288 * Check for symmetric-difference arguments, and if present, arrange
289 * everything we need to know to handle them correctly. As a bonus,
290 * weed out all bogus range-based revision specifications, e.g.,
291 * "git diff A..B C..D" or "git diff A..B C" get rejected.
293 * For an actual symmetric diff, *symdiff is set this way:
295 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
296 * indices that the caller should ignore (extra merge bases, of
297 * which there might be many, and A in A...B). Note that the
298 * chosen merge base and right side are NOT marked.
299 * - warn is set if there are multiple merge bases.
300 * - base, left, and right point to the names to use in a
301 * warning about multiple merge bases.
303 * If there is no symmetric diff argument, sym->skip is NULL and
304 * sym->warn is cleared. The remaining fields are not set.
306 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
308 int i, is_symdiff = 0, basecount = 0, othercount = 0;
309 int lpos = -1, rpos = -1, basepos = -1;
310 struct bitmap *map = NULL;
313 * Use the whence fields to find merge bases and left and
314 * right parts of symmetric difference, so that we do not
315 * depend on the order that revisions are parsed. If there
316 * are any revs that aren't from these sources, we have a
317 * "git diff C A...B" or "git diff A...B C" case. Or we
318 * could even get "git diff A...B C...E", for instance.
320 * If we don't have just one merge base, we pick one
323 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
324 * so we must check for SYMMETRIC_LEFT too. The two arrays
325 * rev->pending.objects and rev->cmdline.rev are parallel.
327 for (i = 0; i < rev->cmdline.nr; i++) {
328 struct object *obj = rev->pending.objects[i].item;
329 switch (rev->cmdline.rev[i].whence) {
330 case REV_CMD_MERGE_BASE:
334 break; /* do mark all bases */
337 usage(builtin_diff_usage);
339 if (obj->flags & SYMMETRIC_LEFT) {
341 break; /* do mark A */
346 usage(builtin_diff_usage);
348 continue; /* don't mark B */
349 case REV_CMD_PARENTS_ONLY:
361 * Forbid any additional revs for both A...B and A..B.
363 if (lpos >= 0 && othercount > 0)
364 usage(builtin_diff_usage);
373 sym->left = rev->pending.objects[lpos].name;
374 sym->right = rev->pending.objects[rpos].name;
376 die(_("%s...%s: no merge base"), sym->left, sym->right);
377 sym->base = rev->pending.objects[basepos].name;
378 bitmap_unset(map, basepos); /* unmark the base we want */
379 sym->warn = basecount > 1;
383 int cmd_diff(int argc, const char **argv, const char *prefix)
387 struct object_array ent = OBJECT_ARRAY_INIT;
388 int blobs = 0, paths = 0;
389 struct object_array_entry *blob[2];
390 int nongit = 0, no_index = 0;
392 struct symdiff sdiff;
395 * We could get N tree-ish in the rev.pending_objects list.
396 * Also there could be M blobs there, and P pathspecs. --cached may
400 * cache vs files (diff-files)
402 * N=0, M=0, --cached:
403 * HEAD vs cache (diff-index --cached)
406 * compare two random blobs. P must be zero.
409 * compare a blob with a working tree file.
412 * tree vs files (diff-index)
414 * N=1, M=0, --cached:
415 * tree vs cache (diff-index --cached)
418 * tree vs tree (diff-tree)
421 * compare two filesystem entities (aka --no-index).
423 * Other cases are errors.
426 /* Were we asked to do --no-index explicitly? */
427 for (i = 1; i < argc; i++) {
428 if (!strcmp(argv[i], "--")) {
432 if (!strcmp(argv[i], "--no-index"))
433 no_index = DIFF_NO_INDEX_EXPLICIT;
434 if (argv[i][0] != '-')
438 prefix = setup_git_directory_gently(&nongit);
442 * Treat git diff with at least one path outside of the
443 * repo the same as if the command would have been executed
444 * outside of a git repository. In this case it behaves
445 * the same way as "git diff --no-index <a> <b>", which acts
446 * as a colourful "diff" replacement.
448 if (nongit || ((argc == i + 2) &&
449 (!path_inside_repo(prefix, argv[i]) ||
450 !path_inside_repo(prefix, argv[i + 1]))))
451 no_index = DIFF_NO_INDEX_IMPLICIT;
454 init_diff_ui_defaults();
455 git_config(git_diff_ui_config, NULL);
456 precompose_argv(argc, argv);
458 repo_init_revisions(the_repository, &rev, prefix);
460 /* Set up defaults that will apply to both no-index and regular diffs. */
461 rev.diffopt.stat_width = -1;
462 rev.diffopt.stat_graph_width = -1;
463 rev.diffopt.flags.allow_external = 1;
464 rev.diffopt.flags.allow_textconv = 1;
466 /* If this is a no-index diff, just run it and exit there. */
468 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
473 * Otherwise, we are doing the usual "git" diff; set up any
474 * further defaults that apply to regular diffs.
476 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
479 * Default to intent-to-add entries invisible in the
480 * index. This makes them show up as new files in diff-files
481 * and not at all in diff-cached.
483 rev.diffopt.ita_invisible_in_index = 1;
486 die(_("Not a git repository"));
487 argc = setup_revisions(argc, argv, &rev, NULL);
488 if (!rev.diffopt.output_format) {
489 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
490 diff_setup_done(&rev.diffopt);
493 rev.diffopt.flags.recursive = 1;
495 setup_diff_pager(&rev.diffopt);
498 * Do we have --cached and not have a pending object, then
499 * default to HEAD by hand. Eek.
501 if (!rev.pending.nr) {
503 for (i = 1; i < argc; i++) {
504 const char *arg = argv[i];
505 if (!strcmp(arg, "--"))
507 else if (!strcmp(arg, "--cached") ||
508 !strcmp(arg, "--staged")) {
509 add_head_to_pending(&rev);
510 if (!rev.pending.nr) {
512 tree = lookup_tree(the_repository,
513 the_repository->hash_algo->empty_tree);
514 add_pending_object(&rev, &tree->object, "HEAD");
521 symdiff_prepare(&rev, &sdiff);
522 for (i = 0; i < rev.pending.nr; i++) {
523 struct object_array_entry *entry = &rev.pending.objects[i];
524 struct object *obj = entry->item;
525 const char *name = entry->name;
526 int flags = (obj->flags & UNINTERESTING);
528 obj = parse_object(the_repository, &obj->oid);
529 obj = deref_tag(the_repository, obj, NULL, 0);
531 die(_("invalid object '%s' given."), name);
532 if (obj->type == OBJ_COMMIT)
533 obj = &get_commit_tree(((struct commit *)obj))->object;
535 if (obj->type == OBJ_TREE) {
536 if (sdiff.skip && bitmap_get(sdiff.skip, i))
539 add_object_array(obj, name, &ent);
540 } else if (obj->type == OBJ_BLOB) {
542 die(_("more than two blobs given: '%s'"), name);
547 die(_("unhandled object '%s' given."), name);
550 if (rev.prune_data.nr)
551 paths += rev.prune_data.nr;
554 * Now, do the arguments look reasonable?
559 result = builtin_diff_files(&rev, argc, argv);
563 usage(builtin_diff_usage);
564 result = builtin_diff_b_f(&rev, argc, argv, blob);
568 usage(builtin_diff_usage);
569 result = builtin_diff_blobs(&rev, argc, argv, blob);
572 usage(builtin_diff_usage);
576 usage(builtin_diff_usage);
577 else if (ent.nr == 1)
578 result = builtin_diff_index(&rev, argc, argv);
579 else if (ent.nr == 2) {
581 warning(_("%s...%s: multiple merge bases, using %s"),
582 sdiff.left, sdiff.right, sdiff.base);
583 result = builtin_diff_tree(&rev, argc, argv,
584 &ent.objects[0], &ent.objects[1]);
586 result = builtin_diff_combined(&rev, argc, argv,
587 ent.objects, ent.nr);
588 result = diff_result_code(&rev.diffopt, result);
589 if (1 < rev.diffopt.skip_stat_unmatch)
590 refresh_index_quietly();