blob: 12c9a88884ec3bb70a1744e44235c578a44e08e6 [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
Linus Torvalds304de2d2007-03-17 20:06:24 -07009static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070010{
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070011 char *newbase = xmalloc(baselen + pathlen + 2);
12 memcpy(newbase, base, baselen);
13 memcpy(newbase + baselen, path, pathlen);
14 memcpy(newbase + baselen + pathlen, "/", 2);
15 return newbase;
16}
17
Dmitry Potapovfd55a192008-07-16 18:54:02 +040018static char *malloc_fullname(const char *base, int baselen, const char *path, int pathlen)
19{
20 char *fullname = xmalloc(baselen + pathlen + 1);
21 memcpy(fullname, base, baselen);
22 memcpy(fullname + baselen, path, pathlen);
23 fullname[baselen + pathlen] = 0;
24 return fullname;
25}
26
David Rientjescf995ed2006-08-14 13:39:27 -070027static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
Linus Torvalds304de2d2007-03-17 20:06:24 -070028 const char *base, int baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070029
Linus Torvalds304de2d2007-03-17 20:06:24 -070030static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070031{
32 unsigned mode1, mode2;
33 const char *path1, *path2;
34 const unsigned char *sha1, *sha2;
35 int cmp, pathlen1, pathlen2;
Dmitry Potapovfd55a192008-07-16 18:54:02 +040036 char *fullname;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070037
Linus Torvalds50f9a852006-01-31 14:10:56 -080038 sha1 = tree_entry_extract(t1, &path1, &mode1);
39 sha2 = tree_entry_extract(t2, &path2, &mode2);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070040
Linus Torvalds304de2d2007-03-17 20:06:24 -070041 pathlen1 = tree_entry_len(path1, sha1);
42 pathlen2 = tree_entry_len(path2, sha2);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070043 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
44 if (cmp < 0) {
Linus Torvalds304de2d2007-03-17 20:06:24 -070045 show_entry(opt, "-", t1, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070046 return -1;
47 }
48 if (cmp > 0) {
Linus Torvalds304de2d2007-03-17 20:06:24 -070049 show_entry(opt, "+", t2, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070050 return 1;
51 }
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010052 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070053 return 0;
54
55 /*
56 * If the filemode has changed to/from a directory from/to a regular
57 * file, we need to consider it a remove and an add.
58 */
59 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
Linus Torvalds304de2d2007-03-17 20:06:24 -070060 show_entry(opt, "-", t1, base, baselen);
61 show_entry(opt, "+", t2, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070062 return 0;
63 }
64
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010065 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070066 int retval;
Linus Torvalds304de2d2007-03-17 20:06:24 -070067 char *newbase = malloc_base(base, baselen, path1, pathlen1);
Dmitry Potapovfd55a192008-07-16 18:54:02 +040068 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
69 newbase[baselen + pathlen1] = 0;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070070 opt->change(opt, mode1, mode2,
Jens Lehmanne3d42c42010-01-18 21:26:18 +010071 sha1, sha2, newbase, 0, 0);
Dmitry Potapovfd55a192008-07-16 18:54:02 +040072 newbase[baselen + pathlen1] = '/';
73 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070074 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
75 free(newbase);
76 return retval;
77 }
78
Dmitry Potapovfd55a192008-07-16 18:54:02 +040079 fullname = malloc_fullname(base, baselen, path1, pathlen1);
Jens Lehmanne3d42c42010-01-18 21:26:18 +010080 opt->change(opt, mode1, mode2, sha1, sha2, fullname, 0, 0);
Dmitry Potapovfd55a192008-07-16 18:54:02 +040081 free(fullname);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070082 return 0;
83}
84
Linus Torvalds5d865012007-03-18 15:18:30 -070085/*
86 * Is a tree entry interesting given the pathspec we have?
87 *
Elijah Newrenb6b987a2010-08-26 00:21:46 -060088 * Pre-condition: baselen == 0 || base[baselen-1] == '/'
89 *
Linus Torvalds5d865012007-03-18 15:18:30 -070090 * Return:
Junio C Hamano1d848f62007-03-21 17:00:27 -070091 * - 2 for "yes, and all subsequent entries will be"
92 * - 1 for yes
Linus Torvalds5d865012007-03-18 15:18:30 -070093 * - zero for no
94 * - negative for "no, and no subsequent entries will be either"
95 */
96static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070097{
98 const char *path;
Linus Torvalds304de2d2007-03-17 20:06:24 -070099 const unsigned char *sha1;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700100 unsigned mode;
101 int i;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700102 int pathlen;
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700103 int never_interesting = -1;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700104
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700105 if (!opt->nr_paths)
Elijah Newren4a5e74f2010-08-26 00:21:48 -0600106 return 2;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700107
Linus Torvalds304de2d2007-03-17 20:06:24 -0700108 sha1 = tree_entry_extract(desc, &path, &mode);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700109
Linus Torvalds304de2d2007-03-17 20:06:24 -0700110 pathlen = tree_entry_len(path, sha1);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700111
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700112 for (i = 0; i < opt->nr_paths; i++) {
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700113 const char *match = opt->paths[i];
114 int matchlen = opt->pathlens[i];
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700115 int m = -1; /* signals that we haven't called strncmp() */
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700116
117 if (baselen >= matchlen) {
118 /* If it doesn't match, move along... */
119 if (strncmp(base, match, matchlen))
120 continue;
121
Junio C Hamano1d848f62007-03-21 17:00:27 -0700122 /*
Björn Steinbrinkf0946cb2009-03-31 17:05:01 +0200123 * If the base is a subdirectory of a path which
124 * was specified, all of them are interesting.
Junio C Hamano1d848f62007-03-21 17:00:27 -0700125 */
Björn Steinbrinkf0946cb2009-03-31 17:05:01 +0200126 if (!matchlen ||
127 base[matchlen] == '/' ||
128 match[matchlen - 1] == '/')
129 return 2;
130
131 /* Just a random prefix match */
132 continue;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700133 }
134
135 /* Does the base match? */
136 if (strncmp(base, match, baselen))
137 continue;
138
139 match += baselen;
140 matchlen -= baselen;
141
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700142 if (never_interesting) {
143 /*
144 * We have not seen any match that sorts later
145 * than the current path.
146 */
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700147
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700148 /*
149 * Does match sort strictly earlier than path
150 * with their common parts?
151 */
152 m = strncmp(match, path,
153 (matchlen < pathlen) ? matchlen : pathlen);
154 if (m < 0)
155 continue;
156
157 /*
158 * If we come here even once, that means there is at
159 * least one pathspec that would sort equal to or
160 * later than the path we are currently looking at.
161 * In other words, if we have never reached this point
162 * after iterating all pathspecs, it means all
163 * pathspecs are either outside of base, or inside the
164 * base but sorts strictly earlier than the current
165 * one. In either case, they will never match the
166 * subsequent entries. In such a case, we initialized
167 * the variable to -1 and that is what will be
168 * returned, allowing the caller to terminate early.
169 */
170 never_interesting = 0;
171 }
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700172
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700173 if (pathlen > matchlen)
174 continue;
175
176 if (matchlen > pathlen) {
177 if (match[pathlen] != '/')
178 continue;
179 if (!S_ISDIR(mode))
180 continue;
181 }
182
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700183 if (m == -1)
184 /*
185 * we cheated and did not do strncmp(), so we do
186 * that here.
187 */
188 m = strncmp(match, path, pathlen);
189
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700190 /*
191 * If common part matched earlier then it is a hit,
192 * because we rejected the case where path is not a
193 * leading directory and is shorter than match.
194 */
195 if (!m)
196 return 1;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700197 }
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700198 return never_interesting; /* No matches */
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700199}
200
201/* A whole sub-tree went away or appeared */
Linus Torvalds304de2d2007-03-17 20:06:24 -0700202static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700203{
Junio C Hamano1d848f62007-03-21 17:00:27 -0700204 int all_interesting = 0;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700205 while (desc->size) {
Junio C Hamano1d848f62007-03-21 17:00:27 -0700206 int show;
207
208 if (all_interesting)
209 show = 1;
210 else {
211 show = tree_entry_interesting(desc, base, baselen,
212 opt);
213 if (show == 2)
214 all_interesting = 1;
215 }
Linus Torvalds5d865012007-03-18 15:18:30 -0700216 if (show < 0)
217 break;
218 if (show)
Linus Torvalds304de2d2007-03-17 20:06:24 -0700219 show_entry(opt, prefix, desc, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700220 update_tree_entry(desc);
221 }
222}
223
224/* A file entry went away or appeared */
David Rientjescf995ed2006-08-14 13:39:27 -0700225static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
Linus Torvalds304de2d2007-03-17 20:06:24 -0700226 const char *base, int baselen)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700227{
228 unsigned mode;
229 const char *path;
Linus Torvalds50f9a852006-01-31 14:10:56 -0800230 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
Dmitry Potapovfd55a192008-07-16 18:54:02 +0400231 int pathlen = tree_entry_len(path, sha1);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700232
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100233 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
Nicolas Pitre21666f12007-02-26 14:55:59 -0500234 enum object_type type;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700235 char *newbase = malloc_base(base, baselen, path, pathlen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700236 struct tree_desc inner;
237 void *tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700238 unsigned long size;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700239
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700240 tree = read_sha1_file(sha1, &type, &size);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500241 if (!tree || type != OBJ_TREE)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700242 die("corrupt tree sha %s", sha1_to_hex(sha1));
243
Nick Edelendf533f32009-06-13 17:06:09 -0700244 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
245 newbase[baselen + pathlen] = 0;
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100246 opt->add_remove(opt, *prefix, mode, sha1, newbase, 0);
Nick Edelendf533f32009-06-13 17:06:09 -0700247 newbase[baselen + pathlen] = '/';
248 }
249
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700250 init_tree_desc(&inner, tree, size);
Linus Torvalds304de2d2007-03-17 20:06:24 -0700251 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700252
253 free(tree);
254 free(newbase);
David Rientjescf995ed2006-08-14 13:39:27 -0700255 } else {
Dmitry Potapovfd55a192008-07-16 18:54:02 +0400256 char *fullname = malloc_fullname(base, baselen, path, pathlen);
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100257 opt->add_remove(opt, prefix[0], mode, sha1, fullname, 0);
Dmitry Potapovfd55a192008-07-16 18:54:02 +0400258 free(fullname);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700259 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700260}
261
Elijah Newren7e1ec0d2010-08-26 00:21:49 -0600262static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt, int *all_interesting)
Linus Torvalds5d865012007-03-18 15:18:30 -0700263{
264 while (t->size) {
Elijah Newren7e1ec0d2010-08-26 00:21:49 -0600265 int show = tree_entry_interesting(t, base, baselen, opt);
266 if (show == 2)
267 *all_interesting = 1;
Linus Torvalds5d865012007-03-18 15:18:30 -0700268 if (!show) {
269 update_tree_entry(t);
270 continue;
271 }
272 /* Skip it all? */
273 if (show < 0)
274 t->size = 0;
275 return;
276 }
277}
278
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700279int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
280{
Linus Torvalds304de2d2007-03-17 20:06:24 -0700281 int baselen = strlen(base);
Elijah Newren7e1ec0d2010-08-26 00:21:49 -0600282 int all_t1_interesting = 0;
283 int all_t2_interesting = 0;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700284
Linus Torvalds5d865012007-03-18 15:18:30 -0700285 for (;;) {
Junio C Hamano90b19942009-05-23 01:15:35 -0700286 if (DIFF_OPT_TST(opt, QUICK) &&
Junio C Hamanof2451942009-05-22 12:45:29 -0700287 DIFF_OPT_TST(opt, HAS_CHANGES))
Junio C Hamano822cac02007-03-14 11:12:51 -0700288 break;
Linus Torvalds5d865012007-03-18 15:18:30 -0700289 if (opt->nr_paths) {
Elijah Newren7e1ec0d2010-08-26 00:21:49 -0600290 if (!all_t1_interesting)
291 skip_uninteresting(t1, base, baselen, opt,
292 &all_t1_interesting);
293 if (!all_t2_interesting)
294 skip_uninteresting(t2, base, baselen, opt,
295 &all_t2_interesting);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700296 }
297 if (!t1->size) {
Linus Torvalds5d865012007-03-18 15:18:30 -0700298 if (!t2->size)
299 break;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700300 show_entry(opt, "+", t2, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700301 update_tree_entry(t2);
302 continue;
303 }
304 if (!t2->size) {
Linus Torvalds304de2d2007-03-17 20:06:24 -0700305 show_entry(opt, "-", t1, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700306 update_tree_entry(t1);
307 continue;
308 }
Linus Torvalds304de2d2007-03-17 20:06:24 -0700309 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700310 case -1:
311 update_tree_entry(t1);
312 continue;
313 case 0:
314 update_tree_entry(t1);
315 /* Fallthrough */
316 case 1:
317 update_tree_entry(t2);
318 continue;
319 }
Junio C Hamano7e44c932008-08-31 09:39:19 -0700320 die("git diff-tree: internal error");
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700321 }
322 return 0;
323}
324
Linus Torvalds750f7b62007-06-19 14:22:46 -0700325/*
326 * Does it look like the resulting diff might be due to a rename?
327 * - single entry
328 * - not a valid previous file
329 */
330static inline int diff_might_be_rename(void)
331{
332 return diff_queued_diff.nr == 1 &&
333 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
334}
335
336static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
337{
338 struct diff_options diff_opts;
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700339 struct diff_queue_struct *q = &diff_queued_diff;
340 struct diff_filepair *choice;
341 const char *paths[1];
Linus Torvalds750f7b62007-06-19 14:22:46 -0700342 int i;
343
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700344 /* Remove the file creation entry from the diff queue, and remember it */
345 choice = q->queue[0];
346 q->nr = 0;
347
Linus Torvalds750f7b62007-06-19 14:22:46 -0700348 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100349 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Bo Yang0cdca132010-05-06 21:52:29 -0700350 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700351 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
352 diff_opts.single_follow = opt->paths[0];
Linus Torvalds6dd4b662007-10-20 12:31:31 -0700353 diff_opts.break_opt = opt->break_opt;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700354 paths[0] = NULL;
355 diff_tree_setup_paths(paths, &diff_opts);
356 if (diff_setup_done(&diff_opts) < 0)
357 die("unable to set up diff options to follow renames");
358 diff_tree(t1, t2, base, &diff_opts);
359 diffcore_std(&diff_opts);
Mike Hommey03b69c72007-12-11 22:59:55 +0100360 diff_tree_release_paths(&diff_opts);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700361
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700362 /* Go through the new set of filepairing, and see if we find a more interesting one */
Junio C Hamano44c48a92010-08-13 12:17:45 -0700363 opt->found_follow = 0;
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700364 for (i = 0; i < q->nr; i++) {
365 struct diff_filepair *p = q->queue[i];
Linus Torvalds750f7b62007-06-19 14:22:46 -0700366
367 /*
368 * Found a source? Not only do we use that for the new
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700369 * diff_queued_diff, we will also use that as the path in
Linus Torvalds750f7b62007-06-19 14:22:46 -0700370 * the future!
371 */
372 if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) {
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700373 /* Switch the file-pairs around */
374 q->queue[i] = choice;
375 choice = p;
376
377 /* Update the path we use from now on.. */
Mike Hommey03b69c72007-12-11 22:59:55 +0100378 diff_tree_release_paths(opt);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700379 opt->paths[0] = xstrdup(p->one->path);
380 diff_tree_setup_paths(opt->paths, opt);
Junio C Hamano44c48a92010-08-13 12:17:45 -0700381
382 /*
383 * The caller expects us to return a set of vanilla
384 * filepairs to let a later call to diffcore_std()
385 * it makes to sort the renames out (among other
386 * things), but we already have found renames
387 * ourselves; signal diffcore_std() not to muck with
388 * rename information.
389 */
390 opt->found_follow = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700391 break;
392 }
393 }
394
395 /*
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100396 * Then, discard all the non-relevant file pairs...
Linus Torvalds750f7b62007-06-19 14:22:46 -0700397 */
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700398 for (i = 0; i < q->nr; i++) {
399 struct diff_filepair *p = q->queue[i];
400 diff_free_filepair(p);
401 }
402
403 /*
404 * .. and re-instate the one we want (which might be either the
405 * original one, or the rename/copy we found)
406 */
407 q->queue[0] = choice;
408 q->nr = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700409}
410
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700411int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
412{
413 void *tree1, *tree2;
414 struct tree_desc t1, t2;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700415 unsigned long size1, size2;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700416 int retval;
417
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700418 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700419 if (!tree1)
420 die("unable to read source tree (%s)", sha1_to_hex(old));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700421 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700422 if (!tree2)
423 die("unable to read destination tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700424 init_tree_desc(&t1, tree1, size1);
425 init_tree_desc(&t2, tree2, size2);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700426 retval = diff_tree(&t1, &t2, base, opt);
Junio C Hamano39f75d22010-08-13 10:36:01 -0700427 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
Linus Torvalds750f7b62007-06-19 14:22:46 -0700428 init_tree_desc(&t1, tree1, size1);
429 init_tree_desc(&t2, tree2, size2);
430 try_to_follow_renames(&t1, &t2, base, opt);
431 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700432 free(tree1);
433 free(tree2);
434 return retval;
435}
436
Rene Scharfe2b603562006-10-26 18:52:39 +0200437int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
438{
439 int retval;
440 void *tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700441 unsigned long size;
Rene Scharfe2b603562006-10-26 18:52:39 +0200442 struct tree_desc empty, real;
443
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700444 tree = read_object_with_reference(new, tree_type, &size, NULL);
Rene Scharfe2b603562006-10-26 18:52:39 +0200445 if (!tree)
446 die("unable to read root tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700447 init_tree_desc(&real, tree, size);
Rene Scharfe2b603562006-10-26 18:52:39 +0200448
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700449 init_tree_desc(&empty, "", 0);
Rene Scharfe2b603562006-10-26 18:52:39 +0200450 retval = diff_tree(&empty, &real, base, opt);
451 free(tree);
452 return retval;
453}
454
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700455static int count_paths(const char **paths)
456{
457 int i = 0;
458 while (*paths++)
459 i++;
460 return i;
461}
462
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700463void diff_tree_release_paths(struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700464{
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700465 free(opt->pathlens);
466}
467
468void diff_tree_setup_paths(const char **p, struct diff_options *opt)
469{
470 opt->nr_paths = 0;
471 opt->pathlens = NULL;
472 opt->paths = NULL;
473
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700474 if (p) {
475 int i;
476
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700477 opt->paths = p;
478 opt->nr_paths = count_paths(p);
479 if (opt->nr_paths == 0) {
480 opt->pathlens = NULL;
Junio C Hamano7e4a2a82005-12-26 12:34:56 -0800481 return;
482 }
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700483 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
484 for (i=0; i < opt->nr_paths; i++)
485 opt->pathlens[i] = strlen(p[i]);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700486 }
487}