The second batch
[git.git] / builtin / diff.c
blobefc37483b329a66611936de65c4cb80ec0dae54f
1 /*
2 * Builtin "git diff"
4 * Copyright (c) 2006 Junio C Hamano
5 */
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 "gettext.h"
14 #include "tag.h"
15 #include "diff.h"
16 #include "diff-merges.h"
17 #include "diffcore.h"
18 #include "preload-index.h"
19 #include "read-cache-ll.h"
20 #include "revision.h"
21 #include "log-tree.h"
22 #include "setup.h"
23 #include "oid-array.h"
24 #include "tree.h"
26 #define DIFF_NO_INDEX_EXPLICIT 1
27 #define DIFF_NO_INDEX_IMPLICIT 2
29 static const char builtin_diff_usage[] =
30 "git diff [<options>] [<commit>] [--] [<path>...]\n"
31 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
32 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
33 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
34 " or: git diff [<options>] <blob> <blob>\n"
35 " or: git diff [<options>] --no-index [--] <path> <path>"
36 "\n"
37 COMMON_DIFF_OPTIONS_HELP;
39 static const char *blob_path(struct object_array_entry *entry)
41 return entry->path ? entry->path : entry->name;
44 static void stuff_change(struct diff_options *opt,
45 unsigned old_mode, unsigned new_mode,
46 const struct object_id *old_oid,
47 const struct object_id *new_oid,
48 int old_oid_valid,
49 int new_oid_valid,
50 const char *old_path,
51 const char *new_path)
53 struct diff_filespec *one, *two;
55 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
56 oideq(old_oid, new_oid) && (old_mode == new_mode))
57 return;
59 if (opt->flags.reverse_diff) {
60 SWAP(old_mode, new_mode);
61 SWAP(old_oid, new_oid);
62 SWAP(old_path, new_path);
65 if (opt->prefix &&
66 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
67 strncmp(new_path, opt->prefix, opt->prefix_length)))
68 return;
70 one = alloc_filespec(old_path);
71 two = alloc_filespec(new_path);
72 fill_filespec(one, old_oid, old_oid_valid, old_mode);
73 fill_filespec(two, new_oid, new_oid_valid, new_mode);
75 diff_queue(&diff_queued_diff, one, two);
78 static void builtin_diff_b_f(struct rev_info *revs,
79 int argc, const char **argv UNUSED,
80 struct object_array_entry **blob)
82 /* Blob vs file in the working tree*/
83 struct stat st;
84 const char *path;
86 if (argc > 1)
87 usage(builtin_diff_usage);
89 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
90 path = revs->prune_data.items[0].match;
92 if (lstat(path, &st))
93 die_errno(_("failed to stat '%s'"), path);
94 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
95 die(_("'%s': not a regular file or symlink"), path);
97 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
99 if (blob[0]->mode == S_IFINVALID)
100 blob[0]->mode = canon_mode(st.st_mode);
102 stuff_change(&revs->diffopt,
103 blob[0]->mode, canon_mode(st.st_mode),
104 &blob[0]->item->oid, null_oid(),
105 1, 0,
106 blob[0]->path ? blob[0]->path : path,
107 path);
108 diffcore_std(&revs->diffopt);
109 diff_flush(&revs->diffopt);
112 static void builtin_diff_blobs(struct rev_info *revs,
113 int argc, const char **argv UNUSED,
114 struct object_array_entry **blob)
116 const unsigned mode = canon_mode(S_IFREG | 0644);
118 if (argc > 1)
119 usage(builtin_diff_usage);
121 if (blob[0]->mode == S_IFINVALID)
122 blob[0]->mode = mode;
124 if (blob[1]->mode == S_IFINVALID)
125 blob[1]->mode = mode;
127 stuff_change(&revs->diffopt,
128 blob[0]->mode, blob[1]->mode,
129 &blob[0]->item->oid, &blob[1]->item->oid,
130 1, 1,
131 blob_path(blob[0]), blob_path(blob[1]));
132 diffcore_std(&revs->diffopt);
133 diff_flush(&revs->diffopt);
136 static void builtin_diff_index(struct rev_info *revs,
137 int argc, const char **argv)
139 unsigned int option = 0;
140 while (1 < argc) {
141 const char *arg = argv[1];
142 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
143 option |= DIFF_INDEX_CACHED;
144 else if (!strcmp(arg, "--merge-base"))
145 option |= DIFF_INDEX_MERGE_BASE;
146 else
147 usage(builtin_diff_usage);
148 argv++; argc--;
151 * Make sure there is one revision (i.e. pending object),
152 * and there is no revision filtering parameters.
154 if (revs->pending.nr != 1 ||
155 revs->max_count != -1 || revs->min_age != -1 ||
156 revs->max_age != -1)
157 usage(builtin_diff_usage);
158 if (!(option & DIFF_INDEX_CACHED)) {
159 setup_work_tree();
160 if (repo_read_index_preload(the_repository,
161 &revs->diffopt.pathspec, 0) < 0) {
162 die_errno("repo_read_index_preload");
164 } else if (repo_read_index(the_repository) < 0) {
165 die_errno("repo_read_cache");
167 run_diff_index(revs, option);
170 static void 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);
208 static void builtin_diff_combined(struct rev_info *revs,
209 int argc, const char **argv UNUSED,
210 struct object_array_entry *ent,
211 int ents, int first_non_parent)
213 struct oid_array parents = OID_ARRAY_INIT;
214 int i;
216 if (argc > 1)
217 usage(builtin_diff_usage);
219 if (first_non_parent < 0)
220 die(_("no merge given, only parents."));
221 if (first_non_parent >= ents)
222 BUG("first_non_parent out of range: %d", first_non_parent);
224 diff_merges_set_dense_combined_if_unset(revs);
226 for (i = 0; i < ents; i++) {
227 if (i != first_non_parent)
228 oid_array_append(&parents, &ent[i].item->oid);
230 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
231 oid_array_clear(&parents);
234 static void refresh_index_quietly(void)
236 struct lock_file lock_file = LOCK_INIT;
237 int fd;
239 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
240 if (fd < 0)
241 return;
242 discard_index(the_repository->index);
243 repo_read_index(the_repository);
244 refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
245 NULL);
246 repo_update_index_if_able(the_repository, &lock_file);
249 static void builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
251 unsigned int options = 0;
253 while (1 < argc && argv[1][0] == '-') {
254 if (!strcmp(argv[1], "--base"))
255 revs->max_count = 1;
256 else if (!strcmp(argv[1], "--ours"))
257 revs->max_count = 2;
258 else if (!strcmp(argv[1], "--theirs"))
259 revs->max_count = 3;
260 else if (!strcmp(argv[1], "-q"))
261 options |= DIFF_SILENT_ON_REMOVED;
262 else if (!strcmp(argv[1], "-h"))
263 usage(builtin_diff_usage);
264 else {
265 error(_("invalid option: %s"), argv[1]);
266 usage(builtin_diff_usage);
268 argv++; argc--;
272 * "diff --base" should not combine merges because it was not
273 * asked to. "diff -c" should not densify (if the user wants
274 * dense one, --cc can be explicitly asked for, or just rely
275 * on the default).
277 if (revs->max_count == -1 &&
278 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
279 diff_merges_set_dense_combined_if_unset(revs);
281 setup_work_tree();
282 if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
283 0) < 0) {
284 die_errno("repo_read_index_preload");
286 run_diff_files(revs, options);
289 struct symdiff {
290 struct bitmap *skip;
291 int warn;
292 const char *base, *left, *right;
296 * Check for symmetric-difference arguments, and if present, arrange
297 * everything we need to know to handle them correctly. As a bonus,
298 * weed out all bogus range-based revision specifications, e.g.,
299 * "git diff A..B C..D" or "git diff A..B C" get rejected.
301 * For an actual symmetric diff, *symdiff is set this way:
303 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
304 * indices that the caller should ignore (extra merge bases, of
305 * which there might be many, and A in A...B). Note that the
306 * chosen merge base and right side are NOT marked.
307 * - warn is set if there are multiple merge bases.
308 * - base, left, and right point to the names to use in a
309 * warning about multiple merge bases.
311 * If there is no symmetric diff argument, sym->skip is NULL and
312 * sym->warn is cleared. The remaining fields are not set.
314 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
316 int i, is_symdiff = 0, basecount = 0, othercount = 0;
317 int lpos = -1, rpos = -1, basepos = -1;
318 struct bitmap *map = NULL;
321 * Use the whence fields to find merge bases and left and
322 * right parts of symmetric difference, so that we do not
323 * depend on the order that revisions are parsed. If there
324 * are any revs that aren't from these sources, we have a
325 * "git diff C A...B" or "git diff A...B C" case. Or we
326 * could even get "git diff A...B C...E", for instance.
328 * If we don't have just one merge base, we pick one
329 * at random.
331 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
332 * so we must check for SYMMETRIC_LEFT too. The two arrays
333 * rev->pending.objects and rev->cmdline.rev are parallel.
335 for (i = 0; i < rev->cmdline.nr; i++) {
336 struct object *obj = rev->pending.objects[i].item;
337 switch (rev->cmdline.rev[i].whence) {
338 case REV_CMD_MERGE_BASE:
339 if (basepos < 0)
340 basepos = i;
341 basecount++;
342 break; /* do mark all bases */
343 case REV_CMD_LEFT:
344 if (lpos >= 0)
345 usage(builtin_diff_usage);
346 lpos = i;
347 if (obj->flags & SYMMETRIC_LEFT) {
348 is_symdiff = 1;
349 break; /* do mark A */
351 continue;
352 case REV_CMD_RIGHT:
353 if (rpos >= 0)
354 usage(builtin_diff_usage);
355 rpos = i;
356 continue; /* don't mark B */
357 case REV_CMD_PARENTS_ONLY:
358 case REV_CMD_REF:
359 case REV_CMD_REV:
360 othercount++;
361 continue;
363 if (!map)
364 map = bitmap_new();
365 bitmap_set(map, i);
369 * Forbid any additional revs for both A...B and A..B.
371 if (lpos >= 0 && othercount > 0)
372 usage(builtin_diff_usage);
374 if (!is_symdiff) {
375 bitmap_free(map);
376 sym->warn = 0;
377 sym->skip = NULL;
378 return;
381 sym->left = rev->pending.objects[lpos].name;
382 sym->right = rev->pending.objects[rpos].name;
383 if (basecount == 0)
384 die(_("%s...%s: no merge base"), sym->left, sym->right);
385 sym->base = rev->pending.objects[basepos].name;
386 bitmap_unset(map, basepos); /* unmark the base we want */
387 sym->warn = basecount > 1;
388 sym->skip = map;
391 int cmd_diff(int argc, const char **argv, const char *prefix)
393 int i;
394 struct rev_info rev;
395 struct object_array ent = OBJECT_ARRAY_INIT;
396 int first_non_parent = -1;
397 int blobs = 0, paths = 0;
398 struct object_array_entry *blob[2];
399 int nongit = 0, no_index = 0;
400 int result;
401 struct symdiff sdiff;
404 * We could get N tree-ish in the rev.pending_objects list.
405 * Also there could be M blobs there, and P pathspecs. --cached may
406 * also be present.
408 * N=0, M=0:
409 * cache vs files (diff-files)
411 * N=0, M=0, --cached:
412 * HEAD vs cache (diff-index --cached)
414 * N=0, M=2:
415 * compare two random blobs. P must be zero.
417 * N=0, M=1, P=1:
418 * compare a blob with a working tree file.
420 * N=1, M=0:
421 * tree vs files (diff-index)
423 * N=1, M=0, --cached:
424 * tree vs cache (diff-index --cached)
426 * N=2, M=0:
427 * tree vs tree (diff-tree)
429 * N=0, M=0, P=2:
430 * compare two filesystem entities (aka --no-index).
432 * Other cases are errors.
435 /* Were we asked to do --no-index explicitly? */
436 for (i = 1; i < argc; i++) {
437 if (!strcmp(argv[i], "--")) {
438 i++;
439 break;
441 if (!strcmp(argv[i], "--no-index"))
442 no_index = DIFF_NO_INDEX_EXPLICIT;
443 if (argv[i][0] != '-')
444 break;
447 prefix = setup_git_directory_gently(&nongit);
449 if (!nongit) {
450 prepare_repo_settings(the_repository);
451 the_repository->settings.command_requires_full_index = 0;
454 if (!no_index) {
456 * Treat git diff with at least one path outside of the
457 * repo the same as if the command would have been executed
458 * outside of a git repository. In this case it behaves
459 * the same way as "git diff --no-index <a> <b>", which acts
460 * as a colourful "diff" replacement.
462 if (nongit || ((argc == i + 2) &&
463 (!path_inside_repo(prefix, argv[i]) ||
464 !path_inside_repo(prefix, argv[i + 1]))))
465 no_index = DIFF_NO_INDEX_IMPLICIT;
468 init_diff_ui_defaults();
469 git_config(git_diff_ui_config, NULL);
470 prefix = precompose_argv_prefix(argc, argv, prefix);
472 repo_init_revisions(the_repository, &rev, prefix);
474 /* Set up defaults that will apply to both no-index and regular diffs. */
475 init_diffstat_widths(&rev.diffopt);
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 = &repo_get_commit_tree(the_repository,
548 ((struct commit *)obj))->object;
550 if (obj->type == OBJ_TREE) {
551 if (sdiff.skip && bitmap_get(sdiff.skip, i))
552 continue;
553 obj->flags |= flags;
554 add_object_array(obj, name, &ent);
555 if (first_non_parent < 0 &&
556 (i >= rev.cmdline.nr || /* HEAD by hand. */
557 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
558 first_non_parent = ent.nr - 1;
559 } else if (obj->type == OBJ_BLOB) {
560 if (2 <= blobs)
561 die(_("more than two blobs given: '%s'"), name);
562 blob[blobs] = entry;
563 blobs++;
565 } else {
566 die(_("unhandled object '%s' given."), name);
569 if (rev.prune_data.nr)
570 paths += rev.prune_data.nr;
573 * Now, do the arguments look reasonable?
575 if (!ent.nr) {
576 switch (blobs) {
577 case 0:
578 builtin_diff_files(&rev, argc, argv);
579 break;
580 case 1:
581 if (paths != 1)
582 usage(builtin_diff_usage);
583 builtin_diff_b_f(&rev, argc, argv, blob);
584 break;
585 case 2:
586 if (paths)
587 usage(builtin_diff_usage);
588 builtin_diff_blobs(&rev, argc, argv, blob);
589 break;
590 default:
591 usage(builtin_diff_usage);
594 else if (blobs)
595 usage(builtin_diff_usage);
596 else if (ent.nr == 1)
597 builtin_diff_index(&rev, argc, argv);
598 else if (ent.nr == 2) {
599 if (sdiff.warn)
600 warning(_("%s...%s: multiple merge bases, using %s"),
601 sdiff.left, sdiff.right, sdiff.base);
602 builtin_diff_tree(&rev, argc, argv,
603 &ent.objects[0], &ent.objects[1]);
604 } else
605 builtin_diff_combined(&rev, argc, argv,
606 ent.objects, ent.nr,
607 first_non_parent);
608 result = diff_result_code(&rev.diffopt);
609 if (1 < rev.diffopt.skip_stat_unmatch)
610 refresh_index_quietly();
611 release_revisions(&rev);
612 object_array_clear(&ent);
613 UNLEAK(blob);
614 return result;