1 /*
2 * Builtin "git diff"
3 *
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"
17 struct blobinfo {
18 unsigned char sha1[20];
19 const char *name;
20 unsigned mode;
21 };
23 static const char builtin_diff_usage[] =
24 "git diff <options> <rev>{0,2} -- <path>*";
26 static void stuff_change(struct diff_options *opt,
27 unsigned old_mode, unsigned new_mode,
28 const unsigned char *old_sha1,
29 const unsigned char *new_sha1,
30 const char *old_name,
31 const char *new_name)
32 {
33 struct diff_filespec *one, *two;
35 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
36 !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
37 return;
39 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
40 unsigned tmp;
41 const unsigned char *tmp_u;
42 const char *tmp_c;
43 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
44 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
45 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
46 }
48 if (opt->prefix &&
49 (strncmp(old_name, opt->prefix, opt->prefix_length) ||
50 strncmp(new_name, opt->prefix, opt->prefix_length)))
51 return;
53 one = alloc_filespec(old_name);
54 two = alloc_filespec(new_name);
55 fill_filespec(one, old_sha1, old_mode);
56 fill_filespec(two, new_sha1, new_mode);
58 diff_queue(&diff_queued_diff, one, two);
59 }
61 static int builtin_diff_b_f(struct rev_info *revs,
62 int argc, const char **argv,
63 struct blobinfo *blob,
64 const char *path)
65 {
66 /* Blob vs file in the working tree*/
67 struct stat st;
69 if (argc > 1)
70 usage(builtin_diff_usage);
72 if (lstat(path, &st))
73 die("'%s': %s", path, strerror(errno));
74 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
75 die("'%s': not a regular file or symlink", path);
77 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
79 if (blob[0].mode == S_IFINVALID)
80 blob[0].mode = canon_mode(st.st_mode);
82 stuff_change(&revs->diffopt,
83 blob[0].mode, canon_mode(st.st_mode),
84 blob[0].sha1, null_sha1,
85 path, path);
86 diffcore_std(&revs->diffopt);
87 diff_flush(&revs->diffopt);
88 return 0;
89 }
91 static int builtin_diff_blobs(struct rev_info *revs,
92 int argc, const char **argv,
93 struct blobinfo *blob)
94 {
95 unsigned mode = canon_mode(S_IFREG | 0644);
97 if (argc > 1)
98 usage(builtin_diff_usage);
100 if (blob[0].mode == S_IFINVALID)
101 blob[0].mode = mode;
103 if (blob[1].mode == S_IFINVALID)
104 blob[1].mode = mode;
106 stuff_change(&revs->diffopt,
107 blob[0].mode, blob[1].mode,
108 blob[0].sha1, blob[1].sha1,
109 blob[0].name, blob[1].name);
110 diffcore_std(&revs->diffopt);
111 diff_flush(&revs->diffopt);
112 return 0;
113 }
115 static int builtin_diff_index(struct rev_info *revs,
116 int argc, const char **argv)
117 {
118 int cached = 0;
119 while (1 < argc) {
120 const char *arg = argv[1];
121 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
122 cached = 1;
123 else
124 usage(builtin_diff_usage);
125 argv++; argc--;
126 }
127 if (!cached)
128 setup_work_tree();
129 /*
130 * Make sure there is one revision (i.e. pending object),
131 * and there is no revision filtering parameters.
132 */
133 if (revs->pending.nr != 1 ||
134 revs->max_count != -1 || revs->min_age != -1 ||
135 revs->max_age != -1)
136 usage(builtin_diff_usage);
137 if (read_cache() < 0) {
138 perror("read_cache");
139 return -1;
140 }
141 return run_diff_index(revs, cached);
142 }
144 static int builtin_diff_tree(struct rev_info *revs,
145 int argc, const char **argv,
146 struct object_array_entry *ent)
147 {
148 const unsigned char *(sha1[2]);
149 int swap = 0;
151 if (argc > 1)
152 usage(builtin_diff_usage);
154 /* We saw two trees, ent[0] and ent[1].
155 * if ent[1] is uninteresting, they are swapped
156 */
157 if (ent[1].item->flags & UNINTERESTING)
158 swap = 1;
159 sha1[swap] = ent[0].item->sha1;
160 sha1[1-swap] = ent[1].item->sha1;
161 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
162 log_tree_diff_flush(revs);
163 return 0;
164 }
166 static int builtin_diff_combined(struct rev_info *revs,
167 int argc, const char **argv,
168 struct object_array_entry *ent,
169 int ents)
170 {
171 const unsigned char (*parent)[20];
172 int i;
174 if (argc > 1)
175 usage(builtin_diff_usage);
177 if (!revs->dense_combined_merges && !revs->combine_merges)
178 revs->dense_combined_merges = revs->combine_merges = 1;
179 parent = xmalloc(ents * sizeof(*parent));
180 for (i = 0; i < ents; i++)
181 hashcpy((unsigned char *)(parent + i), ent[i].item->sha1);
182 diff_tree_combined(parent[0], parent + 1, ents - 1,
183 revs->dense_combined_merges, revs);
184 return 0;
185 }
187 static void refresh_index_quietly(void)
188 {
189 struct lock_file *lock_file;
190 int fd;
192 lock_file = xcalloc(1, sizeof(struct lock_file));
193 fd = hold_locked_index(lock_file, 0);
194 if (fd < 0)
195 return;
196 discard_cache();
197 read_cache();
198 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
200 if (active_cache_changed &&
201 !write_cache(fd, active_cache, active_nr))
202 commit_locked_index(lock_file);
204 rollback_lock_file(lock_file);
205 }
207 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
208 {
209 int result;
210 unsigned int options = 0;
212 while (1 < argc && argv[1][0] == '-') {
213 if (!strcmp(argv[1], "--base"))
214 revs->max_count = 1;
215 else if (!strcmp(argv[1], "--ours"))
216 revs->max_count = 2;
217 else if (!strcmp(argv[1], "--theirs"))
218 revs->max_count = 3;
219 else if (!strcmp(argv[1], "-q"))
220 options |= DIFF_SILENT_ON_REMOVED;
221 else
222 return error("invalid option: %s", argv[1]);
223 argv++; argc--;
224 }
226 /*
227 * "diff --base" should not combine merges because it was not
228 * asked to. "diff -c" should not densify (if the user wants
229 * dense one, --cc can be explicitly asked for, or just rely
230 * on the default).
231 */
232 if (revs->max_count == -1 && !revs->combine_merges &&
233 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
234 revs->combine_merges = revs->dense_combined_merges = 1;
236 setup_work_tree();
237 if (read_cache() < 0) {
238 perror("read_cache");
239 return -1;
240 }
241 result = run_diff_files(revs, options);
242 return diff_result_code(&revs->diffopt, result);
243 }
245 int cmd_diff(int argc, const char **argv, const char *prefix)
246 {
247 int i;
248 struct rev_info rev;
249 struct object_array_entry ent[100];
250 int ents = 0, blobs = 0, paths = 0;
251 const char *path = NULL;
252 struct blobinfo blob[2];
253 int nongit;
254 int result = 0;
256 /*
257 * We could get N tree-ish in the rev.pending_objects list.
258 * Also there could be M blobs there, and P pathspecs.
259 *
260 * N=0, M=0:
261 * cache vs files (diff-files)
262 * N=0, M=2:
263 * compare two random blobs. P must be zero.
264 * N=0, M=1, P=1:
265 * compare a blob with a working tree file.
266 *
267 * N=1, M=0:
268 * tree vs cache (diff-index --cached)
269 *
270 * N=2, M=0:
271 * tree vs tree (diff-tree)
272 *
273 * N=0, M=0, P=2:
274 * compare two filesystem entities (aka --no-index).
275 *
276 * Other cases are errors.
277 */
279 prefix = setup_git_directory_gently(&nongit);
280 git_config(git_diff_ui_config, NULL);
282 if (diff_use_color_default == -1)
283 diff_use_color_default = git_use_color_default;
285 init_revisions(&rev, prefix);
287 /* If this is a no-index diff, just run it and exit there. */
288 diff_no_index(&rev, argc, argv, nongit, prefix);
290 /* Otherwise, we are doing the usual "git" diff */
291 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
293 if (nongit)
294 die("Not a git repository");
295 argc = setup_revisions(argc, argv, &rev, NULL);
296 if (!rev.diffopt.output_format) {
297 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
298 if (diff_setup_done(&rev.diffopt) < 0)
299 die("diff_setup_done failed");
300 }
301 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
302 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
304 /*
305 * If the user asked for our exit code then don't start a
306 * pager or we would end up reporting its exit code instead.
307 */
308 if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS) &&
309 check_pager_config("diff") != 0)
310 setup_pager();
312 /*
313 * Do we have --cached and not have a pending object, then
314 * default to HEAD by hand. Eek.
315 */
316 if (!rev.pending.nr) {
317 int i;
318 for (i = 1; i < argc; i++) {
319 const char *arg = argv[i];
320 if (!strcmp(arg, "--"))
321 break;
322 else if (!strcmp(arg, "--cached") ||
323 !strcmp(arg, "--staged")) {
324 add_head_to_pending(&rev);
325 if (!rev.pending.nr)
326 die("No HEAD commit to compare with (yet)");
327 break;
328 }
329 }
330 }
332 for (i = 0; i < rev.pending.nr; i++) {
333 struct object_array_entry *list = rev.pending.objects+i;
334 struct object *obj = list->item;
335 const char *name = list->name;
336 int flags = (obj->flags & UNINTERESTING);
337 if (!obj->parsed)
338 obj = parse_object(obj->sha1);
339 obj = deref_tag(obj, NULL, 0);
340 if (!obj)
341 die("invalid object '%s' given.", name);
342 if (obj->type == OBJ_COMMIT)
343 obj = &((struct commit *)obj)->tree->object;
344 if (obj->type == OBJ_TREE) {
345 if (ARRAY_SIZE(ent) <= ents)
346 die("more than %d trees given: '%s'",
347 (int) ARRAY_SIZE(ent), name);
348 obj->flags |= flags;
349 ent[ents].item = obj;
350 ent[ents].name = name;
351 ents++;
352 continue;
353 }
354 if (obj->type == OBJ_BLOB) {
355 if (2 <= blobs)
356 die("more than two blobs given: '%s'", name);
357 hashcpy(blob[blobs].sha1, obj->sha1);
358 blob[blobs].name = name;
359 blob[blobs].mode = list->mode;
360 blobs++;
361 continue;
363 }
364 die("unhandled object '%s' given.", name);
365 }
366 if (rev.prune_data) {
367 const char **pathspec = rev.prune_data;
368 while (*pathspec) {
369 if (!path)
370 path = *pathspec;
371 paths++;
372 pathspec++;
373 }
374 }
376 /*
377 * Now, do the arguments look reasonable?
378 */
379 if (!ents) {
380 switch (blobs) {
381 case 0:
382 result = builtin_diff_files(&rev, argc, argv);
383 break;
384 case 1:
385 if (paths != 1)
386 usage(builtin_diff_usage);
387 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
388 break;
389 case 2:
390 if (paths)
391 usage(builtin_diff_usage);
392 result = builtin_diff_blobs(&rev, argc, argv, blob);
393 break;
394 default:
395 usage(builtin_diff_usage);
396 }
397 }
398 else if (blobs)
399 usage(builtin_diff_usage);
400 else if (ents == 1)
401 result = builtin_diff_index(&rev, argc, argv);
402 else if (ents == 2)
403 result = builtin_diff_tree(&rev, argc, argv, ent);
404 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
405 /* diff A...B where there is one sane merge base between
406 * A and B. We have ent[0] == merge-base, ent[1] == A,
407 * and ent[2] == B. Show diff between the base and B.
408 */
409 ent[1] = ent[2];
410 result = builtin_diff_tree(&rev, argc, argv, ent);
411 }
412 else
413 result = builtin_diff_combined(&rev, argc, argv,
414 ent, ents);
415 result = diff_result_code(&rev.diffopt, result);
416 if (1 < rev.diffopt.skip_stat_unmatch)
417 refresh_index_quietly();
418 return result;
419 }