4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
20 #include "submodule.h"
21 #include "sha1-array.h"
23 #define DIFF_NO_INDEX_EXPLICIT 1
24 #define DIFF_NO_INDEX_IMPLICIT 2
26 static const char builtin_diff_usage[] =
27 "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
29 static const char *blob_path(struct object_array_entry *entry)
31 return entry->path ? entry->path : entry->name;
34 static void stuff_change(struct diff_options *opt,
35 unsigned old_mode, unsigned new_mode,
36 const struct object_id *old_oid,
37 const struct object_id *new_oid,
43 struct diff_filespec *one, *two;
45 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
46 oideq(old_oid, new_oid) && (old_mode == new_mode))
49 if (opt->flags.reverse_diff) {
50 SWAP(old_mode, new_mode);
51 SWAP(old_oid, new_oid);
52 SWAP(old_path, new_path);
56 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
57 strncmp(new_path, opt->prefix, opt->prefix_length)))
60 one = alloc_filespec(old_path);
61 two = alloc_filespec(new_path);
62 fill_filespec(one, old_oid, old_oid_valid, old_mode);
63 fill_filespec(two, new_oid, new_oid_valid, new_mode);
65 diff_queue(&diff_queued_diff, one, two);
68 static int builtin_diff_b_f(struct rev_info *revs,
69 int argc, const char **argv,
70 struct object_array_entry **blob)
72 /* Blob vs file in the working tree*/
77 usage(builtin_diff_usage);
79 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
80 path = revs->prune_data.items[0].match;
83 die_errno(_("failed to stat '%s'"), path);
84 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
85 die(_("'%s': not a regular file or symlink"), path);
87 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
89 if (blob[0]->mode == S_IFINVALID)
90 blob[0]->mode = canon_mode(st.st_mode);
92 stuff_change(&revs->diffopt,
93 blob[0]->mode, canon_mode(st.st_mode),
94 &blob[0]->item->oid, &null_oid,
96 blob[0]->path ? blob[0]->path : path,
98 diffcore_std(&revs->diffopt);
99 diff_flush(&revs->diffopt);
103 static int builtin_diff_blobs(struct rev_info *revs,
104 int argc, const char **argv,
105 struct object_array_entry **blob)
107 const unsigned mode = canon_mode(S_IFREG | 0644);
110 usage(builtin_diff_usage);
112 if (blob[0]->mode == S_IFINVALID)
113 blob[0]->mode = mode;
115 if (blob[1]->mode == S_IFINVALID)
116 blob[1]->mode = mode;
118 stuff_change(&revs->diffopt,
119 blob[0]->mode, blob[1]->mode,
120 &blob[0]->item->oid, &blob[1]->item->oid,
122 blob_path(blob[0]), blob_path(blob[1]));
123 diffcore_std(&revs->diffopt);
124 diff_flush(&revs->diffopt);
128 static int builtin_diff_index(struct rev_info *revs,
129 int argc, const char **argv)
133 const char *arg = argv[1];
134 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
137 usage(builtin_diff_usage);
141 * Make sure there is one revision (i.e. pending object),
142 * and there is no revision filtering parameters.
144 if (revs->pending.nr != 1 ||
145 revs->max_count != -1 || revs->min_age != -1 ||
147 usage(builtin_diff_usage);
150 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
151 perror("read_cache_preload");
154 } else if (read_cache() < 0) {
155 perror("read_cache");
158 return run_diff_index(revs, cached);
161 static int builtin_diff_tree(struct rev_info *revs,
162 int argc, const char **argv,
163 struct object_array_entry *ent0,
164 struct object_array_entry *ent1)
166 const struct object_id *(oid[2]);
170 usage(builtin_diff_usage);
173 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
176 if (ent1->item->flags & UNINTERESTING)
178 oid[swap] = &ent0->item->oid;
179 oid[1 - swap] = &ent1->item->oid;
180 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
181 log_tree_diff_flush(revs);
185 static int builtin_diff_combined(struct rev_info *revs,
186 int argc, const char **argv,
187 struct object_array_entry *ent,
190 struct oid_array parents = OID_ARRAY_INIT;
194 usage(builtin_diff_usage);
196 if (!revs->dense_combined_merges && !revs->combine_merges)
197 revs->dense_combined_merges = revs->combine_merges = 1;
198 for (i = 1; i < ents; i++)
199 oid_array_append(&parents, &ent[i].item->oid);
200 diff_tree_combined(&ent[0].item->oid, &parents,
201 revs->dense_combined_merges, revs);
202 oid_array_clear(&parents);
206 static void refresh_index_quietly(void)
208 struct lock_file lock_file = LOCK_INIT;
211 fd = hold_locked_index(&lock_file, 0);
216 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
217 repo_update_index_if_able(the_repository, &lock_file);
220 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
222 unsigned int options = 0;
224 while (1 < argc && argv[1][0] == '-') {
225 if (!strcmp(argv[1], "--base"))
227 else if (!strcmp(argv[1], "--ours"))
229 else if (!strcmp(argv[1], "--theirs"))
231 else if (!strcmp(argv[1], "-q"))
232 options |= DIFF_SILENT_ON_REMOVED;
233 else if (!strcmp(argv[1], "-h"))
234 usage(builtin_diff_usage);
236 return error(_("invalid option: %s"), argv[1]);
241 * "diff --base" should not combine merges because it was not
242 * asked to. "diff -c" should not densify (if the user wants
243 * dense one, --cc can be explicitly asked for, or just rely
246 if (revs->max_count == -1 && !revs->combine_merges &&
247 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
248 revs->combine_merges = revs->dense_combined_merges = 1;
251 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
252 perror("read_cache_preload");
255 return run_diff_files(revs, options);
261 const char *base, *left, *right;
265 * Check for symmetric-difference arguments, and if present, arrange
266 * everything we need to know to handle them correctly. As a bonus,
267 * weed out all bogus range-based revision specifications, e.g.,
268 * "git diff A..B C..D" or "git diff A..B C" get rejected.
270 * For an actual symmetric diff, *symdiff is set this way:
272 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
273 * indices that the caller should ignore (extra merge bases, of
274 * which there might be many, and A in A...B). Note that the
275 * chosen merge base and right side are NOT marked.
276 * - warn is set if there are multiple merge bases.
277 * - base, left, and right point to the names to use in a
278 * warning about multiple merge bases.
280 * If there is no symmetric diff argument, sym->skip is NULL and
281 * sym->warn is cleared. The remaining fields are not set.
283 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
285 int i, is_symdiff = 0, basecount = 0, othercount = 0;
286 int lpos = -1, rpos = -1, basepos = -1;
287 struct bitmap *map = NULL;
290 * Use the whence fields to find merge bases and left and
291 * right parts of symmetric difference, so that we do not
292 * depend on the order that revisions are parsed. If there
293 * are any revs that aren't from these sources, we have a
294 * "git diff C A...B" or "git diff A...B C" case. Or we
295 * could even get "git diff A...B C...E", for instance.
297 * If we don't have just one merge base, we pick one
300 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
301 * so we must check for SYMMETRIC_LEFT too. The two arrays
302 * rev->pending.objects and rev->cmdline.rev are parallel.
304 for (i = 0; i < rev->cmdline.nr; i++) {
305 struct object *obj = rev->pending.objects[i].item;
306 switch (rev->cmdline.rev[i].whence) {
307 case REV_CMD_MERGE_BASE:
311 break; /* do mark all bases */
314 usage(builtin_diff_usage);
316 if (obj->flags & SYMMETRIC_LEFT) {
318 break; /* do mark A */
323 usage(builtin_diff_usage);
325 continue; /* don't mark B */
326 case REV_CMD_PARENTS_ONLY:
338 * Forbid any additional revs for both A...B and A..B.
340 if (lpos >= 0 && othercount > 0)
341 usage(builtin_diff_usage);
350 sym->left = rev->pending.objects[lpos].name;
351 sym->right = rev->pending.objects[rpos].name;
352 sym->base = rev->pending.objects[basepos].name;
354 die(_("%s...%s: no merge base"), sym->left, sym->right);
355 bitmap_unset(map, basepos); /* unmark the base we want */
356 sym->warn = basecount > 1;
360 int cmd_diff(int argc, const char **argv, const char *prefix)
364 struct object_array ent = OBJECT_ARRAY_INIT;
365 int blobs = 0, paths = 0;
366 struct object_array_entry *blob[2];
367 int nongit = 0, no_index = 0;
369 struct symdiff sdiff;
372 * We could get N tree-ish in the rev.pending_objects list.
373 * Also there could be M blobs there, and P pathspecs.
376 * cache vs files (diff-files)
378 * compare two random blobs. P must be zero.
380 * compare a blob with a working tree file.
383 * tree vs cache (diff-index --cached)
386 * tree vs tree (diff-tree)
389 * compare two filesystem entities (aka --no-index).
391 * Other cases are errors.
394 /* Were we asked to do --no-index explicitly? */
395 for (i = 1; i < argc; i++) {
396 if (!strcmp(argv[i], "--")) {
400 if (!strcmp(argv[i], "--no-index"))
401 no_index = DIFF_NO_INDEX_EXPLICIT;
402 if (argv[i][0] != '-')
406 prefix = setup_git_directory_gently(&nongit);
410 * Treat git diff with at least one path outside of the
411 * repo the same as if the command would have been executed
412 * outside of a git repository. In this case it behaves
413 * the same way as "git diff --no-index <a> <b>", which acts
414 * as a colourful "diff" replacement.
416 if (nongit || ((argc == i + 2) &&
417 (!path_inside_repo(prefix, argv[i]) ||
418 !path_inside_repo(prefix, argv[i + 1]))))
419 no_index = DIFF_NO_INDEX_IMPLICIT;
422 init_diff_ui_defaults();
423 git_config(git_diff_ui_config, NULL);
424 precompose_argv(argc, argv);
426 repo_init_revisions(the_repository, &rev, prefix);
428 /* Set up defaults that will apply to both no-index and regular diffs. */
429 rev.diffopt.stat_width = -1;
430 rev.diffopt.stat_graph_width = -1;
431 rev.diffopt.flags.allow_external = 1;
432 rev.diffopt.flags.allow_textconv = 1;
434 /* If this is a no-index diff, just run it and exit there. */
436 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
441 * Otherwise, we are doing the usual "git" diff; set up any
442 * further defaults that apply to regular diffs.
444 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
447 * Default to intent-to-add entries invisible in the
448 * index. This makes them show up as new files in diff-files
449 * and not at all in diff-cached.
451 rev.diffopt.ita_invisible_in_index = 1;
454 die(_("Not a git repository"));
455 argc = setup_revisions(argc, argv, &rev, NULL);
456 if (!rev.diffopt.output_format) {
457 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
458 diff_setup_done(&rev.diffopt);
461 rev.diffopt.flags.recursive = 1;
463 setup_diff_pager(&rev.diffopt);
466 * Do we have --cached and not have a pending object, then
467 * default to HEAD by hand. Eek.
469 if (!rev.pending.nr) {
471 for (i = 1; i < argc; i++) {
472 const char *arg = argv[i];
473 if (!strcmp(arg, "--"))
475 else if (!strcmp(arg, "--cached") ||
476 !strcmp(arg, "--staged")) {
477 add_head_to_pending(&rev);
478 if (!rev.pending.nr) {
480 tree = lookup_tree(the_repository,
481 the_repository->hash_algo->empty_tree);
482 add_pending_object(&rev, &tree->object, "HEAD");
489 symdiff_prepare(&rev, &sdiff);
490 for (i = 0; i < rev.pending.nr; i++) {
491 struct object_array_entry *entry = &rev.pending.objects[i];
492 struct object *obj = entry->item;
493 const char *name = entry->name;
494 int flags = (obj->flags & UNINTERESTING);
496 obj = parse_object(the_repository, &obj->oid);
497 obj = deref_tag(the_repository, obj, NULL, 0);
499 die(_("invalid object '%s' given."), name);
500 if (obj->type == OBJ_COMMIT)
501 obj = &get_commit_tree(((struct commit *)obj))->object;
503 if (obj->type == OBJ_TREE) {
504 if (sdiff.skip && bitmap_get(sdiff.skip, i))
507 add_object_array(obj, name, &ent);
508 } else if (obj->type == OBJ_BLOB) {
510 die(_("more than two blobs given: '%s'"), name);
515 die(_("unhandled object '%s' given."), name);
518 if (rev.prune_data.nr)
519 paths += rev.prune_data.nr;
522 * Now, do the arguments look reasonable?
527 result = builtin_diff_files(&rev, argc, argv);
531 usage(builtin_diff_usage);
532 result = builtin_diff_b_f(&rev, argc, argv, blob);
536 usage(builtin_diff_usage);
537 result = builtin_diff_blobs(&rev, argc, argv, blob);
540 usage(builtin_diff_usage);
544 usage(builtin_diff_usage);
545 else if (ent.nr == 1)
546 result = builtin_diff_index(&rev, argc, argv);
547 else if (ent.nr == 2) {
549 warning(_("%s...%s: multiple merge bases, using %s"),
550 sdiff.left, sdiff.right, sdiff.base);
551 result = builtin_diff_tree(&rev, argc, argv,
552 &ent.objects[0], &ent.objects[1]);
554 result = builtin_diff_combined(&rev, argc, argv,
555 ent.objects, ent.nr);
556 result = diff_result_code(&rev.diffopt, result);
557 if (1 < rev.diffopt.skip_stat_unmatch)
558 refresh_index_quietly();