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 (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;
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);
55 static int builtin_diff_b_f(struct rev_info *revs,
56 int argc, const char **argv,
57 struct blobinfo *blob,
60 /* Blob vs file in the working tree*/
64 usage(builtin_diff_usage);
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,
78 diffcore_std(&revs->diffopt);
79 diff_flush(&revs->diffopt);
83 static int builtin_diff_blobs(struct rev_info *revs,
84 int argc, const char **argv,
85 struct blobinfo *blob)
87 unsigned mode = canon_mode(S_IFREG | 0644);
90 usage(builtin_diff_usage);
92 if (blob[0].mode == S_IFINVALID)
95 if (blob[1].mode == S_IFINVALID)
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);
107 static int builtin_diff_index(struct rev_info *revs,
108 int argc, const char **argv)
112 const char *arg = argv[1];
113 if (!strcmp(arg, "--cached"))
116 usage(builtin_diff_usage);
120 * Make sure there is one revision (i.e. pending object),
121 * and there is no revision filtering parameters.
123 if (revs->pending.nr != 1 ||
124 revs->max_count != -1 || revs->min_age != -1 ||
126 usage(builtin_diff_usage);
127 if (read_cache() < 0) {
128 perror("read_cache");
131 return run_diff_index(revs, cached);
134 static int builtin_diff_tree(struct rev_info *revs,
135 int argc, const char **argv,
136 struct object_array_entry *ent)
138 const unsigned char *(sha1[2]);
142 usage(builtin_diff_usage);
144 /* We saw two trees, ent[0] and ent[1].
145 * if ent[1] is uninteresting, they are swapped
147 if (ent[1].item->flags & UNINTERESTING)
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);
156 static int builtin_diff_combined(struct rev_info *revs,
157 int argc, const char **argv,
158 struct object_array_entry *ent,
161 const unsigned char (*parent)[20];
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);
179 void add_head(struct rev_info *revs)
181 unsigned char sha1[20];
183 if (get_sha1("HEAD", sha1))
185 obj = parse_object(sha1);
188 add_pending_object(revs, obj, "HEAD");
191 static void refresh_index_quietly(void)
193 struct lock_file *lock_file;
196 lock_file = xcalloc(1, sizeof(struct lock_file));
197 fd = hold_locked_index(lock_file, 0);
202 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
203 if (active_cache_changed) {
204 if (write_cache(fd, active_cache, active_nr) ||
206 commit_locked_index(lock_file))
208 * silently ignore it -- we haven't mucked
209 * with the real index.
212 rollback_lock_file(lock_file);
215 int cmd_diff(int argc, const char **argv, const char *prefix)
219 struct object_array_entry ent[100];
220 int ents = 0, blobs = 0, paths = 0;
221 const char *path = NULL;
222 struct blobinfo blob[2];
227 * We could get N tree-ish in the rev.pending_objects list.
228 * Also there could be M blobs there, and P pathspecs.
231 * cache vs files (diff-files)
233 * compare two random blobs. P must be zero.
235 * compare a blob with a working tree file.
238 * tree vs cache (diff-index --cached)
241 * tree vs tree (diff-tree)
243 * Other cases are errors.
246 prefix = setup_git_directory_gently(&nongit);
247 git_config(git_diff_ui_config);
248 init_revisions(&rev, prefix);
249 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
251 if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
254 argc = setup_revisions(argc, argv, &rev, NULL);
255 if (!rev.diffopt.output_format) {
256 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
257 if (diff_setup_done(&rev.diffopt) < 0)
258 die("diff_setup_done failed");
260 rev.diffopt.allow_external = 1;
261 rev.diffopt.recursive = 1;
263 /* If the user asked for our exit code then don't start a
264 * pager or we would end up reporting its exit code instead.
266 if (!rev.diffopt.exit_with_status)
269 /* Do we have --cached and not have a pending object, then
270 * default to HEAD by hand. Eek.
272 if (!rev.pending.nr) {
274 for (i = 1; i < argc; i++) {
275 const char *arg = argv[i];
276 if (!strcmp(arg, "--"))
278 else if (!strcmp(arg, "--cached")) {
281 die("No HEAD commit to compare with (yet)");
287 for (i = 0; i < rev.pending.nr; i++) {
288 struct object_array_entry *list = rev.pending.objects+i;
289 struct object *obj = list->item;
290 const char *name = list->name;
291 int flags = (obj->flags & UNINTERESTING);
293 obj = parse_object(obj->sha1);
294 obj = deref_tag(obj, NULL, 0);
296 die("invalid object '%s' given.", name);
297 if (obj->type == OBJ_COMMIT)
298 obj = &((struct commit *)obj)->tree->object;
299 if (obj->type == OBJ_TREE) {
300 if (ARRAY_SIZE(ent) <= ents)
301 die("more than %d trees given: '%s'",
302 (int) ARRAY_SIZE(ent), name);
304 ent[ents].item = obj;
305 ent[ents].name = name;
309 if (obj->type == OBJ_BLOB) {
311 die("more than two blobs given: '%s'", name);
312 hashcpy(blob[blobs].sha1, obj->sha1);
313 blob[blobs].name = name;
314 blob[blobs].mode = list->mode;
319 die("unhandled object '%s' given.", name);
321 if (rev.prune_data) {
322 const char **pathspec = rev.prune_data;
332 * Now, do the arguments look reasonable?
337 result = run_diff_files_cmd(&rev, argc, argv);
341 usage(builtin_diff_usage);
342 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
346 usage(builtin_diff_usage);
347 result = builtin_diff_blobs(&rev, argc, argv, blob);
350 usage(builtin_diff_usage);
354 usage(builtin_diff_usage);
356 result = builtin_diff_index(&rev, argc, argv);
358 result = builtin_diff_tree(&rev, argc, argv, ent);
359 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
360 /* diff A...B where there is one sane merge base between
361 * A and B. We have ent[0] == merge-base, ent[1] == A,
362 * and ent[2] == B. Show diff between the base and B.
365 result = builtin_diff_tree(&rev, argc, argv, ent);
368 result = builtin_diff_combined(&rev, argc, argv,
370 if (rev.diffopt.exit_with_status)
371 result = rev.diffopt.has_changes;
373 if (1 < rev.diffopt.skip_stat_unmatch)
374 refresh_index_quietly();