55fb84c730e4141bdb53fec6602a2bdafc99f8fd
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 }
46 one = alloc_filespec(old_name);
47 two = alloc_filespec(new_name);
48 fill_filespec(one, old_sha1, old_mode);
49 fill_filespec(two, new_sha1, new_mode);
51 /* NEEDSWORK: shouldn't this part of diffopt??? */
52 diff_queue(&diff_queued_diff, one, two);
53 }
55 static int builtin_diff_b_f(struct rev_info *revs,
56 int argc, const char **argv,
57 struct blobinfo *blob,
58 const char *path)
59 {
60 /* Blob vs file in the working tree*/
61 struct stat st;
63 if (argc > 1)
64 usage(builtin_diff_usage);
66 if (lstat(path, &st))
67 die("'%s': %s", path, strerror(errno));
68 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
69 die("'%s': not a regular file or symlink", path);
71 if (blob[0].mode == S_IFINVALID)
72 blob[0].mode = canon_mode(st.st_mode);
74 stuff_change(&revs->diffopt,
75 blob[0].mode, canon_mode(st.st_mode),
76 blob[0].sha1, null_sha1,
77 path, path);
78 diffcore_std(&revs->diffopt);
79 diff_flush(&revs->diffopt);
80 return 0;
81 }
83 static int builtin_diff_blobs(struct rev_info *revs,
84 int argc, const char **argv,
85 struct blobinfo *blob)
86 {
87 unsigned mode = canon_mode(S_IFREG | 0644);
89 if (argc > 1)
90 usage(builtin_diff_usage);
92 if (blob[0].mode == S_IFINVALID)
93 blob[0].mode = mode;
95 if (blob[1].mode == S_IFINVALID)
96 blob[1].mode = mode;
98 stuff_change(&revs->diffopt,
99 blob[0].mode, blob[1].mode,
100 blob[0].sha1, blob[1].sha1,
101 blob[0].name, blob[1].name);
102 diffcore_std(&revs->diffopt);
103 diff_flush(&revs->diffopt);
104 return 0;
105 }
107 static int builtin_diff_index(struct rev_info *revs,
108 int argc, const char **argv)
109 {
110 int cached = 0;
111 while (1 < argc) {
112 const char *arg = argv[1];
113 if (!strcmp(arg, "--cached"))
114 cached = 1;
115 else
116 usage(builtin_diff_usage);
117 argv++; argc--;
118 }
119 /*
120 * Make sure there is one revision (i.e. pending object),
121 * and there is no revision filtering parameters.
122 */
123 if (revs->pending.nr != 1 ||
124 revs->max_count != -1 || revs->min_age != -1 ||
125 revs->max_age != -1)
126 usage(builtin_diff_usage);
127 if (read_cache() < 0) {
128 perror("read_cache");
129 return -1;
130 }
131 return run_diff_index(revs, cached);
132 }
134 static int builtin_diff_tree(struct rev_info *revs,
135 int argc, const char **argv,
136 struct object_array_entry *ent)
137 {
138 const unsigned char *(sha1[2]);
139 int swap = 0;
141 if (argc > 1)
142 usage(builtin_diff_usage);
144 /* We saw two trees, ent[0] and ent[1].
145 * if ent[1] is uninteresting, they are swapped
146 */
147 if (ent[1].item->flags & UNINTERESTING)
148 swap = 1;
149 sha1[swap] = ent[0].item->sha1;
150 sha1[1-swap] = ent[1].item->sha1;
151 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
152 log_tree_diff_flush(revs);
153 return 0;
154 }
156 static int builtin_diff_combined(struct rev_info *revs,
157 int argc, const char **argv,
158 struct object_array_entry *ent,
159 int ents)
160 {
161 const unsigned char (*parent)[20];
162 int i;
164 if (argc > 1)
165 usage(builtin_diff_usage);
167 if (!revs->dense_combined_merges && !revs->combine_merges)
168 revs->dense_combined_merges = revs->combine_merges = 1;
169 parent = xmalloc(ents * sizeof(*parent));
170 /* Again, the revs are all reverse */
171 for (i = 0; i < ents; i++)
172 hashcpy((unsigned char *)(parent + i),
173 ent[ents - 1 - i].item->sha1);
174 diff_tree_combined(parent[0], parent + 1, ents - 1,
175 revs->dense_combined_merges, revs);
176 return 0;
177 }
179 static void refresh_index_quietly(void)
180 {
181 struct lock_file *lock_file;
182 int fd;
184 lock_file = xcalloc(1, sizeof(struct lock_file));
185 fd = hold_locked_index(lock_file, 0);
186 if (fd < 0)
187 return;
188 discard_cache();
189 read_cache();
190 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
192 if (active_cache_changed &&
193 !write_cache(fd, active_cache, active_nr) && !close(fd))
194 commit_locked_index(lock_file);
196 rollback_lock_file(lock_file);
197 }
199 int cmd_diff(int argc, const char **argv, const char *prefix)
200 {
201 int i;
202 struct rev_info rev;
203 struct object_array_entry ent[100];
204 int ents = 0, blobs = 0, paths = 0;
205 const char *path = NULL;
206 struct blobinfo blob[2];
207 int nongit = 0;
208 int result = 0;
210 /*
211 * We could get N tree-ish in the rev.pending_objects list.
212 * Also there could be M blobs there, and P pathspecs.
213 *
214 * N=0, M=0:
215 * cache vs files (diff-files)
216 * N=0, M=2:
217 * compare two random blobs. P must be zero.
218 * N=0, M=1, P=1:
219 * compare a blob with a working tree file.
220 *
221 * N=1, M=0:
222 * tree vs cache (diff-index --cached)
223 *
224 * N=2, M=0:
225 * tree vs tree (diff-tree)
226 *
227 * Other cases are errors.
228 */
230 prefix = setup_git_directory_gently(&nongit);
231 git_config(git_diff_ui_config);
232 init_revisions(&rev, prefix);
233 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
235 if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
236 argc = 0;
237 else
238 argc = setup_revisions(argc, argv, &rev, NULL);
239 if (!rev.diffopt.output_format) {
240 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
241 if (diff_setup_done(&rev.diffopt) < 0)
242 die("diff_setup_done failed");
243 }
244 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
245 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
247 /* If the user asked for our exit code then don't start a
248 * pager or we would end up reporting its exit code instead.
249 */
250 if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
251 setup_pager();
253 /* Do we have --cached and not have a pending object, then
254 * default to HEAD by hand. Eek.
255 */
256 if (!rev.pending.nr) {
257 int i;
258 for (i = 1; i < argc; i++) {
259 const char *arg = argv[i];
260 if (!strcmp(arg, "--"))
261 break;
262 else if (!strcmp(arg, "--cached")) {
263 add_head_to_pending(&rev);
264 if (!rev.pending.nr)
265 die("No HEAD commit to compare with (yet)");
266 break;
267 }
268 }
269 }
271 for (i = 0; i < rev.pending.nr; i++) {
272 struct object_array_entry *list = rev.pending.objects+i;
273 struct object *obj = list->item;
274 const char *name = list->name;
275 int flags = (obj->flags & UNINTERESTING);
276 if (!obj->parsed)
277 obj = parse_object(obj->sha1);
278 obj = deref_tag(obj, NULL, 0);
279 if (!obj)
280 die("invalid object '%s' given.", name);
281 if (obj->type == OBJ_COMMIT)
282 obj = &((struct commit *)obj)->tree->object;
283 if (obj->type == OBJ_TREE) {
284 if (ARRAY_SIZE(ent) <= ents)
285 die("more than %d trees given: '%s'",
286 (int) ARRAY_SIZE(ent), name);
287 obj->flags |= flags;
288 ent[ents].item = obj;
289 ent[ents].name = name;
290 ents++;
291 continue;
292 }
293 if (obj->type == OBJ_BLOB) {
294 if (2 <= blobs)
295 die("more than two blobs given: '%s'", name);
296 hashcpy(blob[blobs].sha1, obj->sha1);
297 blob[blobs].name = name;
298 blob[blobs].mode = list->mode;
299 blobs++;
300 continue;
302 }
303 die("unhandled object '%s' given.", name);
304 }
305 if (rev.prune_data) {
306 const char **pathspec = rev.prune_data;
307 while (*pathspec) {
308 if (!path)
309 path = *pathspec;
310 paths++;
311 pathspec++;
312 }
313 }
315 /*
316 * Now, do the arguments look reasonable?
317 */
318 if (!ents) {
319 switch (blobs) {
320 case 0:
321 result = run_diff_files_cmd(&rev, argc, argv);
322 break;
323 case 1:
324 if (paths != 1)
325 usage(builtin_diff_usage);
326 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
327 break;
328 case 2:
329 if (paths)
330 usage(builtin_diff_usage);
331 result = builtin_diff_blobs(&rev, argc, argv, blob);
332 break;
333 default:
334 usage(builtin_diff_usage);
335 }
336 }
337 else if (blobs)
338 usage(builtin_diff_usage);
339 else if (ents == 1)
340 result = builtin_diff_index(&rev, argc, argv);
341 else if (ents == 2)
342 result = builtin_diff_tree(&rev, argc, argv, ent);
343 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
344 /* diff A...B where there is one sane merge base between
345 * A and B. We have ent[0] == merge-base, ent[1] == A,
346 * and ent[2] == B. Show diff between the base and B.
347 */
348 ent[1] = ent[2];
349 result = builtin_diff_tree(&rev, argc, argv, ent);
350 }
351 else
352 result = builtin_diff_combined(&rev, argc, argv,
353 ent, ents);
354 if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
355 result = DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
357 if (1 < rev.diffopt.skip_stat_unmatch)
358 refresh_index_quietly();
359 return result;
360 }