4 * Copyright (c) 2006 Junio C Hamano
17 #include "submodule.h"
18 #include "sha1-array.h"
20 #define DIFF_NO_INDEX_EXPLICIT 1
21 #define DIFF_NO_INDEX_IMPLICIT 2
24 unsigned char sha1[20];
29 static const char builtin_diff_usage[] =
30 "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
32 static 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,
41 struct diff_filespec *one, *two;
43 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
44 !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
47 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
48 SWAP(old_mode, new_mode);
49 SWAP(old_sha1, new_sha1);
50 SWAP(old_name, new_name);
54 (strncmp(old_name, opt->prefix, opt->prefix_length) ||
55 strncmp(new_name, opt->prefix, opt->prefix_length)))
58 one = alloc_filespec(old_name);
59 two = alloc_filespec(new_name);
60 fill_filespec(one, old_sha1, old_sha1_valid, old_mode);
61 fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
63 diff_queue(&diff_queued_diff, one, two);
66 static int builtin_diff_b_f(struct rev_info *revs,
67 int argc, const char **argv,
68 struct blobinfo *blob)
70 /* Blob vs file in the working tree*/
75 usage(builtin_diff_usage);
77 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
78 path = revs->prune_data.items[0].match;
81 die_errno(_("failed to stat '%s'"), path);
82 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
83 die(_("'%s': not a regular file or symlink"), path);
85 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
87 if (blob[0].mode == S_IFINVALID)
88 blob[0].mode = canon_mode(st.st_mode);
90 stuff_change(&revs->diffopt,
91 blob[0].mode, canon_mode(st.st_mode),
92 blob[0].sha1, null_sha1,
95 diffcore_std(&revs->diffopt);
96 diff_flush(&revs->diffopt);
100 static int builtin_diff_blobs(struct rev_info *revs,
101 int argc, const char **argv,
102 struct blobinfo *blob)
104 unsigned mode = canon_mode(S_IFREG | 0644);
107 usage(builtin_diff_usage);
109 if (blob[0].mode == S_IFINVALID)
112 if (blob[1].mode == S_IFINVALID)
115 stuff_change(&revs->diffopt,
116 blob[0].mode, blob[1].mode,
117 blob[0].sha1, blob[1].sha1,
119 blob[0].name, blob[1].name);
120 diffcore_std(&revs->diffopt);
121 diff_flush(&revs->diffopt);
125 static int builtin_diff_index(struct rev_info *revs,
126 int argc, const char **argv)
130 const char *arg = argv[1];
131 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
134 usage(builtin_diff_usage);
138 * Make sure there is one revision (i.e. pending object),
139 * and there is no revision filtering parameters.
141 if (revs->pending.nr != 1 ||
142 revs->max_count != -1 || revs->min_age != -1 ||
144 usage(builtin_diff_usage);
147 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
148 perror("read_cache_preload");
151 } else if (read_cache() < 0) {
152 perror("read_cache");
155 return run_diff_index(revs, cached);
158 static int builtin_diff_tree(struct rev_info *revs,
159 int argc, const char **argv,
160 struct object_array_entry *ent0,
161 struct object_array_entry *ent1)
163 const unsigned char *(sha1[2]);
167 usage(builtin_diff_usage);
170 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
173 if (ent1->item->flags & UNINTERESTING)
175 sha1[swap] = ent0->item->oid.hash;
176 sha1[1 - swap] = ent1->item->oid.hash;
177 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
178 log_tree_diff_flush(revs);
182 static int builtin_diff_combined(struct rev_info *revs,
183 int argc, const char **argv,
184 struct object_array_entry *ent,
187 struct sha1_array parents = SHA1_ARRAY_INIT;
191 usage(builtin_diff_usage);
193 if (!revs->dense_combined_merges && !revs->combine_merges)
194 revs->dense_combined_merges = revs->combine_merges = 1;
195 for (i = 1; i < ents; i++)
196 sha1_array_append(&parents, ent[i].item->oid.hash);
197 diff_tree_combined(ent[0].item->oid.hash, &parents,
198 revs->dense_combined_merges, revs);
199 sha1_array_clear(&parents);
203 static void refresh_index_quietly(void)
205 struct lock_file *lock_file;
208 lock_file = xcalloc(1, sizeof(struct lock_file));
209 fd = hold_locked_index(lock_file, 0);
214 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
215 update_index_if_able(&the_index, lock_file);
218 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
220 unsigned int options = 0;
222 while (1 < argc && argv[1][0] == '-') {
223 if (!strcmp(argv[1], "--base"))
225 else if (!strcmp(argv[1], "--ours"))
227 else if (!strcmp(argv[1], "--theirs"))
229 else if (!strcmp(argv[1], "-q"))
230 options |= DIFF_SILENT_ON_REMOVED;
231 else if (!strcmp(argv[1], "-h"))
232 usage(builtin_diff_usage);
234 return error(_("invalid option: %s"), argv[1]);
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
244 if (revs->max_count == -1 && !revs->combine_merges &&
245 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
246 revs->combine_merges = revs->dense_combined_merges = 1;
249 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
250 perror("read_cache_preload");
253 return run_diff_files(revs, options);
256 int cmd_diff(int argc, const char **argv, const char *prefix)
260 struct object_array ent = OBJECT_ARRAY_INIT;
261 int blobs = 0, paths = 0;
262 struct blobinfo blob[2];
263 int nongit = 0, no_index = 0;
267 * We could get N tree-ish in the rev.pending_objects list.
268 * Also there could be M blobs there, and P pathspecs.
271 * cache vs files (diff-files)
273 * compare two random blobs. P must be zero.
275 * compare a blob with a working tree file.
278 * tree vs cache (diff-index --cached)
281 * tree vs tree (diff-tree)
284 * compare two filesystem entities (aka --no-index).
286 * Other cases are errors.
289 /* Were we asked to do --no-index explicitly? */
290 for (i = 1; i < argc; i++) {
291 if (!strcmp(argv[i], "--")) {
295 if (!strcmp(argv[i], "--no-index"))
296 no_index = DIFF_NO_INDEX_EXPLICIT;
297 if (argv[i][0] != '-')
301 prefix = setup_git_directory_gently(&nongit);
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.
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;
319 init_diff_ui_defaults();
320 git_config(git_diff_ui_config, NULL);
321 precompose_argv(argc, argv);
323 init_revisions(&rev, prefix);
325 if (no_index && argc != i + 2) {
326 if (no_index == DIFF_NO_INDEX_IMPLICIT) {
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.
332 fprintf(stderr, "Not a git repository\n");
334 "To compare two paths outside a working tree:\n");
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]");
343 /* If this is a no-index diff, just run it and exit there. */
344 diff_no_index(&rev, argc, argv);
346 /* Otherwise, we are doing the usual "git" diff */
347 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
349 /* Scale to real terminal size and respect statGraphWidth config */
350 rev.diffopt.stat_width = -1;
351 rev.diffopt.stat_graph_width = -1;
353 /* Default to let external and textconv be used */
354 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
355 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
358 die(_("Not a git repository"));
359 argc = setup_revisions(argc, argv, &rev, NULL);
360 if (!rev.diffopt.output_format) {
361 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
362 diff_setup_done(&rev.diffopt);
365 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
367 setup_diff_pager(&rev.diffopt);
370 * Do we have --cached and not have a pending object, then
371 * default to HEAD by hand. Eek.
373 if (!rev.pending.nr) {
375 for (i = 1; i < argc; i++) {
376 const char *arg = argv[i];
377 if (!strcmp(arg, "--"))
379 else if (!strcmp(arg, "--cached") ||
380 !strcmp(arg, "--staged")) {
381 add_head_to_pending(&rev);
382 if (!rev.pending.nr) {
384 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
385 add_pending_object(&rev, &tree->object, "HEAD");
392 for (i = 0; i < rev.pending.nr; i++) {
393 struct object_array_entry *entry = &rev.pending.objects[i];
394 struct object *obj = entry->item;
395 const char *name = entry->name;
396 int flags = (obj->flags & UNINTERESTING);
398 obj = parse_object(obj->oid.hash);
399 obj = deref_tag(obj, NULL, 0);
401 die(_("invalid object '%s' given."), name);
402 if (obj->type == OBJ_COMMIT)
403 obj = &((struct commit *)obj)->tree->object;
405 if (obj->type == OBJ_TREE) {
407 add_object_array(obj, name, &ent);
408 } else if (obj->type == OBJ_BLOB) {
410 die(_("more than two blobs given: '%s'"), name);
411 hashcpy(blob[blobs].sha1, obj->oid.hash);
412 blob[blobs].name = name;
413 blob[blobs].mode = entry->mode;
417 die(_("unhandled object '%s' given."), name);
420 if (rev.prune_data.nr)
421 paths += rev.prune_data.nr;
424 * Now, do the arguments look reasonable?
429 result = builtin_diff_files(&rev, argc, argv);
433 usage(builtin_diff_usage);
434 result = builtin_diff_b_f(&rev, argc, argv, blob);
438 usage(builtin_diff_usage);
439 result = builtin_diff_blobs(&rev, argc, argv, blob);
442 usage(builtin_diff_usage);
446 usage(builtin_diff_usage);
447 else if (ent.nr == 1)
448 result = builtin_diff_index(&rev, argc, argv);
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) {
454 * diff A...B where there is at least one merge base
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.
461 result = builtin_diff_tree(&rev, argc, argv,
463 &ent.objects[ent.nr-1]);
465 result = builtin_diff_combined(&rev, argc, argv,
466 ent.objects, ent.nr);
467 result = diff_result_code(&rev.diffopt, result);
468 if (1 < rev.diffopt.skip_stat_unmatch)
469 refresh_index_quietly();