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