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