blob: ccf9d7c8fd41c00f13136ef99d24180b36a098c1 [file] [log] [blame]
Linus Torvaldsac1b3d12005-10-20 21:05:05 -07001/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
Linus Torvalds750f7b62007-06-19 14:22:46 -07006#include "diffcore.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02007#include "tree.h"
Linus Torvaldsac1b3d12005-10-20 21:05:05 -07008
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +07009static void show_entry(struct diff_options *opt, const char *prefix,
10 struct tree_desc *desc, struct strbuf *base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070011
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070012static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
13 struct strbuf *base, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070014{
15 unsigned mode1, mode2;
16 const char *path1, *path2;
17 const unsigned char *sha1, *sha2;
18 int cmp, pathlen1, pathlen2;
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070019 int old_baselen = base->len;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070020
Linus Torvalds50f9a852006-01-31 14:10:56 -080021 sha1 = tree_entry_extract(t1, &path1, &mode1);
22 sha2 = tree_entry_extract(t2, &path2, &mode2);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070023
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +110024 pathlen1 = tree_entry_len(&t1->entry);
25 pathlen2 = tree_entry_len(&t2->entry);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070026 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
27 if (cmp < 0) {
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070028 show_entry(opt, "-", t1, base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070029 return -1;
30 }
31 if (cmp > 0) {
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070032 show_entry(opt, "+", t2, base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070033 return 1;
34 }
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010035 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070036 return 0;
37
38 /*
39 * If the filemode has changed to/from a directory from/to a regular
40 * file, we need to consider it a remove and an add.
41 */
42 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070043 show_entry(opt, "-", t1, base);
44 show_entry(opt, "+", t2, base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070045 return 0;
46 }
47
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070048 strbuf_add(base, path1, pathlen1);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010049 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
Dmitry Potapovfd55a192008-07-16 18:54:02 +040050 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070051 opt->change(opt, mode1, mode2,
Jeff Kinge5450102012-07-28 11:03:01 -040052 sha1, sha2, 1, 1, base->buf, 0, 0);
Dmitry Potapovfd55a192008-07-16 18:54:02 +040053 }
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070054 strbuf_addch(base, '/');
Johannes Schindelinc0aa3352011-03-22 13:50:08 +010055 diff_tree_sha1(sha1, sha2, base->buf, opt);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070056 } else {
Jeff Kinge5450102012-07-28 11:03:01 -040057 opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070058 }
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070059 strbuf_setlen(base, old_baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070060 return 0;
61}
62
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070063/* A whole sub-tree went away or appeared */
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070064static void show_tree(struct diff_options *opt, const char *prefix,
65 struct tree_desc *desc, struct strbuf *base)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070066{
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110067 enum interesting match = entry_not_interesting;
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +070068 for (; desc->size; update_tree_entry(desc)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110069 if (match != all_entries_interesting) {
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +070070 match = tree_entry_interesting(&desc->entry, base, 0,
71 &opt->pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110072 if (match == all_entries_not_interesting)
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +070073 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110074 if (match == entry_not_interesting)
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +070075 continue;
Junio C Hamano1d848f62007-03-21 17:00:27 -070076 }
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +070077 show_entry(opt, prefix, desc, base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070078 }
79}
80
81/* A file entry went away or appeared */
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070082static void show_entry(struct diff_options *opt, const char *prefix,
83 struct tree_desc *desc, struct strbuf *base)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070084{
85 unsigned mode;
86 const char *path;
Linus Torvalds50f9a852006-01-31 14:10:56 -080087 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +110088 int pathlen = tree_entry_len(&desc->entry);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070089 int old_baselen = base->len;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070090
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +070091 strbuf_add(base, path, pathlen);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010092 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050093 enum object_type type;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070094 struct tree_desc inner;
95 void *tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -070096 unsigned long size;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070097
Linus Torvalds6fda5e52007-03-21 10:08:25 -070098 tree = read_sha1_file(sha1, &type, &size);
Nicolas Pitre21666f12007-02-26 14:55:59 -050099 if (!tree || type != OBJ_TREE)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700100 die("corrupt tree sha %s", sha1_to_hex(sha1));
101
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700102 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
Jeff Kinge5450102012-07-28 11:03:01 -0400103 opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700104
105 strbuf_addch(base, '/');
Nick Edelendf533f32009-06-13 17:06:09 -0700106
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700107 init_tree_desc(&inner, tree, size);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700108 show_tree(opt, prefix, &inner, base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700109 free(tree);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700110 } else
Jeff Kinge5450102012-07-28 11:03:01 -0400111 opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700112
113 strbuf_setlen(base, old_baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700114}
115
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700116static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +1100117 struct diff_options *opt,
118 enum interesting *match)
Linus Torvalds5d865012007-03-18 15:18:30 -0700119{
120 while (t->size) {
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +0700121 *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
122 if (*match) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +1100123 if (*match == all_entries_not_interesting)
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +0700124 t->size = 0;
125 break;
Linus Torvalds5d865012007-03-18 15:18:30 -0700126 }
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +0700127 update_tree_entry(t);
Linus Torvalds5d865012007-03-18 15:18:30 -0700128 }
129}
130
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700131int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
132 const char *base_str, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700133{
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700134 struct strbuf base;
135 int baselen = strlen(base_str);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +1100136 enum interesting t1_match = entry_not_interesting;
137 enum interesting t2_match = entry_not_interesting;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700138
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +0700139 /* Enable recursion indefinitely */
140 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +0700141
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700142 strbuf_init(&base, PATH_MAX);
143 strbuf_add(&base, base_str, baselen);
144
Linus Torvalds5d865012007-03-18 15:18:30 -0700145 for (;;) {
Junio C Hamano28b92642011-05-31 09:14:17 -0700146 if (diff_can_quit_early(opt))
Junio C Hamano822cac02007-03-14 11:12:51 -0700147 break;
Nguyễn Thái Ngọc Duy66f13622010-12-15 22:02:38 +0700148 if (opt->pathspec.nr) {
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +0700149 skip_uninteresting(t1, &base, opt, &t1_match);
150 skip_uninteresting(t2, &base, opt, &t2_match);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700151 }
152 if (!t1->size) {
Linus Torvalds5d865012007-03-18 15:18:30 -0700153 if (!t2->size)
154 break;
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700155 show_entry(opt, "+", t2, &base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700156 update_tree_entry(t2);
157 continue;
158 }
159 if (!t2->size) {
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700160 show_entry(opt, "-", t1, &base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700161 update_tree_entry(t1);
162 continue;
163 }
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700164 switch (compare_tree_entry(t1, t2, &base, opt)) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700165 case -1:
166 update_tree_entry(t1);
167 continue;
168 case 0:
169 update_tree_entry(t1);
170 /* Fallthrough */
171 case 1:
172 update_tree_entry(t2);
173 continue;
174 }
Junio C Hamano7e44c932008-08-31 09:39:19 -0700175 die("git diff-tree: internal error");
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700176 }
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700177
178 strbuf_release(&base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700179 return 0;
180}
181
Linus Torvalds750f7b62007-06-19 14:22:46 -0700182/*
183 * Does it look like the resulting diff might be due to a rename?
184 * - single entry
185 * - not a valid previous file
186 */
187static inline int diff_might_be_rename(void)
188{
189 return diff_queued_diff.nr == 1 &&
190 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
191}
192
193static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
194{
195 struct diff_options diff_opts;
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700196 struct diff_queue_struct *q = &diff_queued_diff;
197 struct diff_filepair *choice;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700198 int i;
199
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +0700200 /*
201 * follow-rename code is very specific, we need exactly one
202 * path. Magic that matches more than one path is not
203 * supported.
204 */
Nguyễn Thái Ngọc Duy5c6933d2013-07-14 15:36:06 +0700205 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +0700206#if 0
207 /*
208 * We should reject wildcards as well. Unfortunately we
209 * haven't got a reliable way to detect that 'foo\*bar' in
210 * fact has no wildcards. nowildcard_len is merely a hint for
211 * optimization. Let it slip for now until wildmatch is taught
212 * about dry-run mode and returns wildcard info.
213 */
214 if (opt->pathspec.has_wildcard)
215 die("BUG:%s:%d: wildcards are not supported",
216 __FILE__, __LINE__);
217#endif
218
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700219 /* Remove the file creation entry from the diff queue, and remember it */
220 choice = q->queue[0];
221 q->nr = 0;
222
Linus Torvalds750f7b62007-06-19 14:22:46 -0700223 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100224 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Bo Yang0cdca132010-05-06 21:52:29 -0700225 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700226 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
Nguyễn Thái Ngọc Duy61588cc2013-07-14 15:36:01 +0700227 diff_opts.single_follow = opt->pathspec.items[0].match;
Linus Torvalds6dd4b662007-10-20 12:31:31 -0700228 diff_opts.break_opt = opt->break_opt;
Jeff Kingdd98d882011-12-16 06:27:50 -0500229 diff_opts.rename_score = opt->rename_score;
Thomas Rast28452652012-08-03 14:16:24 +0200230 diff_setup_done(&diff_opts);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700231 diff_tree(t1, t2, base, &diff_opts);
232 diffcore_std(&diff_opts);
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700233 free_pathspec(&diff_opts.pathspec);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700234
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700235 /* Go through the new set of filepairing, and see if we find a more interesting one */
Junio C Hamano44c48a92010-08-13 12:17:45 -0700236 opt->found_follow = 0;
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700237 for (i = 0; i < q->nr; i++) {
238 struct diff_filepair *p = q->queue[i];
Linus Torvalds750f7b62007-06-19 14:22:46 -0700239
240 /*
241 * Found a source? Not only do we use that for the new
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700242 * diff_queued_diff, we will also use that as the path in
Linus Torvalds750f7b62007-06-19 14:22:46 -0700243 * the future!
244 */
Nguyễn Thái Ngọc Duy66f13622010-12-15 22:02:38 +0700245 if ((p->status == 'R' || p->status == 'C') &&
Nguyễn Thái Ngọc Duy61588cc2013-07-14 15:36:01 +0700246 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700247 const char *path[2];
248
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700249 /* Switch the file-pairs around */
250 q->queue[i] = choice;
251 choice = p;
252
253 /* Update the path we use from now on.. */
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700254 path[0] = p->one->path;
255 path[1] = NULL;
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700256 free_pathspec(&opt->pathspec);
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700257 parse_pathspec(&opt->pathspec, PATHSPEC_ALL_MAGIC, 0, "", path);
Junio C Hamano44c48a92010-08-13 12:17:45 -0700258
259 /*
260 * The caller expects us to return a set of vanilla
261 * filepairs to let a later call to diffcore_std()
262 * it makes to sort the renames out (among other
263 * things), but we already have found renames
264 * ourselves; signal diffcore_std() not to muck with
265 * rename information.
266 */
267 opt->found_follow = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700268 break;
269 }
270 }
271
272 /*
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100273 * Then, discard all the non-relevant file pairs...
Linus Torvalds750f7b62007-06-19 14:22:46 -0700274 */
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700275 for (i = 0; i < q->nr; i++) {
276 struct diff_filepair *p = q->queue[i];
277 diff_free_filepair(p);
278 }
279
280 /*
281 * .. and re-instate the one we want (which might be either the
282 * original one, or the rename/copy we found)
283 */
284 q->queue[0] = choice;
285 q->nr = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700286}
287
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700288int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
289{
290 void *tree1, *tree2;
291 struct tree_desc t1, t2;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700292 unsigned long size1, size2;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700293 int retval;
294
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700295 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700296 if (!tree1)
297 die("unable to read source tree (%s)", sha1_to_hex(old));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700298 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700299 if (!tree2)
300 die("unable to read destination tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700301 init_tree_desc(&t1, tree1, size1);
302 init_tree_desc(&t2, tree2, size2);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700303 retval = diff_tree(&t1, &t2, base, opt);
Junio C Hamano39f75d22010-08-13 10:36:01 -0700304 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
Linus Torvalds750f7b62007-06-19 14:22:46 -0700305 init_tree_desc(&t1, tree1, size1);
306 init_tree_desc(&t2, tree2, size2);
307 try_to_follow_renames(&t1, &t2, base, opt);
308 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700309 free(tree1);
310 free(tree2);
311 return retval;
312}
313
Rene Scharfe2b603562006-10-26 18:52:39 +0200314int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
315{
316 int retval;
317 void *tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700318 unsigned long size;
Rene Scharfe2b603562006-10-26 18:52:39 +0200319 struct tree_desc empty, real;
320
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700321 tree = read_object_with_reference(new, tree_type, &size, NULL);
Rene Scharfe2b603562006-10-26 18:52:39 +0200322 if (!tree)
323 die("unable to read root tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700324 init_tree_desc(&real, tree, size);
Rene Scharfe2b603562006-10-26 18:52:39 +0200325
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700326 init_tree_desc(&empty, "", 0);
Rene Scharfe2b603562006-10-26 18:52:39 +0200327 retval = diff_tree(&empty, &real, base, opt);
328 free(tree);
329 return retval;
330}