banned.h: mark `strtok()` and `strtok_r()` as banned
[git/debian.git] / builtin / diff.c
blob5a6a5d7f4b4a8a7b53f8138fd398664972180aab
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"
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 int 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);
110 return 0;
113 static int builtin_diff_blobs(struct rev_info *revs,
114 int argc, const char **argv UNUSED,
115 struct object_array_entry **blob)
117 const unsigned mode = canon_mode(S_IFREG | 0644);
119 if (argc > 1)
120 usage(builtin_diff_usage);
122 if (blob[0]->mode == S_IFINVALID)
123 blob[0]->mode = mode;
125 if (blob[1]->mode == S_IFINVALID)
126 blob[1]->mode = mode;
128 stuff_change(&revs->diffopt,
129 blob[0]->mode, blob[1]->mode,
130 &blob[0]->item->oid, &blob[1]->item->oid,
131 1, 1,
132 blob_path(blob[0]), blob_path(blob[1]));
133 diffcore_std(&revs->diffopt);
134 diff_flush(&revs->diffopt);
135 return 0;
138 static int builtin_diff_index(struct rev_info *revs,
139 int argc, const char **argv)
141 unsigned int option = 0;
142 while (1 < argc) {
143 const char *arg = argv[1];
144 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
145 option |= DIFF_INDEX_CACHED;
146 else if (!strcmp(arg, "--merge-base"))
147 option |= DIFF_INDEX_MERGE_BASE;
148 else
149 usage(builtin_diff_usage);
150 argv++; argc--;
153 * Make sure there is one revision (i.e. pending object),
154 * and there is no revision filtering parameters.
156 if (revs->pending.nr != 1 ||
157 revs->max_count != -1 || revs->min_age != -1 ||
158 revs->max_age != -1)
159 usage(builtin_diff_usage);
160 if (!(option & DIFF_INDEX_CACHED)) {
161 setup_work_tree();
162 if (repo_read_index_preload(the_repository,
163 &revs->diffopt.pathspec, 0) < 0) {
164 perror("repo_read_index_preload");
165 return -1;
167 } else if (repo_read_index(the_repository) < 0) {
168 perror("repo_read_cache");
169 return -1;
171 return run_diff_index(revs, option);
174 static int builtin_diff_tree(struct rev_info *revs,
175 int argc, const char **argv,
176 struct object_array_entry *ent0,
177 struct object_array_entry *ent1)
179 const struct object_id *(oid[2]);
180 struct object_id mb_oid;
181 int merge_base = 0;
183 while (1 < argc) {
184 const char *arg = argv[1];
185 if (!strcmp(arg, "--merge-base"))
186 merge_base = 1;
187 else
188 usage(builtin_diff_usage);
189 argv++; argc--;
192 if (merge_base) {
193 diff_get_merge_base(revs, &mb_oid);
194 oid[0] = &mb_oid;
195 oid[1] = &revs->pending.objects[1].item->oid;
196 } else {
197 int swap = 0;
200 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
201 * swap them.
203 if (ent1->item->flags & UNINTERESTING)
204 swap = 1;
205 oid[swap] = &ent0->item->oid;
206 oid[1 - swap] = &ent1->item->oid;
208 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
209 log_tree_diff_flush(revs);
210 return 0;
213 static int builtin_diff_combined(struct rev_info *revs,
214 int argc, const char **argv UNUSED,
215 struct object_array_entry *ent,
216 int ents, int first_non_parent)
218 struct oid_array parents = OID_ARRAY_INIT;
219 int i;
221 if (argc > 1)
222 usage(builtin_diff_usage);
224 if (first_non_parent < 0)
225 die(_("no merge given, only parents."));
226 if (first_non_parent >= ents)
227 BUG("first_non_parent out of range: %d", first_non_parent);
229 diff_merges_set_dense_combined_if_unset(revs);
231 for (i = 0; i < ents; i++) {
232 if (i != first_non_parent)
233 oid_array_append(&parents, &ent[i].item->oid);
235 diff_tree_combined(&ent[first_non_parent].item->oid, &parents, revs);
236 oid_array_clear(&parents);
237 return 0;
240 static void refresh_index_quietly(void)
242 struct lock_file lock_file = LOCK_INIT;
243 int fd;
245 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
246 if (fd < 0)
247 return;
248 discard_index(&the_index);
249 repo_read_index(the_repository);
250 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
251 NULL);
252 repo_update_index_if_able(the_repository, &lock_file);
255 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
257 unsigned int options = 0;
259 while (1 < argc && argv[1][0] == '-') {
260 if (!strcmp(argv[1], "--base"))
261 revs->max_count = 1;
262 else if (!strcmp(argv[1], "--ours"))
263 revs->max_count = 2;
264 else if (!strcmp(argv[1], "--theirs"))
265 revs->max_count = 3;
266 else if (!strcmp(argv[1], "-q"))
267 options |= DIFF_SILENT_ON_REMOVED;
268 else if (!strcmp(argv[1], "-h"))
269 usage(builtin_diff_usage);
270 else
271 return error(_("invalid option: %s"), argv[1]);
272 argv++; argc--;
276 * "diff --base" should not combine merges because it was not
277 * asked to. "diff -c" should not densify (if the user wants
278 * dense one, --cc can be explicitly asked for, or just rely
279 * on the default).
281 if (revs->max_count == -1 &&
282 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
283 diff_merges_set_dense_combined_if_unset(revs);
285 setup_work_tree();
286 if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
287 0) < 0) {
288 perror("repo_read_index_preload");
289 return -1;
291 return run_diff_files(revs, options);
294 struct symdiff {
295 struct bitmap *skip;
296 int warn;
297 const char *base, *left, *right;
301 * Check for symmetric-difference arguments, and if present, arrange
302 * everything we need to know to handle them correctly. As a bonus,
303 * weed out all bogus range-based revision specifications, e.g.,
304 * "git diff A..B C..D" or "git diff A..B C" get rejected.
306 * For an actual symmetric diff, *symdiff is set this way:
308 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
309 * indices that the caller should ignore (extra merge bases, of
310 * which there might be many, and A in A...B). Note that the
311 * chosen merge base and right side are NOT marked.
312 * - warn is set if there are multiple merge bases.
313 * - base, left, and right point to the names to use in a
314 * warning about multiple merge bases.
316 * If there is no symmetric diff argument, sym->skip is NULL and
317 * sym->warn is cleared. The remaining fields are not set.
319 static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
321 int i, is_symdiff = 0, basecount = 0, othercount = 0;
322 int lpos = -1, rpos = -1, basepos = -1;
323 struct bitmap *map = NULL;
326 * Use the whence fields to find merge bases and left and
327 * right parts of symmetric difference, so that we do not
328 * depend on the order that revisions are parsed. If there
329 * are any revs that aren't from these sources, we have a
330 * "git diff C A...B" or "git diff A...B C" case. Or we
331 * could even get "git diff A...B C...E", for instance.
333 * If we don't have just one merge base, we pick one
334 * at random.
336 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
337 * so we must check for SYMMETRIC_LEFT too. The two arrays
338 * rev->pending.objects and rev->cmdline.rev are parallel.
340 for (i = 0; i < rev->cmdline.nr; i++) {
341 struct object *obj = rev->pending.objects[i].item;
342 switch (rev->cmdline.rev[i].whence) {
343 case REV_CMD_MERGE_BASE:
344 if (basepos < 0)
345 basepos = i;
346 basecount++;
347 break; /* do mark all bases */
348 case REV_CMD_LEFT:
349 if (lpos >= 0)
350 usage(builtin_diff_usage);
351 lpos = i;
352 if (obj->flags & SYMMETRIC_LEFT) {
353 is_symdiff = 1;
354 break; /* do mark A */
356 continue;
357 case REV_CMD_RIGHT:
358 if (rpos >= 0)
359 usage(builtin_diff_usage);
360 rpos = i;
361 continue; /* don't mark B */
362 case REV_CMD_PARENTS_ONLY:
363 case REV_CMD_REF:
364 case REV_CMD_REV:
365 othercount++;
366 continue;
368 if (!map)
369 map = bitmap_new();
370 bitmap_set(map, i);
374 * Forbid any additional revs for both A...B and A..B.
376 if (lpos >= 0 && othercount > 0)
377 usage(builtin_diff_usage);
379 if (!is_symdiff) {
380 bitmap_free(map);
381 sym->warn = 0;
382 sym->skip = NULL;
383 return;
386 sym->left = rev->pending.objects[lpos].name;
387 sym->right = rev->pending.objects[rpos].name;
388 if (basecount == 0)
389 die(_("%s...%s: no merge base"), sym->left, sym->right);
390 sym->base = rev->pending.objects[basepos].name;
391 bitmap_unset(map, basepos); /* unmark the base we want */
392 sym->warn = basecount > 1;
393 sym->skip = map;
396 int cmd_diff(int argc, const char **argv, const char *prefix)
398 int i;
399 struct rev_info rev;
400 struct object_array ent = OBJECT_ARRAY_INIT;
401 int first_non_parent = -1;
402 int blobs = 0, paths = 0;
403 struct object_array_entry *blob[2];
404 int nongit = 0, no_index = 0;
405 int result = 0;
406 struct symdiff sdiff;
409 * We could get N tree-ish in the rev.pending_objects list.
410 * Also there could be M blobs there, and P pathspecs. --cached may
411 * also be present.
413 * N=0, M=0:
414 * cache vs files (diff-files)
416 * N=0, M=0, --cached:
417 * HEAD vs cache (diff-index --cached)
419 * N=0, M=2:
420 * compare two random blobs. P must be zero.
422 * N=0, M=1, P=1:
423 * compare a blob with a working tree file.
425 * N=1, M=0:
426 * tree vs files (diff-index)
428 * N=1, M=0, --cached:
429 * tree vs cache (diff-index --cached)
431 * N=2, M=0:
432 * tree vs tree (diff-tree)
434 * N=0, M=0, P=2:
435 * compare two filesystem entities (aka --no-index).
437 * Other cases are errors.
440 /* Were we asked to do --no-index explicitly? */
441 for (i = 1; i < argc; i++) {
442 if (!strcmp(argv[i], "--")) {
443 i++;
444 break;
446 if (!strcmp(argv[i], "--no-index"))
447 no_index = DIFF_NO_INDEX_EXPLICIT;
448 if (argv[i][0] != '-')
449 break;
452 prefix = setup_git_directory_gently(&nongit);
454 if (!nongit) {
455 prepare_repo_settings(the_repository);
456 the_repository->settings.command_requires_full_index = 0;
459 if (!no_index) {
461 * Treat git diff with at least one path outside of the
462 * repo the same as if the command would have been executed
463 * outside of a git repository. In this case it behaves
464 * the same way as "git diff --no-index <a> <b>", which acts
465 * as a colourful "diff" replacement.
467 if (nongit || ((argc == i + 2) &&
468 (!path_inside_repo(prefix, argv[i]) ||
469 !path_inside_repo(prefix, argv[i + 1]))))
470 no_index = DIFF_NO_INDEX_IMPLICIT;
473 init_diff_ui_defaults();
474 git_config(git_diff_ui_config, NULL);
475 prefix = precompose_argv_prefix(argc, argv, prefix);
477 repo_init_revisions(the_repository, &rev, prefix);
479 /* Set up defaults that will apply to both no-index and regular diffs. */
480 rev.diffopt.stat_width = -1;
481 rev.diffopt.stat_graph_width = -1;
482 rev.diffopt.flags.allow_external = 1;
483 rev.diffopt.flags.allow_textconv = 1;
485 /* If this is a no-index diff, just run it and exit there. */
486 if (no_index)
487 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
488 argc, argv));
492 * Otherwise, we are doing the usual "git" diff; set up any
493 * further defaults that apply to regular diffs.
495 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
498 * Default to intent-to-add entries invisible in the
499 * index. This makes them show up as new files in diff-files
500 * and not at all in diff-cached.
502 rev.diffopt.ita_invisible_in_index = 1;
504 if (nongit)
505 die(_("Not a git repository"));
506 argc = setup_revisions(argc, argv, &rev, NULL);
507 if (!rev.diffopt.output_format) {
508 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
509 diff_setup_done(&rev.diffopt);
512 rev.diffopt.flags.recursive = 1;
513 rev.diffopt.rotate_to_strict = 1;
515 setup_diff_pager(&rev.diffopt);
518 * Do we have --cached and not have a pending object, then
519 * default to HEAD by hand. Eek.
521 if (!rev.pending.nr) {
522 int i;
523 for (i = 1; i < argc; i++) {
524 const char *arg = argv[i];
525 if (!strcmp(arg, "--"))
526 break;
527 else if (!strcmp(arg, "--cached") ||
528 !strcmp(arg, "--staged")) {
529 add_head_to_pending(&rev);
530 if (!rev.pending.nr) {
531 struct tree *tree;
532 tree = lookup_tree(the_repository,
533 the_repository->hash_algo->empty_tree);
534 add_pending_object(&rev, &tree->object, "HEAD");
536 break;
541 symdiff_prepare(&rev, &sdiff);
542 for (i = 0; i < rev.pending.nr; i++) {
543 struct object_array_entry *entry = &rev.pending.objects[i];
544 struct object *obj = entry->item;
545 const char *name = entry->name;
546 int flags = (obj->flags & UNINTERESTING);
547 if (!obj->parsed)
548 obj = parse_object(the_repository, &obj->oid);
549 obj = deref_tag(the_repository, obj, NULL, 0);
550 if (!obj)
551 die(_("invalid object '%s' given."), name);
552 if (obj->type == OBJ_COMMIT)
553 obj = &repo_get_commit_tree(the_repository,
554 ((struct commit *)obj))->object;
556 if (obj->type == OBJ_TREE) {
557 if (sdiff.skip && bitmap_get(sdiff.skip, i))
558 continue;
559 obj->flags |= flags;
560 add_object_array(obj, name, &ent);
561 if (first_non_parent < 0 &&
562 (i >= rev.cmdline.nr || /* HEAD by hand. */
563 rev.cmdline.rev[i].whence != REV_CMD_PARENTS_ONLY))
564 first_non_parent = ent.nr - 1;
565 } else if (obj->type == OBJ_BLOB) {
566 if (2 <= blobs)
567 die(_("more than two blobs given: '%s'"), name);
568 blob[blobs] = entry;
569 blobs++;
571 } else {
572 die(_("unhandled object '%s' given."), name);
575 if (rev.prune_data.nr)
576 paths += rev.prune_data.nr;
579 * Now, do the arguments look reasonable?
581 if (!ent.nr) {
582 switch (blobs) {
583 case 0:
584 result = builtin_diff_files(&rev, argc, argv);
585 break;
586 case 1:
587 if (paths != 1)
588 usage(builtin_diff_usage);
589 result = builtin_diff_b_f(&rev, argc, argv, blob);
590 break;
591 case 2:
592 if (paths)
593 usage(builtin_diff_usage);
594 result = builtin_diff_blobs(&rev, argc, argv, blob);
595 break;
596 default:
597 usage(builtin_diff_usage);
600 else if (blobs)
601 usage(builtin_diff_usage);
602 else if (ent.nr == 1)
603 result = builtin_diff_index(&rev, argc, argv);
604 else if (ent.nr == 2) {
605 if (sdiff.warn)
606 warning(_("%s...%s: multiple merge bases, using %s"),
607 sdiff.left, sdiff.right, sdiff.base);
608 result = builtin_diff_tree(&rev, argc, argv,
609 &ent.objects[0], &ent.objects[1]);
610 } else
611 result = builtin_diff_combined(&rev, argc, argv,
612 ent.objects, ent.nr,
613 first_non_parent);
614 result = diff_result_code(&rev.diffopt, result);
615 if (1 < rev.diffopt.skip_stat_unmatch)
616 refresh_index_quietly();
617 release_revisions(&rev);
618 object_array_clear(&ent);
619 UNLEAK(blob);
620 return result;