]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-diff.c
autoconf: Add tests for memmem, strtoumax and mkdtemp functions
[thirdparty/git.git] / builtin-diff.c
CommitLineData
65056021
JH
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
65056021
JH
16struct blobinfo {
17 unsigned char sha1[20];
18 const char *name;
01618a3a 19 unsigned mode;
65056021
JH
20};
21
22static const char builtin_diff_usage[] =
15e593e4 23"git-diff <options> <rev>{0,2} -- <path>*";
65056021 24
65056021
JH
25static 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
0bef57ee 34 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
43342941 35 !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
65056021
JH
36 return;
37
8f67f8ae 38 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
65056021 39 unsigned tmp;
d92f1dc6 40 const unsigned char *tmp_u;
65056021
JH
41 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
a610786f
TH
63 if (argc > 1)
64 usage(builtin_diff_usage);
65
65056021
JH
66 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);
01618a3a
MK
70
71 if (blob[0].mode == S_IFINVALID)
72 blob[0].mode = canon_mode(st.st_mode);
73
65056021 74 stuff_change(&revs->diffopt,
01618a3a 75 blob[0].mode, canon_mode(st.st_mode),
65056021 76 blob[0].sha1, null_sha1,
065e0b12 77 path, path);
65056021
JH
78 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{
65056021
JH
87 unsigned mode = canon_mode(S_IFREG | 0644);
88
a610786f
TH
89 if (argc > 1)
90 usage(builtin_diff_usage);
91
01618a3a
MK
92 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
65056021 98 stuff_change(&revs->diffopt,
01618a3a 99 blob[0].mode, blob[1].mode,
f82cd3c6 100 blob[0].sha1, blob[1].sha1,
53dd8a9c 101 blob[0].name, blob[1].name);
65056021
JH
102 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];
f2dd1c9a 113 if (!strcmp(arg, "--cached"))
65056021 114 cached = 1;
65056021
JH
115 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 */
1f1e895f 123 if (revs->pending.nr != 1 ||
65056021
JH
124 revs->max_count != -1 || revs->min_age != -1 ||
125 revs->max_age != -1)
126 usage(builtin_diff_usage);
b4e1e4a7
JH
127 if (read_cache() < 0) {
128 perror("read_cache");
129 return -1;
130 }
65056021
JH
131 return run_diff_index(revs, cached);
132}
133
134static int builtin_diff_tree(struct rev_info *revs,
135 int argc, const char **argv,
1f1e895f 136 struct object_array_entry *ent)
65056021 137{
65056021 138 const unsigned char *(sha1[2]);
1f1e895f 139 int swap = 0;
a610786f
TH
140
141 if (argc > 1)
142 usage(builtin_diff_usage);
0fe7c1de
JH
143
144 /* We saw two trees, ent[0] and ent[1].
82e5a82f 145 * if ent[1] is uninteresting, they are swapped
0fe7c1de 146 */
1f1e895f
LT
147 if (ent[1].item->flags & UNINTERESTING)
148 swap = 1;
65056021
JH
149 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
0fe7c1de
JH
156static int builtin_diff_combined(struct rev_info *revs,
157 int argc, const char **argv,
1f1e895f 158 struct object_array_entry *ent,
0fe7c1de
JH
159 int ents)
160{
161 const unsigned char (*parent)[20];
162 int i;
163
a610786f
TH
164 if (argc > 1)
165 usage(builtin_diff_usage);
166
0fe7c1de
JH
167 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++)
75b62b48
JS
172 hashcpy((unsigned char *)(parent + i),
173 ent[ents - 1 - i].item->sha1);
0fe7c1de
JH
174 diff_tree_combined(parent[0], parent + 1, ents - 1,
175 revs->dense_combined_merges, revs);
176 return 0;
177}
178
e686eb98 179void add_head(struct rev_info *revs)
65056021
JH
180{
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;
1f1e895f 188 add_pending_object(revs, obj, "HEAD");
65056021
JH
189}
190
aecbf914
JH
191static void refresh_index_quietly(void)
192{
193 struct lock_file *lock_file;
194 int fd;
195
196 lock_file = xcalloc(1, sizeof(struct lock_file));
197 fd = hold_locked_index(lock_file, 0);
198 if (fd < 0)
199 return;
200 discard_cache();
201 read_cache();
202 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
fbbdbfc3
JH
203
204 if (active_cache_changed &&
205 !write_cache(fd, active_cache, active_nr) && !close(fd))
206 commit_locked_index(lock_file);
207
aecbf914
JH
208 rollback_lock_file(lock_file);
209}
210
a633fca0 211int cmd_diff(int argc, const char **argv, const char *prefix)
65056021 212{
1f1e895f 213 int i;
65056021 214 struct rev_info rev;
1f1e895f 215 struct object_array_entry ent[100];
65056021 216 int ents = 0, blobs = 0, paths = 0;
a633fca0 217 const char *path = NULL;
65056021 218 struct blobinfo blob[2];
d516c2d1 219 int nongit = 0;
41bbf9d5 220 int result = 0;
65056021
JH
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 */
230f544e 241
d516c2d1 242 prefix = setup_git_directory_gently(&nongit);
ef1d9c5a 243 git_config(git_diff_ui_config);
db6296a5 244 init_revisions(&rev, prefix);
aecbf914 245 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
65056021 246
fcfa33ec
JS
247 if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
248 argc = 0;
249 else
250 argc = setup_revisions(argc, argv, &rev, NULL);
047fbe90 251 if (!rev.diffopt.output_format) {
c9b5ef99 252 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
72ee96c0
JH
253 if (diff_setup_done(&rev.diffopt) < 0)
254 die("diff_setup_done failed");
047fbe90 255 }
8f67f8ae
PH
256 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
257 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
c9b5ef99 258
89d07f75
RS
259 /* If the user asked for our exit code then don't start a
260 * pager or we would end up reporting its exit code instead.
261 */
8f67f8ae 262 if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
89d07f75
RS
263 setup_pager();
264
65056021
JH
265 /* Do we have --cached and not have a pending object, then
266 * default to HEAD by hand. Eek.
267 */
1f1e895f 268 if (!rev.pending.nr) {
65056021
JH
269 int i;
270 for (i = 1; i < argc; i++) {
271 const char *arg = argv[i];
272 if (!strcmp(arg, "--"))
273 break;
274 else if (!strcmp(arg, "--cached")) {
275 add_head(&rev);
6c09c451
JH
276 if (!rev.pending.nr)
277 die("No HEAD commit to compare with (yet)");
65056021
JH
278 break;
279 }
280 }
281 }
282
1f1e895f
LT
283 for (i = 0; i < rev.pending.nr; i++) {
284 struct object_array_entry *list = rev.pending.objects+i;
65056021
JH
285 struct object *obj = list->item;
286 const char *name = list->name;
287 int flags = (obj->flags & UNINTERESTING);
288 if (!obj->parsed)
289 obj = parse_object(obj->sha1);
290 obj = deref_tag(obj, NULL, 0);
291 if (!obj)
292 die("invalid object '%s' given.", name);
1974632c 293 if (obj->type == OBJ_COMMIT)
65056021 294 obj = &((struct commit *)obj)->tree->object;
1974632c 295 if (obj->type == OBJ_TREE) {
0fe7c1de
JH
296 if (ARRAY_SIZE(ent) <= ents)
297 die("more than %d trees given: '%s'",
334b506a 298 (int) ARRAY_SIZE(ent), name);
65056021
JH
299 obj->flags |= flags;
300 ent[ents].item = obj;
301 ent[ents].name = name;
302 ents++;
303 continue;
304 }
1974632c 305 if (obj->type == OBJ_BLOB) {
65056021
JH
306 if (2 <= blobs)
307 die("more than two blobs given: '%s'", name);
e702496e 308 hashcpy(blob[blobs].sha1, obj->sha1);
65056021 309 blob[blobs].name = name;
01618a3a 310 blob[blobs].mode = list->mode;
65056021
JH
311 blobs++;
312 continue;
230f544e 313
65056021
JH
314 }
315 die("unhandled object '%s' given.", name);
316 }
317 if (rev.prune_data) {
318 const char **pathspec = rev.prune_data;
319 while (*pathspec) {
320 if (!path)
321 path = *pathspec;
322 paths++;
323 pathspec++;
324 }
325 }
326
327 /*
328 * Now, do the arguments look reasonable?
329 */
330 if (!ents) {
331 switch (blobs) {
332 case 0:
41bbf9d5 333 result = run_diff_files_cmd(&rev, argc, argv);
65056021
JH
334 break;
335 case 1:
336 if (paths != 1)
337 usage(builtin_diff_usage);
41bbf9d5 338 result = builtin_diff_b_f(&rev, argc, argv, blob, path);
65056021
JH
339 break;
340 case 2:
0fe7c1de
JH
341 if (paths)
342 usage(builtin_diff_usage);
41bbf9d5 343 result = builtin_diff_blobs(&rev, argc, argv, blob);
65056021
JH
344 break;
345 default:
346 usage(builtin_diff_usage);
347 }
348 }
349 else if (blobs)
350 usage(builtin_diff_usage);
351 else if (ents == 1)
41bbf9d5 352 result = builtin_diff_index(&rev, argc, argv);
65056021 353 else if (ents == 2)
41bbf9d5 354 result = builtin_diff_tree(&rev, argc, argv, ent);
9919f41c
JH
355 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
356 /* diff A...B where there is one sane merge base between
357 * A and B. We have ent[0] == merge-base, ent[1] == A,
358 * and ent[2] == B. Show diff between the base and B.
359 */
306ea2df 360 ent[1] = ent[2];
41bbf9d5 361 result = builtin_diff_tree(&rev, argc, argv, ent);
9919f41c 362 }
0fe7c1de 363 else
41bbf9d5 364 result = builtin_diff_combined(&rev, argc, argv,
9919f41c 365 ent, ents);
8f67f8ae
PH
366 if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
367 result = DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
fb13227e 368
aecbf914
JH
369 if (1 < rev.diffopt.skip_stat_unmatch)
370 refresh_index_quietly();
41bbf9d5 371 return result;
65056021 372}