t5559: make SSL/TLS the default
[git.git] / builtin / diff.c
blob163f2c6a874266f183b276fa30961e76feb11316
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 "tag.h"
15 #include "diff.h"
16 #include "diff-merges.h"
17 #include "diffcore.h"
18 #include "revision.h"
19 #include "log-tree.h"
20 #include "builtin.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 [--merge-base] [<commit>] [--] [<path>...]\n"
30 " or: git diff [<options>] [--merge-base] <commit> [<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>"
34 "\n"
35 COMMON_DIFF_OPTIONS_HELP;
37 static const char *blob_path(struct object_array_entry *entry)
39 return entry->path ? entry->path : entry->name;
42 static void stuff_change(struct diff_options *opt,
43 unsigned old_mode, unsigned new_mode,
44 const struct object_id *old_oid,
45 const struct object_id *new_oid,
46 int old_oid_valid,
47 int new_oid_valid,
48 const char *old_path,
49 const char *new_path)
51 struct diff_filespec *one, *two;
53 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
54 oideq(old_oid, new_oid) && (old_mode == new_mode))
55 return;
57 if (opt->flags.reverse_diff) {
58 SWAP(old_mode, new_mode);
59 SWAP(old_oid, new_oid);
60 SWAP(old_path, new_path);
63 if (opt->prefix &&
64 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
65 strncmp(new_path, opt->prefix, opt->prefix_length)))
66 return;
68 one = alloc_filespec(old_path);
69 two = alloc_filespec(new_path);
70 fill_filespec(one, old_oid, old_oid_valid, old_mode);
71 fill_filespec(two, new_oid, new_oid_valid, new_mode);
73 diff_queue(&diff_queued_diff, one, two);
76 static int builtin_diff_b_f(struct rev_info *revs,
77 int argc, const char **argv,
78 struct object_array_entry **blob)
80 /* Blob vs file in the working tree*/
81 struct stat st;
82 const char *path;
84 if (argc > 1)
85 usage(builtin_diff_usage);
87 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
88 path = revs->prune_data.items[0].match;
90 if (lstat(path, &st))
91 die_errno(_("failed to stat '%s'"), path);
92 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
93 die(_("'%s': not a regular file or symlink"), path);
95 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
97 if (blob[0]->mode == S_IFINVALID)
98 blob[0]->mode = canon_mode(st.st_mode);
100 stuff_change(&revs->diffopt,
101 blob[0]->mode, canon_mode(st.st_mode),
102 &blob[0]->item->oid, null_oid(),
103 1, 0,
104 blob[0]->path ? blob[0]->path : path,
105 path);
106 diffcore_std(&revs->diffopt);
107 diff_flush(&revs->diffopt);
108 return 0;
111 static int builtin_diff_blobs(struct rev_info *revs,
112 int argc, const char **argv,
113 struct object_array_entry **blob)
115 const unsigned mode = canon_mode(S_IFREG | 0644);
117 if (argc > 1)
118 usage(builtin_diff_usage);
120 if (blob[0]->mode == S_IFINVALID)
121 blob[0]->mode = mode;
123 if (blob[1]->mode == S_IFINVALID)
124 blob[1]->mode = mode;
126 stuff_change(&revs->diffopt,
127 blob[0]->mode, blob[1]->mode,
128 &blob[0]->item->oid, &blob[1]->item->oid,
129 1, 1,
130 blob_path(blob[0]), blob_path(blob[1]));
131 diffcore_std(&revs->diffopt);
132 diff_flush(&revs->diffopt);
133 return 0;
136 static int 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 perror("repo_read_index_preload");
163 return -1;
165 } else if (repo_read_index(the_repository) < 0) {
166 perror("repo_read_cache");
167 return -1;
169 return run_diff_index(revs, option);
172 static int 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);
208 return 0;
211 static int builtin_diff_combined(struct rev_info *revs,
212 int argc, const char **argv,
213 struct object_array_entry *ent,
214 int ents, int first_non_parent)
216 struct oid_array parents = OID_ARRAY_INIT;
217 int i;
219 if (argc > 1)
220 usage(builtin_diff_usage);
222 if (first_non_parent < 0)
223 die(_("no merge given, only parents."));
224 if (first_non_parent >= ents)
225 BUG("first_non_parent out of range: %d", first_non_parent);
227 diff_merges_set_dense_combined_if_unset(revs);
229 for (i = 0; i < ents; i++) {
230 if (i != first_non_parent)
231 oid_array_append(&parents, &ent[i].item->oid);
233 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
234 oid_array_clear(&parents);
235 return 0;
238 static void refresh_index_quietly(void)
240 struct lock_file lock_file = LOCK_INIT;
241 int fd;
243 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
244 if (fd < 0)
245 return;
246 discard_index(&the_index);
247 repo_read_index(the_repository);
248 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
249 NULL);
250 repo_update_index_if_able(the_repository, &lock_file);
253 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
255 unsigned int options = 0;
257 while (1 < argc && argv[1][0] == '-') {
258 if (!strcmp(argv[1], "--base"))
259 revs->max_count = 1;
260 else if (!strcmp(argv[1], "--ours"))
261 revs->max_count = 2;
262 else if (!strcmp(argv[1], "--theirs"))
263 revs->max_count = 3;
264 else if (!strcmp(argv[1], "-q"))
265 options |= DIFF_SILENT_ON_REMOVED;
266 else if (!strcmp(argv[1], "-h"))
267 usage(builtin_diff_usage);
268 else
269 return error(_("invalid option: %s"), argv[1]);
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 perror("repo_read_index_preload");
287 return -1;
289 return run_diff_files(revs, options);
292 struct symdiff {
293 struct bitmap *skip;
294 int warn;
295 const char *base, *left, *right;
299 * Check for symmetric-difference arguments, and if present, arrange
300 * everything we need to know to handle them correctly. As a bonus,
301 * weed out all bogus range-based revision specifications, e.g.,
302 * "git diff A..B C..D" or "git diff A..B C" get rejected.
304 * For an actual symmetric diff, *symdiff is set this way:
306 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
307 * indices that the caller should ignore (extra merge bases, of
308 * which there might be many, and A in A...B). Note that the
309 * chosen merge base and right side are NOT marked.
310 * - warn is set if there are multiple merge bases.
311 * - base, left, and right point to the names to use in a
312 * warning about multiple merge bases.
314 * If there is no symmetric diff argument, sym->skip is NULL and
315 * sym->warn is cleared. The remaining fields are not set.
317 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
319 int i, is_symdiff = 0, basecount = 0, othercount = 0;
320 int lpos = -1, rpos = -1, basepos = -1;
321 struct bitmap *map = NULL;
324 * Use the whence fields to find merge bases and left and
325 * right parts of symmetric difference, so that we do not
326 * depend on the order that revisions are parsed. If there
327 * are any revs that aren't from these sources, we have a
328 * "git diff C A...B" or "git diff A...B C" case. Or we
329 * could even get "git diff A...B C...E", for instance.
331 * If we don't have just one merge base, we pick one
332 * at random.
334 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
335 * so we must check for SYMMETRIC_LEFT too. The two arrays
336 * rev->pending.objects and rev->cmdline.rev are parallel.
338 for (i = 0; i < rev->cmdline.nr; i++) {
339 struct object *obj = rev->pending.objects[i].item;
340 switch (rev->cmdline.rev[i].whence) {
341 case REV_CMD_MERGE_BASE:
342 if (basepos < 0)
343 basepos = i;
344 basecount++;
345 break; /* do mark all bases */
346 case REV_CMD_LEFT:
347 if (lpos >= 0)
348 usage(builtin_diff_usage);
349 lpos = i;
350 if (obj->flags & SYMMETRIC_LEFT) {
351 is_symdiff = 1;
352 break; /* do mark A */
354 continue;
355 case REV_CMD_RIGHT:
356 if (rpos >= 0)
357 usage(builtin_diff_usage);
358 rpos = i;
359 continue; /* don't mark B */
360 case REV_CMD_PARENTS_ONLY:
361 case REV_CMD_REF:
362 case REV_CMD_REV:
363 othercount++;
364 continue;
366 if (!map)
367 map = bitmap_new();
368 bitmap_set(map, i);
372 * Forbid any additional revs for both A...B and A..B.
374 if (lpos >= 0 && othercount > 0)
375 usage(builtin_diff_usage);
377 if (!is_symdiff) {
378 bitmap_free(map);
379 sym->warn = 0;
380 sym->skip = NULL;
381 return;
384 sym->left = rev->pending.objects[lpos].name;
385 sym->right = rev->pending.objects[rpos].name;
386 if (basecount == 0)
387 die(_("%s...%s: no merge base"), sym->left, sym->right);
388 sym->base = rev->pending.objects[basepos].name;
389 bitmap_unset(map, basepos); /* unmark the base we want */
390 sym->warn = basecount > 1;
391 sym->skip = map;
394 int cmd_diff(int argc, const char **argv, const char *prefix)
396 int i;
397 struct rev_info rev;
398 struct object_array ent = OBJECT_ARRAY_INIT;
399 int first_non_parent = -1;
400 int blobs = 0, paths = 0;
401 struct object_array_entry *blob[2];
402 int nongit = 0, no_index = 0;
403 int result = 0;
404 struct symdiff sdiff;
407 * We could get N tree-ish in the rev.pending_objects list.
408 * Also there could be M blobs there, and P pathspecs. --cached may
409 * also be present.
411 * N=0, M=0:
412 * cache vs files (diff-files)
414 * N=0, M=0, --cached:
415 * HEAD vs cache (diff-index --cached)
417 * N=0, M=2:
418 * compare two random blobs. P must be zero.
420 * N=0, M=1, P=1:
421 * compare a blob with a working tree file.
423 * N=1, M=0:
424 * tree vs files (diff-index)
426 * N=1, M=0, --cached:
427 * tree vs cache (diff-index --cached)
429 * N=2, M=0:
430 * tree vs tree (diff-tree)
432 * N=0, M=0, P=2:
433 * compare two filesystem entities (aka --no-index).
435 * Other cases are errors.
438 /* Were we asked to do --no-index explicitly? */
439 for (i = 1; i < argc; i++) {
440 if (!strcmp(argv[i], "--")) {
441 i++;
442 break;
444 if (!strcmp(argv[i], "--no-index"))
445 no_index = DIFF_NO_INDEX_EXPLICIT;
446 if (argv[i][0] != '-')
447 break;
450 prefix = setup_git_directory_gently(&nongit);
452 if (!nongit) {
453 prepare_repo_settings(the_repository);
454 the_repository->settings.command_requires_full_index = 0;
457 if (!no_index) {
459 * Treat git diff with at least one path outside of the
460 * repo the same as if the command would have been executed
461 * outside of a git repository. In this case it behaves
462 * the same way as "git diff --no-index <a> <b>", which acts
463 * as a colourful "diff" replacement.
465 if (nongit || ((argc == i + 2) &&
466 (!path_inside_repo(prefix, argv[i]) ||
467 !path_inside_repo(prefix, argv[i + 1]))))
468 no_index = DIFF_NO_INDEX_IMPLICIT;
471 init_diff_ui_defaults();
472 git_config(git_diff_ui_config, NULL);
473 prefix = precompose_argv_prefix(argc, argv, prefix);
475 repo_init_revisions(the_repository, &rev, prefix);
477 /* Set up defaults that will apply to both no-index and regular diffs. */
478 rev.diffopt.stat_width = -1;
479 rev.diffopt.stat_graph_width = -1;
480 rev.diffopt.flags.allow_external = 1;
481 rev.diffopt.flags.allow_textconv = 1;
483 /* If this is a no-index diff, just run it and exit there. */
484 if (no_index)
485 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
486 argc, argv));
490 * Otherwise, we are doing the usual "git" diff; set up any
491 * further defaults that apply to regular diffs.
493 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
496 * Default to intent-to-add entries invisible in the
497 * index. This makes them show up as new files in diff-files
498 * and not at all in diff-cached.
500 rev.diffopt.ita_invisible_in_index = 1;
502 if (nongit)
503 die(_("Not a git repository"));
504 argc = setup_revisions(argc, argv, &rev, NULL);
505 if (!rev.diffopt.output_format) {
506 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
507 diff_setup_done(&rev.diffopt);
510 rev.diffopt.flags.recursive = 1;
511 rev.diffopt.rotate_to_strict = 1;
513 setup_diff_pager(&rev.diffopt);
516 * Do we have --cached and not have a pending object, then
517 * default to HEAD by hand. Eek.
519 if (!rev.pending.nr) {
520 int i;
521 for (i = 1; i < argc; i++) {
522 const char *arg = argv[i];
523 if (!strcmp(arg, "--"))
524 break;
525 else if (!strcmp(arg, "--cached") ||
526 !strcmp(arg, "--staged")) {
527 add_head_to_pending(&rev);
528 if (!rev.pending.nr) {
529 struct tree *tree;
530 tree = lookup_tree(the_repository,
531 the_repository->hash_algo->empty_tree);
532 add_pending_object(&rev, &tree->object, "HEAD");
534 break;
539 symdiff_prepare(&rev, &sdiff);
540 for (i = 0; i < rev.pending.nr; i++) {
541 struct object_array_entry *entry = &rev.pending.objects[i];
542 struct object *obj = entry->item;
543 const char *name = entry->name;
544 int flags = (obj->flags & UNINTERESTING);
545 if (!obj->parsed)
546 obj = parse_object(the_repository, &obj->oid);
547 obj = deref_tag(the_repository, obj, NULL, 0);
548 if (!obj)
549 die(_("invalid object '%s' given."), name);
550 if (obj->type == OBJ_COMMIT)
551 obj = &get_commit_tree(((struct commit *)obj))->object;
553 if (obj->type == OBJ_TREE) {
554 if (sdiff.skip && bitmap_get(sdiff.skip, i))
555 continue;
556 obj->flags |= flags;
557 add_object_array(obj, name, &ent);
558 if (first_non_parent < 0 &&
559 (i >= rev.cmdline.nr || /* HEAD by hand. */
560 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
561 first_non_parent = ent.nr - 1;
562 } else if (obj->type == OBJ_BLOB) {
563 if (2 <= blobs)
564 die(_("more than two blobs given: '%s'"), name);
565 blob[blobs] = entry;
566 blobs++;
568 } else {
569 die(_("unhandled object '%s' given."), name);
572 if (rev.prune_data.nr)
573 paths += rev.prune_data.nr;
576 * Now, do the arguments look reasonable?
578 if (!ent.nr) {
579 switch (blobs) {
580 case 0:
581 result = builtin_diff_files(&rev, argc, argv);
582 break;
583 case 1:
584 if (paths != 1)
585 usage(builtin_diff_usage);
586 result = builtin_diff_b_f(&rev, argc, argv, blob);
587 break;
588 case 2:
589 if (paths)
590 usage(builtin_diff_usage);
591 result = builtin_diff_blobs(&rev, argc, argv, blob);
592 break;
593 default:
594 usage(builtin_diff_usage);
597 else if (blobs)
598 usage(builtin_diff_usage);
599 else if (ent.nr == 1)
600 result = builtin_diff_index(&rev, argc, argv);
601 else if (ent.nr == 2) {
602 if (sdiff.warn)
603 warning(_("%s...%s: multiple merge bases, using %s"),
604 sdiff.left, sdiff.right, sdiff.base);
605 result = builtin_diff_tree(&rev, argc, argv,
606 &ent.objects[0], &ent.objects[1]);
607 } else
608 result = builtin_diff_combined(&rev, argc, argv,
609 ent.objects, ent.nr,
610 first_non_parent);
611 result = diff_result_code(&rev.diffopt, result);
612 if (1 < rev.diffopt.skip_stat_unmatch)
613 refresh_index_quietly();
614 release_revisions(&rev);
615 UNLEAK(ent);
616 UNLEAK(blob);
617 return result;