blob: fa4683377ebbe51ee42d964d29a30011f12f1261 [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 */
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07006#define USE_THE_INDEX_COMPATIBILITY_MACROS
Junio C Hamano65056022006-04-28 23:20:52 -07007#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Chris Torek8bfcb3a2020-06-12 16:19:59 +00009#include "ewah/ewok.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020010#include "lockfile.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010011#include "color.h"
Junio C Hamano65056022006-04-28 23:20:52 -070012#include "commit.h"
13#include "blob.h"
14#include "tag.h"
15#include "diff.h"
Sergey Organov3b6c17b2020-12-21 18:19:39 +030016#include "diff-merges.h"
Junio C Hamano65056022006-04-28 23:20:52 -070017#include "diffcore.h"
18#include "revision.h"
19#include "log-tree.h"
20#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020021#include "submodule.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040022#include "oid-array.h"
Junio C Hamano65056022006-04-28 23:20:52 -070023
Thomas Gummerer470faf92013-12-11 10:58:42 +010024#define DIFF_NO_INDEX_EXPLICIT 1
25#define DIFF_NO_INDEX_IMPLICIT 2
26
Junio C Hamano65056022006-04-28 23:20:52 -070027static const char builtin_diff_usage[] =
Chris Torekb7e10b22020-06-12 16:20:00 +000028"git diff [<options>] [<commit>] [--] [<path>...]\n"
Denton Liueb448632021-07-10 02:28:31 -070029" or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
30" or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
Chris Torekb7e10b22020-06-12 16:20:00 +000031" or: git diff [<options>] <commit>...<commit>] [--] [<path>...]\n"
32" or: git diff [<options>] <blob> <blob>]\n"
33" or: git diff [<options>] --no-index [--] <path> <path>]\n"
34COMMON_DIFF_OPTIONS_HELP;
Junio C Hamano65056022006-04-28 23:20:52 -070035
Jeff King158b06c2017-05-19 08:59:15 -040036static const char *blob_path(struct object_array_entry *entry)
37{
38 return entry->path ? entry->path : entry->name;
39}
40
Junio C Hamano65056022006-04-28 23:20:52 -070041static void stuff_change(struct diff_options *opt,
42 unsigned old_mode, unsigned new_mode,
brian m. carlson9c4b0f62017-03-26 16:01:26 +000043 const struct object_id *old_oid,
44 const struct object_id *new_oid,
45 int old_oid_valid,
46 int new_oid_valid,
Jeff Kingd04ec742017-05-19 08:58:05 -040047 const char *old_path,
48 const char *new_path)
Junio C Hamano65056022006-04-28 23:20:52 -070049{
50 struct diff_filespec *one, *two;
51
brian m. carlson9c4b0f62017-03-26 16:01:26 +000052 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
Jeff King4a7e27e2018-08-28 17:22:40 -040053 oideq(old_oid, new_oid) && (old_mode == new_mode))
Junio C Hamano65056022006-04-28 23:20:52 -070054 return;
55
Brandon Williams0d1e0e72017-10-31 11:19:11 -070056 if (opt->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 22:40:58 +010057 SWAP(old_mode, new_mode);
brian m. carlson9c4b0f62017-03-26 16:01:26 +000058 SWAP(old_oid, new_oid);
Jeff Kingd04ec742017-05-19 08:58:05 -040059 SWAP(old_path, new_path);
Junio C Hamano65056022006-04-28 23:20:52 -070060 }
Junio C Hamanocd676a52008-02-12 14:26:02 -080061
62 if (opt->prefix &&
Jeff Kingd04ec742017-05-19 08:58:05 -040063 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
64 strncmp(new_path, opt->prefix, opt->prefix_length)))
Junio C Hamanocd676a52008-02-12 14:26:02 -080065 return;
66
Jeff Kingd04ec742017-05-19 08:58:05 -040067 one = alloc_filespec(old_path);
68 two = alloc_filespec(new_path);
Brandon Williamsf9704c22017-05-30 10:30:50 -070069 fill_filespec(one, old_oid, old_oid_valid, old_mode);
70 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Junio C Hamano65056022006-04-28 23:20:52 -070071
Junio C Hamano65056022006-04-28 23:20:52 -070072 diff_queue(&diff_queued_diff, one, two);
73}
74
75static int builtin_diff_b_f(struct rev_info *revs,
76 int argc, const char **argv,
Jeff King42f5ba52017-05-19 08:57:30 -040077 struct object_array_entry **blob)
Junio C Hamano65056022006-04-28 23:20:52 -070078{
79 /* Blob vs file in the working tree*/
80 struct stat st;
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070081 const char *path;
Junio C Hamano65056022006-04-28 23:20:52 -070082
Timo Hirvonena6107862006-06-24 20:23:06 +030083 if (argc > 1)
84 usage(builtin_diff_usage);
85
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070086 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
87 path = revs->prune_data.items[0].match;
88
Junio C Hamano65056022006-04-28 23:20:52 -070089 if (lstat(path, &st))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000090 die_errno(_("failed to stat '%s'"), path);
Junio C Hamano65056022006-04-28 23:20:52 -070091 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000092 die(_("'%s': not a regular file or symlink"), path);
Martin Koegler01618a32007-04-22 18:44:00 +020093
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070094 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
95
Jeff King42f5ba52017-05-19 08:57:30 -040096 if (blob[0]->mode == S_IFINVALID)
97 blob[0]->mode = canon_mode(st.st_mode);
Martin Koegler01618a32007-04-22 18:44:00 +020098
Junio C Hamano65056022006-04-28 23:20:52 -070099 stuff_change(&revs->diffopt,
Jeff King42f5ba52017-05-19 08:57:30 -0400100 blob[0]->mode, canon_mode(st.st_mode),
brian m. carlson14228442021-04-26 01:02:56 +0000101 &blob[0]->item->oid, null_oid(),
Jeff Kinge5450102012-07-28 11:03:01 -0400102 1, 0,
Jeff King30d005c2017-05-19 08:59:34 -0400103 blob[0]->path ? blob[0]->path : path,
104 path);
Junio C Hamano65056022006-04-28 23:20:52 -0700105 diffcore_std(&revs->diffopt);
106 diff_flush(&revs->diffopt);
107 return 0;
108}
109
110static int builtin_diff_blobs(struct rev_info *revs,
111 int argc, const char **argv,
Jeff King42f5ba52017-05-19 08:57:30 -0400112 struct object_array_entry **blob)
Junio C Hamano65056022006-04-28 23:20:52 -0700113{
Shahzad Lone33de80b2019-02-02 23:16:45 +0000114 const unsigned mode = canon_mode(S_IFREG | 0644);
Junio C Hamano65056022006-04-28 23:20:52 -0700115
Timo Hirvonena6107862006-06-24 20:23:06 +0300116 if (argc > 1)
117 usage(builtin_diff_usage);
118
Jeff King42f5ba52017-05-19 08:57:30 -0400119 if (blob[0]->mode == S_IFINVALID)
120 blob[0]->mode = mode;
Martin Koegler01618a32007-04-22 18:44:00 +0200121
Jeff King42f5ba52017-05-19 08:57:30 -0400122 if (blob[1]->mode == S_IFINVALID)
123 blob[1]->mode = mode;
Martin Koegler01618a32007-04-22 18:44:00 +0200124
Junio C Hamano65056022006-04-28 23:20:52 -0700125 stuff_change(&revs->diffopt,
Jeff King42f5ba52017-05-19 08:57:30 -0400126 blob[0]->mode, blob[1]->mode,
127 &blob[0]->item->oid, &blob[1]->item->oid,
Jeff Kinge5450102012-07-28 11:03:01 -0400128 1, 1,
Jeff King158b06c2017-05-19 08:59:15 -0400129 blob_path(blob[0]), blob_path(blob[1]));
Junio C Hamano65056022006-04-28 23:20:52 -0700130 diffcore_std(&revs->diffopt);
131 diff_flush(&revs->diffopt);
132 return 0;
133}
134
135static int builtin_diff_index(struct rev_info *revs,
136 int argc, const char **argv)
137{
Denton Liu4c3fe822020-09-20 04:22:22 -0700138 unsigned int option = 0;
Junio C Hamano65056022006-04-28 23:20:52 -0700139 while (1 < argc) {
140 const char *arg = argv[1];
David Symonds2baf1852008-10-29 09:15:36 -0700141 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
Denton Liu4c3fe822020-09-20 04:22:22 -0700142 option |= DIFF_INDEX_CACHED;
Denton Liu0f5a1d42020-09-20 04:22:25 -0700143 else if (!strcmp(arg, "--merge-base"))
144 option |= DIFF_INDEX_MERGE_BASE;
Junio C Hamano65056022006-04-28 23:20:52 -0700145 else
146 usage(builtin_diff_usage);
147 argv++; argc--;
148 }
149 /*
150 * Make sure there is one revision (i.e. pending object),
151 * and there is no revision filtering parameters.
152 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700153 if (revs->pending.nr != 1 ||
Junio C Hamano65056022006-04-28 23:20:52 -0700154 revs->max_count != -1 || revs->min_age != -1 ||
155 revs->max_age != -1)
156 usage(builtin_diff_usage);
Denton Liu4c3fe822020-09-20 04:22:22 -0700157 if (!(option & DIFF_INDEX_CACHED)) {
Karsten Blees7349afd2012-10-30 10:50:42 +0100158 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700159 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Karsten Blees7349afd2012-10-30 10:50:42 +0100160 perror("read_cache_preload");
161 return -1;
162 }
163 } else if (read_cache() < 0) {
164 perror("read_cache");
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -0800165 return -1;
166 }
Denton Liu4c3fe822020-09-20 04:22:22 -0700167 return run_diff_index(revs, option);
Junio C Hamano65056022006-04-28 23:20:52 -0700168}
169
170static int builtin_diff_tree(struct rev_info *revs,
171 int argc, const char **argv,
Michael Haggerty91de3442013-05-25 11:08:03 +0200172 struct object_array_entry *ent0,
173 struct object_array_entry *ent1)
Junio C Hamano65056022006-04-28 23:20:52 -0700174{
brian m. carlson9c4b0f62017-03-26 16:01:26 +0000175 const struct object_id *(oid[2]);
Denton Liu3d09c222020-09-14 11:36:52 -0700176 struct object_id mb_oid;
177 int merge_base = 0;
Timo Hirvonena6107862006-06-24 20:23:06 +0300178
Denton Liu3d09c222020-09-14 11:36:52 -0700179 while (1 < argc) {
180 const char *arg = argv[1];
181 if (!strcmp(arg, "--merge-base"))
182 merge_base = 1;
183 else
184 usage(builtin_diff_usage);
185 argv++; argc--;
186 }
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700187
Denton Liu3d09c222020-09-14 11:36:52 -0700188 if (merge_base) {
189 diff_get_merge_base(revs, &mb_oid);
190 oid[0] = &mb_oid;
191 oid[1] = &revs->pending.objects[1].item->oid;
192 } else {
193 int swap = 0;
194
195 /*
196 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
197 * swap them.
198 */
199 if (ent1->item->flags & UNINTERESTING)
200 swap = 1;
201 oid[swap] = &ent0->item->oid;
202 oid[1 - swap] = &ent1->item->oid;
203 }
Brandon Williams66f414f2017-05-30 10:31:03 -0700204 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
Junio C Hamano65056022006-04-28 23:20:52 -0700205 log_tree_diff_flush(revs);
206 return 0;
207}
208
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700209static int builtin_diff_combined(struct rev_info *revs,
210 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700211 struct object_array_entry *ent,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700212 int ents)
213{
brian m. carlson910650d2017-03-31 01:40:00 +0000214 struct oid_array parents = OID_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700215 int i;
216
Timo Hirvonena6107862006-06-24 20:23:06 +0300217 if (argc > 1)
218 usage(builtin_diff_usage);
219
Sergey Organov3b6c17b2020-12-21 18:19:39 +0300220 diff_merges_set_dense_combined_if_unset(revs);
221
René Scharfe0041f092011-12-17 11:15:48 +0100222 for (i = 1; i < ents; i++)
brian m. carlson910650d2017-03-31 01:40:00 +0000223 oid_array_append(&parents, &ent[i].item->oid);
Sergey Organovd01141d2020-09-29 14:31:22 +0300224 diff_tree_combined(&ent[0].item->oid, &parents, revs);
brian m. carlson910650d2017-03-31 01:40:00 +0000225 oid_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700226 return 0;
227}
228
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700229static void refresh_index_quietly(void)
230{
Martin Ågren837e34e2017-10-05 22:32:04 +0200231 struct lock_file lock_file = LOCK_INIT;
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700232 int fd;
233
Martin Ågren837e34e2017-10-05 22:32:04 +0200234 fd = hold_locked_index(&lock_file, 0);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700235 if (fd < 0)
236 return;
237 discard_cache();
238 read_cache();
239 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +0700240 repo_update_index_if_able(the_repository, &lock_file);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700241}
242
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700243static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
244{
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700245 unsigned int options = 0;
246
247 while (1 < argc && argv[1][0] == '-') {
248 if (!strcmp(argv[1], "--base"))
249 revs->max_count = 1;
250 else if (!strcmp(argv[1], "--ours"))
251 revs->max_count = 2;
252 else if (!strcmp(argv[1], "--theirs"))
253 revs->max_count = 3;
254 else if (!strcmp(argv[1], "-q"))
255 options |= DIFF_SILENT_ON_REMOVED;
Matthieu Moy1c370ea2009-08-06 12:47:21 +0200256 else if (!strcmp(argv[1], "-h"))
257 usage(builtin_diff_usage);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700258 else
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000259 return error(_("invalid option: %s"), argv[1]);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700260 argv++; argc--;
261 }
262
Junio C Hamano903e09a2008-09-18 00:32:37 -0700263 /*
264 * "diff --base" should not combine merges because it was not
265 * asked to. "diff -c" should not densify (if the user wants
266 * dense one, --cc can be explicitly asked for, or just rely
267 * on the default).
268 */
Sergey Organov3b6c17b2020-12-21 18:19:39 +0300269 if (revs->max_count == -1 &&
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700270 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
Sergey Organov3b6c17b2020-12-21 18:19:39 +0300271 diff_merges_set_dense_combined_if_unset(revs);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700272
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +0700273 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700274 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -0800275 perror("read_cache_preload");
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700276 return -1;
277 }
Junio C Hamanoe7c3a592011-03-22 14:17:30 -0700278 return run_diff_files(revs, options);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700279}
280
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000281struct symdiff {
282 struct bitmap *skip;
283 int warn;
284 const char *base, *left, *right;
285};
286
287/*
288 * Check for symmetric-difference arguments, and if present, arrange
289 * everything we need to know to handle them correctly. As a bonus,
290 * weed out all bogus range-based revision specifications, e.g.,
291 * "git diff A..B C..D" or "git diff A..B C" get rejected.
292 *
293 * For an actual symmetric diff, *symdiff is set this way:
294 *
295 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
296 * indices that the caller should ignore (extra merge bases, of
297 * which there might be many, and A in A...B). Note that the
298 * chosen merge base and right side are NOT marked.
299 * - warn is set if there are multiple merge bases.
300 * - base, left, and right point to the names to use in a
301 * warning about multiple merge bases.
302 *
303 * If there is no symmetric diff argument, sym->skip is NULL and
304 * sym->warn is cleared. The remaining fields are not set.
305 */
306static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
307{
308 int i, is_symdiff = 0, basecount = 0, othercount = 0;
309 int lpos = -1, rpos = -1, basepos = -1;
310 struct bitmap *map = NULL;
311
312 /*
313 * Use the whence fields to find merge bases and left and
314 * right parts of symmetric difference, so that we do not
315 * depend on the order that revisions are parsed. If there
316 * are any revs that aren't from these sources, we have a
317 * "git diff C A...B" or "git diff A...B C" case. Or we
318 * could even get "git diff A...B C...E", for instance.
319 *
320 * If we don't have just one merge base, we pick one
321 * at random.
322 *
323 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
324 * so we must check for SYMMETRIC_LEFT too. The two arrays
325 * rev->pending.objects and rev->cmdline.rev are parallel.
326 */
327 for (i = 0; i < rev->cmdline.nr; i++) {
328 struct object *obj = rev->pending.objects[i].item;
329 switch (rev->cmdline.rev[i].whence) {
330 case REV_CMD_MERGE_BASE:
331 if (basepos < 0)
332 basepos = i;
333 basecount++;
334 break; /* do mark all bases */
335 case REV_CMD_LEFT:
336 if (lpos >= 0)
337 usage(builtin_diff_usage);
338 lpos = i;
339 if (obj->flags & SYMMETRIC_LEFT) {
340 is_symdiff = 1;
341 break; /* do mark A */
342 }
343 continue;
344 case REV_CMD_RIGHT:
345 if (rpos >= 0)
346 usage(builtin_diff_usage);
347 rpos = i;
348 continue; /* don't mark B */
349 case REV_CMD_PARENTS_ONLY:
350 case REV_CMD_REF:
351 case REV_CMD_REV:
352 othercount++;
353 continue;
354 }
355 if (map == NULL)
356 map = bitmap_new();
357 bitmap_set(map, i);
358 }
359
360 /*
361 * Forbid any additional revs for both A...B and A..B.
362 */
363 if (lpos >= 0 && othercount > 0)
364 usage(builtin_diff_usage);
365
366 if (!is_symdiff) {
367 bitmap_free(map);
368 sym->warn = 0;
369 sym->skip = NULL;
370 return;
371 }
372
373 sym->left = rev->pending.objects[lpos].name;
374 sym->right = rev->pending.objects[rpos].name;
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000375 if (basecount == 0)
376 die(_("%s...%s: no merge base"), sym->left, sym->right);
Jeff King5f46e612020-07-08 00:38:19 -0400377 sym->base = rev->pending.objects[basepos].name;
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000378 bitmap_unset(map, basepos); /* unmark the base we want */
379 sym->warn = basecount > 1;
380 sym->skip = map;
381}
382
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700383int cmd_diff(int argc, const char **argv, const char *prefix)
Junio C Hamano65056022006-04-28 23:20:52 -0700384{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700385 int i;
Junio C Hamano65056022006-04-28 23:20:52 -0700386 struct rev_info rev;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200387 struct object_array ent = OBJECT_ARRAY_INIT;
388 int blobs = 0, paths = 0;
Jeff King42f5ba52017-05-19 08:57:30 -0400389 struct object_array_entry *blob[2];
Thomas Gummerer470faf92013-12-11 10:58:42 +0100390 int nongit = 0, no_index = 0;
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100391 int result = 0;
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000392 struct symdiff sdiff;
Junio C Hamano65056022006-04-28 23:20:52 -0700393
394 /*
395 * We could get N tree-ish in the rev.pending_objects list.
Denton Liua9d76892020-06-18 06:43:34 -0400396 * Also there could be M blobs there, and P pathspecs. --cached may
397 * also be present.
Junio C Hamano65056022006-04-28 23:20:52 -0700398 *
399 * N=0, M=0:
Denton Liua9d76892020-06-18 06:43:34 -0400400 * cache vs files (diff-files)
401 *
402 * N=0, M=0, --cached:
403 * HEAD vs cache (diff-index --cached)
404 *
Junio C Hamano65056022006-04-28 23:20:52 -0700405 * N=0, M=2:
406 * compare two random blobs. P must be zero.
Denton Liua9d76892020-06-18 06:43:34 -0400407 *
Junio C Hamano65056022006-04-28 23:20:52 -0700408 * N=0, M=1, P=1:
Denton Liua9d76892020-06-18 06:43:34 -0400409 * compare a blob with a working tree file.
Junio C Hamano65056022006-04-28 23:20:52 -0700410 *
411 * N=1, M=0:
Denton Liua9d76892020-06-18 06:43:34 -0400412 * tree vs files (diff-index)
Junio C Hamano65056022006-04-28 23:20:52 -0700413 *
Denton Liua9d76892020-06-18 06:43:34 -0400414 * N=1, M=0, --cached:
Junio C Hamano65056022006-04-28 23:20:52 -0700415 * tree vs cache (diff-index --cached)
416 *
417 * N=2, M=0:
418 * tree vs tree (diff-tree)
419 *
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700420 * N=0, M=0, P=2:
421 * compare two filesystem entities (aka --no-index).
422 *
Junio C Hamano65056022006-04-28 23:20:52 -0700423 * Other cases are errors.
424 */
Junio C Hamano230f5442006-05-03 23:54:34 -0700425
Thomas Gummerer470faf92013-12-11 10:58:42 +0100426 /* Were we asked to do --no-index explicitly? */
427 for (i = 1; i < argc; i++) {
428 if (!strcmp(argv[i], "--")) {
429 i++;
430 break;
431 }
432 if (!strcmp(argv[i], "--no-index"))
433 no_index = DIFF_NO_INDEX_EXPLICIT;
434 if (argv[i][0] != '-')
435 break;
436 }
437
Jeff King28a4e582016-09-12 20:23:36 -0700438 prefix = setup_git_directory_gently(&nongit);
Thomas Gummerer6df57622013-12-11 10:58:43 +0100439
Lessley Dennington51ba65b2021-12-06 15:56:00 +0000440 if (!nongit) {
441 prepare_repo_settings(the_repository);
442 the_repository->settings.command_requires_full_index = 0;
443 }
444
Jeff King28a4e582016-09-12 20:23:36 -0700445 if (!no_index) {
Jeff King475b3622016-09-12 20:23:27 -0700446 /*
447 * Treat git diff with at least one path outside of the
448 * repo the same as if the command would have been executed
449 * outside of a git repository. In this case it behaves
450 * the same way as "git diff --no-index <a> <b>", which acts
451 * as a colourful "diff" replacement.
452 */
453 if (nongit || ((argc == i + 2) &&
454 (!path_inside_repo(prefix, argv[i]) ||
455 !path_inside_repo(prefix, argv[i + 1]))))
456 no_index = DIFF_NO_INDEX_IMPLICIT;
457 }
Thomas Gummerer470faf92013-12-11 10:58:42 +0100458
Matthieu Moy5404c112016-02-25 09:59:21 +0100459 init_diff_ui_defaults();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100460 git_config(git_diff_ui_config, NULL);
Torsten Bögershausen5c327502021-02-03 17:28:23 +0100461 prefix = precompose_argv_prefix(argc, argv, prefix);
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100462
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200463 repo_init_revisions(the_repository, &rev, prefix);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700464
Jeff King287ab282019-02-16 01:57:56 -0500465 /* Set up defaults that will apply to both no-index and regular diffs. */
Zbigniew Jędrzejewski-Szmekaf9fedc2012-03-01 13:26:39 +0100466 rev.diffopt.stat_width = -1;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100467 rev.diffopt.stat_graph_width = -1;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700468 rev.diffopt.flags.allow_external = 1;
469 rev.diffopt.flags.allow_textconv = 1;
Junio C Hamano61af4942008-11-26 09:58:41 -0800470
Jeff King287ab282019-02-16 01:57:56 -0500471 /* If this is a no-index diff, just run it and exit there. */
472 if (no_index)
Junio C Hamanodcd6a8c2019-04-25 16:41:12 +0900473 exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
474 argc, argv));
475
Jeff King287ab282019-02-16 01:57:56 -0500476
477 /*
478 * Otherwise, we are doing the usual "git" diff; set up any
479 * further defaults that apply to regular diffs.
480 */
481 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
482
Nguyễn Thái Ngọc Duy0231ae72018-05-26 14:08:44 +0200483 /*
484 * Default to intent-to-add entries invisible in the
485 * index. This makes them show up as new files in diff-files
486 * and not at all in diff-cached.
487 */
488 rev.diffopt.ita_invisible_in_index = 1;
489
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700490 if (nongit)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000491 die(_("Not a git repository"));
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700492 argc = setup_revisions(argc, argv, &rev, NULL);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700493 if (!rev.diffopt.output_format) {
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300494 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
Thomas Rast28452652012-08-03 14:16:24 +0200495 diff_setup_done(&rev.diffopt);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700496 }
Junio C Hamano61af4942008-11-26 09:58:41 -0800497
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700498 rev.diffopt.flags.recursive = 1;
Junio C Hamano1eb41362021-02-11 11:57:50 -0800499 rev.diffopt.rotate_to_strict = 1;
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300500
Jeff King1af3d972012-06-15 16:29:48 -0400501 setup_diff_pager(&rev.diffopt);
René Scharfe89d07f72007-08-12 19:46:55 +0200502
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700503 /*
504 * Do we have --cached and not have a pending object, then
Junio C Hamano65056022006-04-28 23:20:52 -0700505 * default to HEAD by hand. Eek.
506 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700507 if (!rev.pending.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700508 int i;
509 for (i = 1; i < argc; i++) {
510 const char *arg = argv[i];
511 if (!strcmp(arg, "--"))
512 break;
David Symonds2baf1852008-10-29 09:15:36 -0700513 else if (!strcmp(arg, "--cached") ||
514 !strcmp(arg, "--staged")) {
Junio C Hamano3384a2d2007-12-11 10:09:04 -0800515 add_head_to_pending(&rev);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700516 if (!rev.pending.nr) {
517 struct tree *tree;
Stefan Bellerf86bcc72018-06-28 18:21:56 -0700518 tree = lookup_tree(the_repository,
519 the_repository->hash_algo->empty_tree);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700520 add_pending_object(&rev, &tree->object, "HEAD");
521 }
Junio C Hamano65056022006-04-28 23:20:52 -0700522 break;
523 }
524 }
525 }
526
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000527 symdiff_prepare(&rev, &sdiff);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700528 for (i = 0; i < rev.pending.nr; i++) {
Michael Haggerty026f09e2013-05-25 11:08:05 +0200529 struct object_array_entry *entry = &rev.pending.objects[i];
530 struct object *obj = entry->item;
531 const char *name = entry->name;
Junio C Hamano65056022006-04-28 23:20:52 -0700532 int flags = (obj->flags & UNINTERESTING);
533 if (!obj->parsed)
Stefan Beller109cd762018-06-28 18:21:51 -0700534 obj = parse_object(the_repository, &obj->oid);
Stefan Bellera74093d2018-06-28 18:22:05 -0700535 obj = deref_tag(the_repository, obj, NULL, 0);
Junio C Hamano65056022006-04-28 23:20:52 -0700536 if (!obj)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000537 die(_("invalid object '%s' given."), name);
Linus Torvalds19746322006-07-11 20:45:31 -0700538 if (obj->type == OBJ_COMMIT)
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000539 obj = &get_commit_tree(((struct commit *)obj))->object;
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200540
Linus Torvalds19746322006-07-11 20:45:31 -0700541 if (obj->type == OBJ_TREE) {
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000542 if (sdiff.skip && bitmap_get(sdiff.skip, i))
543 continue;
Junio C Hamano65056022006-04-28 23:20:52 -0700544 obj->flags |= flags;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200545 add_object_array(obj, name, &ent);
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200546 } else if (obj->type == OBJ_BLOB) {
Junio C Hamano65056022006-04-28 23:20:52 -0700547 if (2 <= blobs)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000548 die(_("more than two blobs given: '%s'"), name);
Jeff King42f5ba52017-05-19 08:57:30 -0400549 blob[blobs] = entry;
Junio C Hamano65056022006-04-28 23:20:52 -0700550 blobs++;
Junio C Hamano230f5442006-05-03 23:54:34 -0700551
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200552 } else {
553 die(_("unhandled object '%s' given."), name);
Junio C Hamano65056022006-04-28 23:20:52 -0700554 }
Junio C Hamano65056022006-04-28 23:20:52 -0700555 }
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +0700556 if (rev.prune_data.nr)
Nguyễn Thái Ngọc Duyafe069d2010-12-17 19:43:06 +0700557 paths += rev.prune_data.nr;
Junio C Hamano65056022006-04-28 23:20:52 -0700558
559 /*
560 * Now, do the arguments look reasonable?
561 */
Michael Haggerty33055fa2013-05-25 11:08:04 +0200562 if (!ent.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700563 switch (blobs) {
564 case 0:
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700565 result = builtin_diff_files(&rev, argc, argv);
Junio C Hamano65056022006-04-28 23:20:52 -0700566 break;
567 case 1:
568 if (paths != 1)
569 usage(builtin_diff_usage);
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +0700570 result = builtin_diff_b_f(&rev, argc, argv, blob);
Junio C Hamano65056022006-04-28 23:20:52 -0700571 break;
572 case 2:
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700573 if (paths)
574 usage(builtin_diff_usage);
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100575 result = builtin_diff_blobs(&rev, argc, argv, blob);
Junio C Hamano65056022006-04-28 23:20:52 -0700576 break;
577 default:
578 usage(builtin_diff_usage);
579 }
580 }
581 else if (blobs)
582 usage(builtin_diff_usage);
Michael Haggerty33055fa2013-05-25 11:08:04 +0200583 else if (ent.nr == 1)
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100584 result = builtin_diff_index(&rev, argc, argv);
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000585 else if (ent.nr == 2) {
586 if (sdiff.warn)
587 warning(_("%s...%s: multiple merge bases, using %s"),
588 sdiff.left, sdiff.right, sdiff.base);
Michael Haggerty33055fa2013-05-25 11:08:04 +0200589 result = builtin_diff_tree(&rev, argc, argv,
590 &ent.objects[0], &ent.objects[1]);
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700591 } else
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100592 result = builtin_diff_combined(&rev, argc, argv,
Michael Haggerty33055fa2013-05-25 11:08:04 +0200593 ent.objects, ent.nr);
Junio C Hamanoda31b352007-12-13 23:40:27 -0800594 result = diff_result_code(&rev.diffopt, result);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700595 if (1 < rev.diffopt.skip_stat_unmatch)
596 refresh_index_quietly();
Martin Ågren886e1082017-10-01 19:42:08 +0200597 UNLEAK(rev);
598 UNLEAK(ent);
599 UNLEAK(blob);
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100600 return result;
Junio C Hamano65056022006-04-28 23:20:52 -0700601}