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