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