blob: 7f367b6b9d545ea760224fdacb68056261ce1617 [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
Junio C Hamano65056022006-04-28 23:20:52 -070016struct blobinfo {
17 unsigned char sha1[20];
18 const char *name;
Martin Koegler01618a32007-04-22 18:44:00 +020019 unsigned mode;
Junio C Hamano65056022006-04-28 23:20:52 -070020};
21
22static const char builtin_diff_usage[] =
Ramsay Allan Jones15e593e2006-08-03 16:38:39 +010023"git-diff <options> <rev>{0,2} -- <path>*";
Junio C Hamano65056022006-04-28 23:20:52 -070024
Junio C Hamano65056022006-04-28 23:20:52 -070025static 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,
29 const char *old_name,
30 const char *new_name)
31{
32 struct diff_filespec *one, *two;
33
David Rientjes0bef57e2006-08-15 13:37:19 -070034 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
Junio C Hamano43342942007-04-22 23:56:22 -070035 !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
Junio C Hamano65056022006-04-28 23:20:52 -070036 return;
37
38 if (opt->reverse_diff) {
39 unsigned tmp;
Peter Hagervalld92f1dc2006-05-07 16:50:47 +020040 const unsigned char *tmp_u;
Junio C Hamano65056022006-04-28 23:20:52 -070041 const char *tmp_c;
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;
45 }
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);
50
51 /* NEEDSWORK: shouldn't this part of diffopt??? */
52 diff_queue(&diff_queued_diff, one, two);
53}
54
55static int builtin_diff_b_f(struct rev_info *revs,
56 int argc, const char **argv,
57 struct blobinfo *blob,
58 const char *path)
59{
60 /* Blob vs file in the working tree*/
61 struct stat st;
62
Timo Hirvonena6107862006-06-24 20:23:06 +030063 if (argc > 1)
64 usage(builtin_diff_usage);
65
Junio C Hamano65056022006-04-28 23:20:52 -070066 if (lstat(path, &st))
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);
Martin Koegler01618a32007-04-22 18:44:00 +020070
71 if (blob[0].mode == S_IFINVALID)
72 blob[0].mode = canon_mode(st.st_mode);
73
Junio C Hamano65056022006-04-28 23:20:52 -070074 stuff_change(&revs->diffopt,
Martin Koegler01618a32007-04-22 18:44:00 +020075 blob[0].mode, canon_mode(st.st_mode),
Junio C Hamano65056022006-04-28 23:20:52 -070076 blob[0].sha1, null_sha1,
Junio C Hamano065e0b12006-05-18 14:35:37 -070077 path, path);
Junio C Hamano65056022006-04-28 23:20:52 -070078 diffcore_std(&revs->diffopt);
79 diff_flush(&revs->diffopt);
80 return 0;
81}
82
83static int builtin_diff_blobs(struct rev_info *revs,
84 int argc, const char **argv,
85 struct blobinfo *blob)
86{
Junio C Hamano65056022006-04-28 23:20:52 -070087 unsigned mode = canon_mode(S_IFREG | 0644);
88
Timo Hirvonena6107862006-06-24 20:23:06 +030089 if (argc > 1)
90 usage(builtin_diff_usage);
91
Martin Koegler01618a32007-04-22 18:44:00 +020092 if (blob[0].mode == S_IFINVALID)
93 blob[0].mode = mode;
94
95 if (blob[1].mode == S_IFINVALID)
96 blob[1].mode = mode;
97
Junio C Hamano65056022006-04-28 23:20:52 -070098 stuff_change(&revs->diffopt,
Martin Koegler01618a32007-04-22 18:44:00 +020099 blob[0].mode, blob[1].mode,
Junio C Hamanof82cd3c2006-08-03 11:50:10 -0700100 blob[0].sha1, blob[1].sha1,
Junio C Hamano53dd8a92006-08-03 11:57:11 -0700101 blob[0].name, blob[1].name);
Junio C Hamano65056022006-04-28 23:20:52 -0700102 diffcore_std(&revs->diffopt);
103 diff_flush(&revs->diffopt);
104 return 0;
105}
106
107static int builtin_diff_index(struct rev_info *revs,
108 int argc, const char **argv)
109{
110 int cached = 0;
111 while (1 < argc) {
112 const char *arg = argv[1];
Junio C Hamanof2dd1c92006-12-13 01:33:43 -0800113 if (!strcmp(arg, "--cached"))
Junio C Hamano65056022006-04-28 23:20:52 -0700114 cached = 1;
Junio C Hamano65056022006-04-28 23:20:52 -0700115 else
116 usage(builtin_diff_usage);
117 argv++; argc--;
118 }
119 /*
120 * Make sure there is one revision (i.e. pending object),
121 * and there is no revision filtering parameters.
122 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700123 if (revs->pending.nr != 1 ||
Junio C Hamano65056022006-04-28 23:20:52 -0700124 revs->max_count != -1 || revs->min_age != -1 ||
125 revs->max_age != -1)
126 usage(builtin_diff_usage);
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -0800127 if (read_cache() < 0) {
128 perror("read_cache");
129 return -1;
130 }
Junio C Hamano65056022006-04-28 23:20:52 -0700131 return run_diff_index(revs, cached);
132}
133
134static int builtin_diff_tree(struct rev_info *revs,
135 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700136 struct object_array_entry *ent)
Junio C Hamano65056022006-04-28 23:20:52 -0700137{
Junio C Hamano65056022006-04-28 23:20:52 -0700138 const unsigned char *(sha1[2]);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700139 int swap = 0;
Timo Hirvonena6107862006-06-24 20:23:06 +0300140
141 if (argc > 1)
142 usage(builtin_diff_usage);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700143
144 /* We saw two trees, ent[0] and ent[1].
Pavel Roskin82e5a822006-07-10 01:50:18 -0400145 * if ent[1] is uninteresting, they are swapped
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700146 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700147 if (ent[1].item->flags & UNINTERESTING)
148 swap = 1;
Junio C Hamano65056022006-04-28 23:20:52 -0700149 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);
153 return 0;
154}
155
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700156static int builtin_diff_combined(struct rev_info *revs,
157 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700158 struct object_array_entry *ent,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700159 int ents)
160{
161 const unsigned char (*parent)[20];
162 int i;
163
Timo Hirvonena6107862006-06-24 20:23:06 +0300164 if (argc > 1)
165 usage(builtin_diff_usage);
166
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700167 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++)
Johannes Schindelin75b62b42007-02-23 05:20:32 +0100172 hashcpy((unsigned char *)(parent + i),
173 ent[ents - 1 - i].item->sha1);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700174 diff_tree_combined(parent[0], parent + 1, ents - 1,
175 revs->dense_combined_merges, revs);
176 return 0;
177}
178
Johannes Schindeline686eb92006-05-06 22:56:38 +0200179void add_head(struct rev_info *revs)
Junio C Hamano65056022006-04-28 23:20:52 -0700180{
181 unsigned char sha1[20];
182 struct object *obj;
183 if (get_sha1("HEAD", sha1))
184 return;
185 obj = parse_object(sha1);
186 if (!obj)
187 return;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700188 add_pending_object(revs, obj, "HEAD");
Junio C Hamano65056022006-04-28 23:20:52 -0700189}
190
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700191int cmd_diff(int argc, const char **argv, const char *prefix)
Junio C Hamano65056022006-04-28 23:20:52 -0700192{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700193 int i;
Junio C Hamano65056022006-04-28 23:20:52 -0700194 struct rev_info rev;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700195 struct object_array_entry ent[100];
Junio C Hamano65056022006-04-28 23:20:52 -0700196 int ents = 0, blobs = 0, paths = 0;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700197 const char *path = NULL;
Junio C Hamano65056022006-04-28 23:20:52 -0700198 struct blobinfo blob[2];
Johannes Schindelind516c2d2007-02-22 21:50:10 +0100199 int nongit = 0;
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100200 int result = 0;
Junio C Hamano65056022006-04-28 23:20:52 -0700201
202 /*
203 * We could get N tree-ish in the rev.pending_objects list.
204 * Also there could be M blobs there, and P pathspecs.
205 *
206 * N=0, M=0:
207 * cache vs files (diff-files)
208 * N=0, M=2:
209 * compare two random blobs. P must be zero.
210 * N=0, M=1, P=1:
211 * compare a blob with a working tree file.
212 *
213 * N=1, M=0:
214 * tree vs cache (diff-index --cached)
215 *
216 * N=2, M=0:
217 * tree vs tree (diff-tree)
218 *
219 * Other cases are errors.
220 */
Junio C Hamano230f5442006-05-03 23:54:34 -0700221
Johannes Schindelind516c2d2007-02-22 21:50:10 +0100222 prefix = setup_git_directory_gently(&nongit);
Junio C Hamanoef1d9c52006-07-27 22:55:44 -0700223 git_config(git_diff_ui_config);
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700224 init_revisions(&rev, prefix);
Junio C Hamano65056022006-04-28 23:20:52 -0700225
Johannes Schindelinfcfa33e2007-02-25 23:35:27 +0100226 if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
227 argc = 0;
228 else
229 argc = setup_revisions(argc, argv, &rev, NULL);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700230 if (!rev.diffopt.output_format) {
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300231 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
Junio C Hamano72ee96c2006-08-09 12:45:27 -0700232 if (diff_setup_done(&rev.diffopt) < 0)
233 die("diff_setup_done failed");
Junio C Hamano047fbe92006-07-01 22:15:40 -0700234 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700235 rev.diffopt.allow_external = 1;
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300236
Junio C Hamano65056022006-04-28 23:20:52 -0700237 /* Do we have --cached and not have a pending object, then
238 * default to HEAD by hand. Eek.
239 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700240 if (!rev.pending.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700241 int i;
242 for (i = 1; i < argc; i++) {
243 const char *arg = argv[i];
244 if (!strcmp(arg, "--"))
245 break;
246 else if (!strcmp(arg, "--cached")) {
247 add_head(&rev);
Junio C Hamano6c09c452007-02-24 22:26:33 -0800248 if (!rev.pending.nr)
249 die("No HEAD commit to compare with (yet)");
Junio C Hamano65056022006-04-28 23:20:52 -0700250 break;
251 }
252 }
253 }
254
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700255 for (i = 0; i < rev.pending.nr; i++) {
256 struct object_array_entry *list = rev.pending.objects+i;
Junio C Hamano65056022006-04-28 23:20:52 -0700257 struct object *obj = list->item;
258 const char *name = list->name;
259 int flags = (obj->flags & UNINTERESTING);
260 if (!obj->parsed)
261 obj = parse_object(obj->sha1);
262 obj = deref_tag(obj, NULL, 0);
263 if (!obj)
264 die("invalid object '%s' given.", name);
Linus Torvalds19746322006-07-11 20:45:31 -0700265 if (obj->type == OBJ_COMMIT)
Junio C Hamano65056022006-04-28 23:20:52 -0700266 obj = &((struct commit *)obj)->tree->object;
Linus Torvalds19746322006-07-11 20:45:31 -0700267 if (obj->type == OBJ_TREE) {
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700268 if (ARRAY_SIZE(ent) <= ents)
269 die("more than %d trees given: '%s'",
Junio C Hamano334b5062006-04-30 00:26:41 -0700270 (int) ARRAY_SIZE(ent), name);
Junio C Hamano65056022006-04-28 23:20:52 -0700271 obj->flags |= flags;
272 ent[ents].item = obj;
273 ent[ents].name = name;
274 ents++;
275 continue;
276 }
Linus Torvalds19746322006-07-11 20:45:31 -0700277 if (obj->type == OBJ_BLOB) {
Junio C Hamano65056022006-04-28 23:20:52 -0700278 if (2 <= blobs)
279 die("more than two blobs given: '%s'", name);
Shawn Pearcee7024962006-08-23 02:49:00 -0400280 hashcpy(blob[blobs].sha1, obj->sha1);
Junio C Hamano65056022006-04-28 23:20:52 -0700281 blob[blobs].name = name;
Martin Koegler01618a32007-04-22 18:44:00 +0200282 blob[blobs].mode = list->mode;
Junio C Hamano65056022006-04-28 23:20:52 -0700283 blobs++;
284 continue;
Junio C Hamano230f5442006-05-03 23:54:34 -0700285
Junio C Hamano65056022006-04-28 23:20:52 -0700286 }
287 die("unhandled object '%s' given.", name);
288 }
289 if (rev.prune_data) {
290 const char **pathspec = rev.prune_data;
291 while (*pathspec) {
292 if (!path)
293 path = *pathspec;
294 paths++;
295 pathspec++;
296 }
297 }
298
299 /*
300 * Now, do the arguments look reasonable?
301 */
302 if (!ents) {
303 switch (blobs) {
304 case 0:
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100305 result = run_diff_files_cmd(&rev, argc, argv);
Junio C Hamano65056022006-04-28 23:20:52 -0700306 break;
307 case 1:
308 if (paths != 1)
309 usage(builtin_diff_usage);
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100310 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
Junio C Hamano65056022006-04-28 23:20:52 -0700311 break;
312 case 2:
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700313 if (paths)
314 usage(builtin_diff_usage);
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100315 result = builtin_diff_blobs(&rev, argc, argv, blob);
Junio C Hamano65056022006-04-28 23:20:52 -0700316 break;
317 default:
318 usage(builtin_diff_usage);
319 }
320 }
321 else if (blobs)
322 usage(builtin_diff_usage);
323 else if (ents == 1)
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100324 result = builtin_diff_index(&rev, argc, argv);
Junio C Hamano65056022006-04-28 23:20:52 -0700325 else if (ents == 2)
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100326 result = builtin_diff_tree(&rev, argc, argv, ent);
Junio C Hamano9919f412006-07-17 00:34:44 -0700327 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
328 /* diff A...B where there is one sane merge base between
329 * A and B. We have ent[0] == merge-base, ent[1] == A,
330 * and ent[2] == B. Show diff between the base and B.
331 */
Junio C Hamano306ea2d2006-08-10 00:50:15 -0700332 ent[1] = ent[2];
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100333 result = builtin_diff_tree(&rev, argc, argv, ent);
Junio C Hamano9919f412006-07-17 00:34:44 -0700334 }
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700335 else
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100336 result = builtin_diff_combined(&rev, argc, argv,
Junio C Hamano9919f412006-07-17 00:34:44 -0700337 ent, ents);
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100338 if (rev.diffopt.exit_with_status)
339 result = rev.diffopt.has_changes;
340 return result;
Junio C Hamano65056022006-04-28 23:20:52 -0700341}