blob: a6590205e8f746304f3d06d3e328a05bf2bf954e [file] [log] [blame]
Junio C Hamano65056022006-04-28 23:20:52 -07001/*
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 */
20struct blobinfo {
21 unsigned char sha1[20];
22 const char *name;
23};
24
25static const char builtin_diff_usage[] =
Ramsay Allan Jones15e593e2006-08-03 16:38:39 +010026"git-diff <options> <rev>{0,2} -- <path>*";
Junio C Hamano65056022006-04-28 23:20:52 -070027
28static 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;
Junio C Hamano65056022006-04-28 23:20:52 -070042 else
43 usage(builtin_diff_usage);
44 argv++; argc--;
45 }
46 /*
47 * Make sure there are NO revision (i.e. pending object) parameter,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -070048 * specified rev.max_count is reasonable (0 <= n <= 3), and
49 * there is no other revision filtering parameter.
Junio C Hamano65056022006-04-28 23:20:52 -070050 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -070051 if (revs->pending.nr ||
Junio C Hamano65056022006-04-28 23:20:52 -070052 revs->min_age != -1 ||
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -070053 revs->max_age != -1 ||
54 3 < revs->max_count)
Junio C Hamano65056022006-04-28 23:20:52 -070055 usage(builtin_diff_usage);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -070056 if (revs->max_count < 0 &&
Timo Hirvonenc6744342006-06-24 20:21:53 +030057 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -070058 revs->combine_merges = revs->dense_combined_merges = 1;
Junio C Hamano65056022006-04-28 23:20:52 -070059 return run_diff_files(revs, silent);
60}
61
62static void stuff_change(struct diff_options *opt,
63 unsigned old_mode, unsigned new_mode,
64 const unsigned char *old_sha1,
65 const unsigned char *new_sha1,
66 const char *old_name,
67 const char *new_name)
68{
69 struct diff_filespec *one, *two;
70
David Rientjes0bef57e2006-08-15 13:37:19 -070071 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
David Rientjesa89fccd2006-08-17 11:54:57 -070072 !hashcmp(old_sha1, new_sha1))
Junio C Hamano65056022006-04-28 23:20:52 -070073 return;
74
75 if (opt->reverse_diff) {
76 unsigned tmp;
Peter Hagervalld92f1dc2006-05-07 16:50:47 +020077 const unsigned char *tmp_u;
Junio C Hamano65056022006-04-28 23:20:52 -070078 const char *tmp_c;
79 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
80 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
81 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
82 }
83 one = alloc_filespec(old_name);
84 two = alloc_filespec(new_name);
85 fill_filespec(one, old_sha1, old_mode);
86 fill_filespec(two, new_sha1, new_mode);
87
88 /* NEEDSWORK: shouldn't this part of diffopt??? */
89 diff_queue(&diff_queued_diff, one, two);
90}
91
92static int builtin_diff_b_f(struct rev_info *revs,
93 int argc, const char **argv,
94 struct blobinfo *blob,
95 const char *path)
96{
97 /* Blob vs file in the working tree*/
98 struct stat st;
99
Timo Hirvonena6107862006-06-24 20:23:06 +0300100 if (argc > 1)
101 usage(builtin_diff_usage);
102
Junio C Hamano65056022006-04-28 23:20:52 -0700103 if (lstat(path, &st))
104 die("'%s': %s", path, strerror(errno));
105 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
106 die("'%s': not a regular file or symlink", path);
107 stuff_change(&revs->diffopt,
108 canon_mode(st.st_mode), canon_mode(st.st_mode),
109 blob[0].sha1, null_sha1,
Junio C Hamano065e0b12006-05-18 14:35:37 -0700110 path, path);
Junio C Hamano65056022006-04-28 23:20:52 -0700111 diffcore_std(&revs->diffopt);
112 diff_flush(&revs->diffopt);
113 return 0;
114}
115
116static int builtin_diff_blobs(struct rev_info *revs,
117 int argc, const char **argv,
118 struct blobinfo *blob)
119{
Junio C Hamano65056022006-04-28 23:20:52 -0700120 unsigned mode = canon_mode(S_IFREG | 0644);
121
Timo Hirvonena6107862006-06-24 20:23:06 +0300122 if (argc > 1)
123 usage(builtin_diff_usage);
124
Junio C Hamano65056022006-04-28 23:20:52 -0700125 stuff_change(&revs->diffopt,
126 mode, mode,
Junio C Hamanof82cd3c2006-08-03 11:50:10 -0700127 blob[0].sha1, blob[1].sha1,
Junio C Hamano53dd8a92006-08-03 11:57:11 -0700128 blob[0].name, blob[1].name);
Junio C Hamano65056022006-04-28 23:20:52 -0700129 diffcore_std(&revs->diffopt);
130 diff_flush(&revs->diffopt);
131 return 0;
132}
133
134static int builtin_diff_index(struct rev_info *revs,
135 int argc, const char **argv)
136{
137 int cached = 0;
138 while (1 < argc) {
139 const char *arg = argv[1];
140 if (!strcmp(arg, "--cached"))
141 cached = 1;
Junio C Hamano65056022006-04-28 23:20:52 -0700142 else
143 usage(builtin_diff_usage);
144 argv++; argc--;
145 }
146 /*
147 * Make sure there is one revision (i.e. pending object),
148 * and there is no revision filtering parameters.
149 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700150 if (revs->pending.nr != 1 ||
Junio C Hamano65056022006-04-28 23:20:52 -0700151 revs->max_count != -1 || revs->min_age != -1 ||
152 revs->max_age != -1)
153 usage(builtin_diff_usage);
154 return run_diff_index(revs, cached);
155}
156
157static int builtin_diff_tree(struct rev_info *revs,
158 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700159 struct object_array_entry *ent)
Junio C Hamano65056022006-04-28 23:20:52 -0700160{
Junio C Hamano65056022006-04-28 23:20:52 -0700161 const unsigned char *(sha1[2]);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700162 int swap = 0;
Timo Hirvonena6107862006-06-24 20:23:06 +0300163
164 if (argc > 1)
165 usage(builtin_diff_usage);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700166
167 /* We saw two trees, ent[0] and ent[1].
Pavel Roskin82e5a822006-07-10 01:50:18 -0400168 * if ent[1] is uninteresting, they are swapped
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700169 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700170 if (ent[1].item->flags & UNINTERESTING)
171 swap = 1;
Junio C Hamano65056022006-04-28 23:20:52 -0700172 sha1[swap] = ent[0].item->sha1;
173 sha1[1-swap] = ent[1].item->sha1;
174 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
175 log_tree_diff_flush(revs);
176 return 0;
177}
178
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700179static int builtin_diff_combined(struct rev_info *revs,
180 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700181 struct object_array_entry *ent,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700182 int ents)
183{
184 const unsigned char (*parent)[20];
185 int i;
186
Timo Hirvonena6107862006-06-24 20:23:06 +0300187 if (argc > 1)
188 usage(builtin_diff_usage);
189
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700190 if (!revs->dense_combined_merges && !revs->combine_merges)
191 revs->dense_combined_merges = revs->combine_merges = 1;
192 parent = xmalloc(ents * sizeof(*parent));
193 /* Again, the revs are all reverse */
194 for (i = 0; i < ents; i++)
Shawn Pearcee7024962006-08-23 02:49:00 -0400195 hashcpy((unsigned char*)parent + i, ent[ents - 1 - i].item->sha1);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700196 diff_tree_combined(parent[0], parent + 1, ents - 1,
197 revs->dense_combined_merges, revs);
198 return 0;
199}
200
Johannes Schindeline686eb92006-05-06 22:56:38 +0200201void add_head(struct rev_info *revs)
Junio C Hamano65056022006-04-28 23:20:52 -0700202{
203 unsigned char sha1[20];
204 struct object *obj;
205 if (get_sha1("HEAD", sha1))
206 return;
207 obj = parse_object(sha1);
208 if (!obj)
209 return;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700210 add_pending_object(revs, obj, "HEAD");
Junio C Hamano65056022006-04-28 23:20:52 -0700211}
212
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700213int cmd_diff(int argc, const char **argv, const char *prefix)
Junio C Hamano65056022006-04-28 23:20:52 -0700214{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700215 int i;
Junio C Hamano65056022006-04-28 23:20:52 -0700216 struct rev_info rev;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700217 struct object_array_entry ent[100];
Junio C Hamano65056022006-04-28 23:20:52 -0700218 int ents = 0, blobs = 0, paths = 0;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700219 const char *path = NULL;
Junio C Hamano65056022006-04-28 23:20:52 -0700220 struct blobinfo blob[2];
221
222 /*
223 * We could get N tree-ish in the rev.pending_objects list.
224 * Also there could be M blobs there, and P pathspecs.
225 *
226 * N=0, M=0:
227 * cache vs files (diff-files)
228 * N=0, M=2:
229 * compare two random blobs. P must be zero.
230 * N=0, M=1, P=1:
231 * compare a blob with a working tree file.
232 *
233 * N=1, M=0:
234 * tree vs cache (diff-index --cached)
235 *
236 * N=2, M=0:
237 * tree vs tree (diff-tree)
238 *
239 * Other cases are errors.
240 */
Junio C Hamano230f5442006-05-03 23:54:34 -0700241
Junio C Hamanoef1d9c52006-07-27 22:55:44 -0700242 git_config(git_diff_ui_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700243 init_revisions(&rev, prefix);
Junio C Hamano65056022006-04-28 23:20:52 -0700244
245 argc = setup_revisions(argc, argv, &rev, NULL);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700246 if (!rev.diffopt.output_format) {
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300247 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
Junio C Hamano72ee96c2006-08-09 12:45:27 -0700248 if (diff_setup_done(&rev.diffopt) < 0)
249 die("diff_setup_done failed");
Junio C Hamano047fbe92006-07-01 22:15:40 -0700250 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300251
Junio C Hamano65056022006-04-28 23:20:52 -0700252 /* Do we have --cached and not have a pending object, then
253 * default to HEAD by hand. Eek.
254 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700255 if (!rev.pending.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700256 int i;
257 for (i = 1; i < argc; i++) {
258 const char *arg = argv[i];
259 if (!strcmp(arg, "--"))
260 break;
261 else if (!strcmp(arg, "--cached")) {
262 add_head(&rev);
263 break;
264 }
265 }
266 }
267
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700268 for (i = 0; i < rev.pending.nr; i++) {
269 struct object_array_entry *list = rev.pending.objects+i;
Junio C Hamano65056022006-04-28 23:20:52 -0700270 struct object *obj = list->item;
271 const char *name = list->name;
272 int flags = (obj->flags & UNINTERESTING);
273 if (!obj->parsed)
274 obj = parse_object(obj->sha1);
275 obj = deref_tag(obj, NULL, 0);
276 if (!obj)
277 die("invalid object '%s' given.", name);
Linus Torvalds19746322006-07-11 20:45:31 -0700278 if (obj->type == OBJ_COMMIT)
Junio C Hamano65056022006-04-28 23:20:52 -0700279 obj = &((struct commit *)obj)->tree->object;
Linus Torvalds19746322006-07-11 20:45:31 -0700280 if (obj->type == OBJ_TREE) {
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700281 if (ARRAY_SIZE(ent) <= ents)
282 die("more than %d trees given: '%s'",
Junio C Hamano334b5062006-04-30 00:26:41 -0700283 (int) ARRAY_SIZE(ent), name);
Junio C Hamano65056022006-04-28 23:20:52 -0700284 obj->flags |= flags;
285 ent[ents].item = obj;
286 ent[ents].name = name;
287 ents++;
288 continue;
289 }
Linus Torvalds19746322006-07-11 20:45:31 -0700290 if (obj->type == OBJ_BLOB) {
Junio C Hamano65056022006-04-28 23:20:52 -0700291 if (2 <= blobs)
292 die("more than two blobs given: '%s'", name);
Shawn Pearcee7024962006-08-23 02:49:00 -0400293 hashcpy(blob[blobs].sha1, obj->sha1);
Junio C Hamano65056022006-04-28 23:20:52 -0700294 blob[blobs].name = name;
295 blobs++;
296 continue;
Junio C Hamano230f5442006-05-03 23:54:34 -0700297
Junio C Hamano65056022006-04-28 23:20:52 -0700298 }
299 die("unhandled object '%s' given.", name);
300 }
301 if (rev.prune_data) {
302 const char **pathspec = rev.prune_data;
303 while (*pathspec) {
304 if (!path)
305 path = *pathspec;
306 paths++;
307 pathspec++;
308 }
309 }
310
311 /*
312 * Now, do the arguments look reasonable?
313 */
314 if (!ents) {
315 switch (blobs) {
316 case 0:
317 return builtin_diff_files(&rev, argc, argv);
318 break;
319 case 1:
320 if (paths != 1)
321 usage(builtin_diff_usage);
322 return builtin_diff_b_f(&rev, argc, argv, blob, path);
323 break;
324 case 2:
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700325 if (paths)
326 usage(builtin_diff_usage);
Junio C Hamano65056022006-04-28 23:20:52 -0700327 return builtin_diff_blobs(&rev, argc, argv, blob);
328 break;
329 default:
330 usage(builtin_diff_usage);
331 }
332 }
333 else if (blobs)
334 usage(builtin_diff_usage);
335 else if (ents == 1)
336 return builtin_diff_index(&rev, argc, argv);
337 else if (ents == 2)
338 return builtin_diff_tree(&rev, argc, argv, ent);
Junio C Hamano9919f412006-07-17 00:34:44 -0700339 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
340 /* diff A...B where there is one sane merge base between
341 * A and B. We have ent[0] == merge-base, ent[1] == A,
342 * and ent[2] == B. Show diff between the base and B.
343 */
Junio C Hamano306ea2d2006-08-10 00:50:15 -0700344 ent[1] = ent[2];
Junio C Hamano9919f412006-07-17 00:34:44 -0700345 return builtin_diff_tree(&rev, argc, argv, ent);
346 }
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700347 else
Junio C Hamano9919f412006-07-17 00:34:44 -0700348 return builtin_diff_combined(&rev, argc, argv,
349 ent, ents);
Junio C Hamano65056022006-04-28 23:20:52 -0700350 usage(builtin_diff_usage);
351}