]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff.c
Merge branch 'jk/add-ignore-errors-bit-assignment-fix'
[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"
b2141fc1 7#include "config.h"
697cc8ef 8#include "lockfile.h"
6b2f2d98 9#include "color.h"
65056021
JH
10#include "commit.h"
11#include "blob.h"
12#include "tag.h"
13#include "diff.h"
14#include "diffcore.h"
15#include "revision.h"
16#include "log-tree.h"
17#include "builtin.h"
302ad7a9 18#include "submodule.h"
0041f09d 19#include "sha1-array.h"
65056021 20
470faf96
TG
21#define DIFF_NO_INDEX_EXPLICIT 1
22#define DIFF_NO_INDEX_IMPLICIT 2
23
65056021 24static const char builtin_diff_usage[] =
9edb8a0f 25"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
65056021 26
158b06ca
JK
27static const char *blob_path(struct object_array_entry *entry)
28{
29 return entry->path ? entry->path : entry->name;
30}
31
65056021
JH
32static void stuff_change(struct diff_options *opt,
33 unsigned old_mode, unsigned new_mode,
9c4b0f66 34 const struct object_id *old_oid,
35 const struct object_id *new_oid,
36 int old_oid_valid,
37 int new_oid_valid,
d04ec74b
JK
38 const char *old_path,
39 const char *new_path)
65056021
JH
40{
41 struct diff_filespec *one, *two;
42
9c4b0f66 43 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
4a7e27e9 44 oideq(old_oid, new_oid) && (old_mode == new_mode))
65056021
JH
45 return;
46
0d1e0e78 47 if (opt->flags.reverse_diff) {
35d803bc 48 SWAP(old_mode, new_mode);
9c4b0f66 49 SWAP(old_oid, new_oid);
d04ec74b 50 SWAP(old_path, new_path);
65056021 51 }
cd676a51
JH
52
53 if (opt->prefix &&
d04ec74b
JK
54 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
55 strncmp(new_path, opt->prefix, opt->prefix_length)))
cd676a51
JH
56 return;
57
d04ec74b
JK
58 one = alloc_filespec(old_path);
59 two = alloc_filespec(new_path);
f9704c2d
BW
60 fill_filespec(one, old_oid, old_oid_valid, old_mode);
61 fill_filespec(two, new_oid, new_oid_valid, new_mode);
65056021 62
65056021
JH
63 diff_queue(&diff_queued_diff, one, two);
64}
65
66static int builtin_diff_b_f(struct rev_info *revs,
67 int argc, const char **argv,
42f5ba5b 68 struct object_array_entry **blob)
65056021
JH
69{
70 /* Blob vs file in the working tree*/
71 struct stat st;
887c6c18 72 const char *path;
65056021 73
a610786f
TH
74 if (argc > 1)
75 usage(builtin_diff_usage);
76
887c6c18
NTND
77 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
78 path = revs->prune_data.items[0].match;
79
65056021 80 if (lstat(path, &st))
54214529 81 die_errno(_("failed to stat '%s'"), path);
65056021 82 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
54214529 83 die(_("'%s': not a regular file or symlink"), path);
01618a3a 84
a5a818ee
JH
85 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
86
42f5ba5b
JK
87 if (blob[0]->mode == S_IFINVALID)
88 blob[0]->mode = canon_mode(st.st_mode);
01618a3a 89
65056021 90 stuff_change(&revs->diffopt,
42f5ba5b
JK
91 blob[0]->mode, canon_mode(st.st_mode),
92 &blob[0]->item->oid, &null_oid,
e5450100 93 1, 0,
30d005c0
JK
94 blob[0]->path ? blob[0]->path : path,
95 path);
65056021
JH
96 diffcore_std(&revs->diffopt);
97 diff_flush(&revs->diffopt);
98 return 0;
99}
100
101static int builtin_diff_blobs(struct rev_info *revs,
102 int argc, const char **argv,
42f5ba5b 103 struct object_array_entry **blob)
65056021 104{
65056021
JH
105 unsigned mode = canon_mode(S_IFREG | 0644);
106
a610786f
TH
107 if (argc > 1)
108 usage(builtin_diff_usage);
109
42f5ba5b
JK
110 if (blob[0]->mode == S_IFINVALID)
111 blob[0]->mode = mode;
01618a3a 112
42f5ba5b
JK
113 if (blob[1]->mode == S_IFINVALID)
114 blob[1]->mode = mode;
01618a3a 115
65056021 116 stuff_change(&revs->diffopt,
42f5ba5b
JK
117 blob[0]->mode, blob[1]->mode,
118 &blob[0]->item->oid, &blob[1]->item->oid,
e5450100 119 1, 1,
158b06ca 120 blob_path(blob[0]), blob_path(blob[1]));
65056021
JH
121 diffcore_std(&revs->diffopt);
122 diff_flush(&revs->diffopt);
123 return 0;
124}
125
126static int builtin_diff_index(struct rev_info *revs,
127 int argc, const char **argv)
128{
129 int cached = 0;
130 while (1 < argc) {
131 const char *arg = argv[1];
2baf1850 132 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
65056021 133 cached = 1;
65056021
JH
134 else
135 usage(builtin_diff_usage);
136 argv++; argc--;
137 }
138 /*
139 * Make sure there is one revision (i.e. pending object),
140 * and there is no revision filtering parameters.
141 */
1f1e895f 142 if (revs->pending.nr != 1 ||
65056021
JH
143 revs->max_count != -1 || revs->min_age != -1 ||
144 revs->max_age != -1)
145 usage(builtin_diff_usage);
7349afd2
KB
146 if (!cached) {
147 setup_work_tree();
5ab2a2da 148 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
7349afd2
KB
149 perror("read_cache_preload");
150 return -1;
151 }
152 } else if (read_cache() < 0) {
153 perror("read_cache");
b4e1e4a7
JH
154 return -1;
155 }
65056021
JH
156 return run_diff_index(revs, cached);
157}
158
159static int builtin_diff_tree(struct rev_info *revs,
160 int argc, const char **argv,
91de344d
MH
161 struct object_array_entry *ent0,
162 struct object_array_entry *ent1)
65056021 163{
9c4b0f66 164 const struct object_id *(oid[2]);
1f1e895f 165 int swap = 0;
a610786f
TH
166
167 if (argc > 1)
168 usage(builtin_diff_usage);
0fe7c1de 169
91de344d
MH
170 /*
171 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
172 * swap them.
0fe7c1de 173 */
91de344d 174 if (ent1->item->flags & UNINTERESTING)
1f1e895f 175 swap = 1;
9c4b0f66 176 oid[swap] = &ent0->item->oid;
177 oid[1 - swap] = &ent1->item->oid;
66f414f8 178 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
65056021
JH
179 log_tree_diff_flush(revs);
180 return 0;
181}
182
0fe7c1de
JH
183static int builtin_diff_combined(struct rev_info *revs,
184 int argc, const char **argv,
1f1e895f 185 struct object_array_entry *ent,
0fe7c1de
JH
186 int ents)
187{
910650d2 188 struct oid_array parents = OID_ARRAY_INIT;
0fe7c1de
JH
189 int i;
190
a610786f
TH
191 if (argc > 1)
192 usage(builtin_diff_usage);
193
0fe7c1de
JH
194 if (!revs->dense_combined_merges && !revs->combine_merges)
195 revs->dense_combined_merges = revs->combine_merges = 1;
0041f09d 196 for (i = 1; i < ents; i++)
910650d2 197 oid_array_append(&parents, &ent[i].item->oid);
b9acf54d 198 diff_tree_combined(&ent[0].item->oid, &parents,
0fe7c1de 199 revs->dense_combined_merges, revs);
910650d2 200 oid_array_clear(&parents);
0fe7c1de
JH
201 return 0;
202}
203
aecbf914
JH
204static void refresh_index_quietly(void)
205{
837e34eb 206 struct lock_file lock_file = LOCK_INIT;
aecbf914
JH
207 int fd;
208
837e34eb 209 fd = hold_locked_index(&lock_file, 0);
aecbf914
JH
210 if (fd < 0)
211 return;
212 discard_cache();
213 read_cache();
214 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
837e34eb 215 update_index_if_able(&the_index, &lock_file);
aecbf914
JH
216}
217
0569e9b8
JH
218static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
219{
0569e9b8
JH
220 unsigned int options = 0;
221
222 while (1 < argc && argv[1][0] == '-') {
223 if (!strcmp(argv[1], "--base"))
224 revs->max_count = 1;
225 else if (!strcmp(argv[1], "--ours"))
226 revs->max_count = 2;
227 else if (!strcmp(argv[1], "--theirs"))
228 revs->max_count = 3;
229 else if (!strcmp(argv[1], "-q"))
230 options |= DIFF_SILENT_ON_REMOVED;
1c370ea4
MM
231 else if (!strcmp(argv[1], "-h"))
232 usage(builtin_diff_usage);
0569e9b8 233 else
54214529 234 return error(_("invalid option: %s"), argv[1]);
0569e9b8
JH
235 argv++; argc--;
236 }
237
903e09a3
JH
238 /*
239 * "diff --base" should not combine merges because it was not
240 * asked to. "diff -c" should not densify (if the user wants
241 * dense one, --cc can be explicitly asked for, or just rely
242 * on the default).
243 */
244 if (revs->max_count == -1 && !revs->combine_merges &&
0569e9b8
JH
245 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
246 revs->combine_merges = revs->dense_combined_merges = 1;
247
4f38f6b5 248 setup_work_tree();
5ab2a2da 249 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
671c9b7e 250 perror("read_cache_preload");
0569e9b8
JH
251 return -1;
252 }
e7c3a59c 253 return run_diff_files(revs, options);
0569e9b8
JH
254}
255
a633fca0 256int cmd_diff(int argc, const char **argv, const char *prefix)
65056021 257{
1f1e895f 258 int i;
65056021 259 struct rev_info rev;
33055fa8
MH
260 struct object_array ent = OBJECT_ARRAY_INIT;
261 int blobs = 0, paths = 0;
42f5ba5b 262 struct object_array_entry *blob[2];
470faf96 263 int nongit = 0, no_index = 0;
41bbf9d5 264 int result = 0;
65056021
JH
265
266 /*
267 * We could get N tree-ish in the rev.pending_objects list.
268 * Also there could be M blobs there, and P pathspecs.
269 *
270 * N=0, M=0:
271 * cache vs files (diff-files)
272 * N=0, M=2:
273 * compare two random blobs. P must be zero.
274 * N=0, M=1, P=1:
275 * compare a blob with a working tree file.
276 *
277 * N=1, M=0:
278 * tree vs cache (diff-index --cached)
279 *
280 * N=2, M=0:
281 * tree vs tree (diff-tree)
282 *
0569e9b8
JH
283 * N=0, M=0, P=2:
284 * compare two filesystem entities (aka --no-index).
285 *
65056021
JH
286 * Other cases are errors.
287 */
230f544e 288
470faf96
TG
289 /* Were we asked to do --no-index explicitly? */
290 for (i = 1; i < argc; i++) {
291 if (!strcmp(argv[i], "--")) {
292 i++;
293 break;
294 }
295 if (!strcmp(argv[i], "--no-index"))
296 no_index = DIFF_NO_INDEX_EXPLICIT;
297 if (argv[i][0] != '-')
298 break;
299 }
300
28a4e580 301 prefix = setup_git_directory_gently(&nongit);
6df5762d 302
28a4e580 303 if (!no_index) {
475b362c
JK
304 /*
305 * Treat git diff with at least one path outside of the
306 * repo the same as if the command would have been executed
307 * outside of a git repository. In this case it behaves
308 * the same way as "git diff --no-index <a> <b>", which acts
309 * as a colourful "diff" replacement.
310 */
311 if (nongit || ((argc == i + 2) &&
312 (!path_inside_repo(prefix, argv[i]) ||
313 !path_inside_repo(prefix, argv[i + 1]))))
314 no_index = DIFF_NO_INDEX_IMPLICIT;
315 }
470faf96 316
5404c116 317 init_diff_ui_defaults();
ef90d6d4 318 git_config(git_diff_ui_config, NULL);
90a78b83 319 precompose_argv(argc, argv);
6b2f2d98 320
2abf3503 321 repo_init_revisions(the_repository, &rev, prefix);
0569e9b8 322
aad90e85
TG
323 if (no_index && argc != i + 2) {
324 if (no_index == DIFF_NO_INDEX_IMPLICIT) {
325 /*
326 * There was no --no-index and there were not two
327 * paths. It is possible that the user intended
328 * to do an inside-repository operation.
329 */
330 fprintf(stderr, "Not a git repository\n");
331 fprintf(stderr,
332 "To compare two paths outside a working tree:\n");
470faf96 333 }
aad90e85
TG
334 /* Give the usage message for non-repository usage and exit. */
335 usagef("git diff %s <path> <path>",
336 no_index == DIFF_NO_INDEX_EXPLICIT ?
337 "--no-index" : "[--no-index]");
338
339 }
340 if (no_index)
470faf96 341 /* If this is a no-index diff, just run it and exit there. */
e6757652 342 diff_no_index(the_repository, &rev, argc, argv);
0569e9b8
JH
343
344 /* Otherwise, we are doing the usual "git" diff */
aecbf914 345 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
65056021 346
df44483a 347 /* Scale to real terminal size and respect statGraphWidth config */
af9fedc1 348 rev.diffopt.stat_width = -1;
df44483a 349 rev.diffopt.stat_graph_width = -1;
af9fedc1 350
5ec11af6 351 /* Default to let external and textconv be used */
0d1e0e78
BW
352 rev.diffopt.flags.allow_external = 1;
353 rev.diffopt.flags.allow_textconv = 1;
61af494c 354
0231ae71
NTND
355 /*
356 * Default to intent-to-add entries invisible in the
357 * index. This makes them show up as new files in diff-files
358 * and not at all in diff-cached.
359 */
360 rev.diffopt.ita_invisible_in_index = 1;
361
0569e9b8 362 if (nongit)
54214529 363 die(_("Not a git repository"));
0569e9b8 364 argc = setup_revisions(argc, argv, &rev, NULL);
047fbe90 365 if (!rev.diffopt.output_format) {
c9b5ef99 366 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
28452655 367 diff_setup_done(&rev.diffopt);
047fbe90 368 }
61af494c 369
0d1e0e78 370 rev.diffopt.flags.recursive = 1;
c9b5ef99 371
1af3d977 372 setup_diff_pager(&rev.diffopt);
89d07f75 373
0569e9b8
JH
374 /*
375 * Do we have --cached and not have a pending object, then
65056021
JH
376 * default to HEAD by hand. Eek.
377 */
1f1e895f 378 if (!rev.pending.nr) {
65056021
JH
379 int i;
380 for (i = 1; i < argc; i++) {
381 const char *arg = argv[i];
382 if (!strcmp(arg, "--"))
383 break;
2baf1850
DS
384 else if (!strcmp(arg, "--cached") ||
385 !strcmp(arg, "--staged")) {
3384a2df 386 add_head_to_pending(&rev);
a2b7a3b3
NTND
387 if (!rev.pending.nr) {
388 struct tree *tree;
f86bcc7b
SB
389 tree = lookup_tree(the_repository,
390 the_repository->hash_algo->empty_tree);
a2b7a3b3
NTND
391 add_pending_object(&rev, &tree->object, "HEAD");
392 }
65056021
JH
393 break;
394 }
395 }
396 }
397
1f1e895f 398 for (i = 0; i < rev.pending.nr; i++) {
026f09e7
MH
399 struct object_array_entry *entry = &rev.pending.objects[i];
400 struct object *obj = entry->item;
401 const char *name = entry->name;
65056021
JH
402 int flags = (obj->flags & UNINTERESTING);
403 if (!obj->parsed)
109cd76d 404 obj = parse_object(the_repository, &obj->oid);
a74093da 405 obj = deref_tag(the_repository, obj, NULL, 0);
65056021 406 if (!obj)
54214529 407 die(_("invalid object '%s' given."), name);
1974632c 408 if (obj->type == OBJ_COMMIT)
2e27bd77 409 obj = &get_commit_tree(((struct commit *)obj))->object;
5b1e14ea 410
1974632c 411 if (obj->type == OBJ_TREE) {
65056021 412 obj->flags |= flags;
33055fa8 413 add_object_array(obj, name, &ent);
5b1e14ea 414 } else if (obj->type == OBJ_BLOB) {
65056021 415 if (2 <= blobs)
54214529 416 die(_("more than two blobs given: '%s'"), name);
42f5ba5b 417 blob[blobs] = entry;
65056021 418 blobs++;
230f544e 419
5b1e14ea
MH
420 } else {
421 die(_("unhandled object '%s' given."), name);
65056021 422 }
65056021 423 }
887c6c18 424 if (rev.prune_data.nr)
afe069d1 425 paths += rev.prune_data.nr;
65056021
JH
426
427 /*
428 * Now, do the arguments look reasonable?
429 */
33055fa8 430 if (!ent.nr) {
65056021
JH
431 switch (blobs) {
432 case 0:
0569e9b8 433 result = builtin_diff_files(&rev, argc, argv);
65056021
JH
434 break;
435 case 1:
436 if (paths != 1)
437 usage(builtin_diff_usage);
887c6c18 438 result = builtin_diff_b_f(&rev, argc, argv, blob);
65056021
JH
439 break;
440 case 2:
0fe7c1de
JH
441 if (paths)
442 usage(builtin_diff_usage);
41bbf9d5 443 result = builtin_diff_blobs(&rev, argc, argv, blob);
65056021
JH
444 break;
445 default:
446 usage(builtin_diff_usage);
447 }
448 }
449 else if (blobs)
450 usage(builtin_diff_usage);
33055fa8 451 else if (ent.nr == 1)
41bbf9d5 452 result = builtin_diff_index(&rev, argc, argv);
33055fa8
MH
453 else if (ent.nr == 2)
454 result = builtin_diff_tree(&rev, argc, argv,
455 &ent.objects[0], &ent.objects[1]);
456 else if (ent.objects[0].item->flags & UNINTERESTING) {
c008c0ff
JH
457 /*
458 * diff A...B where there is at least one merge base
33055fa8
MH
459 * between A and B. We have ent.objects[0] ==
460 * merge-base, ent.objects[ents-2] == A, and
461 * ent.objects[ents-1] == B. Show diff between the
462 * base and B. Note that we pick one merge base at
463 * random if there are more than one.
c008c0ff 464 */
33055fa8
MH
465 result = builtin_diff_tree(&rev, argc, argv,
466 &ent.objects[0],
467 &ent.objects[ent.nr-1]);
c008c0ff 468 } else
41bbf9d5 469 result = builtin_diff_combined(&rev, argc, argv,
33055fa8 470 ent.objects, ent.nr);
da31b358 471 result = diff_result_code(&rev.diffopt, result);
aecbf914
JH
472 if (1 < rev.diffopt.skip_stat_unmatch)
473 refresh_index_quietly();
886e1084
474 UNLEAK(rev);
475 UNLEAK(ent);
476 UNLEAK(blob);
41bbf9d5 477 return result;
65056021 478}