]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-diff.c
Merge branch 'fix'
[thirdparty/git.git] / builtin-diff.c
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"
15
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.
19 */
20 struct blobinfo {
21 unsigned char sha1[20];
22 const char *name;
23 };
24
25 static const char builtin_diff_usage[] =
26 "diff <options> <rev>{0,2} -- <path>*";
27
28 static int builtin_diff_files(struct rev_info *revs,
29 int argc, const char **argv)
30 {
31 int silent = 0;
32 while (1 < argc) {
33 const char *arg = argv[1];
34 if (!strcmp(arg, "--base"))
35 revs->max_count = 1;
36 else if (!strcmp(arg, "--ours"))
37 revs->max_count = 2;
38 else if (!strcmp(arg, "--theirs"))
39 revs->max_count = 3;
40 else if (!strcmp(arg, "-q"))
41 silent = 1;
42 else if (!strcmp(arg, "--raw"))
43 revs->diffopt.output_format = DIFF_FORMAT_RAW;
44 else
45 usage(builtin_diff_usage);
46 argv++; argc--;
47 }
48 /*
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.
52 */
53 if (revs->pending_objects ||
54 revs->min_age != -1 ||
55 revs->max_age != -1 ||
56 3 < revs->max_count)
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;
61 /*
62 * Backward compatibility wart - "diff-files -s" used to
63 * defeat the common diff option "-s" which asked for
64 * DIFF_FORMAT_NO_OUTPUT.
65 */
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);
69 }
70
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,
75 const char *old_name,
76 const char *new_name)
77 {
78 struct diff_filespec *one, *two;
79
80 if (memcmp(null_sha1, old_sha1, 20) &&
81 memcmp(null_sha1, new_sha1, 20) &&
82 !memcmp(old_sha1, new_sha1, 20))
83 return;
84
85 if (opt->reverse_diff) {
86 unsigned tmp;
87 const unsigned char *tmp_u;
88 const char *tmp_c;
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;
92 }
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);
97
98 /* NEEDSWORK: shouldn't this part of diffopt??? */
99 diff_queue(&diff_queued_diff, one, two);
100 }
101
102 static int builtin_diff_b_f(struct rev_info *revs,
103 int argc, const char **argv,
104 struct blobinfo *blob,
105 const char *path)
106 {
107 /* Blob vs file in the working tree*/
108 struct stat st;
109
110 while (1 < argc) {
111 const char *arg = argv[1];
112 if (!strcmp(arg, "--raw"))
113 revs->diffopt.output_format = DIFF_FORMAT_RAW;
114 else
115 usage(builtin_diff_usage);
116 argv++; argc--;
117 }
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,
125 blob[0].name, path);
126 diffcore_std(&revs->diffopt);
127 diff_flush(&revs->diffopt);
128 return 0;
129 }
130
131 static int builtin_diff_blobs(struct rev_info *revs,
132 int argc, const char **argv,
133 struct blobinfo *blob)
134 {
135 /* Blobs */
136 unsigned mode = canon_mode(S_IFREG | 0644);
137
138 while (1 < argc) {
139 const char *arg = argv[1];
140 if (!strcmp(arg, "--raw"))
141 revs->diffopt.output_format = DIFF_FORMAT_RAW;
142 else
143 usage(builtin_diff_usage);
144 argv++; argc--;
145 }
146 stuff_change(&revs->diffopt,
147 mode, mode,
148 blob[0].sha1, blob[1].sha1,
149 blob[1].name, blob[1].name);
150 diffcore_std(&revs->diffopt);
151 diff_flush(&revs->diffopt);
152 return 0;
153 }
154
155 static int builtin_diff_index(struct rev_info *revs,
156 int argc, const char **argv)
157 {
158 int cached = 0;
159 while (1 < argc) {
160 const char *arg = argv[1];
161 if (!strcmp(arg, "--cached"))
162 cached = 1;
163 else if (!strcmp(arg, "--raw"))
164 revs->diffopt.output_format = DIFF_FORMAT_RAW;
165 else
166 usage(builtin_diff_usage);
167 argv++; argc--;
168 }
169 /*
170 * Make sure there is one revision (i.e. pending object),
171 * and there is no revision filtering parameters.
172 */
173 if (!revs->pending_objects || revs->pending_objects->next ||
174 revs->max_count != -1 || revs->min_age != -1 ||
175 revs->max_age != -1)
176 usage(builtin_diff_usage);
177 return run_diff_index(revs, cached);
178 }
179
180 static int builtin_diff_tree(struct rev_info *revs,
181 int argc, const char **argv,
182 struct object_list *ent)
183 {
184 const unsigned char *(sha1[2]);
185 int swap = 1;
186 while (1 < argc) {
187 const char *arg = argv[1];
188 if (!strcmp(arg, "--raw"))
189 revs->diffopt.output_format = DIFF_FORMAT_RAW;
190 else
191 usage(builtin_diff_usage);
192 argv++; argc--;
193 }
194
195 /* We saw two trees, ent[0] and ent[1].
196 * unless ent[0] is unintesting, they are swapped
197 */
198 if (ent[0].item->flags & UNINTERESTING)
199 swap = 0;
200 sha1[swap] = ent[0].item->sha1;
201 sha1[1-swap] = ent[1].item->sha1;
202 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
203 log_tree_diff_flush(revs);
204 return 0;
205 }
206
207 static int builtin_diff_combined(struct rev_info *revs,
208 int argc, const char **argv,
209 struct object_list *ent,
210 int ents)
211 {
212 const unsigned char (*parent)[20];
213 int i;
214
215 while (1 < argc) {
216 const char *arg = argv[1];
217 if (!strcmp(arg, "--raw"))
218 revs->diffopt.output_format = DIFF_FORMAT_RAW;
219 else
220 usage(builtin_diff_usage);
221 argv++; argc--;
222 }
223 if (!revs->dense_combined_merges && !revs->combine_merges)
224 revs->dense_combined_merges = revs->combine_merges = 1;
225 parent = xmalloc(ents * sizeof(*parent));
226 /* Again, the revs are all reverse */
227 for (i = 0; i < ents; i++)
228 memcpy(parent + i, ent[ents - 1 - i].item->sha1, 20);
229 diff_tree_combined(parent[0], parent + 1, ents - 1,
230 revs->dense_combined_merges, revs);
231 return 0;
232 }
233
234 static void add_head(struct rev_info *revs)
235 {
236 unsigned char sha1[20];
237 struct object *obj;
238 if (get_sha1("HEAD", sha1))
239 return;
240 obj = parse_object(sha1);
241 if (!obj)
242 return;
243 add_object(obj, &revs->pending_objects, NULL, "HEAD");
244 }
245
246 int cmd_diff(int argc, const char **argv, char **envp)
247 {
248 struct rev_info rev;
249 struct object_list *list, ent[100];
250 int ents = 0, blobs = 0, paths = 0;
251 const char *path = NULL;
252 struct blobinfo blob[2];
253
254 /*
255 * We could get N tree-ish in the rev.pending_objects list.
256 * Also there could be M blobs there, and P pathspecs.
257 *
258 * N=0, M=0:
259 * cache vs files (diff-files)
260 * N=0, M=2:
261 * compare two random blobs. P must be zero.
262 * N=0, M=1, P=1:
263 * compare a blob with a working tree file.
264 *
265 * N=1, M=0:
266 * tree vs cache (diff-index --cached)
267 *
268 * N=2, M=0:
269 * tree vs tree (diff-tree)
270 *
271 * Other cases are errors.
272 */
273
274 git_config(git_diff_config);
275 init_revisions(&rev);
276 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
277
278 argc = setup_revisions(argc, argv, &rev, NULL);
279 /* Do we have --cached and not have a pending object, then
280 * default to HEAD by hand. Eek.
281 */
282 if (!rev.pending_objects) {
283 int i;
284 for (i = 1; i < argc; i++) {
285 const char *arg = argv[i];
286 if (!strcmp(arg, "--"))
287 break;
288 else if (!strcmp(arg, "--cached")) {
289 add_head(&rev);
290 break;
291 }
292 }
293 }
294
295 for (list = rev.pending_objects; list; list = list->next) {
296 struct object *obj = list->item;
297 const char *name = list->name;
298 int flags = (obj->flags & UNINTERESTING);
299 if (!obj->parsed)
300 obj = parse_object(obj->sha1);
301 obj = deref_tag(obj, NULL, 0);
302 if (!obj)
303 die("invalid object '%s' given.", name);
304 if (!strcmp(obj->type, commit_type))
305 obj = &((struct commit *)obj)->tree->object;
306 if (!strcmp(obj->type, tree_type)) {
307 if (ARRAY_SIZE(ent) <= ents)
308 die("more than %d trees given: '%s'",
309 (int) ARRAY_SIZE(ent), name);
310 obj->flags |= flags;
311 ent[ents].item = obj;
312 ent[ents].name = name;
313 ents++;
314 continue;
315 }
316 if (!strcmp(obj->type, blob_type)) {
317 if (2 <= blobs)
318 die("more than two blobs given: '%s'", name);
319 memcpy(blob[blobs].sha1, obj->sha1, 20);
320 blob[blobs].name = name;
321 blobs++;
322 continue;
323
324 }
325 die("unhandled object '%s' given.", name);
326 }
327 if (rev.prune_data) {
328 const char **pathspec = rev.prune_data;
329 while (*pathspec) {
330 if (!path)
331 path = *pathspec;
332 paths++;
333 pathspec++;
334 }
335 }
336
337 /*
338 * Now, do the arguments look reasonable?
339 */
340 if (!ents) {
341 switch (blobs) {
342 case 0:
343 return builtin_diff_files(&rev, argc, argv);
344 break;
345 case 1:
346 if (paths != 1)
347 usage(builtin_diff_usage);
348 return builtin_diff_b_f(&rev, argc, argv, blob, path);
349 break;
350 case 2:
351 if (paths)
352 usage(builtin_diff_usage);
353 return builtin_diff_blobs(&rev, argc, argv, blob);
354 break;
355 default:
356 usage(builtin_diff_usage);
357 }
358 }
359 else if (blobs)
360 usage(builtin_diff_usage);
361 else if (ents == 1)
362 return builtin_diff_index(&rev, argc, argv);
363 else if (ents == 2)
364 return builtin_diff_tree(&rev, argc, argv, ent);
365 else
366 return builtin_diff_combined(&rev, argc, argv, ent, ents);
367 usage(builtin_diff_usage);
368 }