blob: 7f91f6d2267db962fb7c25e92983afd4e811d43e [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"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02007#include "lockfile.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +01008#include "color.h"
Junio C Hamano65056022006-04-28 23:20:52 -07009#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"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020017#include "submodule.h"
René Scharfe0041f092011-12-17 11:15:48 +010018#include "sha1-array.h"
Junio C Hamano65056022006-04-28 23:20:52 -070019
Thomas Gummerer470faf92013-12-11 10:58:42 +010020#define DIFF_NO_INDEX_EXPLICIT 1
21#define DIFF_NO_INDEX_IMPLICIT 2
22
Junio C Hamano65056022006-04-28 23:20:52 -070023struct blobinfo {
24 unsigned char sha1[20];
25 const char *name;
Martin Koegler01618a32007-04-22 18:44:00 +020026 unsigned mode;
Junio C Hamano65056022006-04-28 23:20:52 -070027};
28
29static const char builtin_diff_usage[] =
Štěpán Němec9edb8a02010-11-04 18:18:17 +010030"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
Junio C Hamano65056022006-04-28 23:20:52 -070031
Junio C Hamano65056022006-04-28 23:20:52 -070032static 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,
Jeff Kinge5450102012-07-28 11:03:01 -040036 int old_sha1_valid,
37 int new_sha1_valid,
Junio C Hamano65056022006-04-28 23:20:52 -070038 const char *old_name,
39 const char *new_name)
40{
41 struct diff_filespec *one, *two;
42
David Rientjes0bef57e2006-08-15 13:37:19 -070043 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
Junio C Hamano43342942007-04-22 23:56:22 -070044 !hashcmp(old_sha1, new_sha1) && (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)) {
Junio C Hamano65056022006-04-28 23:20:52 -070048 unsigned tmp;
Peter Hagervalld92f1dc2006-05-07 16:50:47 +020049 const unsigned char *tmp_u;
Junio C Hamano65056022006-04-28 23:20:52 -070050 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 }
Junio C Hamanocd676a52008-02-12 14:26:02 -080055
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
Junio C Hamano65056022006-04-28 23:20:52 -070061 one = alloc_filespec(old_name);
62 two = alloc_filespec(new_name);
Jeff Kinge5450102012-07-28 11:03:01 -040063 fill_filespec(one, old_sha1, old_sha1_valid, old_mode);
64 fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
Junio C Hamano65056022006-04-28 23:20:52 -070065
Junio C Hamano65056022006-04-28 23:20:52 -070066 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,
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070071 struct blobinfo *blob)
Junio C Hamano65056022006-04-28 23:20:52 -070072{
73 /* Blob vs file in the working tree*/
74 struct stat st;
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070075 const char *path;
Junio C Hamano65056022006-04-28 23:20:52 -070076
Timo Hirvonena6107862006-06-24 20:23:06 +030077 if (argc > 1)
78 usage(builtin_diff_usage);
79
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +070080 GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
81 path = revs->prune_data.items[0].match;
82
Junio C Hamano65056022006-04-28 23:20:52 -070083 if (lstat(path, &st))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000084 die_errno(_("failed to stat '%s'"), path);
Junio C Hamano65056022006-04-28 23:20:52 -070085 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +000086 die(_("'%s': not a regular file or symlink"), path);
Martin Koegler01618a32007-04-22 18:44:00 +020087
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070088 diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
89
Martin Koegler01618a32007-04-22 18:44:00 +020090 if (blob[0].mode == S_IFINVALID)
91 blob[0].mode = canon_mode(st.st_mode);
92
Junio C Hamano65056022006-04-28 23:20:52 -070093 stuff_change(&revs->diffopt,
Martin Koegler01618a32007-04-22 18:44:00 +020094 blob[0].mode, canon_mode(st.st_mode),
Junio C Hamano65056022006-04-28 23:20:52 -070095 blob[0].sha1, null_sha1,
Jeff Kinge5450102012-07-28 11:03:01 -040096 1, 0,
Junio C Hamano065e0b12006-05-18 14:35:37 -070097 path, path);
Junio C Hamano65056022006-04-28 23:20:52 -070098 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{
Junio C Hamano65056022006-04-28 23:20:52 -0700107 unsigned mode = canon_mode(S_IFREG | 0644);
108
Timo Hirvonena6107862006-06-24 20:23:06 +0300109 if (argc > 1)
110 usage(builtin_diff_usage);
111
Martin Koegler01618a32007-04-22 18:44:00 +0200112 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
Junio C Hamano65056022006-04-28 23:20:52 -0700118 stuff_change(&revs->diffopt,
Martin Koegler01618a32007-04-22 18:44:00 +0200119 blob[0].mode, blob[1].mode,
Junio C Hamanof82cd3c2006-08-03 11:50:10 -0700120 blob[0].sha1, blob[1].sha1,
Jeff Kinge5450102012-07-28 11:03:01 -0400121 1, 1,
Junio C Hamano53dd8a92006-08-03 11:57:11 -0700122 blob[0].name, blob[1].name);
Junio C Hamano65056022006-04-28 23:20:52 -0700123 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];
David Symonds2baf1852008-10-29 09:15:36 -0700134 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
Junio C Hamano65056022006-04-28 23:20:52 -0700135 cached = 1;
Junio C Hamano65056022006-04-28 23:20:52 -0700136 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 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700144 if (revs->pending.nr != 1 ||
Junio C Hamano65056022006-04-28 23:20:52 -0700145 revs->max_count != -1 || revs->min_age != -1 ||
146 revs->max_age != -1)
147 usage(builtin_diff_usage);
Karsten Blees7349afd2012-10-30 10:50:42 +0100148 if (!cached) {
149 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700150 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Karsten Blees7349afd2012-10-30 10:50:42 +0100151 perror("read_cache_preload");
152 return -1;
153 }
154 } else if (read_cache() < 0) {
155 perror("read_cache");
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -0800156 return -1;
157 }
Junio C Hamano65056022006-04-28 23:20:52 -0700158 return run_diff_index(revs, cached);
159}
160
161static int builtin_diff_tree(struct rev_info *revs,
162 int argc, const char **argv,
Michael Haggerty91de3442013-05-25 11:08:03 +0200163 struct object_array_entry *ent0,
164 struct object_array_entry *ent1)
Junio C Hamano65056022006-04-28 23:20:52 -0700165{
Junio C Hamano65056022006-04-28 23:20:52 -0700166 const unsigned char *(sha1[2]);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700167 int swap = 0;
Timo Hirvonena6107862006-06-24 20:23:06 +0300168
169 if (argc > 1)
170 usage(builtin_diff_usage);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700171
Michael Haggerty91de3442013-05-25 11:08:03 +0200172 /*
173 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
174 * swap them.
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700175 */
Michael Haggerty91de3442013-05-25 11:08:03 +0200176 if (ent1->item->flags & UNINTERESTING)
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700177 swap = 1;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000178 sha1[swap] = ent0->item->oid.hash;
179 sha1[1 - swap] = ent1->item->oid.hash;
Junio C Hamano65056022006-04-28 23:20:52 -0700180 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
181 log_tree_diff_flush(revs);
182 return 0;
183}
184
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700185static int builtin_diff_combined(struct rev_info *revs,
186 int argc, const char **argv,
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700187 struct object_array_entry *ent,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700188 int ents)
189{
René Scharfe0041f092011-12-17 11:15:48 +0100190 struct sha1_array parents = SHA1_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700191 int i;
192
Timo Hirvonena6107862006-06-24 20:23:06 +0300193 if (argc > 1)
194 usage(builtin_diff_usage);
195
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700196 if (!revs->dense_combined_merges && !revs->combine_merges)
197 revs->dense_combined_merges = revs->combine_merges = 1;
René Scharfe0041f092011-12-17 11:15:48 +0100198 for (i = 1; i < ents; i++)
brian m. carlsoned1c9972015-11-10 02:22:29 +0000199 sha1_array_append(&parents, ent[i].item->oid.hash);
200 diff_tree_combined(ent[0].item->oid.hash, &parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700201 revs->dense_combined_merges, revs);
René Scharfe0041f092011-12-17 11:15:48 +0100202 sha1_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700203 return 0;
204}
205
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700206static 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);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -0700218 update_index_if_able(&the_index, lock_file);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700219}
220
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700221static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
222{
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700223 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;
Matthieu Moy1c370ea2009-08-06 12:47:21 +0200234 else if (!strcmp(argv[1], "-h"))
235 usage(builtin_diff_usage);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700236 else
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000237 return error(_("invalid option: %s"), argv[1]);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700238 argv++; argc--;
239 }
240
Junio C Hamano903e09a2008-09-18 00:32:37 -0700241 /*
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 &&
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700248 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
249 revs->combine_merges = revs->dense_combined_merges = 1;
250
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +0700251 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700252 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -0800253 perror("read_cache_preload");
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700254 return -1;
255 }
Junio C Hamanoe7c3a592011-03-22 14:17:30 -0700256 return run_diff_files(revs, options);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700257}
258
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700259int cmd_diff(int argc, const char **argv, const char *prefix)
Junio C Hamano65056022006-04-28 23:20:52 -0700260{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700261 int i;
Junio C Hamano65056022006-04-28 23:20:52 -0700262 struct rev_info rev;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200263 struct object_array ent = OBJECT_ARRAY_INIT;
264 int blobs = 0, paths = 0;
Junio C Hamano65056022006-04-28 23:20:52 -0700265 struct blobinfo blob[2];
Thomas Gummerer470faf92013-12-11 10:58:42 +0100266 int nongit = 0, no_index = 0;
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100267 int result = 0;
Junio C Hamano65056022006-04-28 23:20:52 -0700268
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 *
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700286 * N=0, M=0, P=2:
287 * compare two filesystem entities (aka --no-index).
288 *
Junio C Hamano65056022006-04-28 23:20:52 -0700289 * Other cases are errors.
290 */
Junio C Hamano230f5442006-05-03 23:54:34 -0700291
Thomas Gummerer470faf92013-12-11 10:58:42 +0100292 /* 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
Jeff King28a4e582016-09-12 20:23:36 -0700304 prefix = setup_git_directory_gently(&nongit);
Thomas Gummerer6df57622013-12-11 10:58:43 +0100305
Jeff King28a4e582016-09-12 20:23:36 -0700306 if (!no_index) {
Jeff King475b3622016-09-12 20:23:27 -0700307 /*
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 }
Thomas Gummerer470faf92013-12-11 10:58:42 +0100319
Thomas Gummerer6df57622013-12-11 10:58:43 +0100320 if (!no_index)
321 gitmodules_config();
Matthieu Moy5404c112016-02-25 09:59:21 +0100322 init_diff_ui_defaults();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100323 git_config(git_diff_ui_config, NULL);
Alexander Rinass90a78b82016-05-13 22:41:02 +0200324 precompose_argv(argc, argv);
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100325
Linus Torvaldsdb6296a2006-07-28 21:21:48 -0700326 init_revisions(&rev, prefix);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700327
Thomas Gummereraad90e82013-12-16 21:19:24 +0100328 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");
Thomas Gummerer470faf92013-12-11 10:58:42 +0100338 }
Thomas Gummereraad90e82013-12-16 21:19:24 +0100339 /* 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)
Thomas Gummerer470faf92013-12-11 10:58:42 +0100346 /* 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 +0700347 diff_no_index(&rev, argc, argv);
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700348
349 /* Otherwise, we are doing the usual "git" diff */
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700350 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
Junio C Hamano65056022006-04-28 23:20:52 -0700351
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100352 /* Scale to real terminal size and respect statGraphWidth config */
Zbigniew Jędrzejewski-Szmekaf9fedc2012-03-01 13:26:39 +0100353 rev.diffopt.stat_width = -1;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100354 rev.diffopt.stat_graph_width = -1;
Zbigniew Jędrzejewski-Szmekaf9fedc2012-03-01 13:26:39 +0100355
Jeff King5ec11af2008-12-07 21:54:17 -0500356 /* Default to let external and textconv be used */
Junio C Hamano61af4942008-11-26 09:58:41 -0800357 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
Jeff King5ec11af2008-12-07 21:54:17 -0500358 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
Junio C Hamano61af4942008-11-26 09:58:41 -0800359
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700360 if (nongit)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000361 die(_("Not a git repository"));
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700362 argc = setup_revisions(argc, argv, &rev, NULL);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700363 if (!rev.diffopt.output_format) {
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300364 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
Thomas Rast28452652012-08-03 14:16:24 +0200365 diff_setup_done(&rev.diffopt);
Junio C Hamano047fbe92006-07-01 22:15:40 -0700366 }
Junio C Hamano61af4942008-11-26 09:58:41 -0800367
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100368 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +0300369
Jeff King1af3d972012-06-15 16:29:48 -0400370 setup_diff_pager(&rev.diffopt);
René Scharfe89d07f72007-08-12 19:46:55 +0200371
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700372 /*
373 * Do we have --cached and not have a pending object, then
Junio C Hamano65056022006-04-28 23:20:52 -0700374 * default to HEAD by hand. Eek.
375 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700376 if (!rev.pending.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700377 int i;
378 for (i = 1; i < argc; i++) {
379 const char *arg = argv[i];
380 if (!strcmp(arg, "--"))
381 break;
David Symonds2baf1852008-10-29 09:15:36 -0700382 else if (!strcmp(arg, "--cached") ||
383 !strcmp(arg, "--staged")) {
Junio C Hamano3384a2d2007-12-11 10:09:04 -0800384 add_head_to_pending(&rev);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700385 if (!rev.pending.nr) {
386 struct tree *tree;
Jeff Kingcba595b2012-03-22 14:53:24 -0400387 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
Nguyễn Thái Ngọc Duya2b7a3b2011-02-03 13:23:34 +0700388 add_pending_object(&rev, &tree->object, "HEAD");
389 }
Junio C Hamano65056022006-04-28 23:20:52 -0700390 break;
391 }
392 }
393 }
394
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700395 for (i = 0; i < rev.pending.nr; i++) {
Michael Haggerty026f09e2013-05-25 11:08:05 +0200396 struct object_array_entry *entry = &rev.pending.objects[i];
397 struct object *obj = entry->item;
398 const char *name = entry->name;
Junio C Hamano65056022006-04-28 23:20:52 -0700399 int flags = (obj->flags & UNINTERESTING);
400 if (!obj->parsed)
brian m. carlsoned1c9972015-11-10 02:22:29 +0000401 obj = parse_object(obj->oid.hash);
Junio C Hamano65056022006-04-28 23:20:52 -0700402 obj = deref_tag(obj, NULL, 0);
403 if (!obj)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000404 die(_("invalid object '%s' given."), name);
Linus Torvalds19746322006-07-11 20:45:31 -0700405 if (obj->type == OBJ_COMMIT)
Junio C Hamano65056022006-04-28 23:20:52 -0700406 obj = &((struct commit *)obj)->tree->object;
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200407
Linus Torvalds19746322006-07-11 20:45:31 -0700408 if (obj->type == OBJ_TREE) {
Junio C Hamano65056022006-04-28 23:20:52 -0700409 obj->flags |= flags;
Michael Haggerty33055fa2013-05-25 11:08:04 +0200410 add_object_array(obj, name, &ent);
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200411 } else if (obj->type == OBJ_BLOB) {
Junio C Hamano65056022006-04-28 23:20:52 -0700412 if (2 <= blobs)
Ævar Arnfjörð Bjarmason54214522011-02-22 23:41:50 +0000413 die(_("more than two blobs given: '%s'"), name);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000414 hashcpy(blob[blobs].sha1, obj->oid.hash);
Junio C Hamano65056022006-04-28 23:20:52 -0700415 blob[blobs].name = name;
Michael Haggerty026f09e2013-05-25 11:08:05 +0200416 blob[blobs].mode = entry->mode;
Junio C Hamano65056022006-04-28 23:20:52 -0700417 blobs++;
Junio C Hamano230f5442006-05-03 23:54:34 -0700418
Michael Haggerty5b1e14e2013-05-25 11:08:06 +0200419 } else {
420 die(_("unhandled object '%s' given."), name);
Junio C Hamano65056022006-04-28 23:20:52 -0700421 }
Junio C Hamano65056022006-04-28 23:20:52 -0700422 }
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +0700423 if (rev.prune_data.nr)
Nguyễn Thái Ngọc Duyafe069d2010-12-17 19:43:06 +0700424 paths += rev.prune_data.nr;
Junio C Hamano65056022006-04-28 23:20:52 -0700425
426 /*
427 * Now, do the arguments look reasonable?
428 */
Michael Haggerty33055fa2013-05-25 11:08:04 +0200429 if (!ent.nr) {
Junio C Hamano65056022006-04-28 23:20:52 -0700430 switch (blobs) {
431 case 0:
Junio C Hamano0569e9b2008-05-23 22:28:56 -0700432 result = builtin_diff_files(&rev, argc, argv);
Junio C Hamano65056022006-04-28 23:20:52 -0700433 break;
434 case 1:
435 if (paths != 1)
436 usage(builtin_diff_usage);
Nguyễn Thái Ngọc Duy887c6c12013-11-20 08:26:41 +0700437 result = builtin_diff_b_f(&rev, argc, argv, blob);
Junio C Hamano65056022006-04-28 23:20:52 -0700438 break;
439 case 2:
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700440 if (paths)
441 usage(builtin_diff_usage);
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100442 result = builtin_diff_blobs(&rev, argc, argv, blob);
Junio C Hamano65056022006-04-28 23:20:52 -0700443 break;
444 default:
445 usage(builtin_diff_usage);
446 }
447 }
448 else if (blobs)
449 usage(builtin_diff_usage);
Michael Haggerty33055fa2013-05-25 11:08:04 +0200450 else if (ent.nr == 1)
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100451 result = builtin_diff_index(&rev, argc, argv);
Michael Haggerty33055fa2013-05-25 11:08:04 +0200452 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) {
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700456 /*
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700457 * diff A...B where there is at least one merge base
Michael Haggerty33055fa2013-05-25 11:08:04 +0200458 * 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.
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700463 */
Michael Haggerty33055fa2013-05-25 11:08:04 +0200464 result = builtin_diff_tree(&rev, argc, argv,
465 &ent.objects[0],
466 &ent.objects[ent.nr-1]);
Junio C Hamanoc008c0f2010-07-12 17:27:46 -0700467 } else
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100468 result = builtin_diff_combined(&rev, argc, argv,
Michael Haggerty33055fa2013-05-25 11:08:04 +0200469 ent.objects, ent.nr);
Junio C Hamanoda31b352007-12-13 23:40:27 -0800470 result = diff_result_code(&rev.diffopt, result);
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700471 if (1 < rev.diffopt.skip_stat_unmatch)
472 refresh_index_quietly();
Alex Riesen41bbf9d2007-03-14 01:17:04 +0100473 return result;
Junio C Hamano65056022006-04-28 23:20:52 -0700474}