]>
Commit | Line | Data |
---|---|---|
65056021 JH |
1 | /* |
2 | * Builtin "git diff" | |
3 | * | |
4 | * Copyright (c) 2006 Junio C Hamano | |
5 | */ | |
6 | #include "cache.h" | |
6b2f2d98 | 7 | #include "color.h" |
65056021 JH |
8 | #include "commit.h" |
9 | #include "blob.h" | |
10 | #include "tag.h" | |
11 | #include "diff.h" | |
12 | #include "diffcore.h" | |
13 | #include "revision.h" | |
14 | #include "log-tree.h" | |
15 | #include "builtin.h" | |
302ad7a9 | 16 | #include "submodule.h" |
0041f09d | 17 | #include "sha1-array.h" |
65056021 | 18 | |
470faf96 TG |
19 | #define DIFF_NO_INDEX_EXPLICIT 1 |
20 | #define DIFF_NO_INDEX_IMPLICIT 2 | |
21 | ||
65056021 JH |
22 | struct blobinfo { |
23 | unsigned char sha1[20]; | |
24 | const char *name; | |
01618a3a | 25 | unsigned mode; |
65056021 JH |
26 | }; |
27 | ||
28 | static const char builtin_diff_usage[] = | |
9edb8a0f | 29 | "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]"; |
65056021 | 30 | |
65056021 JH |
31 | static void stuff_change(struct diff_options *opt, |
32 | unsigned old_mode, unsigned new_mode, | |
33 | const unsigned char *old_sha1, | |
34 | const unsigned char *new_sha1, | |
e5450100 JK |
35 | int old_sha1_valid, |
36 | int new_sha1_valid, | |
65056021 JH |
37 | const char *old_name, |
38 | const char *new_name) | |
39 | { | |
40 | struct diff_filespec *one, *two; | |
41 | ||
0bef57ee | 42 | if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) && |
43342941 | 43 | !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode)) |
65056021 JH |
44 | return; |
45 | ||
8f67f8ae | 46 | if (DIFF_OPT_TST(opt, REVERSE_DIFF)) { |
65056021 | 47 | unsigned tmp; |
d92f1dc6 | 48 | const unsigned char *tmp_u; |
65056021 JH |
49 | const char *tmp_c; |
50 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
51 | tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u; | |
52 | tmp_c = old_name; old_name = new_name; new_name = tmp_c; | |
53 | } | |
cd676a51 JH |
54 | |
55 | if (opt->prefix && | |
56 | (strncmp(old_name, opt->prefix, opt->prefix_length) || | |
57 | strncmp(new_name, opt->prefix, opt->prefix_length))) | |
58 | return; | |
59 | ||
65056021 JH |
60 | one = alloc_filespec(old_name); |
61 | two = alloc_filespec(new_name); | |
e5450100 JK |
62 | fill_filespec(one, old_sha1, old_sha1_valid, old_mode); |
63 | fill_filespec(two, new_sha1, new_sha1_valid, new_mode); | |
65056021 | 64 | |
65056021 JH |
65 | diff_queue(&diff_queued_diff, one, two); |
66 | } | |
67 | ||
68 | static int builtin_diff_b_f(struct rev_info *revs, | |
69 | int argc, const char **argv, | |
887c6c18 | 70 | struct blobinfo *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 | ||
01618a3a MK |
89 | if (blob[0].mode == S_IFINVALID) |
90 | blob[0].mode = canon_mode(st.st_mode); | |
91 | ||
65056021 | 92 | stuff_change(&revs->diffopt, |
01618a3a | 93 | blob[0].mode, canon_mode(st.st_mode), |
65056021 | 94 | blob[0].sha1, null_sha1, |
e5450100 | 95 | 1, 0, |
065e0b12 | 96 | path, path); |
65056021 JH |
97 | diffcore_std(&revs->diffopt); |
98 | diff_flush(&revs->diffopt); | |
99 | return 0; | |
100 | } | |
101 | ||
102 | static int builtin_diff_blobs(struct rev_info *revs, | |
103 | int argc, const char **argv, | |
104 | struct blobinfo *blob) | |
105 | { | |
65056021 JH |
106 | unsigned mode = canon_mode(S_IFREG | 0644); |
107 | ||
a610786f TH |
108 | if (argc > 1) |
109 | usage(builtin_diff_usage); | |
110 | ||
01618a3a MK |
111 | if (blob[0].mode == S_IFINVALID) |
112 | blob[0].mode = mode; | |
113 | ||
114 | if (blob[1].mode == S_IFINVALID) | |
115 | blob[1].mode = mode; | |
116 | ||
65056021 | 117 | stuff_change(&revs->diffopt, |
01618a3a | 118 | blob[0].mode, blob[1].mode, |
f82cd3c6 | 119 | blob[0].sha1, blob[1].sha1, |
e5450100 | 120 | 1, 1, |
53dd8a9c | 121 | blob[0].name, blob[1].name); |
65056021 JH |
122 | diffcore_std(&revs->diffopt); |
123 | diff_flush(&revs->diffopt); | |
124 | return 0; | |
125 | } | |
126 | ||
127 | static 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 | ||
160 | static 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 | { |
65056021 | 165 | const unsigned char *(sha1[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; |
91de344d | 177 | sha1[swap] = ent0->item->sha1; |
4e7e4b6b | 178 | sha1[1 - swap] = ent1->item->sha1; |
65056021 JH |
179 | diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt); |
180 | log_tree_diff_flush(revs); | |
181 | return 0; | |
182 | } | |
183 | ||
0fe7c1de JH |
184 | static 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 | { | |
0041f09d | 189 | struct sha1_array parents = SHA1_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 RS |
197 | for (i = 1; i < ents; i++) |
198 | sha1_array_append(&parents, ent[i].item->sha1); | |
199 | diff_tree_combined(ent[0].item->sha1, &parents, | |
0fe7c1de | 200 | revs->dense_combined_merges, revs); |
0041f09d | 201 | sha1_array_clear(&parents); |
0fe7c1de JH |
202 | return 0; |
203 | } | |
204 | ||
aecbf914 JH |
205 | static void refresh_index_quietly(void) |
206 | { | |
207 | struct lock_file *lock_file; | |
208 | int fd; | |
209 | ||
210 | lock_file = xcalloc(1, sizeof(struct lock_file)); | |
211 | fd = hold_locked_index(lock_file, 0); | |
212 | if (fd < 0) | |
213 | return; | |
214 | discard_cache(); | |
215 | read_cache(); | |
216 | refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED); | |
ccdc4ec3 | 217 | update_index_if_able(&the_index, lock_file); |
aecbf914 JH |
218 | } |
219 | ||
0569e9b8 JH |
220 | static 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 | ||
a633fca0 | 258 | int cmd_diff(int argc, const char **argv, const char *prefix) |
65056021 | 259 | { |
1f1e895f | 260 | int i; |
65056021 | 261 | struct rev_info rev; |
33055fa8 MH |
262 | struct object_array ent = OBJECT_ARRAY_INIT; |
263 | int blobs = 0, paths = 0; | |
65056021 | 264 | struct blobinfo blob[2]; |
470faf96 | 265 | int nongit = 0, no_index = 0; |
41bbf9d5 | 266 | int result = 0; |
65056021 JH |
267 | |
268 | /* | |
269 | * We could get N tree-ish in the rev.pending_objects list. | |
270 | * Also there could be M blobs there, and P pathspecs. | |
271 | * | |
272 | * N=0, M=0: | |
273 | * cache vs files (diff-files) | |
274 | * N=0, M=2: | |
275 | * compare two random blobs. P must be zero. | |
276 | * N=0, M=1, P=1: | |
277 | * compare a blob with a working tree file. | |
278 | * | |
279 | * N=1, M=0: | |
280 | * tree vs cache (diff-index --cached) | |
281 | * | |
282 | * N=2, M=0: | |
283 | * tree vs tree (diff-tree) | |
284 | * | |
0569e9b8 JH |
285 | * N=0, M=0, P=2: |
286 | * compare two filesystem entities (aka --no-index). | |
287 | * | |
65056021 JH |
288 | * Other cases are errors. |
289 | */ | |
230f544e | 290 | |
470faf96 TG |
291 | /* Were we asked to do --no-index explicitly? */ |
292 | for (i = 1; i < argc; i++) { | |
293 | if (!strcmp(argv[i], "--")) { | |
294 | i++; | |
295 | break; | |
296 | } | |
297 | if (!strcmp(argv[i], "--no-index")) | |
298 | no_index = DIFF_NO_INDEX_EXPLICIT; | |
299 | if (argv[i][0] != '-') | |
300 | break; | |
301 | } | |
302 | ||
6df5762d TG |
303 | if (!no_index) |
304 | prefix = setup_git_directory_gently(&nongit); | |
305 | ||
470faf96 TG |
306 | /* |
307 | * Treat git diff with at least one path outside of the | |
308 | * repo the same as if the command would have been executed | |
309 | * outside of a git repository. In this case it behaves | |
310 | * the same way as "git diff --no-index <a> <b>", which acts | |
311 | * as a colourful "diff" replacement. | |
312 | */ | |
313 | if (nongit || ((argc == i + 2) && | |
314 | (!path_inside_repo(prefix, argv[i]) || | |
315 | !path_inside_repo(prefix, argv[i + 1])))) | |
316 | no_index = DIFF_NO_INDEX_IMPLICIT; | |
317 | ||
6df5762d TG |
318 | if (!no_index) |
319 | gitmodules_config(); | |
ef90d6d4 | 320 | git_config(git_diff_ui_config, NULL); |
6b2f2d98 | 321 | |
db6296a5 | 322 | init_revisions(&rev, prefix); |
0569e9b8 | 323 | |
aad90e85 TG |
324 | if (no_index && argc != i + 2) { |
325 | if (no_index == DIFF_NO_INDEX_IMPLICIT) { | |
326 | /* | |
327 | * There was no --no-index and there were not two | |
328 | * paths. It is possible that the user intended | |
329 | * to do an inside-repository operation. | |
330 | */ | |
331 | fprintf(stderr, "Not a git repository\n"); | |
332 | fprintf(stderr, | |
333 | "To compare two paths outside a working tree:\n"); | |
470faf96 | 334 | } |
aad90e85 TG |
335 | /* Give the usage message for non-repository usage and exit. */ |
336 | usagef("git diff %s <path> <path>", | |
337 | no_index == DIFF_NO_INDEX_EXPLICIT ? | |
338 | "--no-index" : "[--no-index]"); | |
339 | ||
340 | } | |
341 | if (no_index) | |
470faf96 TG |
342 | /* If this is a no-index diff, just run it and exit there. */ |
343 | diff_no_index(&rev, argc, argv, prefix); | |
0569e9b8 JH |
344 | |
345 | /* Otherwise, we are doing the usual "git" diff */ | |
aecbf914 | 346 | rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index; |
65056021 | 347 | |
df44483a | 348 | /* Scale to real terminal size and respect statGraphWidth config */ |
af9fedc1 | 349 | rev.diffopt.stat_width = -1; |
df44483a | 350 | rev.diffopt.stat_graph_width = -1; |
af9fedc1 | 351 | |
5ec11af6 | 352 | /* Default to let external and textconv be used */ |
61af494c | 353 | DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL); |
5ec11af6 | 354 | DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV); |
61af494c | 355 | |
0569e9b8 | 356 | if (nongit) |
54214529 | 357 | die(_("Not a git repository")); |
0569e9b8 | 358 | argc = setup_revisions(argc, argv, &rev, NULL); |
047fbe90 | 359 | if (!rev.diffopt.output_format) { |
c9b5ef99 | 360 | rev.diffopt.output_format = DIFF_FORMAT_PATCH; |
28452655 | 361 | diff_setup_done(&rev.diffopt); |
047fbe90 | 362 | } |
61af494c | 363 | |
8f67f8ae | 364 | DIFF_OPT_SET(&rev.diffopt, RECURSIVE); |
c9b5ef99 | 365 | |
1af3d977 | 366 | setup_diff_pager(&rev.diffopt); |
89d07f75 | 367 | |
0569e9b8 JH |
368 | /* |
369 | * Do we have --cached and not have a pending object, then | |
65056021 JH |
370 | * default to HEAD by hand. Eek. |
371 | */ | |
1f1e895f | 372 | if (!rev.pending.nr) { |
65056021 JH |
373 | int i; |
374 | for (i = 1; i < argc; i++) { | |
375 | const char *arg = argv[i]; | |
376 | if (!strcmp(arg, "--")) | |
377 | break; | |
2baf1850 DS |
378 | else if (!strcmp(arg, "--cached") || |
379 | !strcmp(arg, "--staged")) { | |
3384a2df | 380 | add_head_to_pending(&rev); |
a2b7a3b3 NTND |
381 | if (!rev.pending.nr) { |
382 | struct tree *tree; | |
cba595bd | 383 | tree = lookup_tree(EMPTY_TREE_SHA1_BIN); |
a2b7a3b3 NTND |
384 | add_pending_object(&rev, &tree->object, "HEAD"); |
385 | } | |
65056021 JH |
386 | break; |
387 | } | |
388 | } | |
389 | } | |
390 | ||
1f1e895f | 391 | for (i = 0; i < rev.pending.nr; i++) { |
026f09e7 MH |
392 | struct object_array_entry *entry = &rev.pending.objects[i]; |
393 | struct object *obj = entry->item; | |
394 | const char *name = entry->name; | |
65056021 JH |
395 | int flags = (obj->flags & UNINTERESTING); |
396 | if (!obj->parsed) | |
397 | obj = parse_object(obj->sha1); | |
398 | obj = deref_tag(obj, NULL, 0); | |
399 | if (!obj) | |
54214529 | 400 | die(_("invalid object '%s' given."), name); |
1974632c | 401 | if (obj->type == OBJ_COMMIT) |
65056021 | 402 | obj = &((struct commit *)obj)->tree->object; |
5b1e14ea | 403 | |
1974632c | 404 | if (obj->type == OBJ_TREE) { |
65056021 | 405 | obj->flags |= flags; |
33055fa8 | 406 | add_object_array(obj, name, &ent); |
5b1e14ea | 407 | } else if (obj->type == OBJ_BLOB) { |
65056021 | 408 | if (2 <= blobs) |
54214529 | 409 | die(_("more than two blobs given: '%s'"), name); |
e702496e | 410 | hashcpy(blob[blobs].sha1, obj->sha1); |
65056021 | 411 | blob[blobs].name = name; |
026f09e7 | 412 | blob[blobs].mode = entry->mode; |
65056021 | 413 | blobs++; |
230f544e | 414 | |
5b1e14ea MH |
415 | } else { |
416 | die(_("unhandled object '%s' given."), name); | |
65056021 | 417 | } |
65056021 | 418 | } |
887c6c18 | 419 | if (rev.prune_data.nr) |
afe069d1 | 420 | paths += rev.prune_data.nr; |
65056021 JH |
421 | |
422 | /* | |
423 | * Now, do the arguments look reasonable? | |
424 | */ | |
33055fa8 | 425 | if (!ent.nr) { |
65056021 JH |
426 | switch (blobs) { |
427 | case 0: | |
0569e9b8 | 428 | result = builtin_diff_files(&rev, argc, argv); |
65056021 JH |
429 | break; |
430 | case 1: | |
431 | if (paths != 1) | |
432 | usage(builtin_diff_usage); | |
887c6c18 | 433 | result = builtin_diff_b_f(&rev, argc, argv, blob); |
65056021 JH |
434 | break; |
435 | case 2: | |
0fe7c1de JH |
436 | if (paths) |
437 | usage(builtin_diff_usage); | |
41bbf9d5 | 438 | result = builtin_diff_blobs(&rev, argc, argv, blob); |
65056021 JH |
439 | break; |
440 | default: | |
441 | usage(builtin_diff_usage); | |
442 | } | |
443 | } | |
444 | else if (blobs) | |
445 | usage(builtin_diff_usage); | |
33055fa8 | 446 | else if (ent.nr == 1) |
41bbf9d5 | 447 | result = builtin_diff_index(&rev, argc, argv); |
33055fa8 MH |
448 | else if (ent.nr == 2) |
449 | result = builtin_diff_tree(&rev, argc, argv, | |
450 | &ent.objects[0], &ent.objects[1]); | |
451 | else if (ent.objects[0].item->flags & UNINTERESTING) { | |
c008c0ff JH |
452 | /* |
453 | * diff A...B where there is at least one merge base | |
33055fa8 MH |
454 | * between A and B. We have ent.objects[0] == |
455 | * merge-base, ent.objects[ents-2] == A, and | |
456 | * ent.objects[ents-1] == B. Show diff between the | |
457 | * base and B. Note that we pick one merge base at | |
458 | * random if there are more than one. | |
c008c0ff | 459 | */ |
33055fa8 MH |
460 | result = builtin_diff_tree(&rev, argc, argv, |
461 | &ent.objects[0], | |
462 | &ent.objects[ent.nr-1]); | |
c008c0ff | 463 | } else |
41bbf9d5 | 464 | result = builtin_diff_combined(&rev, argc, argv, |
33055fa8 | 465 | ent.objects, ent.nr); |
da31b358 | 466 | result = diff_result_code(&rev.diffopt, result); |
aecbf914 JH |
467 | if (1 < rev.diffopt.skip_stat_unmatch) |
468 | refresh_index_quietly(); | |
41bbf9d5 | 469 | return result; |
65056021 | 470 | } |