diff: don't read index when --no-index is given
[alt-git.git] / builtin / diff.c
blobea1dd65a2f17e257c195aa561400ed04fd0ef636
1 /*
2 * Builtin "git diff"
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #include "cache.h"
7 #include "color.h"
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"
16 #include "submodule.h"
17 #include "sha1-array.h"
19 #define DIFF_NO_INDEX_EXPLICIT 1
20 #define DIFF_NO_INDEX_IMPLICIT 2
22 struct blobinfo {
23 unsigned char sha1[20];
24 const char *name;
25 unsigned mode;
28 static const char builtin_diff_usage[] =
29 "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
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,
35 int old_sha1_valid,
36 int new_sha1_valid,
37 const char *old_name,
38 const char *new_name)
40 struct diff_filespec *one, *two;
42 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
43 !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
44 return;
46 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
47 unsigned tmp;
48 const unsigned char *tmp_u;
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;
55 if (opt->prefix &&
56 (strncmp(old_name, opt->prefix, opt->prefix_length) ||
57 strncmp(new_name, opt->prefix, opt->prefix_length)))
58 return;
60 one = alloc_filespec(old_name);
61 two = alloc_filespec(new_name);
62 fill_filespec(one, old_sha1, old_sha1_valid, old_mode);
63 fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
65 diff_queue(&diff_queued_diff, one, two);
68 static int builtin_diff_b_f(struct rev_info *revs,
69 int argc, const char **argv,
70 struct blobinfo *blob,
71 const char *path)
73 /* Blob vs file in the working tree*/
74 struct stat st;
76 if (argc > 1)
77 usage(builtin_diff_usage);
79 if (lstat(path, &st))
80 die_errno(_("failed to stat '%s'"), path);
81 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
82 die(_("'%s': not a regular file or symlink"), path);
84 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
86 if (blob[0].mode == S_IFINVALID)
87 blob[0].mode = canon_mode(st.st_mode);
89 stuff_change(&revs->diffopt,
90 blob[0].mode, canon_mode(st.st_mode),
91 blob[0].sha1, null_sha1,
92 1, 0,
93 path, path);
94 diffcore_std(&revs->diffopt);
95 diff_flush(&revs->diffopt);
96 return 0;
99 static int builtin_diff_blobs(struct rev_info *revs,
100 int argc, const char **argv,
101 struct blobinfo *blob)
103 unsigned mode = canon_mode(S_IFREG | 0644);
105 if (argc > 1)
106 usage(builtin_diff_usage);
108 if (blob[0].mode == S_IFINVALID)
109 blob[0].mode = mode;
111 if (blob[1].mode == S_IFINVALID)
112 blob[1].mode = mode;
114 stuff_change(&revs->diffopt,
115 blob[0].mode, blob[1].mode,
116 blob[0].sha1, blob[1].sha1,
117 1, 1,
118 blob[0].name, blob[1].name);
119 diffcore_std(&revs->diffopt);
120 diff_flush(&revs->diffopt);
121 return 0;
124 static int builtin_diff_index(struct rev_info *revs,
125 int argc, const char **argv)
127 int cached = 0;
128 while (1 < argc) {
129 const char *arg = argv[1];
130 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
131 cached = 1;
132 else
133 usage(builtin_diff_usage);
134 argv++; argc--;
137 * Make sure there is one revision (i.e. pending object),
138 * and there is no revision filtering parameters.
140 if (revs->pending.nr != 1 ||
141 revs->max_count != -1 || revs->min_age != -1 ||
142 revs->max_age != -1)
143 usage(builtin_diff_usage);
144 if (!cached) {
145 setup_work_tree();
146 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
147 perror("read_cache_preload");
148 return -1;
150 } else if (read_cache() < 0) {
151 perror("read_cache");
152 return -1;
154 return run_diff_index(revs, cached);
157 static int builtin_diff_tree(struct rev_info *revs,
158 int argc, const char **argv,
159 struct object_array_entry *ent0,
160 struct object_array_entry *ent1)
162 const unsigned char *(sha1[2]);
163 int swap = 0;
165 if (argc > 1)
166 usage(builtin_diff_usage);
169 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
170 * swap them.
172 if (ent1->item->flags & UNINTERESTING)
173 swap = 1;
174 sha1[swap] = ent0->item->sha1;
175 sha1[1 - swap] = ent1->item->sha1;
176 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
177 log_tree_diff_flush(revs);
178 return 0;
181 static int builtin_diff_combined(struct rev_info *revs,
182 int argc, const char **argv,
183 struct object_array_entry *ent,
184 int ents)
186 struct sha1_array parents = SHA1_ARRAY_INIT;
187 int i;
189 if (argc > 1)
190 usage(builtin_diff_usage);
192 if (!revs->dense_combined_merges && !revs->combine_merges)
193 revs->dense_combined_merges = revs->combine_merges = 1;
194 for (i = 1; i < ents; i++)
195 sha1_array_append(&parents, ent[i].item->sha1);
196 diff_tree_combined(ent[0].item->sha1, &parents,
197 revs->dense_combined_merges, revs);
198 sha1_array_clear(&parents);
199 return 0;
202 static void refresh_index_quietly(void)
204 struct lock_file *lock_file;
205 int fd;
207 lock_file = xcalloc(1, sizeof(struct lock_file));
208 fd = hold_locked_index(lock_file, 0);
209 if (fd < 0)
210 return;
211 discard_cache();
212 read_cache();
213 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
214 update_index_if_able(&the_index, lock_file);
217 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
219 unsigned int options = 0;
221 while (1 < argc && argv[1][0] == '-') {
222 if (!strcmp(argv[1], "--base"))
223 revs->max_count = 1;
224 else if (!strcmp(argv[1], "--ours"))
225 revs->max_count = 2;
226 else if (!strcmp(argv[1], "--theirs"))
227 revs->max_count = 3;
228 else if (!strcmp(argv[1], "-q"))
229 options |= DIFF_SILENT_ON_REMOVED;
230 else if (!strcmp(argv[1], "-h"))
231 usage(builtin_diff_usage);
232 else
233 return error(_("invalid option: %s"), argv[1]);
234 argv++; argc--;
238 * "diff --base" should not combine merges because it was not
239 * asked to. "diff -c" should not densify (if the user wants
240 * dense one, --cc can be explicitly asked for, or just rely
241 * on the default).
243 if (revs->max_count == -1 && !revs->combine_merges &&
244 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
245 revs->combine_merges = revs->dense_combined_merges = 1;
247 setup_work_tree();
248 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
249 perror("read_cache_preload");
250 return -1;
252 return run_diff_files(revs, options);
255 int cmd_diff(int argc, const char **argv, const char *prefix)
257 int i;
258 struct rev_info rev;
259 struct object_array ent = OBJECT_ARRAY_INIT;
260 int blobs = 0, paths = 0;
261 const char *path = NULL;
262 struct blobinfo blob[2];
263 int nongit = 0, no_index = 0;
264 int result = 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.
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.
277 * N=1, M=0:
278 * tree vs cache (diff-index --cached)
280 * N=2, M=0:
281 * tree vs tree (diff-tree)
283 * N=0, M=0, P=2:
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], "--")) {
292 i++;
293 break;
295 if (!strcmp(argv[i], "--no-index"))
296 no_index = DIFF_NO_INDEX_EXPLICIT;
297 if (argv[i][0] != '-')
298 break;
301 if (!no_index)
302 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;
316 if (!no_index)
317 gitmodules_config();
318 git_config(git_diff_ui_config, NULL);
320 init_revisions(&rev, prefix);
322 if (no_index) {
323 if (argc != i + 2) {
324 if (no_index == DIFF_NO_INDEX_IMPLICIT) {
326 * There was no --no-index and there were not two
327 * paths. It is possible that the user intended
328 * to do an inside-repository operation.
330 fprintf(stderr, "Not a git repository\n");
331 fprintf(stderr,
332 "To compare two paths outside a working tree:\n");
334 /* Give the usage message for non-repository usage and exit. */
335 usagef("git diff %s <path> <path>",
336 no_index == DIFF_NO_INDEX_EXPLICIT ?
337 "--no-index" : "[--no-index]");
340 /* If this is a no-index diff, just run it and exit there. */
341 diff_no_index(&rev, argc, argv, prefix);
344 /* Otherwise, we are doing the usual "git" diff */
345 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
347 /* Scale to real terminal size and respect statGraphWidth config */
348 rev.diffopt.stat_width = -1;
349 rev.diffopt.stat_graph_width = -1;
351 /* Default to let external and textconv be used */
352 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
353 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
355 if (nongit)
356 die(_("Not a git repository"));
357 argc = setup_revisions(argc, argv, &rev, NULL);
358 if (!rev.diffopt.output_format) {
359 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
360 diff_setup_done(&rev.diffopt);
363 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
365 setup_diff_pager(&rev.diffopt);
368 * Do we have --cached and not have a pending object, then
369 * default to HEAD by hand. Eek.
371 if (!rev.pending.nr) {
372 int i;
373 for (i = 1; i < argc; i++) {
374 const char *arg = argv[i];
375 if (!strcmp(arg, "--"))
376 break;
377 else if (!strcmp(arg, "--cached") ||
378 !strcmp(arg, "--staged")) {
379 add_head_to_pending(&rev);
380 if (!rev.pending.nr) {
381 struct tree *tree;
382 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
383 add_pending_object(&rev, &tree->object, "HEAD");
385 break;
390 for (i = 0; i < rev.pending.nr; i++) {
391 struct object_array_entry *entry = &rev.pending.objects[i];
392 struct object *obj = entry->item;
393 const char *name = entry->name;
394 int flags = (obj->flags & UNINTERESTING);
395 if (!obj->parsed)
396 obj = parse_object(obj->sha1);
397 obj = deref_tag(obj, NULL, 0);
398 if (!obj)
399 die(_("invalid object '%s' given."), name);
400 if (obj->type == OBJ_COMMIT)
401 obj = &((struct commit *)obj)->tree->object;
403 if (obj->type == OBJ_TREE) {
404 obj->flags |= flags;
405 add_object_array(obj, name, &ent);
406 } else if (obj->type == OBJ_BLOB) {
407 if (2 <= blobs)
408 die(_("more than two blobs given: '%s'"), name);
409 hashcpy(blob[blobs].sha1, obj->sha1);
410 blob[blobs].name = name;
411 blob[blobs].mode = entry->mode;
412 blobs++;
414 } else {
415 die(_("unhandled object '%s' given."), name);
418 if (rev.prune_data.nr) {
419 /* builtin_diff_b_f() */
420 GUARD_PATHSPEC(&rev.prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
421 if (!path)
422 path = rev.prune_data.items[0].match;
423 paths += rev.prune_data.nr;
427 * Now, do the arguments look reasonable?
429 if (!ent.nr) {
430 switch (blobs) {
431 case 0:
432 result = builtin_diff_files(&rev, argc, argv);
433 break;
434 case 1:
435 if (paths != 1)
436 usage(builtin_diff_usage);
437 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
438 break;
439 case 2:
440 if (paths)
441 usage(builtin_diff_usage);
442 result = builtin_diff_blobs(&rev, argc, argv, blob);
443 break;
444 default:
445 usage(builtin_diff_usage);
448 else if (blobs)
449 usage(builtin_diff_usage);
450 else if (ent.nr == 1)
451 result = builtin_diff_index(&rev, argc, argv);
452 else if (ent.nr == 2)
453 result = builtin_diff_tree(&rev, argc, argv,
454 &ent.objects[0], &ent.objects[1]);
455 else if (ent.objects[0].item->flags & UNINTERESTING) {
457 * diff A...B where there is at least one merge base
458 * between A and B. We have ent.objects[0] ==
459 * merge-base, ent.objects[ents-2] == A, and
460 * ent.objects[ents-1] == B. Show diff between the
461 * base and B. Note that we pick one merge base at
462 * random if there are more than one.
464 result = builtin_diff_tree(&rev, argc, argv,
465 &ent.objects[0],
466 &ent.objects[ent.nr-1]);
467 } else
468 result = builtin_diff_combined(&rev, argc, argv,
469 ent.objects, ent.nr);
470 result = diff_result_code(&rev.diffopt, result);
471 if (1 < rev.diffopt.skip_stat_unmatch)
472 refresh_index_quietly();
473 return result;