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