4 * Copyright (c) 2006 Junio C Hamano
17 unsigned char sha1[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,
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))
38 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
40 const unsigned char *tmp_u;
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;
48 (strncmp(old_name, opt->prefix, opt->prefix_length) ||
49 strncmp(new_name, opt->prefix, opt->prefix_length)))
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);
60 static int builtin_diff_b_f(struct rev_info *revs,
61 int argc, const char **argv,
62 struct blobinfo *blob,
65 /* Blob vs file in the working tree*/
69 usage(builtin_diff_usage);
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,
83 diffcore_std(&revs->diffopt);
84 diff_flush(&revs->diffopt);
88 static int builtin_diff_blobs(struct rev_info *revs,
89 int argc, const char **argv,
90 struct blobinfo *blob)
92 unsigned mode = canon_mode(S_IFREG | 0644);
95 usage(builtin_diff_usage);
97 if (blob[0].mode == S_IFINVALID)
100 if (blob[1].mode == S_IFINVALID)
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);
112 static int builtin_diff_index(struct rev_info *revs,
113 int argc, const char **argv)
117 const char *arg = argv[1];
118 if (!strcmp(arg, "--cached"))
121 usage(builtin_diff_usage);
125 * Make sure there is one revision (i.e. pending object),
126 * and there is no revision filtering parameters.
128 if (revs->pending.nr != 1 ||
129 revs->max_count != -1 || revs->min_age != -1 ||
131 usage(builtin_diff_usage);
132 if (read_cache() < 0) {
133 perror("read_cache");
136 return run_diff_index(revs, cached);
139 static int builtin_diff_tree(struct rev_info *revs,
140 int argc, const char **argv,
141 struct object_array_entry *ent)
143 const unsigned char *(sha1[2]);
147 usage(builtin_diff_usage);
149 /* We saw two trees, ent[0] and ent[1].
150 * if ent[1] is uninteresting, they are swapped
152 if (ent[1].item->flags & UNINTERESTING)
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);
161 static int builtin_diff_combined(struct rev_info *revs,
162 int argc, const char **argv,
163 struct object_array_entry *ent,
166 const unsigned char (*parent)[20];
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);
184 static void refresh_index_quietly(void)
186 struct lock_file *lock_file;
189 lock_file = xcalloc(1, sizeof(struct lock_file));
190 fd = hold_locked_index(lock_file, 0);
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);
204 int cmd_diff(int argc, const char **argv, const char *prefix)
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];
216 * We could get N tree-ish in the rev.pending_objects list.
217 * Also there could be M blobs there, and P pathspecs.
220 * cache vs files (diff-files)
222 * compare two random blobs. P must be zero.
224 * compare a blob with a working tree file.
227 * tree vs cache (diff-index --cached)
230 * tree vs tree (diff-tree)
232 * Other cases are errors.
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))
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");
249 if (rev.diffopt.prefix && nongit) {
250 rev.diffopt.prefix = NULL;
251 rev.diffopt.prefix_length = 0;
253 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
254 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
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.
260 if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
263 /* Do we have --cached and not have a pending object, then
264 * default to HEAD by hand. Eek.
266 if (!rev.pending.nr) {
268 for (i = 1; i < argc; i++) {
269 const char *arg = argv[i];
270 if (!strcmp(arg, "--"))
272 else if (!strcmp(arg, "--cached")) {
273 add_head_to_pending(&rev);
275 die("No HEAD commit to compare with (yet)");
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);
287 obj = parse_object(obj->sha1);
288 obj = deref_tag(obj, NULL, 0);
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);
298 ent[ents].item = obj;
299 ent[ents].name = name;
303 if (obj->type == OBJ_BLOB) {
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;
313 die("unhandled object '%s' given.", name);
315 if (rev.prune_data) {
316 const char **pathspec = rev.prune_data;
326 * Now, do the arguments look reasonable?
331 result = run_diff_files_cmd(&rev, argc, argv);
335 usage(builtin_diff_usage);
336 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
340 usage(builtin_diff_usage);
341 result = builtin_diff_blobs(&rev, argc, argv, blob);
344 usage(builtin_diff_usage);
348 usage(builtin_diff_usage);
350 result = builtin_diff_index(&rev, argc, argv);
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.
359 result = builtin_diff_tree(&rev, argc, argv, ent);
362 result = builtin_diff_combined(&rev, argc, argv,
364 result = diff_result_code(&rev.diffopt, result);
365 if (1 < rev.diffopt.skip_stat_unmatch)
366 refresh_index_quietly();