blob: 7cde6abbcf7651af8313bd3d70eb1944e72c9cb3 [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 */
6#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07007#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02008#include "lockfile.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +01009#include "color.h"
Junio C Hamano65056022006-04-28 23:20:52 -070010#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"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020018#include "submodule.h"
René Scharfe0041f092011-12-17 11:15:48 +010019#include "sha1-array.h"
Junio C Hamano65056022006-04-28 23:20:52 -070020
Thomas Gummerer470faf92013-12-11 10:58:42 +010021#define DIFF_NO_INDEX_EXPLICIT 1
22#define DIFF_NO_INDEX_IMPLICIT 2
23
Junio C Hamano65056022006-04-28 23:20:52 -070024static const char builtin_diff_usage[] =
Štěpán Němec9edb8a02010-11-04 18:18:17 +010025"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
Junio C Hamano65056022006-04-28 23:20:52 -070026
Jeff King158b06c2017-05-19 08:59:15 -040027static const char *blob_path(struct object_array_entry *entry)
28{
29 return entry->path ? entry->path : entry->name;
30}
31
Junio C Hamano65056022006-04-28 23:20:52 -070032static void stuff_change(struct diff_options *opt,
33 unsigned old_mode, unsigned new_mode,
brian m. carlson9c4b0f62017-03-26 16:01:26 +000034 const struct object_id *old_oid,
35 const struct object_id *new_oid,
36 int old_oid_valid,
37 int new_oid_valid,
Jeff Kingd04ec742017-05-19 08:58:05 -040038 const char *old_path,
39 const char *new_path)
Junio C Hamano65056022006-04-28 23:20:52 -070040{
41 struct diff_filespec *one, *two;
42
brian m. carlson9c4b0f62017-03-26 16:01:26 +000043 if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
44 !oidcmp(old_oid, new_oid) && (old_mode == new_mode))
Junio C Hamano65056022006-04-28 23:20:52 -070045 return;
46
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010047 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
René Scharfe35d803b2017-01-28 22:40:58 +010048 SWAP(old_mode, new_mode);
brian m. carlson9c4b0f62017-03-26 16:01:26 +000049 SWAP(old_oid, new_oid);
Jeff Kingd04ec742017-05-19 08:58:05 -040050 SWAP(old_path, new_path);
Junio C Hamano65056022006-04-28 23:20:52 -070051 }
Junio C Hamanocd676a52008-02-12 14:26:02 -080052
53 if (opt->prefix &&
Jeff Kingd04ec742017-05-19 08:58:05 -040054 (strncmp(old_path, opt->prefix, opt->prefix_length) ||
55 strncmp(new_path, opt->prefix, opt->prefix_length)))
Junio C Hamanocd676a52008-02-12 14:26:02 -080056 return;
57
Jeff Kingd04ec742017-05-19 08:58:05 -040058 one = alloc_filespec(old_path);
59 two = alloc_filespec(new_path);
Brandon Williamsf9704c22017-05-30 10:30:50 -070060 fill_filespec(one, old_oid, old_oid_valid, old_mode);
61 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Junio C Hamano65056022006-04-28 23:20:52 -070062
Junio C Hamano65056022006-04-28 23:20:52 -070063 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,
Jeff King42f5ba52017-05-19 08:57:30 -040068 struct object_array_entry **blob)
Junio C Hamano65056022006-04-28 23:20:52 -070069{
70 /* Blob vs file in the working tree*/
71 struct stat st;
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070072 const char *path;
Junio C Hamano65056022006-04-28 23:20:52 -070073
Timo Hirvonena6107862006-06-24 20:23:06 +030074 if (argc > 1)
75 usage(builtin_diff_usage);
76
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070077 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
78 path = revs->prune_data.items[0].match;
79
Junio C Hamano65056022006-04-28 23:20:52 -070080 if (lstat(path, &st))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000081 die_errno(_("failed to stat '%s'"), path);
Junio C Hamano65056022006-04-28 23:20:52 -070082 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000083 die(_("'%s': not a regular file or symlink"), path);
Martin Koegler01618a32007-04-22 18:44:00 +020084
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070085 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
86
Jeff King42f5ba52017-05-19 08:57:30 -040087 if (blob[0]->mode == S_IFINVALID)
88 blob[0]->mode = canon_mode(st.st_mode);
Martin Koegler01618a32007-04-22 18:44:00 +020089
Junio C Hamano65056022006-04-28 23:20:52 -070090 stuff_change(&revs->diffopt,
Jeff King42f5ba52017-05-19 08:57:30 -040091 blob[0]->mode, canon_mode(st.st_mode),
92 &blob[0]->item->oid, &null_oid,
Jeff Kinge5450102012-07-28 11:03:01 -040093 1, 0,
Jeff King30d005c2017-05-19 08:59:34 -040094 blob[0]->path ? blob[0]->path : path,
95 path);
Junio C Hamano65056022006-04-28 23:20:52 -070096 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,
Jeff King42f5ba52017-05-19 08:57:30 -0400103 struct object_array_entry **blob)
Junio C Hamano65056022006-04-28 23:20:52 -0700104{
Junio C Hamano65056022006-04-28 23:20:52 -0700105 unsigned mode = canon_mode(S_IFREG | 0644);
106
Timo Hirvonena6107862006-06-24 20:23:06 +0300107 if (argc > 1)
108 usage(builtin_diff_usage);
109
Jeff King42f5ba52017-05-19 08:57:30 -0400110 if (blob[0]->mode == S_IFINVALID)
111 blob[0]->mode = mode;
Martin Koegler01618a32007-04-22 18:44:00 +0200112
Jeff King42f5ba52017-05-19 08:57:30 -0400113 if (blob[1]->mode == S_IFINVALID)
114 blob[1]->mode = mode;
Martin Koegler01618a32007-04-22 18:44:00 +0200115
Junio C Hamano65056022006-04-28 23:20:52 -0700116 stuff_change(&revs->diffopt,
Jeff King42f5ba52017-05-19 08:57:30 -0400117 blob[0]->mode, blob[1]->mode,
118 &blob[0]->item->oid, &blob[1]->item->oid,
Jeff Kinge5450102012-07-28 11:03:01 -0400119 1, 1,
Jeff King158b06c2017-05-19 08:59:15 -0400120 blob_path(blob[0]), blob_path(blob[1]));
Junio C Hamano65056022006-04-28 23:20:52 -0700121 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];
David Symonds2baf1852008-10-29 09:15:36 -0700132 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
Junio C Hamano65056022006-04-28 23:20:52 -0700133 cached = 1;
Junio C Hamano65056022006-04-28 23:20:52 -0700134 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 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700142 if (revs->pending.nr != 1 ||
Junio C Hamano65056022006-04-28 23:20:52 -0700143 revs->max_count != -1 || revs->min_age != -1 ||
144 revs->max_age != -1)
145 usage(builtin_diff_usage);
Karsten Blees7349afd2012-10-30 10:50:42 +0100146 if (!cached) {
147 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700148 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Karsten Blees7349afd2012-10-30 10:50:42 +0100149 perror("read_cache_preload");
150 return -1;
151 }
152 } else if (read_cache() < 0) {
153 perror("read_cache");
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -0800154 return -1;
155 }
Junio C Hamano65056022006-04-28 23:20:52 -0700156 return run_diff_index(revs, cached);
157}
158
159static int builtin_diff_tree(struct rev_info *revs,
160 int argc, const char **argv,
Michael Haggerty91de3442013-05-25 11:08:03 +0200161 struct object_array_entry *ent0,
162 struct object_array_entry *ent1)
Junio C Hamano65056022006-04-28 23:20:52 -0700163{
brian m. carlson9c4b0f62017-03-26 16:01:26 +0000164 const struct object_id *(oid[2]);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700165 int swap = 0;
Timo Hirvonena6107862006-06-24 20:23:06 +0300166
167 if (argc > 1)
168 usage(builtin_diff_usage);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700169
Michael Haggerty91de3442013-05-25 11:08:03 +0200170 /*
171 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
172 * swap them.
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700173 */
Michael Haggerty91de3442013-05-25 11:08:03 +0200174 if (ent1->item->flags & UNINTERESTING)
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700175 swap = 1;
brian m. carlson9c4b0f62017-03-26 16:01:26 +0000176 oid[swap] = &ent0->item->oid;
177 oid[1 - swap] = &ent1->item->oid;
Brandon Williams66f414f2017-05-30 10:31:03 -0700178 diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
Junio C Hamano65056022006-04-28 23:20:52 -0700179 log_tree_diff_flush(revs);
180 return 0;
181}
182
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700183static int builtin_diff_combined(struct rev_info *revs,
184 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700185 struct object_array_entry *ent,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700186 int ents)
187{
brian m. carlson910650d2017-03-31 01:40:00 +0000188 struct oid_array parents = OID_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700189 int i;
190
Timo Hirvonena6107862006-06-24 20:23:06 +0300191 if (argc > 1)
192 usage(builtin_diff_usage);
193
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700194 if (!revs->dense_combined_merges && !revs->combine_merges)
195 revs->dense_combined_merges = revs->combine_merges = 1;
René Scharfe0041f092011-12-17 11:15:48 +0100196 for (i = 1; i < ents; i++)
brian m. carlson910650d2017-03-31 01:40:00 +0000197 oid_array_append(&parents, &ent[i].item->oid);
Brandon Williamsb9acf542017-05-30 10:30:55 -0700198 diff_tree_combined(&ent[0].item->oid, &parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700199 revs->dense_combined_merges, revs);
brian m. carlson910650d2017-03-31 01:40:00 +0000200 oid_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700201 return 0;
202}
203
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700204static 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);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -0700216 update_index_if_able(&the_index, lock_file);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700217}
218
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700219static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
220{
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700221 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;
Matthieu Moy1c370ea2009-08-06 12:47:21 +0200232 else if (!strcmp(argv[1], "-h"))
233 usage(builtin_diff_usage);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700234 else
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000235 return error(_("invalid option: %s"), argv[1]);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700236 argv++; argc--;
237 }
238
Junio C Hamano903e09a2008-09-18 00:32:37 -0700239 /*
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 &&
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700246 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
247 revs->combine_merges = revs->dense_combined_merges = 1;
248
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +0700249 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700250 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -0800251 perror("read_cache_preload");
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700252 return -1;
253 }
Junio C Hamanoe7c3a592011-03-22 14:17:30 -0700254 return run_diff_files(revs, options);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700255}
256
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700257int cmd_diff(int argc, const char **argv, const char *prefix)
Junio C Hamano65056022006-04-28 23:20:52 -0700258{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700259 int i;
Junio C Hamano65056022006-04-28 23:20:52 -0700260 struct rev_info rev;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200261 struct object_array ent = OBJECT_ARRAY_INIT;
262 int blobs = 0, paths = 0;
Jeff King42f5ba52017-05-19 08:57:30 -0400263 struct object_array_entry *blob[2];
Thomas Gummerer470faf92013-12-11 10:58:42 +0100264 int nongit = 0, no_index = 0;
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100265 int result = 0;
Junio C Hamano65056022006-04-28 23:20:52 -0700266
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 *
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700284 * N=0, M=0, P=2:
285 * compare two filesystem entities (aka --no-index).
286 *
Junio C Hamano65056022006-04-28 23:20:52 -0700287 * Other cases are errors.
288 */
Junio C Hamano230f5442006-05-03 23:54:34 -0700289
Thomas Gummerer470faf92013-12-11 10:58:42 +0100290 /* 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
Jeff King28a4e582016-09-12 20:23:36 -0700302 prefix = setup_git_directory_gently(&nongit);
Thomas Gummerer6df57622013-12-11 10:58:43 +0100303
Jeff King28a4e582016-09-12 20:23:36 -0700304 if (!no_index) {
Jeff King475b3622016-09-12 20:23:27 -0700305 /*
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 }
Thomas Gummerer470faf92013-12-11 10:58:42 +0100317
Thomas Gummerer6df57622013-12-11 10:58:43 +0100318 if (!no_index)
319 gitmodules_config();
Matthieu Moy5404c112016-02-25 09:59:21 +0100320 init_diff_ui_defaults();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100321 git_config(git_diff_ui_config, NULL);
Alexander Rinass90a78b82016-05-13 22:41:02 +0200322 precompose_argv(argc, argv);
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100323
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700324 init_revisions(&rev, prefix);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700325
Thomas Gummereraad90e82013-12-16 21:19:24 +0100326 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");
Thomas Gummerer470faf92013-12-11 10:58:42 +0100336 }
Thomas Gummereraad90e82013-12-16 21:19:24 +0100337 /* 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)
Thomas Gummerer470faf92013-12-11 10:58:42 +0100344 /* If this is a no-index diff, just run it and exit there. */
Nguyễn Thái Ngọc Duye5f7a5d2016-01-20 18:06:02 +0700345 diff_no_index(&rev, argc, argv);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700346
347 /* Otherwise, we are doing the usual "git" diff */
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700348 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
Junio C Hamano65056022006-04-28 23:20:52 -0700349
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100350 /* Scale to real terminal size and respect statGraphWidth config */
Zbigniew Jędrzejewski-Szmekaf9fedc2012-03-01 13:26:39 +0100351 rev.diffopt.stat_width = -1;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100352 rev.diffopt.stat_graph_width = -1;
Zbigniew Jędrzejewski-Szmekaf9fedc2012-03-01 13:26:39 +0100353
Jeff King5ec11af2008-12-07 21:54:17 -0500354 /* Default to let external and textconv be used */
Junio C Hamano61af4942008-11-26 09:58:41 -0800355 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
Jeff King5ec11af2008-12-07 21:54:17 -0500356 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
Junio C Hamano61af4942008-11-26 09:58:41 -0800357
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700358 if (nongit)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000359 die(_("Not a git repository"));
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700360 argc = setup_revisions(argc, argv, &rev, NULL);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700361 if (!rev.diffopt.output_format) {
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300362 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
Thomas Rast28452652012-08-03 14:16:24 +0200363 diff_setup_done(&rev.diffopt);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700364 }
Junio C Hamano61af4942008-11-26 09:58:41 -0800365
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100366 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300367
Jeff King1af3d972012-06-15 16:29:48 -0400368 setup_diff_pager(&rev.diffopt);
René Scharfe89d07f72007-08-12 19:46:55 +0200369
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700370 /*
371 * Do we have --cached and not have a pending object, then
Junio C Hamano65056022006-04-28 23:20:52 -0700372 * default to HEAD by hand. Eek.
373 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700374 if (!rev.pending.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700375 int i;
376 for (i = 1; i < argc; i++) {
377 const char *arg = argv[i];
378 if (!strcmp(arg, "--"))
379 break;
David Symonds2baf1852008-10-29 09:15:36 -0700380 else if (!strcmp(arg, "--cached") ||
381 !strcmp(arg, "--staged")) {
Junio C Hamano3384a2d2007-12-11 10:09:04 -0800382 add_head_to_pending(&rev);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700383 if (!rev.pending.nr) {
384 struct tree *tree;
brian m. carlson740ee052017-05-06 22:10:17 +0000385 tree = lookup_tree(&empty_tree_oid);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700386 add_pending_object(&rev, &tree->object, "HEAD");
387 }
Junio C Hamano65056022006-04-28 23:20:52 -0700388 break;
389 }
390 }
391 }
392
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700393 for (i = 0; i < rev.pending.nr; i++) {
Michael Haggerty026f09e2013-05-25 11:08:05 +0200394 struct object_array_entry *entry = &rev.pending.objects[i];
395 struct object *obj = entry->item;
396 const char *name = entry->name;
Junio C Hamano65056022006-04-28 23:20:52 -0700397 int flags = (obj->flags & UNINTERESTING);
398 if (!obj->parsed)
brian m. carlsonc251c832017-05-06 22:10:38 +0000399 obj = parse_object(&obj->oid);
Junio C Hamano65056022006-04-28 23:20:52 -0700400 obj = deref_tag(obj, NULL, 0);
401 if (!obj)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000402 die(_("invalid object '%s' given."), name);
Linus Torvalds19746322006-07-11 20:45:31 -0700403 if (obj->type == OBJ_COMMIT)
Junio C Hamano65056022006-04-28 23:20:52 -0700404 obj = &((struct commit *)obj)->tree->object;
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200405
Linus Torvalds19746322006-07-11 20:45:31 -0700406 if (obj->type == OBJ_TREE) {
Junio C Hamano65056022006-04-28 23:20:52 -0700407 obj->flags |= flags;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200408 add_object_array(obj, name, &ent);
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200409 } else if (obj->type == OBJ_BLOB) {
Junio C Hamano65056022006-04-28 23:20:52 -0700410 if (2 <= blobs)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000411 die(_("more than two blobs given: '%s'"), name);
Jeff King42f5ba52017-05-19 08:57:30 -0400412 blob[blobs] = entry;
Junio C Hamano65056022006-04-28 23:20:52 -0700413 blobs++;
Junio C Hamano230f5442006-05-03 23:54:34 -0700414
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200415 } else {
416 die(_("unhandled object '%s' given."), name);
Junio C Hamano65056022006-04-28 23:20:52 -0700417 }
Junio C Hamano65056022006-04-28 23:20:52 -0700418 }
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +0700419 if (rev.prune_data.nr)
Nguyễn Thái Ngọc Duyafe069d2010-12-17 19:43:06 +0700420 paths += rev.prune_data.nr;
Junio C Hamano65056022006-04-28 23:20:52 -0700421
422 /*
423 * Now, do the arguments look reasonable?
424 */
Michael Haggerty33055fa2013-05-25 11:08:04 +0200425 if (!ent.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700426 switch (blobs) {
427 case 0:
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700428 result = builtin_diff_files(&rev, argc, argv);
Junio C Hamano65056022006-04-28 23:20:52 -0700429 break;
430 case 1:
431 if (paths != 1)
432 usage(builtin_diff_usage);
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +0700433 result = builtin_diff_b_f(&rev, argc, argv, blob);
Junio C Hamano65056022006-04-28 23:20:52 -0700434 break;
435 case 2:
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700436 if (paths)
437 usage(builtin_diff_usage);
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100438 result = builtin_diff_blobs(&rev, argc, argv, blob);
Junio C Hamano65056022006-04-28 23:20:52 -0700439 break;
440 default:
441 usage(builtin_diff_usage);
442 }
443 }
444 else if (blobs)
445 usage(builtin_diff_usage);
Michael Haggerty33055fa2013-05-25 11:08:04 +0200446 else if (ent.nr == 1)
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100447 result = builtin_diff_index(&rev, argc, argv);
Michael Haggerty33055fa2013-05-25 11:08:04 +0200448 else if (ent.nr == 2)
449 result = builtin_diff_tree(&rev, argc, argv,
450 &ent.objects[0], &ent.objects[1]);
451 else if (ent.objects[0].item->flags & UNINTERESTING) {
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700452 /*
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700453 * diff A...B where there is at least one merge base
Michael Haggerty33055fa2013-05-25 11:08:04 +0200454 * between A and B. We have ent.objects[0] ==
455 * merge-base, ent.objects[ents-2] == A, and
456 * ent.objects[ents-1] == B. Show diff between the
457 * base and B. Note that we pick one merge base at
458 * random if there are more than one.
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700459 */
Michael Haggerty33055fa2013-05-25 11:08:04 +0200460 result = builtin_diff_tree(&rev, argc, argv,
461 &ent.objects[0],
462 &ent.objects[ent.nr-1]);
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700463 } else
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100464 result = builtin_diff_combined(&rev, argc, argv,
Michael Haggerty33055fa2013-05-25 11:08:04 +0200465 ent.objects, ent.nr);
Junio C Hamanoda31b352007-12-13 23:40:27 -0800466 result = diff_result_code(&rev.diffopt, result);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700467 if (1 < rev.diffopt.skip_stat_unmatch)
468 refresh_index_quietly();
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100469 return result;
Junio C Hamano65056022006-04-28 23:20:52 -0700470}