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