]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff.c
Merge branch 'ea/blame-use-oideq'
[thirdparty/git.git] / builtin / diff.c
CommitLineData
65056021
JH
1/*
2 * Builtin "git diff"
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
f8adbec9 6#define USE_THE_INDEX_COMPATIBILITY_MACROS
65056021 7#include "cache.h"
b2141fc1 8#include "config.h"
8bfcb3a6 9#include "ewah/ewok.h"
697cc8ef 10#include "lockfile.h"
6b2f2d98 11#include "color.h"
65056021
JH
12#include "commit.h"
13#include "blob.h"
14#include "tag.h"
15#include "diff.h"
16#include "diffcore.h"
17#include "revision.h"
18#include "log-tree.h"
19#include "builtin.h"
302ad7a9 20#include "submodule.h"
fe299ec5 21#include "oid-array.h"
65056021 22
470faf96
TG
23#define DIFF_NO_INDEX_EXPLICIT 1
24#define DIFF_NO_INDEX_IMPLICIT 2
25
65056021 26static const char builtin_diff_usage[] =
b7e10b2c
CT
27"git diff [<options>] [<commit>] [--] [<path>...]\n"
28" or: git diff [<options>] --cached [<commit>] [--] [<path>...]\n"
29" or: git diff [<options>] <commit> [<commit>...] <commit> [--] [<path>...]\n"
30" or: git diff [<options>] <commit>...<commit>] [--] [<path>...]\n"
31" or: git diff [<options>] <blob> <blob>]\n"
32" or: git diff [<options>] --no-index [--] <path> <path>]\n"
33COMMON_DIFF_OPTIONS_HELP;
65056021 34
158b06ca
JK
35static const char *blob_path(struct object_array_entry *entry)
36{
37 return entry->path ? entry->path : entry->name;
38}
39
65056021
JH
40static void stuff_change(struct diff_options *opt,
41 unsigned old_mode, unsigned new_mode,
9c4b0f66 42 const struct object_id *old_oid,
43 const struct object_id *new_oid,
44 int old_oid_valid,
45 int new_oid_valid,
d04ec74b
JK
46 const char *old_path,
47 const char *new_path)
65056021
JH
48{
49 struct diff_filespec *one, *two;
50
9c4b0f66 51 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
4a7e27e9 52 oideq(old_oid, new_oid) && (old_mode == new_mode))
65056021
JH
53 return;
54
0d1e0e78 55 if (opt->flags.reverse_diff) {
35d803bc 56 SWAP(old_mode, new_mode);
9c4b0f66 57 SWAP(old_oid, new_oid);
d04ec74b 58 SWAP(old_path, new_path);
65056021 59 }
cd676a51
JH
60
61 if (opt->prefix &&
d04ec74b
JK
62 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
63 strncmp(new_path, opt->prefix, opt->prefix_length)))
cd676a51
JH
64 return;
65
d04ec74b
JK
66 one = alloc_filespec(old_path);
67 two = alloc_filespec(new_path);
f9704c2d
BW
68 fill_filespec(one, old_oid, old_oid_valid, old_mode);
69 fill_filespec(two, new_oid, new_oid_valid, new_mode);
65056021 70
65056021
JH
71 diff_queue(&diff_queued_diff, one, two);
72}
73
74static int builtin_diff_b_f(struct rev_info *revs,
75 int argc, const char **argv,
42f5ba5b 76 struct object_array_entry **blob)
65056021
JH
77{
78 /* Blob vs file in the working tree*/
79 struct stat st;
887c6c18 80 const char *path;
65056021 81
a610786f
TH
82 if (argc > 1)
83 usage(builtin_diff_usage);
84
887c6c18
NTND
85 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
86 path = revs->prune_data.items[0].match;
87
65056021 88 if (lstat(path, &st))
54214529 89 die_errno(_("failed to stat '%s'"), path);
65056021 90 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
54214529 91 die(_("'%s': not a regular file or symlink"), path);
01618a3a 92
a5a818ee
JH
93 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
94
42f5ba5b
JK
95 if (blob[0]->mode == S_IFINVALID)
96 blob[0]->mode = canon_mode(st.st_mode);
01618a3a 97
65056021 98 stuff_change(&revs->diffopt,
42f5ba5b
JK
99 blob[0]->mode, canon_mode(st.st_mode),
100 &blob[0]->item->oid, &null_oid,
e5450100 101 1, 0,
30d005c0
JK
102 blob[0]->path ? blob[0]->path : path,
103 path);
65056021
JH
104 diffcore_std(&revs->diffopt);
105 diff_flush(&revs->diffopt);
106 return 0;
107}
108
109static int builtin_diff_blobs(struct rev_info *revs,
110 int argc, const char **argv,
42f5ba5b 111 struct object_array_entry **blob)
65056021 112{
33de80b1 113 const unsigned mode = canon_mode(S_IFREG | 0644);
65056021 114
a610786f
TH
115 if (argc > 1)
116 usage(builtin_diff_usage);
117
42f5ba5b
JK
118 if (blob[0]->mode == S_IFINVALID)
119 blob[0]->mode = mode;
01618a3a 120
42f5ba5b
JK
121 if (blob[1]->mode == S_IFINVALID)
122 blob[1]->mode = mode;
01618a3a 123
65056021 124 stuff_change(&revs->diffopt,
42f5ba5b
JK
125 blob[0]->mode, blob[1]->mode,
126 &blob[0]->item->oid, &blob[1]->item->oid,
e5450100 127 1, 1,
158b06ca 128 blob_path(blob[0]), blob_path(blob[1]));
65056021
JH
129 diffcore_std(&revs->diffopt);
130 diff_flush(&revs->diffopt);
131 return 0;
132}
133
134static int builtin_diff_index(struct rev_info *revs,
135 int argc, const char **argv)
136{
137 int cached = 0;
138 while (1 < argc) {
139 const char *arg = argv[1];
2baf1850 140 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
65056021 141 cached = 1;
65056021
JH
142 else
143 usage(builtin_diff_usage);
144 argv++; argc--;
145 }
146 /*
147 * Make sure there is one revision (i.e. pending object),
148 * and there is no revision filtering parameters.
149 */
1f1e895f 150 if (revs->pending.nr != 1 ||
65056021
JH
151 revs->max_count != -1 || revs->min_age != -1 ||
152 revs->max_age != -1)
153 usage(builtin_diff_usage);
7349afd2
KB
154 if (!cached) {
155 setup_work_tree();
5ab2a2da 156 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
7349afd2
KB
157 perror("read_cache_preload");
158 return -1;
159 }
160 } else if (read_cache() < 0) {
161 perror("read_cache");
b4e1e4a7
JH
162 return -1;
163 }
65056021
JH
164 return run_diff_index(revs, cached);
165}
166
167static int builtin_diff_tree(struct rev_info *revs,
168 int argc, const char **argv,
91de344d
MH
169 struct object_array_entry *ent0,
170 struct object_array_entry *ent1)
65056021 171{
9c4b0f66 172 const struct object_id *(oid[2]);
1f1e895f 173 int swap = 0;
a610786f
TH
174
175 if (argc > 1)
176 usage(builtin_diff_usage);
0fe7c1de 177
91de344d
MH
178 /*
179 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
180 * swap them.
0fe7c1de 181 */
91de344d 182 if (ent1->item->flags & UNINTERESTING)
1f1e895f 183 swap = 1;
9c4b0f66 184 oid[swap] = &ent0->item->oid;
185 oid[1 - swap] = &ent1->item->oid;
66f414f8 186 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
65056021
JH
187 log_tree_diff_flush(revs);
188 return 0;
189}
190
0fe7c1de
JH
191static int builtin_diff_combined(struct rev_info *revs,
192 int argc, const char **argv,
1f1e895f 193 struct object_array_entry *ent,
0fe7c1de
JH
194 int ents)
195{
910650d2 196 struct oid_array parents = OID_ARRAY_INIT;
0fe7c1de
JH
197 int i;
198
a610786f
TH
199 if (argc > 1)
200 usage(builtin_diff_usage);
201
0fe7c1de
JH
202 if (!revs->dense_combined_merges && !revs->combine_merges)
203 revs->dense_combined_merges = revs->combine_merges = 1;
0041f09d 204 for (i = 1; i < ents; i++)
910650d2 205 oid_array_append(&parents, &ent[i].item->oid);
b9acf54d 206 diff_tree_combined(&ent[0].item->oid, &parents,
0fe7c1de 207 revs->dense_combined_merges, revs);
910650d2 208 oid_array_clear(&parents);
0fe7c1de
JH
209 return 0;
210}
211
aecbf914
JH
212static void refresh_index_quietly(void)
213{
837e34eb 214 struct lock_file lock_file = LOCK_INIT;
aecbf914
JH
215 int fd;
216
837e34eb 217 fd = hold_locked_index(&lock_file, 0);
aecbf914
JH
218 if (fd < 0)
219 return;
220 discard_cache();
221 read_cache();
222 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
1b0d968b 223 repo_update_index_if_able(the_repository, &lock_file);
aecbf914
JH
224}
225
0569e9b8
JH
226static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
227{
0569e9b8
JH
228 unsigned int options = 0;
229
230 while (1 < argc && argv[1][0] == '-') {
231 if (!strcmp(argv[1], "--base"))
232 revs->max_count = 1;
233 else if (!strcmp(argv[1], "--ours"))
234 revs->max_count = 2;
235 else if (!strcmp(argv[1], "--theirs"))
236 revs->max_count = 3;
237 else if (!strcmp(argv[1], "-q"))
238 options |= DIFF_SILENT_ON_REMOVED;
1c370ea4
MM
239 else if (!strcmp(argv[1], "-h"))
240 usage(builtin_diff_usage);
0569e9b8 241 else
54214529 242 return error(_("invalid option: %s"), argv[1]);
0569e9b8
JH
243 argv++; argc--;
244 }
245
903e09a3
JH
246 /*
247 * "diff --base" should not combine merges because it was not
248 * asked to. "diff -c" should not densify (if the user wants
249 * dense one, --cc can be explicitly asked for, or just rely
250 * on the default).
251 */
252 if (revs->max_count == -1 && !revs->combine_merges &&
0569e9b8
JH
253 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
254 revs->combine_merges = revs->dense_combined_merges = 1;
255
4f38f6b5 256 setup_work_tree();
5ab2a2da 257 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
671c9b7e 258 perror("read_cache_preload");
0569e9b8
JH
259 return -1;
260 }
e7c3a59c 261 return run_diff_files(revs, options);
0569e9b8
JH
262}
263
8bfcb3a6
CT
264struct symdiff {
265 struct bitmap *skip;
266 int warn;
267 const char *base, *left, *right;
268};
269
270/*
271 * Check for symmetric-difference arguments, and if present, arrange
272 * everything we need to know to handle them correctly. As a bonus,
273 * weed out all bogus range-based revision specifications, e.g.,
274 * "git diff A..B C..D" or "git diff A..B C" get rejected.
275 *
276 * For an actual symmetric diff, *symdiff is set this way:
277 *
278 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
279 * indices that the caller should ignore (extra merge bases, of
280 * which there might be many, and A in A...B). Note that the
281 * chosen merge base and right side are NOT marked.
282 * - warn is set if there are multiple merge bases.
283 * - base, left, and right point to the names to use in a
284 * warning about multiple merge bases.
285 *
286 * If there is no symmetric diff argument, sym->skip is NULL and
287 * sym->warn is cleared. The remaining fields are not set.
288 */
289static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
290{
291 int i, is_symdiff = 0, basecount = 0, othercount = 0;
292 int lpos = -1, rpos = -1, basepos = -1;
293 struct bitmap *map = NULL;
294
295 /*
296 * Use the whence fields to find merge bases and left and
297 * right parts of symmetric difference, so that we do not
298 * depend on the order that revisions are parsed. If there
299 * are any revs that aren't from these sources, we have a
300 * "git diff C A...B" or "git diff A...B C" case. Or we
301 * could even get "git diff A...B C...E", for instance.
302 *
303 * If we don't have just one merge base, we pick one
304 * at random.
305 *
306 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
307 * so we must check for SYMMETRIC_LEFT too. The two arrays
308 * rev->pending.objects and rev->cmdline.rev are parallel.
309 */
310 for (i = 0; i < rev->cmdline.nr; i++) {
311 struct object *obj = rev->pending.objects[i].item;
312 switch (rev->cmdline.rev[i].whence) {
313 case REV_CMD_MERGE_BASE:
314 if (basepos < 0)
315 basepos = i;
316 basecount++;
317 break; /* do mark all bases */
318 case REV_CMD_LEFT:
319 if (lpos >= 0)
320 usage(builtin_diff_usage);
321 lpos = i;
322 if (obj->flags & SYMMETRIC_LEFT) {
323 is_symdiff = 1;
324 break; /* do mark A */
325 }
326 continue;
327 case REV_CMD_RIGHT:
328 if (rpos >= 0)
329 usage(builtin_diff_usage);
330 rpos = i;
331 continue; /* don't mark B */
332 case REV_CMD_PARENTS_ONLY:
333 case REV_CMD_REF:
334 case REV_CMD_REV:
335 othercount++;
336 continue;
337 }
338 if (map == NULL)
339 map = bitmap_new();
340 bitmap_set(map, i);
341 }
342
343 /*
344 * Forbid any additional revs for both A...B and A..B.
345 */
346 if (lpos >= 0 && othercount > 0)
347 usage(builtin_diff_usage);
348
349 if (!is_symdiff) {
350 bitmap_free(map);
351 sym->warn = 0;
352 sym->skip = NULL;
353 return;
354 }
355
356 sym->left = rev->pending.objects[lpos].name;
357 sym->right = rev->pending.objects[rpos].name;
8bfcb3a6
CT
358 if (basecount == 0)
359 die(_("%s...%s: no merge base"), sym->left, sym->right);
5f46e610 360 sym->base = rev->pending.objects[basepos].name;
8bfcb3a6
CT
361 bitmap_unset(map, basepos); /* unmark the base we want */
362 sym->warn = basecount > 1;
363 sym->skip = map;
364}
365
a633fca0 366int cmd_diff(int argc, const char **argv, const char *prefix)
65056021 367{
1f1e895f 368 int i;
65056021 369 struct rev_info rev;
33055fa8
MH
370 struct object_array ent = OBJECT_ARRAY_INIT;
371 int blobs = 0, paths = 0;
42f5ba5b 372 struct object_array_entry *blob[2];
470faf96 373 int nongit = 0, no_index = 0;
41bbf9d5 374 int result = 0;
8bfcb3a6 375 struct symdiff sdiff;
65056021
JH
376
377 /*
378 * We could get N tree-ish in the rev.pending_objects list.
a9d7689c
DL
379 * Also there could be M blobs there, and P pathspecs. --cached may
380 * also be present.
65056021
JH
381 *
382 * N=0, M=0:
a9d7689c
DL
383 * cache vs files (diff-files)
384 *
385 * N=0, M=0, --cached:
386 * HEAD vs cache (diff-index --cached)
387 *
65056021
JH
388 * N=0, M=2:
389 * compare two random blobs. P must be zero.
a9d7689c 390 *
65056021 391 * N=0, M=1, P=1:
a9d7689c 392 * compare a blob with a working tree file.
65056021
JH
393 *
394 * N=1, M=0:
a9d7689c 395 * tree vs files (diff-index)
65056021 396 *
a9d7689c 397 * N=1, M=0, --cached:
65056021
JH
398 * tree vs cache (diff-index --cached)
399 *
400 * N=2, M=0:
401 * tree vs tree (diff-tree)
402 *
0569e9b8
JH
403 * N=0, M=0, P=2:
404 * compare two filesystem entities (aka --no-index).
405 *
65056021
JH
406 * Other cases are errors.
407 */
230f544e 408
470faf96
TG
409 /* Were we asked to do --no-index explicitly? */
410 for (i = 1; i < argc; i++) {
411 if (!strcmp(argv[i], "--")) {
412 i++;
413 break;
414 }
415 if (!strcmp(argv[i], "--no-index"))
416 no_index = DIFF_NO_INDEX_EXPLICIT;
417 if (argv[i][0] != '-')
418 break;
419 }
420
28a4e580 421 prefix = setup_git_directory_gently(&nongit);
6df5762d 422
28a4e580 423 if (!no_index) {
475b362c
JK
424 /*
425 * Treat git diff with at least one path outside of the
426 * repo the same as if the command would have been executed
427 * outside of a git repository. In this case it behaves
428 * the same way as "git diff --no-index <a> <b>", which acts
429 * as a colourful "diff" replacement.
430 */
431 if (nongit || ((argc == i + 2) &&
432 (!path_inside_repo(prefix, argv[i]) ||
433 !path_inside_repo(prefix, argv[i + 1]))))
434 no_index = DIFF_NO_INDEX_IMPLICIT;
435 }
470faf96 436
5404c116 437 init_diff_ui_defaults();
ef90d6d4 438 git_config(git_diff_ui_config, NULL);
90a78b83 439 precompose_argv(argc, argv);
6b2f2d98 440
2abf3503 441 repo_init_revisions(the_repository, &rev, prefix);
0569e9b8 442
287ab28b 443 /* Set up defaults that will apply to both no-index and regular diffs. */
af9fedc1 444 rev.diffopt.stat_width = -1;
df44483a 445 rev.diffopt.stat_graph_width = -1;
0d1e0e78
BW
446 rev.diffopt.flags.allow_external = 1;
447 rev.diffopt.flags.allow_textconv = 1;
61af494c 448
287ab28b
JK
449 /* If this is a no-index diff, just run it and exit there. */
450 if (no_index)
dcd6a8c0
JH
451 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
452 argc, argv));
453
287ab28b
JK
454
455 /*
456 * Otherwise, we are doing the usual "git" diff; set up any
457 * further defaults that apply to regular diffs.
458 */
459 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
460
0231ae71
NTND
461 /*
462 * Default to intent-to-add entries invisible in the
463 * index. This makes them show up as new files in diff-files
464 * and not at all in diff-cached.
465 */
466 rev.diffopt.ita_invisible_in_index = 1;
467
0569e9b8 468 if (nongit)
54214529 469 die(_("Not a git repository"));
0569e9b8 470 argc = setup_revisions(argc, argv, &rev, NULL);
047fbe90 471 if (!rev.diffopt.output_format) {
c9b5ef99 472 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
28452655 473 diff_setup_done(&rev.diffopt);
047fbe90 474 }
61af494c 475
0d1e0e78 476 rev.diffopt.flags.recursive = 1;
c9b5ef99 477
1af3d977 478 setup_diff_pager(&rev.diffopt);
89d07f75 479
0569e9b8
JH
480 /*
481 * Do we have --cached and not have a pending object, then
65056021
JH
482 * default to HEAD by hand. Eek.
483 */
1f1e895f 484 if (!rev.pending.nr) {
65056021
JH
485 int i;
486 for (i = 1; i < argc; i++) {
487 const char *arg = argv[i];
488 if (!strcmp(arg, "--"))
489 break;
2baf1850
DS
490 else if (!strcmp(arg, "--cached") ||
491 !strcmp(arg, "--staged")) {
3384a2df 492 add_head_to_pending(&rev);
a2b7a3b3
NTND
493 if (!rev.pending.nr) {
494 struct tree *tree;
f86bcc7b
SB
495 tree = lookup_tree(the_repository,
496 the_repository->hash_algo->empty_tree);
a2b7a3b3
NTND
497 add_pending_object(&rev, &tree->object, "HEAD");
498 }
65056021
JH
499 break;
500 }
501 }
502 }
503
8bfcb3a6 504 symdiff_prepare(&rev, &sdiff);
1f1e895f 505 for (i = 0; i < rev.pending.nr; i++) {
026f09e7
MH
506 struct object_array_entry *entry = &rev.pending.objects[i];
507 struct object *obj = entry->item;
508 const char *name = entry->name;
65056021
JH
509 int flags = (obj->flags & UNINTERESTING);
510 if (!obj->parsed)
109cd76d 511 obj = parse_object(the_repository, &obj->oid);
a74093da 512 obj = deref_tag(the_repository, obj, NULL, 0);
65056021 513 if (!obj)
54214529 514 die(_("invalid object '%s' given."), name);
1974632c 515 if (obj->type == OBJ_COMMIT)
2e27bd77 516 obj = &get_commit_tree(((struct commit *)obj))->object;
5b1e14ea 517
1974632c 518 if (obj->type == OBJ_TREE) {
8bfcb3a6
CT
519 if (sdiff.skip && bitmap_get(sdiff.skip, i))
520 continue;
65056021 521 obj->flags |= flags;
33055fa8 522 add_object_array(obj, name, &ent);
5b1e14ea 523 } else if (obj->type == OBJ_BLOB) {
65056021 524 if (2 <= blobs)
54214529 525 die(_("more than two blobs given: '%s'"), name);
42f5ba5b 526 blob[blobs] = entry;
65056021 527 blobs++;
230f544e 528
5b1e14ea
MH
529 } else {
530 die(_("unhandled object '%s' given."), name);
65056021 531 }
65056021 532 }
887c6c18 533 if (rev.prune_data.nr)
afe069d1 534 paths += rev.prune_data.nr;
65056021
JH
535
536 /*
537 * Now, do the arguments look reasonable?
538 */
33055fa8 539 if (!ent.nr) {
65056021
JH
540 switch (blobs) {
541 case 0:
0569e9b8 542 result = builtin_diff_files(&rev, argc, argv);
65056021
JH
543 break;
544 case 1:
545 if (paths != 1)
546 usage(builtin_diff_usage);
887c6c18 547 result = builtin_diff_b_f(&rev, argc, argv, blob);
65056021
JH
548 break;
549 case 2:
0fe7c1de
JH
550 if (paths)
551 usage(builtin_diff_usage);
41bbf9d5 552 result = builtin_diff_blobs(&rev, argc, argv, blob);
65056021
JH
553 break;
554 default:
555 usage(builtin_diff_usage);
556 }
557 }
558 else if (blobs)
559 usage(builtin_diff_usage);
33055fa8 560 else if (ent.nr == 1)
41bbf9d5 561 result = builtin_diff_index(&rev, argc, argv);
8bfcb3a6
CT
562 else if (ent.nr == 2) {
563 if (sdiff.warn)
564 warning(_("%s...%s: multiple merge bases, using %s"),
565 sdiff.left, sdiff.right, sdiff.base);
33055fa8
MH
566 result = builtin_diff_tree(&rev, argc, argv,
567 &ent.objects[0], &ent.objects[1]);
c008c0ff 568 } else
41bbf9d5 569 result = builtin_diff_combined(&rev, argc, argv,
33055fa8 570 ent.objects, ent.nr);
da31b358 571 result = diff_result_code(&rev.diffopt, result);
aecbf914
JH
572 if (1 < rev.diffopt.skip_stat_unmatch)
573 refresh_index_quietly();
886e1084
574 UNLEAK(rev);
575 UNLEAK(ent);
576 UNLEAK(blob);
41bbf9d5 577 return result;
65056021 578}