Merge branch 'master' of git://repo.or.cz/git/fastimport
[git/git-svn.git] / builtin-diff.c
blob4efbb8237bd49e8717a42833b2d9b2db064b45ac
1 /*
2 * Builtin "git diff"
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 /* NEEDSWORK: struct object has place for name but we _do_
17 * know mode when we extracted the blob out of a tree, which
18 * we currently lose.
20 struct blobinfo {
21 unsigned char sha1[20];
22 const char *name;
25 static const char builtin_diff_usage[] =
26 "git-diff <options> <rev>{0,2} -- <path>*";
28 static void stuff_change(struct diff_options *opt,
29 unsigned old_mode, unsigned new_mode,
30 const unsigned char *old_sha1,
31 const unsigned char *new_sha1,
32 const char *old_name,
33 const char *new_name)
35 struct diff_filespec *one, *two;
37 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
38 !hashcmp(old_sha1, new_sha1))
39 return;
41 if (opt->reverse_diff) {
42 unsigned tmp;
43 const unsigned char *tmp_u;
44 const char *tmp_c;
45 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
46 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
47 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
49 one = alloc_filespec(old_name);
50 two = alloc_filespec(new_name);
51 fill_filespec(one, old_sha1, old_mode);
52 fill_filespec(two, new_sha1, new_mode);
54 /* NEEDSWORK: shouldn't this part of diffopt??? */
55 diff_queue(&diff_queued_diff, one, two);
58 static int builtin_diff_b_f(struct rev_info *revs,
59 int argc, const char **argv,
60 struct blobinfo *blob,
61 const char *path)
63 /* Blob vs file in the working tree*/
64 struct stat st;
66 if (argc > 1)
67 usage(builtin_diff_usage);
69 if (lstat(path, &st))
70 die("'%s': %s", path, strerror(errno));
71 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
72 die("'%s': not a regular file or symlink", path);
73 stuff_change(&revs->diffopt,
74 canon_mode(st.st_mode), canon_mode(st.st_mode),
75 blob[0].sha1, null_sha1,
76 path, path);
77 diffcore_std(&revs->diffopt);
78 diff_flush(&revs->diffopt);
79 return 0;
82 static int builtin_diff_blobs(struct rev_info *revs,
83 int argc, const char **argv,
84 struct blobinfo *blob)
86 unsigned mode = canon_mode(S_IFREG | 0644);
88 if (argc > 1)
89 usage(builtin_diff_usage);
91 stuff_change(&revs->diffopt,
92 mode, mode,
93 blob[0].sha1, blob[1].sha1,
94 blob[0].name, blob[1].name);
95 diffcore_std(&revs->diffopt);
96 diff_flush(&revs->diffopt);
97 return 0;
100 static int builtin_diff_index(struct rev_info *revs,
101 int argc, const char **argv)
103 int cached = 0;
104 while (1 < argc) {
105 const char *arg = argv[1];
106 if (!strcmp(arg, "--cached"))
107 cached = 1;
108 else
109 usage(builtin_diff_usage);
110 argv++; argc--;
113 * Make sure there is one revision (i.e. pending object),
114 * and there is no revision filtering parameters.
116 if (revs->pending.nr != 1 ||
117 revs->max_count != -1 || revs->min_age != -1 ||
118 revs->max_age != -1)
119 usage(builtin_diff_usage);
120 if (read_cache() < 0) {
121 perror("read_cache");
122 return -1;
124 return run_diff_index(revs, cached);
127 static int builtin_diff_tree(struct rev_info *revs,
128 int argc, const char **argv,
129 struct object_array_entry *ent)
131 const unsigned char *(sha1[2]);
132 int swap = 0;
134 if (argc > 1)
135 usage(builtin_diff_usage);
137 /* We saw two trees, ent[0] and ent[1].
138 * if ent[1] is uninteresting, they are swapped
140 if (ent[1].item->flags & UNINTERESTING)
141 swap = 1;
142 sha1[swap] = ent[0].item->sha1;
143 sha1[1-swap] = ent[1].item->sha1;
144 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
145 log_tree_diff_flush(revs);
146 return 0;
149 static int builtin_diff_combined(struct rev_info *revs,
150 int argc, const char **argv,
151 struct object_array_entry *ent,
152 int ents)
154 const unsigned char (*parent)[20];
155 int i;
157 if (argc > 1)
158 usage(builtin_diff_usage);
160 if (!revs->dense_combined_merges && !revs->combine_merges)
161 revs->dense_combined_merges = revs->combine_merges = 1;
162 parent = xmalloc(ents * sizeof(*parent));
163 /* Again, the revs are all reverse */
164 for (i = 0; i < ents; i++)
165 hashcpy((unsigned char *)(parent + i),
166 ent[ents - 1 - i].item->sha1);
167 diff_tree_combined(parent[0], parent + 1, ents - 1,
168 revs->dense_combined_merges, revs);
169 return 0;
172 void add_head(struct rev_info *revs)
174 unsigned char sha1[20];
175 struct object *obj;
176 if (get_sha1("HEAD", sha1))
177 return;
178 obj = parse_object(sha1);
179 if (!obj)
180 return;
181 add_pending_object(revs, obj, "HEAD");
184 int cmd_diff(int argc, const char **argv, const char *prefix)
186 int i;
187 struct rev_info rev;
188 struct object_array_entry ent[100];
189 int ents = 0, blobs = 0, paths = 0;
190 const char *path = NULL;
191 struct blobinfo blob[2];
192 int nongit = 0;
195 * We could get N tree-ish in the rev.pending_objects list.
196 * Also there could be M blobs there, and P pathspecs.
198 * N=0, M=0:
199 * cache vs files (diff-files)
200 * N=0, M=2:
201 * compare two random blobs. P must be zero.
202 * N=0, M=1, P=1:
203 * compare a blob with a working tree file.
205 * N=1, M=0:
206 * tree vs cache (diff-index --cached)
208 * N=2, M=0:
209 * tree vs tree (diff-tree)
211 * Other cases are errors.
214 prefix = setup_git_directory_gently(&nongit);
215 git_config(git_diff_ui_config);
216 init_revisions(&rev, prefix);
218 if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
219 argc = 0;
220 else
221 argc = setup_revisions(argc, argv, &rev, NULL);
222 if (!rev.diffopt.output_format) {
223 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
224 if (diff_setup_done(&rev.diffopt) < 0)
225 die("diff_setup_done failed");
228 /* Do we have --cached and not have a pending object, then
229 * default to HEAD by hand. Eek.
231 if (!rev.pending.nr) {
232 int i;
233 for (i = 1; i < argc; i++) {
234 const char *arg = argv[i];
235 if (!strcmp(arg, "--"))
236 break;
237 else if (!strcmp(arg, "--cached")) {
238 add_head(&rev);
239 if (!rev.pending.nr)
240 die("No HEAD commit to compare with (yet)");
241 break;
246 for (i = 0; i < rev.pending.nr; i++) {
247 struct object_array_entry *list = rev.pending.objects+i;
248 struct object *obj = list->item;
249 const char *name = list->name;
250 int flags = (obj->flags & UNINTERESTING);
251 if (!obj->parsed)
252 obj = parse_object(obj->sha1);
253 obj = deref_tag(obj, NULL, 0);
254 if (!obj)
255 die("invalid object '%s' given.", name);
256 if (obj->type == OBJ_COMMIT)
257 obj = &((struct commit *)obj)->tree->object;
258 if (obj->type == OBJ_TREE) {
259 if (ARRAY_SIZE(ent) <= ents)
260 die("more than %d trees given: '%s'",
261 (int) ARRAY_SIZE(ent), name);
262 obj->flags |= flags;
263 ent[ents].item = obj;
264 ent[ents].name = name;
265 ents++;
266 continue;
268 if (obj->type == OBJ_BLOB) {
269 if (2 <= blobs)
270 die("more than two blobs given: '%s'", name);
271 hashcpy(blob[blobs].sha1, obj->sha1);
272 blob[blobs].name = name;
273 blobs++;
274 continue;
277 die("unhandled object '%s' given.", name);
279 if (rev.prune_data) {
280 const char **pathspec = rev.prune_data;
281 while (*pathspec) {
282 if (!path)
283 path = *pathspec;
284 paths++;
285 pathspec++;
290 * Now, do the arguments look reasonable?
292 if (!ents) {
293 switch (blobs) {
294 case 0:
295 return run_diff_files_cmd(&rev, argc, argv);
296 break;
297 case 1:
298 if (paths != 1)
299 usage(builtin_diff_usage);
300 return builtin_diff_b_f(&rev, argc, argv, blob, path);
301 break;
302 case 2:
303 if (paths)
304 usage(builtin_diff_usage);
305 return builtin_diff_blobs(&rev, argc, argv, blob);
306 break;
307 default:
308 usage(builtin_diff_usage);
311 else if (blobs)
312 usage(builtin_diff_usage);
313 else if (ents == 1)
314 return builtin_diff_index(&rev, argc, argv);
315 else if (ents == 2)
316 return builtin_diff_tree(&rev, argc, argv, ent);
317 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
318 /* diff A...B where there is one sane merge base between
319 * A and B. We have ent[0] == merge-base, ent[1] == A,
320 * and ent[2] == B. Show diff between the base and B.
322 ent[1] = ent[2];
323 return builtin_diff_tree(&rev, argc, argv, ent);
325 else
326 return builtin_diff_combined(&rev, argc, argv,
327 ent, ents);
328 usage(builtin_diff_usage);