4 * Copyright (c) 2006 Junio C Hamano
16 /* NEEDSWORK: struct object has place for name but we _do_
17 * know mode when we extracted the blob out of a tree, which
21 unsigned char sha1[20];
25 static const char builtin_diff_usage[] =
26 "diff <options> <rev>{0,2} -- <path>*";
28 static int builtin_diff_files(struct rev_info *revs,
29 int argc, const char **argv)
33 const char *arg = argv[1];
34 if (!strcmp(arg, "--base"))
36 else if (!strcmp(arg, "--ours"))
38 else if (!strcmp(arg, "--theirs"))
40 else if (!strcmp(arg, "-q"))
42 else if (!strcmp(arg, "--raw"))
43 revs->diffopt.output_format = DIFF_FORMAT_RAW;
45 usage(builtin_diff_usage);
49 * Make sure there are NO revision (i.e. pending object) parameter,
50 * specified rev.max_count is reasonable (0 <= n <= 3), and
51 * there is no other revision filtering parameter.
53 if (revs->pending.nr ||
54 revs->min_age != -1 ||
55 revs->max_age != -1 ||
57 usage(builtin_diff_usage);
58 if (revs->max_count < 0 &&
59 (revs->diffopt.output_format == DIFF_FORMAT_PATCH))
60 revs->combine_merges = revs->dense_combined_merges = 1;
62 * Backward compatibility wart - "diff-files -s" used to
63 * defeat the common diff option "-s" which asked for
64 * DIFF_FORMAT_NO_OUTPUT.
66 if (revs->diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
67 revs->diffopt.output_format = DIFF_FORMAT_RAW;
68 return run_diff_files(revs, silent);
71 static void stuff_change(struct diff_options *opt,
72 unsigned old_mode, unsigned new_mode,
73 const unsigned char *old_sha1,
74 const unsigned char *new_sha1,
78 struct diff_filespec *one, *two;
80 if (memcmp(null_sha1, old_sha1, 20) &&
81 memcmp(null_sha1, new_sha1, 20) &&
82 !memcmp(old_sha1, new_sha1, 20))
85 if (opt->reverse_diff) {
87 const unsigned char *tmp_u;
89 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
90 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
91 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
93 one = alloc_filespec(old_name);
94 two = alloc_filespec(new_name);
95 fill_filespec(one, old_sha1, old_mode);
96 fill_filespec(two, new_sha1, new_mode);
98 /* NEEDSWORK: shouldn't this part of diffopt??? */
99 diff_queue(&diff_queued_diff, one, two);
102 static int builtin_diff_b_f(struct rev_info *revs,
103 int argc, const char **argv,
104 struct blobinfo *blob,
107 /* Blob vs file in the working tree*/
111 const char *arg = argv[1];
112 if (!strcmp(arg, "--raw"))
113 revs->diffopt.output_format = DIFF_FORMAT_RAW;
115 usage(builtin_diff_usage);
118 if (lstat(path, &st))
119 die("'%s': %s", path, strerror(errno));
120 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
121 die("'%s': not a regular file or symlink", path);
122 stuff_change(&revs->diffopt,
123 canon_mode(st.st_mode), canon_mode(st.st_mode),
124 blob[0].sha1, null_sha1,
126 diffcore_std(&revs->diffopt);
127 diff_flush(&revs->diffopt);
131 static int builtin_diff_blobs(struct rev_info *revs,
132 int argc, const char **argv,
133 struct blobinfo *blob)
135 /* Blobs: the arguments are reversed when setup_revisions()
138 unsigned mode = canon_mode(S_IFREG | 0644);
141 const char *arg = argv[1];
142 if (!strcmp(arg, "--raw"))
143 revs->diffopt.output_format = DIFF_FORMAT_RAW;
145 usage(builtin_diff_usage);
148 stuff_change(&revs->diffopt,
150 blob[1].sha1, blob[0].sha1,
151 blob[0].name, blob[0].name);
152 diffcore_std(&revs->diffopt);
153 diff_flush(&revs->diffopt);
157 static int builtin_diff_index(struct rev_info *revs,
158 int argc, const char **argv)
162 const char *arg = argv[1];
163 if (!strcmp(arg, "--cached"))
165 else if (!strcmp(arg, "--raw"))
166 revs->diffopt.output_format = DIFF_FORMAT_RAW;
168 usage(builtin_diff_usage);
172 * Make sure there is one revision (i.e. pending object),
173 * and there is no revision filtering parameters.
175 if (revs->pending.nr != 1 ||
176 revs->max_count != -1 || revs->min_age != -1 ||
178 usage(builtin_diff_usage);
179 return run_diff_index(revs, cached);
182 static int builtin_diff_tree(struct rev_info *revs,
183 int argc, const char **argv,
184 struct object_array_entry *ent)
186 const unsigned char *(sha1[2]);
189 const char *arg = argv[1];
190 if (!strcmp(arg, "--raw"))
191 revs->diffopt.output_format = DIFF_FORMAT_RAW;
193 usage(builtin_diff_usage);
197 /* We saw two trees, ent[0] and ent[1].
198 * if ent[1] is unintesting, they are swapped
200 if (ent[1].item->flags & UNINTERESTING)
202 sha1[swap] = ent[0].item->sha1;
203 sha1[1-swap] = ent[1].item->sha1;
204 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
205 log_tree_diff_flush(revs);
209 static int builtin_diff_combined(struct rev_info *revs,
210 int argc, const char **argv,
211 struct object_array_entry *ent,
214 const unsigned char (*parent)[20];
218 const char *arg = argv[1];
219 if (!strcmp(arg, "--raw"))
220 revs->diffopt.output_format = DIFF_FORMAT_RAW;
222 usage(builtin_diff_usage);
225 if (!revs->dense_combined_merges && !revs->combine_merges)
226 revs->dense_combined_merges = revs->combine_merges = 1;
227 parent = xmalloc(ents * sizeof(*parent));
228 /* Again, the revs are all reverse */
229 for (i = 0; i < ents; i++)
230 memcpy(parent + i, ent[ents - 1 - i].item->sha1, 20);
231 diff_tree_combined(parent[0], parent + 1, ents - 1,
232 revs->dense_combined_merges, revs);
236 void add_head(struct rev_info *revs)
238 unsigned char sha1[20];
240 if (get_sha1("HEAD", sha1))
242 obj = parse_object(sha1);
245 add_pending_object(revs, obj, "HEAD");
248 int cmd_diff(int argc, const char **argv, char **envp)
252 struct object_array_entry ent[100];
253 int ents = 0, blobs = 0, paths = 0;
254 const char *path = NULL;
255 struct blobinfo blob[2];
258 * We could get N tree-ish in the rev.pending_objects list.
259 * Also there could be M blobs there, and P pathspecs.
262 * cache vs files (diff-files)
264 * compare two random blobs. P must be zero.
266 * compare a blob with a working tree file.
269 * tree vs cache (diff-index --cached)
272 * tree vs tree (diff-tree)
274 * Other cases are errors.
277 git_config(git_diff_config);
278 init_revisions(&rev);
279 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
281 argc = setup_revisions(argc, argv, &rev, NULL);
282 /* Do we have --cached and not have a pending object, then
283 * default to HEAD by hand. Eek.
285 if (!rev.pending.nr) {
287 for (i = 1; i < argc; i++) {
288 const char *arg = argv[i];
289 if (!strcmp(arg, "--"))
291 else if (!strcmp(arg, "--cached")) {
298 for (i = 0; i < rev.pending.nr; i++) {
299 struct object_array_entry *list = rev.pending.objects+i;
300 struct object *obj = list->item;
301 const char *name = list->name;
302 int flags = (obj->flags & UNINTERESTING);
304 obj = parse_object(obj->sha1);
305 obj = deref_tag(obj, NULL, 0);
307 die("invalid object '%s' given.", name);
308 if (obj->type == TYPE_COMMIT)
309 obj = &((struct commit *)obj)->tree->object;
310 if (obj->type == TYPE_TREE) {
311 if (ARRAY_SIZE(ent) <= ents)
312 die("more than %d trees given: '%s'",
313 (int) ARRAY_SIZE(ent), name);
315 ent[ents].item = obj;
316 ent[ents].name = name;
320 if (obj->type == TYPE_BLOB) {
322 die("more than two blobs given: '%s'", name);
323 memcpy(blob[blobs].sha1, obj->sha1, 20);
324 blob[blobs].name = name;
329 die("unhandled object '%s' given.", name);
331 if (rev.prune_data) {
332 const char **pathspec = rev.prune_data;
342 * Now, do the arguments look reasonable?
347 return builtin_diff_files(&rev, argc, argv);
351 usage(builtin_diff_usage);
352 return builtin_diff_b_f(&rev, argc, argv, blob, path);
356 usage(builtin_diff_usage);
357 return builtin_diff_blobs(&rev, argc, argv, blob);
360 usage(builtin_diff_usage);
364 usage(builtin_diff_usage);
366 return builtin_diff_index(&rev, argc, argv);
368 return builtin_diff_tree(&rev, argc, argv, ent);
370 return builtin_diff_combined(&rev, argc, argv, ent, ents);
371 usage(builtin_diff_usage);