blob: 0f247d24008a4dc7ba5958e716c13f7582a820f1 [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"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +01007#include "color.h"
Junio C Hamano65056022006-04-28 23:20:52 -07008#include "commit.h"
9#include "blob.h"
10#include "tag.h"
11#include "diff.h"
12#include "diffcore.h"
13#include "revision.h"
14#include "log-tree.h"
15#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020016#include "submodule.h"
René Scharfe0041f092011-12-17 11:15:48 +010017#include "sha1-array.h"
Junio C Hamano65056022006-04-28 23:20:52 -070018
Thomas Gummerer470faf92013-12-11 10:58:42 +010019#define DIFF_NO_INDEX_EXPLICIT 1
20#define DIFF_NO_INDEX_IMPLICIT 2
21
Junio C Hamano65056022006-04-28 23:20:52 -070022struct blobinfo {
23 unsigned char sha1[20];
24 const char *name;
Martin Koegler01618a32007-04-22 18:44:00 +020025 unsigned mode;
Junio C Hamano65056022006-04-28 23:20:52 -070026};
27
28static const char builtin_diff_usage[] =
Štěpán Němec9edb8a02010-11-04 18:18:17 +010029"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
Junio C Hamano65056022006-04-28 23:20:52 -070030
Junio C Hamano65056022006-04-28 23:20:52 -070031static void stuff_change(struct diff_options *opt,
32 unsigned old_mode, unsigned new_mode,
33 const unsigned char *old_sha1,
34 const unsigned char *new_sha1,
Jeff Kinge5450102012-07-28 11:03:01 -040035 int old_sha1_valid,
36 int new_sha1_valid,
Junio C Hamano65056022006-04-28 23:20:52 -070037 const char *old_name,
38 const char *new_name)
39{
40 struct diff_filespec *one, *two;
41
David Rientjes0bef57e2006-08-15 13:37:19 -070042 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
Junio C Hamano43342942007-04-22 23:56:22 -070043 !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
Junio C Hamano65056022006-04-28 23:20:52 -070044 return;
45
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010046 if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
Junio C Hamano65056022006-04-28 23:20:52 -070047 unsigned tmp;
Peter Hagervalld92f1dc2006-05-07 16:50:47 +020048 const unsigned char *tmp_u;
Junio C Hamano65056022006-04-28 23:20:52 -070049 const char *tmp_c;
50 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
51 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
52 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
53 }
Junio C Hamanocd676a52008-02-12 14:26:02 -080054
55 if (opt->prefix &&
56 (strncmp(old_name, opt->prefix, opt->prefix_length) ||
57 strncmp(new_name, opt->prefix, opt->prefix_length)))
58 return;
59
Junio C Hamano65056022006-04-28 23:20:52 -070060 one = alloc_filespec(old_name);
61 two = alloc_filespec(new_name);
Jeff Kinge5450102012-07-28 11:03:01 -040062 fill_filespec(one, old_sha1, old_sha1_valid, old_mode);
63 fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
Junio C Hamano65056022006-04-28 23:20:52 -070064
Junio C Hamano65056022006-04-28 23:20:52 -070065 diff_queue(&diff_queued_diff, one, two);
66}
67
68static int builtin_diff_b_f(struct rev_info *revs,
69 int argc, const char **argv,
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070070 struct blobinfo *blob)
Junio C Hamano65056022006-04-28 23:20:52 -070071{
72 /* Blob vs file in the working tree*/
73 struct stat st;
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070074 const char *path;
Junio C Hamano65056022006-04-28 23:20:52 -070075
Timo Hirvonena6107862006-06-24 20:23:06 +030076 if (argc > 1)
77 usage(builtin_diff_usage);
78
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070079 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
80 path = revs->prune_data.items[0].match;
81
Junio C Hamano65056022006-04-28 23:20:52 -070082 if (lstat(path, &st))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000083 die_errno(_("failed to stat '%s'"), path);
Junio C Hamano65056022006-04-28 23:20:52 -070084 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000085 die(_("'%s': not a regular file or symlink"), path);
Martin Koegler01618a32007-04-22 18:44:00 +020086
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070087 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
88
Martin Koegler01618a32007-04-22 18:44:00 +020089 if (blob[0].mode == S_IFINVALID)
90 blob[0].mode = canon_mode(st.st_mode);
91
Junio C Hamano65056022006-04-28 23:20:52 -070092 stuff_change(&revs->diffopt,
Martin Koegler01618a32007-04-22 18:44:00 +020093 blob[0].mode, canon_mode(st.st_mode),
Junio C Hamano65056022006-04-28 23:20:52 -070094 blob[0].sha1, null_sha1,
Jeff Kinge5450102012-07-28 11:03:01 -040095 1, 0,
Junio C Hamano065e0b12006-05-18 14:35:37 -070096 path, path);
Junio C Hamano65056022006-04-28 23:20:52 -070097 diffcore_std(&revs->diffopt);
98 diff_flush(&revs->diffopt);
99 return 0;
100}
101
102static int builtin_diff_blobs(struct rev_info *revs,
103 int argc, const char **argv,
104 struct blobinfo *blob)
105{
Junio C Hamano65056022006-04-28 23:20:52 -0700106 unsigned mode = canon_mode(S_IFREG | 0644);
107
Timo Hirvonena6107862006-06-24 20:23:06 +0300108 if (argc > 1)
109 usage(builtin_diff_usage);
110
Martin Koegler01618a32007-04-22 18:44:00 +0200111 if (blob[0].mode == S_IFINVALID)
112 blob[0].mode = mode;
113
114 if (blob[1].mode == S_IFINVALID)
115 blob[1].mode = mode;
116
Junio C Hamano65056022006-04-28 23:20:52 -0700117 stuff_change(&revs->diffopt,
Martin Koegler01618a32007-04-22 18:44:00 +0200118 blob[0].mode, blob[1].mode,
Junio C Hamanof82cd3c2006-08-03 11:50:10 -0700119 blob[0].sha1, blob[1].sha1,
Jeff Kinge5450102012-07-28 11:03:01 -0400120 1, 1,
Junio C Hamano53dd8a92006-08-03 11:57:11 -0700121 blob[0].name, blob[1].name);
Junio C Hamano65056022006-04-28 23:20:52 -0700122 diffcore_std(&revs->diffopt);
123 diff_flush(&revs->diffopt);
124 return 0;
125}
126
127static int builtin_diff_index(struct rev_info *revs,
128 int argc, const char **argv)
129{
130 int cached = 0;
131 while (1 < argc) {
132 const char *arg = argv[1];
David Symonds2baf1852008-10-29 09:15:36 -0700133 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
Junio C Hamano65056022006-04-28 23:20:52 -0700134 cached = 1;
Junio C Hamano65056022006-04-28 23:20:52 -0700135 else
136 usage(builtin_diff_usage);
137 argv++; argc--;
138 }
139 /*
140 * Make sure there is one revision (i.e. pending object),
141 * and there is no revision filtering parameters.
142 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700143 if (revs->pending.nr != 1 ||
Junio C Hamano65056022006-04-28 23:20:52 -0700144 revs->max_count != -1 || revs->min_age != -1 ||
145 revs->max_age != -1)
146 usage(builtin_diff_usage);
Karsten Blees7349afd2012-10-30 10:50:42 +0100147 if (!cached) {
148 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700149 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Karsten Blees7349afd2012-10-30 10:50:42 +0100150 perror("read_cache_preload");
151 return -1;
152 }
153 } else if (read_cache() < 0) {
154 perror("read_cache");
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -0800155 return -1;
156 }
Junio C Hamano65056022006-04-28 23:20:52 -0700157 return run_diff_index(revs, cached);
158}
159
160static int builtin_diff_tree(struct rev_info *revs,
161 int argc, const char **argv,
Michael Haggerty91de3442013-05-25 11:08:03 +0200162 struct object_array_entry *ent0,
163 struct object_array_entry *ent1)
Junio C Hamano65056022006-04-28 23:20:52 -0700164{
Junio C Hamano65056022006-04-28 23:20:52 -0700165 const unsigned char *(sha1[2]);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700166 int swap = 0;
Timo Hirvonena6107862006-06-24 20:23:06 +0300167
168 if (argc > 1)
169 usage(builtin_diff_usage);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700170
Michael Haggerty91de3442013-05-25 11:08:03 +0200171 /*
172 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
173 * swap them.
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700174 */
Michael Haggerty91de3442013-05-25 11:08:03 +0200175 if (ent1->item->flags & UNINTERESTING)
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700176 swap = 1;
Michael Haggerty91de3442013-05-25 11:08:03 +0200177 sha1[swap] = ent0->item->sha1;
Felipe Contreras4e7e4b62013-10-31 03:25:44 -0600178 sha1[1 - swap] = ent1->item->sha1;
Junio C Hamano65056022006-04-28 23:20:52 -0700179 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
180 log_tree_diff_flush(revs);
181 return 0;
182}
183
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700184static int builtin_diff_combined(struct rev_info *revs,
185 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700186 struct object_array_entry *ent,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700187 int ents)
188{
René Scharfe0041f092011-12-17 11:15:48 +0100189 struct sha1_array parents = SHA1_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700190 int i;
191
Timo Hirvonena6107862006-06-24 20:23:06 +0300192 if (argc > 1)
193 usage(builtin_diff_usage);
194
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700195 if (!revs->dense_combined_merges && !revs->combine_merges)
196 revs->dense_combined_merges = revs->combine_merges = 1;
René Scharfe0041f092011-12-17 11:15:48 +0100197 for (i = 1; i < ents; i++)
198 sha1_array_append(&parents, ent[i].item->sha1);
199 diff_tree_combined(ent[0].item->sha1, &parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700200 revs->dense_combined_merges, revs);
René Scharfe0041f092011-12-17 11:15:48 +0100201 sha1_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700202 return 0;
203}
204
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700205static void refresh_index_quietly(void)
206{
207 struct lock_file *lock_file;
208 int fd;
209
210 lock_file = xcalloc(1, sizeof(struct lock_file));
211 fd = hold_locked_index(lock_file, 0);
212 if (fd < 0)
213 return;
214 discard_cache();
215 read_cache();
216 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -0700217 update_index_if_able(&the_index, lock_file);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700218}
219
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700220static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
221{
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700222 unsigned int options = 0;
223
224 while (1 < argc && argv[1][0] == '-') {
225 if (!strcmp(argv[1], "--base"))
226 revs->max_count = 1;
227 else if (!strcmp(argv[1], "--ours"))
228 revs->max_count = 2;
229 else if (!strcmp(argv[1], "--theirs"))
230 revs->max_count = 3;
231 else if (!strcmp(argv[1], "-q"))
232 options |= DIFF_SILENT_ON_REMOVED;
Matthieu Moy1c370ea2009-08-06 12:47:21 +0200233 else if (!strcmp(argv[1], "-h"))
234 usage(builtin_diff_usage);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700235 else
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000236 return error(_("invalid option: %s"), argv[1]);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700237 argv++; argc--;
238 }
239
Junio C Hamano903e09a2008-09-18 00:32:37 -0700240 /*
241 * "diff --base" should not combine merges because it was not
242 * asked to. "diff -c" should not densify (if the user wants
243 * dense one, --cc can be explicitly asked for, or just rely
244 * on the default).
245 */
246 if (revs->max_count == -1 && !revs->combine_merges &&
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700247 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
248 revs->combine_merges = revs->dense_combined_merges = 1;
249
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +0700250 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700251 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -0800252 perror("read_cache_preload");
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700253 return -1;
254 }
Junio C Hamanoe7c3a592011-03-22 14:17:30 -0700255 return run_diff_files(revs, options);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700256}
257
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700258int cmd_diff(int argc, const char **argv, const char *prefix)
Junio C Hamano65056022006-04-28 23:20:52 -0700259{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700260 int i;
Junio C Hamano65056022006-04-28 23:20:52 -0700261 struct rev_info rev;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200262 struct object_array ent = OBJECT_ARRAY_INIT;
263 int blobs = 0, paths = 0;
Junio C Hamano65056022006-04-28 23:20:52 -0700264 struct blobinfo blob[2];
Thomas Gummerer470faf92013-12-11 10:58:42 +0100265 int nongit = 0, no_index = 0;
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100266 int result = 0;
Junio C Hamano65056022006-04-28 23:20:52 -0700267
268 /*
269 * We could get N tree-ish in the rev.pending_objects list.
270 * Also there could be M blobs there, and P pathspecs.
271 *
272 * N=0, M=0:
273 * cache vs files (diff-files)
274 * N=0, M=2:
275 * compare two random blobs. P must be zero.
276 * N=0, M=1, P=1:
277 * compare a blob with a working tree file.
278 *
279 * N=1, M=0:
280 * tree vs cache (diff-index --cached)
281 *
282 * N=2, M=0:
283 * tree vs tree (diff-tree)
284 *
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700285 * N=0, M=0, P=2:
286 * compare two filesystem entities (aka --no-index).
287 *
Junio C Hamano65056022006-04-28 23:20:52 -0700288 * Other cases are errors.
289 */
Junio C Hamano230f5442006-05-03 23:54:34 -0700290
Thomas Gummerer470faf92013-12-11 10:58:42 +0100291 /* Were we asked to do --no-index explicitly? */
292 for (i = 1; i < argc; i++) {
293 if (!strcmp(argv[i], "--")) {
294 i++;
295 break;
296 }
297 if (!strcmp(argv[i], "--no-index"))
298 no_index = DIFF_NO_INDEX_EXPLICIT;
299 if (argv[i][0] != '-')
300 break;
301 }
302
Thomas Gummerer6df57622013-12-11 10:58:43 +0100303 if (!no_index)
304 prefix = setup_git_directory_gently(&nongit);
305
Thomas Gummerer470faf92013-12-11 10:58:42 +0100306 /*
307 * Treat git diff with at least one path outside of the
308 * repo the same as if the command would have been executed
309 * outside of a git repository. In this case it behaves
310 * the same way as "git diff --no-index <a> <b>", which acts
311 * as a colourful "diff" replacement.
312 */
313 if (nongit || ((argc == i + 2) &&
314 (!path_inside_repo(prefix, argv[i]) ||
315 !path_inside_repo(prefix, argv[i + 1]))))
316 no_index = DIFF_NO_INDEX_IMPLICIT;
317
Thomas Gummerer6df57622013-12-11 10:58:43 +0100318 if (!no_index)
319 gitmodules_config();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100320 git_config(git_diff_ui_config, NULL);
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100321
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700322 init_revisions(&rev, prefix);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700323
Thomas Gummereraad90e82013-12-16 21:19:24 +0100324 if (no_index && argc != i + 2) {
325 if (no_index == DIFF_NO_INDEX_IMPLICIT) {
326 /*
327 * There was no --no-index and there were not two
328 * paths. It is possible that the user intended
329 * to do an inside-repository operation.
330 */
331 fprintf(stderr, "Not a git repository\n");
332 fprintf(stderr,
333 "To compare two paths outside a working tree:\n");
Thomas Gummerer470faf92013-12-11 10:58:42 +0100334 }
Thomas Gummereraad90e82013-12-16 21:19:24 +0100335 /* Give the usage message for non-repository usage and exit. */
336 usagef("git diff %s <path> <path>",
337 no_index == DIFF_NO_INDEX_EXPLICIT ?
338 "--no-index" : "[--no-index]");
339
340 }
341 if (no_index)
Thomas Gummerer470faf92013-12-11 10:58:42 +0100342 /* If this is a no-index diff, just run it and exit there. */
343 diff_no_index(&rev, argc, argv, prefix);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700344
345 /* Otherwise, we are doing the usual "git" diff */
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700346 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
Junio C Hamano65056022006-04-28 23:20:52 -0700347
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100348 /* Scale to real terminal size and respect statGraphWidth config */
Zbigniew Jędrzejewski-Szmekaf9fedc2012-03-01 13:26:39 +0100349 rev.diffopt.stat_width = -1;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100350 rev.diffopt.stat_graph_width = -1;
Zbigniew Jędrzejewski-Szmekaf9fedc2012-03-01 13:26:39 +0100351
Jeff King5ec11af2008-12-07 21:54:17 -0500352 /* Default to let external and textconv be used */
Junio C Hamano61af4942008-11-26 09:58:41 -0800353 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
Jeff King5ec11af2008-12-07 21:54:17 -0500354 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
Junio C Hamano61af4942008-11-26 09:58:41 -0800355
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700356 if (nongit)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000357 die(_("Not a git repository"));
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700358 argc = setup_revisions(argc, argv, &rev, NULL);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700359 if (!rev.diffopt.output_format) {
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300360 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
Thomas Rast28452652012-08-03 14:16:24 +0200361 diff_setup_done(&rev.diffopt);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700362 }
Junio C Hamano61af4942008-11-26 09:58:41 -0800363
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100364 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300365
Jeff King1af3d972012-06-15 16:29:48 -0400366 setup_diff_pager(&rev.diffopt);
René Scharfe89d07f72007-08-12 19:46:55 +0200367
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700368 /*
369 * Do we have --cached and not have a pending object, then
Junio C Hamano65056022006-04-28 23:20:52 -0700370 * default to HEAD by hand. Eek.
371 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700372 if (!rev.pending.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700373 int i;
374 for (i = 1; i < argc; i++) {
375 const char *arg = argv[i];
376 if (!strcmp(arg, "--"))
377 break;
David Symonds2baf1852008-10-29 09:15:36 -0700378 else if (!strcmp(arg, "--cached") ||
379 !strcmp(arg, "--staged")) {
Junio C Hamano3384a2d2007-12-11 10:09:04 -0800380 add_head_to_pending(&rev);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700381 if (!rev.pending.nr) {
382 struct tree *tree;
Jeff Kingcba595b2012-03-22 14:53:24 -0400383 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700384 add_pending_object(&rev, &tree->object, "HEAD");
385 }
Junio C Hamano65056022006-04-28 23:20:52 -0700386 break;
387 }
388 }
389 }
390
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700391 for (i = 0; i < rev.pending.nr; i++) {
Michael Haggerty026f09e2013-05-25 11:08:05 +0200392 struct object_array_entry *entry = &rev.pending.objects[i];
393 struct object *obj = entry->item;
394 const char *name = entry->name;
Junio C Hamano65056022006-04-28 23:20:52 -0700395 int flags = (obj->flags & UNINTERESTING);
396 if (!obj->parsed)
397 obj = parse_object(obj->sha1);
398 obj = deref_tag(obj, NULL, 0);
399 if (!obj)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000400 die(_("invalid object '%s' given."), name);
Linus Torvalds19746322006-07-11 20:45:31 -0700401 if (obj->type == OBJ_COMMIT)
Junio C Hamano65056022006-04-28 23:20:52 -0700402 obj = &((struct commit *)obj)->tree->object;
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200403
Linus Torvalds19746322006-07-11 20:45:31 -0700404 if (obj->type == OBJ_TREE) {
Junio C Hamano65056022006-04-28 23:20:52 -0700405 obj->flags |= flags;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200406 add_object_array(obj, name, &ent);
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200407 } else if (obj->type == OBJ_BLOB) {
Junio C Hamano65056022006-04-28 23:20:52 -0700408 if (2 <= blobs)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000409 die(_("more than two blobs given: '%s'"), name);
Shawn Pearcee7024962006-08-23 02:49:00 -0400410 hashcpy(blob[blobs].sha1, obj->sha1);
Junio C Hamano65056022006-04-28 23:20:52 -0700411 blob[blobs].name = name;
Michael Haggerty026f09e2013-05-25 11:08:05 +0200412 blob[blobs].mode = entry->mode;
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}