blob: fe9f52c4796512f40869e511b55a2d97fa84532e [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 *
88 * Return:
Junio C Hamano1d848f62007-03-21 17:00:27 -070089 * - 2 for "yes, and all subsequent entries will be"
90 * - 1 for yes
Linus Torvalds5d865012007-03-18 15:18:30 -070091 * - zero for no
92 * - negative for "no, and no subsequent entries will be either"
93 */
94static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070095{
96 const char *path;
Linus Torvalds304de2d2007-03-17 20:06:24 -070097 const unsigned char *sha1;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070098 unsigned mode;
99 int i;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700100 int pathlen;
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700101 int never_interesting = -1;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700102
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700103 if (!opt->nr_paths)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700104 return 1;
105
Linus Torvalds304de2d2007-03-17 20:06:24 -0700106 sha1 = tree_entry_extract(desc, &path, &mode);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700107
Linus Torvalds304de2d2007-03-17 20:06:24 -0700108 pathlen = tree_entry_len(path, sha1);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700109
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700110 for (i = 0; i < opt->nr_paths; i++) {
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700111 const char *match = opt->paths[i];
112 int matchlen = opt->pathlens[i];
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700113 int m = -1; /* signals that we haven't called strncmp() */
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700114
115 if (baselen >= matchlen) {
116 /* If it doesn't match, move along... */
117 if (strncmp(base, match, matchlen))
118 continue;
119
Junio C Hamano1d848f62007-03-21 17:00:27 -0700120 /*
Björn Steinbrinkf0946cb2009-03-31 17:05:01 +0200121 * If the base is a subdirectory of a path which
122 * was specified, all of them are interesting.
Junio C Hamano1d848f62007-03-21 17:00:27 -0700123 */
Björn Steinbrinkf0946cb2009-03-31 17:05:01 +0200124 if (!matchlen ||
125 base[matchlen] == '/' ||
126 match[matchlen - 1] == '/')
127 return 2;
128
129 /* Just a random prefix match */
130 continue;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700131 }
132
133 /* Does the base match? */
134 if (strncmp(base, match, baselen))
135 continue;
136
137 match += baselen;
138 matchlen -= baselen;
139
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700140 if (never_interesting) {
141 /*
142 * We have not seen any match that sorts later
143 * than the current path.
144 */
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700145
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700146 /*
147 * Does match sort strictly earlier than path
148 * with their common parts?
149 */
150 m = strncmp(match, path,
151 (matchlen < pathlen) ? matchlen : pathlen);
152 if (m < 0)
153 continue;
154
155 /*
156 * If we come here even once, that means there is at
157 * least one pathspec that would sort equal to or
158 * later than the path we are currently looking at.
159 * In other words, if we have never reached this point
160 * after iterating all pathspecs, it means all
161 * pathspecs are either outside of base, or inside the
162 * base but sorts strictly earlier than the current
163 * one. In either case, they will never match the
164 * subsequent entries. In such a case, we initialized
165 * the variable to -1 and that is what will be
166 * returned, allowing the caller to terminate early.
167 */
168 never_interesting = 0;
169 }
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700170
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700171 if (pathlen > matchlen)
172 continue;
173
174 if (matchlen > pathlen) {
175 if (match[pathlen] != '/')
176 continue;
177 if (!S_ISDIR(mode))
178 continue;
179 }
180
Junio C Hamanoccc744a2007-03-21 12:34:46 -0700181 if (m == -1)
182 /*
183 * we cheated and did not do strncmp(), so we do
184 * that here.
185 */
186 m = strncmp(match, path, pathlen);
187
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700188 /*
189 * If common part matched earlier then it is a hit,
190 * because we rejected the case where path is not a
191 * leading directory and is shorter than match.
192 */
193 if (!m)
194 return 1;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700195 }
Junio C Hamano7d2f6672007-03-21 09:51:47 -0700196 return never_interesting; /* No matches */
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700197}
198
199/* A whole sub-tree went away or appeared */
Linus Torvalds304de2d2007-03-17 20:06:24 -0700200static 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 -0700201{
Junio C Hamano1d848f62007-03-21 17:00:27 -0700202 int all_interesting = 0;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700203 while (desc->size) {
Junio C Hamano1d848f62007-03-21 17:00:27 -0700204 int show;
205
206 if (all_interesting)
207 show = 1;
208 else {
209 show = tree_entry_interesting(desc, base, baselen,
210 opt);
211 if (show == 2)
212 all_interesting = 1;
213 }
Linus Torvalds5d865012007-03-18 15:18:30 -0700214 if (show < 0)
215 break;
216 if (show)
Linus Torvalds304de2d2007-03-17 20:06:24 -0700217 show_entry(opt, prefix, desc, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700218 update_tree_entry(desc);
219 }
220}
221
222/* A file entry went away or appeared */
David Rientjescf995ed2006-08-14 13:39:27 -0700223static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
Linus Torvalds304de2d2007-03-17 20:06:24 -0700224 const char *base, int baselen)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700225{
226 unsigned mode;
227 const char *path;
Linus Torvalds50f9a852006-01-31 14:10:56 -0800228 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
Dmitry Potapovfd55a192008-07-16 18:54:02 +0400229 int pathlen = tree_entry_len(path, sha1);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700230
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100231 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
Nicolas Pitre21666f12007-02-26 14:55:59 -0500232 enum object_type type;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700233 char *newbase = malloc_base(base, baselen, path, pathlen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700234 struct tree_desc inner;
235 void *tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700236 unsigned long size;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700237
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700238 tree = read_sha1_file(sha1, &type, &size);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500239 if (!tree || type != OBJ_TREE)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700240 die("corrupt tree sha %s", sha1_to_hex(sha1));
241
Nick Edelendf533f32009-06-13 17:06:09 -0700242 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
243 newbase[baselen + pathlen] = 0;
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100244 opt->add_remove(opt, *prefix, mode, sha1, newbase, 0);
Nick Edelendf533f32009-06-13 17:06:09 -0700245 newbase[baselen + pathlen] = '/';
246 }
247
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700248 init_tree_desc(&inner, tree, size);
Linus Torvalds304de2d2007-03-17 20:06:24 -0700249 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700250
251 free(tree);
252 free(newbase);
David Rientjescf995ed2006-08-14 13:39:27 -0700253 } else {
Dmitry Potapovfd55a192008-07-16 18:54:02 +0400254 char *fullname = malloc_fullname(base, baselen, path, pathlen);
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100255 opt->add_remove(opt, prefix[0], mode, sha1, fullname, 0);
Dmitry Potapovfd55a192008-07-16 18:54:02 +0400256 free(fullname);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700257 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700258}
259
Linus Torvalds5d865012007-03-18 15:18:30 -0700260static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt)
261{
Junio C Hamano1d848f62007-03-21 17:00:27 -0700262 int all_interesting = 0;
Linus Torvalds5d865012007-03-18 15:18:30 -0700263 while (t->size) {
Junio C Hamano1d848f62007-03-21 17:00:27 -0700264 int show;
265
266 if (all_interesting)
267 show = 1;
268 else {
269 show = tree_entry_interesting(t, base, baselen, opt);
270 if (show == 2)
271 all_interesting = 1;
272 }
Linus Torvalds5d865012007-03-18 15:18:30 -0700273 if (!show) {
274 update_tree_entry(t);
275 continue;
276 }
277 /* Skip it all? */
278 if (show < 0)
279 t->size = 0;
280 return;
281 }
282}
283
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700284int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
285{
Linus Torvalds304de2d2007-03-17 20:06:24 -0700286 int baselen = strlen(base);
287
Linus Torvalds5d865012007-03-18 15:18:30 -0700288 for (;;) {
Junio C Hamano90b19942009-05-23 01:15:35 -0700289 if (DIFF_OPT_TST(opt, QUICK) &&
Junio C Hamanof2451942009-05-22 12:45:29 -0700290 DIFF_OPT_TST(opt, HAS_CHANGES))
Junio C Hamano822cac02007-03-14 11:12:51 -0700291 break;
Linus Torvalds5d865012007-03-18 15:18:30 -0700292 if (opt->nr_paths) {
293 skip_uninteresting(t1, base, baselen, opt);
294 skip_uninteresting(t2, base, baselen, opt);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700295 }
296 if (!t1->size) {
Linus Torvalds5d865012007-03-18 15:18:30 -0700297 if (!t2->size)
298 break;
Linus Torvalds304de2d2007-03-17 20:06:24 -0700299 show_entry(opt, "+", t2, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700300 update_tree_entry(t2);
301 continue;
302 }
303 if (!t2->size) {
Linus Torvalds304de2d2007-03-17 20:06:24 -0700304 show_entry(opt, "-", t1, base, baselen);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700305 update_tree_entry(t1);
306 continue;
307 }
Linus Torvalds304de2d2007-03-17 20:06:24 -0700308 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700309 case -1:
310 update_tree_entry(t1);
311 continue;
312 case 0:
313 update_tree_entry(t1);
314 /* Fallthrough */
315 case 1:
316 update_tree_entry(t2);
317 continue;
318 }
Junio C Hamano7e44c932008-08-31 09:39:19 -0700319 die("git diff-tree: internal error");
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700320 }
321 return 0;
322}
323
Linus Torvalds750f7b62007-06-19 14:22:46 -0700324/*
325 * Does it look like the resulting diff might be due to a rename?
326 * - single entry
327 * - not a valid previous file
328 */
329static inline int diff_might_be_rename(void)
330{
331 return diff_queued_diff.nr == 1 &&
332 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
333}
334
335static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
336{
337 struct diff_options diff_opts;
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700338 struct diff_queue_struct *q = &diff_queued_diff;
339 struct diff_filepair *choice;
340 const char *paths[1];
Linus Torvalds750f7b62007-06-19 14:22:46 -0700341 int i;
342
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700343 /* Remove the file creation entry from the diff queue, and remember it */
344 choice = q->queue[0];
345 q->nr = 0;
346
Linus Torvalds750f7b62007-06-19 14:22:46 -0700347 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100348 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700349 diff_opts.detect_rename = DIFF_DETECT_RENAME;
350 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
351 diff_opts.single_follow = opt->paths[0];
Linus Torvalds6dd4b662007-10-20 12:31:31 -0700352 diff_opts.break_opt = opt->break_opt;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700353 paths[0] = NULL;
354 diff_tree_setup_paths(paths, &diff_opts);
355 if (diff_setup_done(&diff_opts) < 0)
356 die("unable to set up diff options to follow renames");
357 diff_tree(t1, t2, base, &diff_opts);
358 diffcore_std(&diff_opts);
Mike Hommey03b69c72007-12-11 22:59:55 +0100359 diff_tree_release_paths(&diff_opts);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700360
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700361 /* Go through the new set of filepairing, and see if we find a more interesting one */
362 for (i = 0; i < q->nr; i++) {
363 struct diff_filepair *p = q->queue[i];
Linus Torvalds750f7b62007-06-19 14:22:46 -0700364
365 /*
366 * Found a source? Not only do we use that for the new
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700367 * diff_queued_diff, we will also use that as the path in
Linus Torvalds750f7b62007-06-19 14:22:46 -0700368 * the future!
369 */
370 if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) {
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700371 /* Switch the file-pairs around */
372 q->queue[i] = choice;
373 choice = p;
374
375 /* Update the path we use from now on.. */
Mike Hommey03b69c72007-12-11 22:59:55 +0100376 diff_tree_release_paths(opt);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700377 opt->paths[0] = xstrdup(p->one->path);
378 diff_tree_setup_paths(opt->paths, opt);
379 break;
380 }
381 }
382
383 /*
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100384 * Then, discard all the non-relevant file pairs...
Linus Torvalds750f7b62007-06-19 14:22:46 -0700385 */
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700386 for (i = 0; i < q->nr; i++) {
387 struct diff_filepair *p = q->queue[i];
388 diff_free_filepair(p);
389 }
390
391 /*
392 * .. and re-instate the one we want (which might be either the
393 * original one, or the rename/copy we found)
394 */
395 q->queue[0] = choice;
396 q->nr = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700397}
398
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700399int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
400{
401 void *tree1, *tree2;
402 struct tree_desc t1, t2;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700403 unsigned long size1, size2;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700404 int retval;
405
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700406 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700407 if (!tree1)
408 die("unable to read source tree (%s)", sha1_to_hex(old));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700409 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700410 if (!tree2)
411 die("unable to read destination tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700412 init_tree_desc(&t1, tree1, size1);
413 init_tree_desc(&t2, tree2, size2);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700414 retval = diff_tree(&t1, &t2, base, opt);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100415 if (DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
Linus Torvalds750f7b62007-06-19 14:22:46 -0700416 init_tree_desc(&t1, tree1, size1);
417 init_tree_desc(&t2, tree2, size2);
418 try_to_follow_renames(&t1, &t2, base, opt);
419 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700420 free(tree1);
421 free(tree2);
422 return retval;
423}
424
Rene Scharfe2b603562006-10-26 18:52:39 +0200425int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
426{
427 int retval;
428 void *tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700429 unsigned long size;
Rene Scharfe2b603562006-10-26 18:52:39 +0200430 struct tree_desc empty, real;
431
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700432 tree = read_object_with_reference(new, tree_type, &size, NULL);
Rene Scharfe2b603562006-10-26 18:52:39 +0200433 if (!tree)
434 die("unable to read root tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700435 init_tree_desc(&real, tree, size);
Rene Scharfe2b603562006-10-26 18:52:39 +0200436
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700437 init_tree_desc(&empty, "", 0);
Rene Scharfe2b603562006-10-26 18:52:39 +0200438 retval = diff_tree(&empty, &real, base, opt);
439 free(tree);
440 return retval;
441}
442
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700443static int count_paths(const char **paths)
444{
445 int i = 0;
446 while (*paths++)
447 i++;
448 return i;
449}
450
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700451void diff_tree_release_paths(struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700452{
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700453 free(opt->pathlens);
454}
455
456void diff_tree_setup_paths(const char **p, struct diff_options *opt)
457{
458 opt->nr_paths = 0;
459 opt->pathlens = NULL;
460 opt->paths = NULL;
461
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700462 if (p) {
463 int i;
464
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700465 opt->paths = p;
466 opt->nr_paths = count_paths(p);
467 if (opt->nr_paths == 0) {
468 opt->pathlens = NULL;
Junio C Hamano7e4a2a82005-12-26 12:34:56 -0800469 return;
470 }
Junio C Hamanoa8baa7b2006-04-10 16:39:11 -0700471 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
472 for (i=0; i < opt->nr_paths; i++)
473 opt->pathlens[i] = strlen(p[i]);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700474 }
475}