]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff.c
config: don't include config.h by default
[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 24struct blobinfo {
9c4b0f66 25 struct object_id oid;
65056021 26 const char *name;
01618a3a 27 unsigned mode;
65056021
JH
28};
29
30static const char builtin_diff_usage[] =
9edb8a0f 31"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
65056021 32
65056021
JH
33static void stuff_change(struct diff_options *opt,
34 unsigned old_mode, unsigned new_mode,
9c4b0f66 35 const struct object_id *old_oid,
36 const struct object_id *new_oid,
37 int old_oid_valid,
38 int new_oid_valid,
65056021
JH
39 const char *old_name,
40 const char *new_name)
41{
42 struct diff_filespec *one, *two;
43
9c4b0f66 44 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
45 !oidcmp(old_oid, new_oid) && (old_mode == new_mode))
65056021
JH
46 return;
47
8f67f8ae 48 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
35d803bc 49 SWAP(old_mode, new_mode);
9c4b0f66 50 SWAP(old_oid, new_oid);
35d803bc 51 SWAP(old_name, new_name);
65056021 52 }
cd676a51
JH
53
54 if (opt->prefix &&
55 (strncmp(old_name, opt->prefix, opt->prefix_length) ||
56 strncmp(new_name, opt->prefix, opt->prefix_length)))
57 return;
58
65056021
JH
59 one = alloc_filespec(old_name);
60 two = alloc_filespec(new_name);
9c4b0f66 61 fill_filespec(one, old_oid->hash, old_oid_valid, old_mode);
62 fill_filespec(two, new_oid->hash, new_oid_valid, new_mode);
65056021 63
65056021
JH
64 diff_queue(&diff_queued_diff, one, two);
65}
66
67static int builtin_diff_b_f(struct rev_info *revs,
68 int argc, const char **argv,
887c6c18 69 struct blobinfo *blob)
65056021
JH
70{
71 /* Blob vs file in the working tree*/
72 struct stat st;
887c6c18 73 const char *path;
65056021 74
a610786f
TH
75 if (argc > 1)
76 usage(builtin_diff_usage);
77
887c6c18
NTND
78 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
79 path = revs->prune_data.items[0].match;
80
65056021 81 if (lstat(path, &st))
54214529 82 die_errno(_("failed to stat '%s'"), path);
65056021 83 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
54214529 84 die(_("'%s': not a regular file or symlink"), path);
01618a3a 85
a5a818ee
JH
86 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
87
01618a3a
MK
88 if (blob[0].mode == S_IFINVALID)
89 blob[0].mode = canon_mode(st.st_mode);
90
65056021 91 stuff_change(&revs->diffopt,
01618a3a 92 blob[0].mode, canon_mode(st.st_mode),
9c4b0f66 93 &blob[0].oid, &null_oid,
e5450100 94 1, 0,
065e0b12 95 path, 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,
103 struct blobinfo *blob)
104{
65056021
JH
105 unsigned mode = canon_mode(S_IFREG | 0644);
106
a610786f
TH
107 if (argc > 1)
108 usage(builtin_diff_usage);
109
01618a3a
MK
110 if (blob[0].mode == S_IFINVALID)
111 blob[0].mode = mode;
112
113 if (blob[1].mode == S_IFINVALID)
114 blob[1].mode = mode;
115
65056021 116 stuff_change(&revs->diffopt,
01618a3a 117 blob[0].mode, blob[1].mode,
9c4b0f66 118 &blob[0].oid, &blob[1].oid,
e5450100 119 1, 1,
53dd8a9c 120 blob[0].name, blob[1].name);
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;
178 diff_tree_sha1(oid[0]->hash, oid[1]->hash, "", &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);
ed1c9977 198 diff_tree_combined(ent[0].item->oid.hash, &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{
206 struct lock_file *lock_file;
207 int fd;
208
209 lock_file = xcalloc(1, sizeof(struct lock_file));
210 fd = hold_locked_index(lock_file, 0);
211 if (fd < 0)
212 return;
213 discard_cache();
214 read_cache();
215 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
ccdc4ec3 216 update_index_if_able(&the_index, lock_file);
aecbf914
JH
217}
218
0569e9b8
JH
219static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
220{
0569e9b8
JH
221 unsigned int options = 0;
222
223 while (1 < argc && argv[1][0] == '-') {
224 if (!strcmp(argv[1], "--base"))
225 revs->max_count = 1;
226 else if (!strcmp(argv[1], "--ours"))
227 revs->max_count = 2;
228 else if (!strcmp(argv[1], "--theirs"))
229 revs->max_count = 3;
230 else if (!strcmp(argv[1], "-q"))
231 options |= DIFF_SILENT_ON_REMOVED;
1c370ea4
MM
232 else if (!strcmp(argv[1], "-h"))
233 usage(builtin_diff_usage);
0569e9b8 234 else
54214529 235 return error(_("invalid option: %s"), argv[1]);
0569e9b8
JH
236 argv++; argc--;
237 }
238
903e09a3
JH
239 /*
240 * "diff --base" should not combine merges because it was not
241 * asked to. "diff -c" should not densify (if the user wants
242 * dense one, --cc can be explicitly asked for, or just rely
243 * on the default).
244 */
245 if (revs->max_count == -1 && !revs->combine_merges &&
0569e9b8
JH
246 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
247 revs->combine_merges = revs->dense_combined_merges = 1;
248
4f38f6b5 249 setup_work_tree();
5ab2a2da 250 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
671c9b7e 251 perror("read_cache_preload");
0569e9b8
JH
252 return -1;
253 }
e7c3a59c 254 return run_diff_files(revs, options);
0569e9b8
JH
255}
256
a633fca0 257int cmd_diff(int argc, const char **argv, const char *prefix)
65056021 258{
1f1e895f 259 int i;
65056021 260 struct rev_info rev;
33055fa8
MH
261 struct object_array ent = OBJECT_ARRAY_INIT;
262 int blobs = 0, paths = 0;
65056021 263 struct blobinfo blob[2];
470faf96 264 int nongit = 0, no_index = 0;
41bbf9d5 265 int result = 0;
65056021
JH
266
267 /*
268 * We could get N tree-ish in the rev.pending_objects list.
269 * Also there could be M blobs there, and P pathspecs.
270 *
271 * N=0, M=0:
272 * cache vs files (diff-files)
273 * N=0, M=2:
274 * compare two random blobs. P must be zero.
275 * N=0, M=1, P=1:
276 * compare a blob with a working tree file.
277 *
278 * N=1, M=0:
279 * tree vs cache (diff-index --cached)
280 *
281 * N=2, M=0:
282 * tree vs tree (diff-tree)
283 *
0569e9b8
JH
284 * N=0, M=0, P=2:
285 * compare two filesystem entities (aka --no-index).
286 *
65056021
JH
287 * Other cases are errors.
288 */
230f544e 289
470faf96
TG
290 /* Were we asked to do --no-index explicitly? */
291 for (i = 1; i < argc; i++) {
292 if (!strcmp(argv[i], "--")) {
293 i++;
294 break;
295 }
296 if (!strcmp(argv[i], "--no-index"))
297 no_index = DIFF_NO_INDEX_EXPLICIT;
298 if (argv[i][0] != '-')
299 break;
300 }
301
28a4e580 302 prefix = setup_git_directory_gently(&nongit);
6df5762d 303
28a4e580 304 if (!no_index) {
475b362c
JK
305 /*
306 * Treat git diff with at least one path outside of the
307 * repo the same as if the command would have been executed
308 * outside of a git repository. In this case it behaves
309 * the same way as "git diff --no-index <a> <b>", which acts
310 * as a colourful "diff" replacement.
311 */
312 if (nongit || ((argc == i + 2) &&
313 (!path_inside_repo(prefix, argv[i]) ||
314 !path_inside_repo(prefix, argv[i + 1]))))
315 no_index = DIFF_NO_INDEX_IMPLICIT;
316 }
470faf96 317
6df5762d
TG
318 if (!no_index)
319 gitmodules_config();
5404c116 320 init_diff_ui_defaults();
ef90d6d4 321 git_config(git_diff_ui_config, NULL);
90a78b83 322 precompose_argv(argc, argv);
6b2f2d98 323
db6296a5 324 init_revisions(&rev, prefix);
0569e9b8 325
aad90e85
TG
326 if (no_index && argc != i + 2) {
327 if (no_index == DIFF_NO_INDEX_IMPLICIT) {
328 /*
329 * There was no --no-index and there were not two
330 * paths. It is possible that the user intended
331 * to do an inside-repository operation.
332 */
333 fprintf(stderr, "Not a git repository\n");
334 fprintf(stderr,
335 "To compare two paths outside a working tree:\n");
470faf96 336 }
aad90e85
TG
337 /* Give the usage message for non-repository usage and exit. */
338 usagef("git diff %s <path> <path>",
339 no_index == DIFF_NO_INDEX_EXPLICIT ?
340 "--no-index" : "[--no-index]");
341
342 }
343 if (no_index)
470faf96 344 /* If this is a no-index diff, just run it and exit there. */
e5f7a5d1 345 diff_no_index(&rev, argc, argv);
0569e9b8
JH
346
347 /* Otherwise, we are doing the usual "git" diff */
aecbf914 348 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
65056021 349
df44483a 350 /* Scale to real terminal size and respect statGraphWidth config */
af9fedc1 351 rev.diffopt.stat_width = -1;
df44483a 352 rev.diffopt.stat_graph_width = -1;
af9fedc1 353
5ec11af6 354 /* Default to let external and textconv be used */
61af494c 355 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
5ec11af6 356 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
61af494c 357
0569e9b8 358 if (nongit)
54214529 359 die(_("Not a git repository"));
0569e9b8 360 argc = setup_revisions(argc, argv, &rev, NULL);
047fbe90 361 if (!rev.diffopt.output_format) {
c9b5ef99 362 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
28452655 363 diff_setup_done(&rev.diffopt);
047fbe90 364 }
61af494c 365
8f67f8ae 366 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
c9b5ef99 367
1af3d977 368 setup_diff_pager(&rev.diffopt);
89d07f75 369
0569e9b8
JH
370 /*
371 * Do we have --cached and not have a pending object, then
65056021
JH
372 * default to HEAD by hand. Eek.
373 */
1f1e895f 374 if (!rev.pending.nr) {
65056021
JH
375 int i;
376 for (i = 1; i < argc; i++) {
377 const char *arg = argv[i];
378 if (!strcmp(arg, "--"))
379 break;
2baf1850
DS
380 else if (!strcmp(arg, "--cached") ||
381 !strcmp(arg, "--staged")) {
3384a2df 382 add_head_to_pending(&rev);
a2b7a3b3
NTND
383 if (!rev.pending.nr) {
384 struct tree *tree;
cba595bd 385 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
a2b7a3b3
NTND
386 add_pending_object(&rev, &tree->object, "HEAD");
387 }
65056021
JH
388 break;
389 }
390 }
391 }
392
1f1e895f 393 for (i = 0; i < rev.pending.nr; i++) {
026f09e7
MH
394 struct object_array_entry *entry = &rev.pending.objects[i];
395 struct object *obj = entry->item;
396 const char *name = entry->name;
65056021
JH
397 int flags = (obj->flags & UNINTERESTING);
398 if (!obj->parsed)
ed1c9977 399 obj = parse_object(obj->oid.hash);
65056021
JH
400 obj = deref_tag(obj, NULL, 0);
401 if (!obj)
54214529 402 die(_("invalid object '%s' given."), name);
1974632c 403 if (obj->type == OBJ_COMMIT)
65056021 404 obj = &((struct commit *)obj)->tree->object;
5b1e14ea 405
1974632c 406 if (obj->type == OBJ_TREE) {
65056021 407 obj->flags |= flags;
33055fa8 408 add_object_array(obj, name, &ent);
5b1e14ea 409 } else if (obj->type == OBJ_BLOB) {
65056021 410 if (2 <= blobs)
54214529 411 die(_("more than two blobs given: '%s'"), name);
9c4b0f66 412 hashcpy(blob[blobs].oid.hash, obj->oid.hash);
65056021 413 blob[blobs].name = name;
026f09e7 414 blob[blobs].mode = entry->mode;
65056021 415 blobs++;
230f544e 416
5b1e14ea
MH
417 } else {
418 die(_("unhandled object '%s' given."), name);
65056021 419 }
65056021 420 }
887c6c18 421 if (rev.prune_data.nr)
afe069d1 422 paths += rev.prune_data.nr;
65056021
JH
423
424 /*
425 * Now, do the arguments look reasonable?
426 */
33055fa8 427 if (!ent.nr) {
65056021
JH
428 switch (blobs) {
429 case 0:
0569e9b8 430 result = builtin_diff_files(&rev, argc, argv);
65056021
JH
431 break;
432 case 1:
433 if (paths != 1)
434 usage(builtin_diff_usage);
887c6c18 435 result = builtin_diff_b_f(&rev, argc, argv, blob);
65056021
JH
436 break;
437 case 2:
0fe7c1de
JH
438 if (paths)
439 usage(builtin_diff_usage);
41bbf9d5 440 result = builtin_diff_blobs(&rev, argc, argv, blob);
65056021
JH
441 break;
442 default:
443 usage(builtin_diff_usage);
444 }
445 }
446 else if (blobs)
447 usage(builtin_diff_usage);
33055fa8 448 else if (ent.nr == 1)
41bbf9d5 449 result = builtin_diff_index(&rev, argc, argv);
33055fa8
MH
450 else if (ent.nr == 2)
451 result = builtin_diff_tree(&rev, argc, argv,
452 &ent.objects[0], &ent.objects[1]);
453 else if (ent.objects[0].item->flags & UNINTERESTING) {
c008c0ff
JH
454 /*
455 * diff A...B where there is at least one merge base
33055fa8
MH
456 * between A and B. We have ent.objects[0] ==
457 * merge-base, ent.objects[ents-2] == A, and
458 * ent.objects[ents-1] == B. Show diff between the
459 * base and B. Note that we pick one merge base at
460 * random if there are more than one.
c008c0ff 461 */
33055fa8
MH
462 result = builtin_diff_tree(&rev, argc, argv,
463 &ent.objects[0],
464 &ent.objects[ent.nr-1]);
c008c0ff 465 } else
41bbf9d5 466 result = builtin_diff_combined(&rev, argc, argv,
33055fa8 467 ent.objects, ent.nr);
da31b358 468 result = diff_result_code(&rev.diffopt, result);
aecbf914
JH
469 if (1 < rev.diffopt.skip_stat_unmatch)
470 refresh_index_quietly();
41bbf9d5 471 return result;
65056021 472}