]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff.c
Merge branch 'jt/fetch-pack-record-refs-in-the-dot-promisor'
[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"
697cc8ef 9#include "lockfile.h"
6b2f2d98 10#include "color.h"
65056021
JH
11#include "commit.h"
12#include "blob.h"
13#include "tag.h"
14#include "diff.h"
15#include "diffcore.h"
16#include "revision.h"
17#include "log-tree.h"
18#include "builtin.h"
302ad7a9 19#include "submodule.h"
0041f09d 20#include "sha1-array.h"
65056021 21
470faf96
TG
22#define DIFF_NO_INDEX_EXPLICIT 1
23#define DIFF_NO_INDEX_IMPLICIT 2
24
65056021 25static const char builtin_diff_usage[] =
9edb8a0f 26"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
65056021 27
158b06ca
JK
28static const char *blob_path(struct object_array_entry *entry)
29{
30 return entry->path ? entry->path : entry->name;
31}
32
65056021
JH
33static void stuff_change(struct diff_options *opt,
34 unsigned old_mode, unsigned new_mode,
9c4b0f66 35 const struct object_id *old_oid,
36 const struct object_id *new_oid,
37 int old_oid_valid,
38 int new_oid_valid,
d04ec74b
JK
39 const char *old_path,
40 const char *new_path)
65056021
JH
41{
42 struct diff_filespec *one, *two;
43
9c4b0f66 44 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
4a7e27e9 45 oideq(old_oid, new_oid) && (old_mode == new_mode))
65056021
JH
46 return;
47
0d1e0e78 48 if (opt->flags.reverse_diff) {
35d803bc 49 SWAP(old_mode, new_mode);
9c4b0f66 50 SWAP(old_oid, new_oid);
d04ec74b 51 SWAP(old_path, new_path);
65056021 52 }
cd676a51
JH
53
54 if (opt->prefix &&
d04ec74b
JK
55 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
56 strncmp(new_path, opt->prefix, opt->prefix_length)))
cd676a51
JH
57 return;
58
d04ec74b
JK
59 one = alloc_filespec(old_path);
60 two = alloc_filespec(new_path);
f9704c2d
BW
61 fill_filespec(one, old_oid, old_oid_valid, old_mode);
62 fill_filespec(two, new_oid, new_oid_valid, new_mode);
65056021 63
65056021
JH
64 diff_queue(&diff_queued_diff, one, two);
65}
66
67static int builtin_diff_b_f(struct rev_info *revs,
68 int argc, const char **argv,
42f5ba5b 69 struct object_array_entry **blob)
65056021
JH
70{
71 /* Blob vs file in the working tree*/
72 struct stat st;
887c6c18 73 const char *path;
65056021 74
a610786f
TH
75 if (argc > 1)
76 usage(builtin_diff_usage);
77
887c6c18
NTND
78 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
79 path = revs->prune_data.items[0].match;
80
65056021 81 if (lstat(path, &st))
54214529 82 die_errno(_("failed to stat '%s'"), path);
65056021 83 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
54214529 84 die(_("'%s': not a regular file or symlink"), path);
01618a3a 85
a5a818ee
JH
86 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
87
42f5ba5b
JK
88 if (blob[0]->mode == S_IFINVALID)
89 blob[0]->mode = canon_mode(st.st_mode);
01618a3a 90
65056021 91 stuff_change(&revs->diffopt,
42f5ba5b
JK
92 blob[0]->mode, canon_mode(st.st_mode),
93 &blob[0]->item->oid, &null_oid,
e5450100 94 1, 0,
30d005c0
JK
95 blob[0]->path ? blob[0]->path : path,
96 path);
65056021
JH
97 diffcore_std(&revs->diffopt);
98 diff_flush(&revs->diffopt);
99 return 0;
100}
101
102static int builtin_diff_blobs(struct rev_info *revs,
103 int argc, const char **argv,
42f5ba5b 104 struct object_array_entry **blob)
65056021 105{
33de80b1 106 const unsigned mode = canon_mode(S_IFREG | 0644);
65056021 107
a610786f
TH
108 if (argc > 1)
109 usage(builtin_diff_usage);
110
42f5ba5b
JK
111 if (blob[0]->mode == S_IFINVALID)
112 blob[0]->mode = mode;
01618a3a 113
42f5ba5b
JK
114 if (blob[1]->mode == S_IFINVALID)
115 blob[1]->mode = mode;
01618a3a 116
65056021 117 stuff_change(&revs->diffopt,
42f5ba5b
JK
118 blob[0]->mode, blob[1]->mode,
119 &blob[0]->item->oid, &blob[1]->item->oid,
e5450100 120 1, 1,
158b06ca 121 blob_path(blob[0]), blob_path(blob[1]));
65056021
JH
122 diffcore_std(&revs->diffopt);
123 diff_flush(&revs->diffopt);
124 return 0;
125}
126
127static int builtin_diff_index(struct rev_info *revs,
128 int argc, const char **argv)
129{
130 int cached = 0;
131 while (1 < argc) {
132 const char *arg = argv[1];
2baf1850 133 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
65056021 134 cached = 1;
65056021
JH
135 else
136 usage(builtin_diff_usage);
137 argv++; argc--;
138 }
139 /*
140 * Make sure there is one revision (i.e. pending object),
141 * and there is no revision filtering parameters.
142 */
1f1e895f 143 if (revs->pending.nr != 1 ||
65056021
JH
144 revs->max_count != -1 || revs->min_age != -1 ||
145 revs->max_age != -1)
146 usage(builtin_diff_usage);
7349afd2
KB
147 if (!cached) {
148 setup_work_tree();
5ab2a2da 149 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
7349afd2
KB
150 perror("read_cache_preload");
151 return -1;
152 }
153 } else if (read_cache() < 0) {
154 perror("read_cache");
b4e1e4a7
JH
155 return -1;
156 }
65056021
JH
157 return run_diff_index(revs, cached);
158}
159
160static int builtin_diff_tree(struct rev_info *revs,
161 int argc, const char **argv,
91de344d
MH
162 struct object_array_entry *ent0,
163 struct object_array_entry *ent1)
65056021 164{
9c4b0f66 165 const struct object_id *(oid[2]);
1f1e895f 166 int swap = 0;
a610786f
TH
167
168 if (argc > 1)
169 usage(builtin_diff_usage);
0fe7c1de 170
91de344d
MH
171 /*
172 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
173 * swap them.
0fe7c1de 174 */
91de344d 175 if (ent1->item->flags & UNINTERESTING)
1f1e895f 176 swap = 1;
9c4b0f66 177 oid[swap] = &ent0->item->oid;
178 oid[1 - swap] = &ent1->item->oid;
66f414f8 179 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
65056021
JH
180 log_tree_diff_flush(revs);
181 return 0;
182}
183
0fe7c1de
JH
184static int builtin_diff_combined(struct rev_info *revs,
185 int argc, const char **argv,
1f1e895f 186 struct object_array_entry *ent,
0fe7c1de
JH
187 int ents)
188{
910650d2 189 struct oid_array parents = OID_ARRAY_INIT;
0fe7c1de
JH
190 int i;
191
a610786f
TH
192 if (argc > 1)
193 usage(builtin_diff_usage);
194
0fe7c1de
JH
195 if (!revs->dense_combined_merges && !revs->combine_merges)
196 revs->dense_combined_merges = revs->combine_merges = 1;
0041f09d 197 for (i = 1; i < ents; i++)
910650d2 198 oid_array_append(&parents, &ent[i].item->oid);
b9acf54d 199 diff_tree_combined(&ent[0].item->oid, &parents,
0fe7c1de 200 revs->dense_combined_merges, revs);
910650d2 201 oid_array_clear(&parents);
0fe7c1de
JH
202 return 0;
203}
204
aecbf914
JH
205static void refresh_index_quietly(void)
206{
837e34eb 207 struct lock_file lock_file = LOCK_INIT;
aecbf914
JH
208 int fd;
209
837e34eb 210 fd = hold_locked_index(&lock_file, 0);
aecbf914
JH
211 if (fd < 0)
212 return;
213 discard_cache();
214 read_cache();
215 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
1b0d968b 216 repo_update_index_if_able(the_repository, &lock_file);
aecbf914
JH
217}
218
0569e9b8
JH
219static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
220{
0569e9b8
JH
221 unsigned int options = 0;
222
223 while (1 < argc && argv[1][0] == '-') {
224 if (!strcmp(argv[1], "--base"))
225 revs->max_count = 1;
226 else if (!strcmp(argv[1], "--ours"))
227 revs->max_count = 2;
228 else if (!strcmp(argv[1], "--theirs"))
229 revs->max_count = 3;
230 else if (!strcmp(argv[1], "-q"))
231 options |= DIFF_SILENT_ON_REMOVED;
1c370ea4
MM
232 else if (!strcmp(argv[1], "-h"))
233 usage(builtin_diff_usage);
0569e9b8 234 else
54214529 235 return error(_("invalid option: %s"), argv[1]);
0569e9b8
JH
236 argv++; argc--;
237 }
238
903e09a3
JH
239 /*
240 * "diff --base" should not combine merges because it was not
241 * asked to. "diff -c" should not densify (if the user wants
242 * dense one, --cc can be explicitly asked for, or just rely
243 * on the default).
244 */
245 if (revs->max_count == -1 && !revs->combine_merges &&
0569e9b8
JH
246 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
247 revs->combine_merges = revs->dense_combined_merges = 1;
248
4f38f6b5 249 setup_work_tree();
5ab2a2da 250 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
671c9b7e 251 perror("read_cache_preload");
0569e9b8
JH
252 return -1;
253 }
e7c3a59c 254 return run_diff_files(revs, options);
0569e9b8
JH
255}
256
a633fca0 257int cmd_diff(int argc, const char **argv, const char *prefix)
65056021 258{
1f1e895f 259 int i;
65056021 260 struct rev_info rev;
33055fa8
MH
261 struct object_array ent = OBJECT_ARRAY_INIT;
262 int blobs = 0, paths = 0;
42f5ba5b 263 struct object_array_entry *blob[2];
470faf96 264 int nongit = 0, no_index = 0;
41bbf9d5 265 int result = 0;
65056021
JH
266
267 /*
268 * We could get N tree-ish in the rev.pending_objects list.
269 * Also there could be M blobs there, and P pathspecs.
270 *
271 * N=0, M=0:
272 * cache vs files (diff-files)
273 * N=0, M=2:
274 * compare two random blobs. P must be zero.
275 * N=0, M=1, P=1:
276 * compare a blob with a working tree file.
277 *
278 * N=1, M=0:
279 * tree vs cache (diff-index --cached)
280 *
281 * N=2, M=0:
282 * tree vs tree (diff-tree)
283 *
0569e9b8
JH
284 * N=0, M=0, P=2:
285 * compare two filesystem entities (aka --no-index).
286 *
65056021
JH
287 * Other cases are errors.
288 */
230f544e 289
470faf96
TG
290 /* Were we asked to do --no-index explicitly? */
291 for (i = 1; i < argc; i++) {
292 if (!strcmp(argv[i], "--")) {
293 i++;
294 break;
295 }
296 if (!strcmp(argv[i], "--no-index"))
297 no_index = DIFF_NO_INDEX_EXPLICIT;
298 if (argv[i][0] != '-')
299 break;
300 }
301
28a4e580 302 prefix = setup_git_directory_gently(&nongit);
6df5762d 303
28a4e580 304 if (!no_index) {
475b362c
JK
305 /*
306 * Treat git diff with at least one path outside of the
307 * repo the same as if the command would have been executed
308 * outside of a git repository. In this case it behaves
309 * the same way as "git diff --no-index <a> <b>", which acts
310 * as a colourful "diff" replacement.
311 */
312 if (nongit || ((argc == i + 2) &&
313 (!path_inside_repo(prefix, argv[i]) ||
314 !path_inside_repo(prefix, argv[i + 1]))))
315 no_index = DIFF_NO_INDEX_IMPLICIT;
316 }
470faf96 317
5404c116 318 init_diff_ui_defaults();
ef90d6d4 319 git_config(git_diff_ui_config, NULL);
90a78b83 320 precompose_argv(argc, argv);
6b2f2d98 321
2abf3503 322 repo_init_revisions(the_repository, &rev, prefix);
0569e9b8 323
287ab28b 324 /* Set up defaults that will apply to both no-index and regular diffs. */
af9fedc1 325 rev.diffopt.stat_width = -1;
df44483a 326 rev.diffopt.stat_graph_width = -1;
0d1e0e78
BW
327 rev.diffopt.flags.allow_external = 1;
328 rev.diffopt.flags.allow_textconv = 1;
61af494c 329
287ab28b
JK
330 /* If this is a no-index diff, just run it and exit there. */
331 if (no_index)
dcd6a8c0
JH
332 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
333 argc, argv));
334
287ab28b
JK
335
336 /*
337 * Otherwise, we are doing the usual "git" diff; set up any
338 * further defaults that apply to regular diffs.
339 */
340 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
341
0231ae71
NTND
342 /*
343 * Default to intent-to-add entries invisible in the
344 * index. This makes them show up as new files in diff-files
345 * and not at all in diff-cached.
346 */
347 rev.diffopt.ita_invisible_in_index = 1;
348
0569e9b8 349 if (nongit)
54214529 350 die(_("Not a git repository"));
0569e9b8 351 argc = setup_revisions(argc, argv, &rev, NULL);
047fbe90 352 if (!rev.diffopt.output_format) {
c9b5ef99 353 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
28452655 354 diff_setup_done(&rev.diffopt);
047fbe90 355 }
61af494c 356
0d1e0e78 357 rev.diffopt.flags.recursive = 1;
c9b5ef99 358
1af3d977 359 setup_diff_pager(&rev.diffopt);
89d07f75 360
0569e9b8
JH
361 /*
362 * Do we have --cached and not have a pending object, then
65056021
JH
363 * default to HEAD by hand. Eek.
364 */
1f1e895f 365 if (!rev.pending.nr) {
65056021
JH
366 int i;
367 for (i = 1; i < argc; i++) {
368 const char *arg = argv[i];
369 if (!strcmp(arg, "--"))
370 break;
2baf1850
DS
371 else if (!strcmp(arg, "--cached") ||
372 !strcmp(arg, "--staged")) {
3384a2df 373 add_head_to_pending(&rev);
a2b7a3b3
NTND
374 if (!rev.pending.nr) {
375 struct tree *tree;
f86bcc7b
SB
376 tree = lookup_tree(the_repository,
377 the_repository->hash_algo->empty_tree);
a2b7a3b3
NTND
378 add_pending_object(&rev, &tree->object, "HEAD");
379 }
65056021
JH
380 break;
381 }
382 }
383 }
384
1f1e895f 385 for (i = 0; i < rev.pending.nr; i++) {
026f09e7
MH
386 struct object_array_entry *entry = &rev.pending.objects[i];
387 struct object *obj = entry->item;
388 const char *name = entry->name;
65056021
JH
389 int flags = (obj->flags & UNINTERESTING);
390 if (!obj->parsed)
109cd76d 391 obj = parse_object(the_repository, &obj->oid);
a74093da 392 obj = deref_tag(the_repository, obj, NULL, 0);
65056021 393 if (!obj)
54214529 394 die(_("invalid object '%s' given."), name);
1974632c 395 if (obj->type == OBJ_COMMIT)
2e27bd77 396 obj = &get_commit_tree(((struct commit *)obj))->object;
5b1e14ea 397
1974632c 398 if (obj->type == OBJ_TREE) {
65056021 399 obj->flags |= flags;
33055fa8 400 add_object_array(obj, name, &ent);
5b1e14ea 401 } else if (obj->type == OBJ_BLOB) {
65056021 402 if (2 <= blobs)
54214529 403 die(_("more than two blobs given: '%s'"), name);
42f5ba5b 404 blob[blobs] = entry;
65056021 405 blobs++;
230f544e 406
5b1e14ea
MH
407 } else {
408 die(_("unhandled object '%s' given."), name);
65056021 409 }
65056021 410 }
887c6c18 411 if (rev.prune_data.nr)
afe069d1 412 paths += rev.prune_data.nr;
65056021
JH
413
414 /*
415 * Now, do the arguments look reasonable?
416 */
33055fa8 417 if (!ent.nr) {
65056021
JH
418 switch (blobs) {
419 case 0:
0569e9b8 420 result = builtin_diff_files(&rev, argc, argv);
65056021
JH
421 break;
422 case 1:
423 if (paths != 1)
424 usage(builtin_diff_usage);
887c6c18 425 result = builtin_diff_b_f(&rev, argc, argv, blob);
65056021
JH
426 break;
427 case 2:
0fe7c1de
JH
428 if (paths)
429 usage(builtin_diff_usage);
41bbf9d5 430 result = builtin_diff_blobs(&rev, argc, argv, blob);
65056021
JH
431 break;
432 default:
433 usage(builtin_diff_usage);
434 }
435 }
436 else if (blobs)
437 usage(builtin_diff_usage);
33055fa8 438 else if (ent.nr == 1)
41bbf9d5 439 result = builtin_diff_index(&rev, argc, argv);
33055fa8
MH
440 else if (ent.nr == 2)
441 result = builtin_diff_tree(&rev, argc, argv,
442 &ent.objects[0], &ent.objects[1]);
443 else if (ent.objects[0].item->flags & UNINTERESTING) {
c008c0ff
JH
444 /*
445 * diff A...B where there is at least one merge base
33055fa8
MH
446 * between A and B. We have ent.objects[0] ==
447 * merge-base, ent.objects[ents-2] == A, and
448 * ent.objects[ents-1] == B. Show diff between the
449 * base and B. Note that we pick one merge base at
450 * random if there are more than one.
c008c0ff 451 */
33055fa8
MH
452 result = builtin_diff_tree(&rev, argc, argv,
453 &ent.objects[0],
454 &ent.objects[ent.nr-1]);
c008c0ff 455 } else
41bbf9d5 456 result = builtin_diff_combined(&rev, argc, argv,
33055fa8 457 ent.objects, ent.nr);
da31b358 458 result = diff_result_code(&rev.diffopt, result);
aecbf914
JH
459 if (1 < rev.diffopt.skip_stat_unmatch)
460 refresh_index_quietly();
886e1084
461 UNLEAK(rev);
462 UNLEAK(ent);
463 UNLEAK(blob);
41bbf9d5 464 return result;
65056021 465}