19fa6681858ee28f99bf6b3bad6adcda252fe3b7
1 /*
2 * Builtin "git diff"
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "blob.h"
9 #include "tag.h"
10 #include "diff.h"
11 #include "diffcore.h"
12 #include "revision.h"
13 #include "log-tree.h"
14 #include "builtin.h"
16 struct blobinfo {
17 unsigned char sha1[20];
18 const char *name;
19 unsigned mode;
20 };
22 static const char builtin_diff_usage[] =
23 "git-diff <options> <rev>{0,2} -- <path>*";
25 static void stuff_change(struct diff_options *opt,
26 unsigned old_mode, unsigned new_mode,
27 const unsigned char *old_sha1,
28 const unsigned char *new_sha1,
29 const char *old_name,
30 const char *new_name)
31 {
32 struct diff_filespec *one, *two;
34 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
35 !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
36 return;
38 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
39 unsigned tmp;
40 const unsigned char *tmp_u;
41 const char *tmp_c;
42 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
43 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
44 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
45 }
47 if (opt->prefix &&
48 (strncmp(old_name, opt->prefix, opt->prefix_length) ||
49 strncmp(new_name, opt->prefix, opt->prefix_length)))
50 return;
52 one = alloc_filespec(old_name);
53 two = alloc_filespec(new_name);
54 fill_filespec(one, old_sha1, old_mode);
55 fill_filespec(two, new_sha1, new_mode);
57 diff_queue(&diff_queued_diff, one, two);
58 }
60 static int builtin_diff_b_f(struct rev_info *revs,
61 int argc, const char **argv,
62 struct blobinfo *blob,
63 const char *path)
64 {
65 /* Blob vs file in the working tree*/
66 struct stat st;
68 if (argc > 1)
69 usage(builtin_diff_usage);
71 if (lstat(path, &st))
72 die("'%s': %s", path, strerror(errno));
73 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
74 die("'%s': not a regular file or symlink", path);
76 if (blob[0].mode == S_IFINVALID)
77 blob[0].mode = canon_mode(st.st_mode);
79 stuff_change(&revs->diffopt,
80 blob[0].mode, canon_mode(st.st_mode),
81 blob[0].sha1, null_sha1,
82 path, path);
83 diffcore_std(&revs->diffopt);
84 diff_flush(&revs->diffopt);
85 return 0;
86 }
88 static int builtin_diff_blobs(struct rev_info *revs,
89 int argc, const char **argv,
90 struct blobinfo *blob)
91 {
92 unsigned mode = canon_mode(S_IFREG | 0644);
94 if (argc > 1)
95 usage(builtin_diff_usage);
97 if (blob[0].mode == S_IFINVALID)
98 blob[0].mode = mode;
100 if (blob[1].mode == S_IFINVALID)
101 blob[1].mode = mode;
103 stuff_change(&revs->diffopt,
104 blob[0].mode, blob[1].mode,
105 blob[0].sha1, blob[1].sha1,
106 blob[0].name, blob[1].name);
107 diffcore_std(&revs->diffopt);
108 diff_flush(&revs->diffopt);
109 return 0;
110 }
112 static int builtin_diff_index(struct rev_info *revs,
113 int argc, const char **argv)
114 {
115 int cached = 0;
116 while (1 < argc) {
117 const char *arg = argv[1];
118 if (!strcmp(arg, "--cached"))
119 cached = 1;
120 else
121 usage(builtin_diff_usage);
122 argv++; argc--;
123 }
124 /*
125 * Make sure there is one revision (i.e. pending object),
126 * and there is no revision filtering parameters.
127 */
128 if (revs->pending.nr != 1 ||
129 revs->max_count != -1 || revs->min_age != -1 ||
130 revs->max_age != -1)
131 usage(builtin_diff_usage);
132 if (read_cache() < 0) {
133 perror("read_cache");
134 return -1;
135 }
136 return run_diff_index(revs, cached);
137 }
139 static int builtin_diff_tree(struct rev_info *revs,
140 int argc, const char **argv,
141 struct object_array_entry *ent)
142 {
143 const unsigned char *(sha1[2]);
144 int swap = 0;
146 if (argc > 1)
147 usage(builtin_diff_usage);
149 /* We saw two trees, ent[0] and ent[1].
150 * if ent[1] is uninteresting, they are swapped
151 */
152 if (ent[1].item->flags & UNINTERESTING)
153 swap = 1;
154 sha1[swap] = ent[0].item->sha1;
155 sha1[1-swap] = ent[1].item->sha1;
156 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
157 log_tree_diff_flush(revs);
158 return 0;
159 }
161 static int builtin_diff_combined(struct rev_info *revs,
162 int argc, const char **argv,
163 struct object_array_entry *ent,
164 int ents)
165 {
166 const unsigned char (*parent)[20];
167 int i;
169 if (argc > 1)
170 usage(builtin_diff_usage);
172 if (!revs->dense_combined_merges && !revs->combine_merges)
173 revs->dense_combined_merges = revs->combine_merges = 1;
174 parent = xmalloc(ents * sizeof(*parent));
175 /* Again, the revs are all reverse */
176 for (i = 0; i < ents; i++)
177 hashcpy((unsigned char *)(parent + i),
178 ent[ents - 1 - i].item->sha1);
179 diff_tree_combined(parent[0], parent + 1, ents - 1,
180 revs->dense_combined_merges, revs);
181 return 0;
182 }
184 static void refresh_index_quietly(void)
185 {
186 struct lock_file *lock_file;
187 int fd;
189 lock_file = xcalloc(1, sizeof(struct lock_file));
190 fd = hold_locked_index(lock_file, 0);
191 if (fd < 0)
192 return;
193 discard_cache();
194 read_cache();
195 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
197 if (active_cache_changed &&
198 !write_cache(fd, active_cache, active_nr))
199 commit_locked_index(lock_file);
201 rollback_lock_file(lock_file);
202 }
204 int cmd_diff(int argc, const char **argv, const char *prefix)
205 {
206 int i;
207 struct rev_info rev;
208 struct object_array_entry ent[100];
209 int ents = 0, blobs = 0, paths = 0;
210 const char *path = NULL;
211 struct blobinfo blob[2];
212 int nongit = 0;
213 int result = 0;
215 /*
216 * We could get N tree-ish in the rev.pending_objects list.
217 * Also there could be M blobs there, and P pathspecs.
218 *
219 * N=0, M=0:
220 * cache vs files (diff-files)
221 * N=0, M=2:
222 * compare two random blobs. P must be zero.
223 * N=0, M=1, P=1:
224 * compare a blob with a working tree file.
225 *
226 * N=1, M=0:
227 * tree vs cache (diff-index --cached)
228 *
229 * N=2, M=0:
230 * tree vs tree (diff-tree)
231 *
232 * Other cases are errors.
233 */
235 prefix = setup_git_directory_gently(&nongit);
236 git_config(git_diff_ui_config);
237 init_revisions(&rev, prefix);
238 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
240 if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
241 argc = 0;
242 else
243 argc = setup_revisions(argc, argv, &rev, NULL);
244 if (!rev.diffopt.output_format) {
245 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
246 if (diff_setup_done(&rev.diffopt) < 0)
247 die("diff_setup_done failed");
248 }
249 if (rev.diffopt.prefix && nongit) {
250 rev.diffopt.prefix = NULL;
251 rev.diffopt.prefix_length = 0;
252 }
253 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
254 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
256 /*
257 * If the user asked for our exit code then don't start a
258 * pager or we would end up reporting its exit code instead.
259 */
260 if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
261 setup_pager();
263 /* Do we have --cached and not have a pending object, then
264 * default to HEAD by hand. Eek.
265 */
266 if (!rev.pending.nr) {
267 int i;
268 for (i = 1; i < argc; i++) {
269 const char *arg = argv[i];
270 if (!strcmp(arg, "--"))
271 break;
272 else if (!strcmp(arg, "--cached")) {
273 add_head_to_pending(&rev);
274 if (!rev.pending.nr)
275 die("No HEAD commit to compare with (yet)");
276 break;
277 }
278 }
279 }
281 for (i = 0; i < rev.pending.nr; i++) {
282 struct object_array_entry *list = rev.pending.objects+i;
283 struct object *obj = list->item;
284 const char *name = list->name;
285 int flags = (obj->flags & UNINTERESTING);
286 if (!obj->parsed)
287 obj = parse_object(obj->sha1);
288 obj = deref_tag(obj, NULL, 0);
289 if (!obj)
290 die("invalid object '%s' given.", name);
291 if (obj->type == OBJ_COMMIT)
292 obj = &((struct commit *)obj)->tree->object;
293 if (obj->type == OBJ_TREE) {
294 if (ARRAY_SIZE(ent) <= ents)
295 die("more than %d trees given: '%s'",
296 (int) ARRAY_SIZE(ent), name);
297 obj->flags |= flags;
298 ent[ents].item = obj;
299 ent[ents].name = name;
300 ents++;
301 continue;
302 }
303 if (obj->type == OBJ_BLOB) {
304 if (2 <= blobs)
305 die("more than two blobs given: '%s'", name);
306 hashcpy(blob[blobs].sha1, obj->sha1);
307 blob[blobs].name = name;
308 blob[blobs].mode = list->mode;
309 blobs++;
310 continue;
312 }
313 die("unhandled object '%s' given.", name);
314 }
315 if (rev.prune_data) {
316 const char **pathspec = rev.prune_data;
317 while (*pathspec) {
318 if (!path)
319 path = *pathspec;
320 paths++;
321 pathspec++;
322 }
323 }
325 /*
326 * Now, do the arguments look reasonable?
327 */
328 if (!ents) {
329 switch (blobs) {
330 case 0:
331 result = run_diff_files_cmd(&rev, argc, argv);
332 break;
333 case 1:
334 if (paths != 1)
335 usage(builtin_diff_usage);
336 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
337 break;
338 case 2:
339 if (paths)
340 usage(builtin_diff_usage);
341 result = builtin_diff_blobs(&rev, argc, argv, blob);
342 break;
343 default:
344 usage(builtin_diff_usage);
345 }
346 }
347 else if (blobs)
348 usage(builtin_diff_usage);
349 else if (ents == 1)
350 result = builtin_diff_index(&rev, argc, argv);
351 else if (ents == 2)
352 result = builtin_diff_tree(&rev, argc, argv, ent);
353 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
354 /* diff A...B where there is one sane merge base between
355 * A and B. We have ent[0] == merge-base, ent[1] == A,
356 * and ent[2] == B. Show diff between the base and B.
357 */
358 ent[1] = ent[2];
359 result = builtin_diff_tree(&rev, argc, argv, ent);
360 }
361 else
362 result = builtin_diff_combined(&rev, argc, argv,
363 ent, ents);
364 result = diff_result_code(&rev.diffopt, result);
365 if (1 < rev.diffopt.skip_stat_unmatch)
366 refresh_index_quietly();
367 return result;
368 }