tree_entry_interesting(): support depth limit
[git/git.git] / tree-diff.c
CommitLineData
ac1b3d12
LT
1/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
750f7b66 6#include "diffcore.h"
8e440259 7#include "tree.h"
ac1b3d12 8
48932677
NTND
9static void show_entry(struct diff_options *opt, const char *prefix,
10 struct tree_desc *desc, struct strbuf *base);
ac1b3d12 11
48932677
NTND
12static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
13 struct strbuf *base, struct diff_options *opt)
ac1b3d12
LT
14{
15 unsigned mode1, mode2;
16 const char *path1, *path2;
17 const unsigned char *sha1, *sha2;
18 int cmp, pathlen1, pathlen2;
48932677
NTND
19 int old_baselen = base->len;
20 int retval = 0;
ac1b3d12 21
50f9a858
LT
22 sha1 = tree_entry_extract(t1, &path1, &mode1);
23 sha2 = tree_entry_extract(t2, &path2, &mode2);
ac1b3d12 24
304de2d2
LT
25 pathlen1 = tree_entry_len(path1, sha1);
26 pathlen2 = tree_entry_len(path2, sha2);
ac1b3d12
LT
27 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
28 if (cmp < 0) {
48932677 29 show_entry(opt, "-", t1, base);
ac1b3d12
LT
30 return -1;
31 }
32 if (cmp > 0) {
48932677 33 show_entry(opt, "+", t2, base);
ac1b3d12
LT
34 return 1;
35 }
8f67f8ae 36 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
ac1b3d12
LT
37 return 0;
38
39 /*
40 * If the filemode has changed to/from a directory from/to a regular
41 * file, we need to consider it a remove and an add.
42 */
43 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
48932677
NTND
44 show_entry(opt, "-", t1, base);
45 show_entry(opt, "+", t2, base);
ac1b3d12
LT
46 return 0;
47 }
48
48932677 49 strbuf_add(base, path1, pathlen1);
8f67f8ae 50 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
fd55a19e 51 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
ac1b3d12 52 opt->change(opt, mode1, mode2,
48932677 53 sha1, sha2, base->buf, 0, 0);
fd55a19e 54 }
48932677
NTND
55 strbuf_addch(base, '/');
56 retval = diff_tree_sha1(sha1, sha2, base->buf, opt);
57 } else {
58 opt->change(opt, mode1, mode2, sha1, sha2, base->buf, 0, 0);
ac1b3d12 59 }
48932677 60 strbuf_setlen(base, old_baselen);
ac1b3d12
LT
61 return 0;
62}
63
ac1b3d12 64/* A whole sub-tree went away or appeared */
48932677
NTND
65static void show_tree(struct diff_options *opt, const char *prefix,
66 struct tree_desc *desc, struct strbuf *base)
ac1b3d12 67{
1d848f64 68 int all_interesting = 0;
ac1b3d12 69 while (desc->size) {
1d848f64
JH
70 int show;
71
72 if (all_interesting)
73 show = 1;
74 else {
48932677
NTND
75 show = tree_entry_interesting(&desc->entry, base,
76 &opt->pathspec);
1d848f64
JH
77 if (show == 2)
78 all_interesting = 1;
79 }
5d865017
LT
80 if (show < 0)
81 break;
82 if (show)
48932677 83 show_entry(opt, prefix, desc, base);
ac1b3d12
LT
84 update_tree_entry(desc);
85 }
86}
87
88/* A file entry went away or appeared */
48932677
NTND
89static void show_entry(struct diff_options *opt, const char *prefix,
90 struct tree_desc *desc, struct strbuf *base)
ac1b3d12
LT
91{
92 unsigned mode;
93 const char *path;
50f9a858 94 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
fd55a19e 95 int pathlen = tree_entry_len(path, sha1);
48932677 96 int old_baselen = base->len;
ac1b3d12 97
48932677 98 strbuf_add(base, path, pathlen);
8f67f8ae 99 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
21666f1a 100 enum object_type type;
ac1b3d12
LT
101 struct tree_desc inner;
102 void *tree;
6fda5e51 103 unsigned long size;
ac1b3d12 104
6fda5e51 105 tree = read_sha1_file(sha1, &type, &size);
21666f1a 106 if (!tree || type != OBJ_TREE)
ac1b3d12
LT
107 die("corrupt tree sha %s", sha1_to_hex(sha1));
108
48932677
NTND
109 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
110 opt->add_remove(opt, *prefix, mode, sha1, base->buf, 0);
df533f34 111
48932677 112 strbuf_addch(base, '/');
ac1b3d12 113
48932677
NTND
114 init_tree_desc(&inner, tree, size);
115 show_tree(opt, prefix, &inner, base);
ac1b3d12 116 free(tree);
48932677
NTND
117 } else
118 opt->add_remove(opt, prefix[0], mode, sha1, base->buf, 0);
119
120 strbuf_setlen(base, old_baselen);
ac1b3d12
LT
121}
122
48932677
NTND
123static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
124 struct diff_options *opt, int *all_interesting)
5d865017
LT
125{
126 while (t->size) {
48932677 127 int show = tree_entry_interesting(&t->entry, base, &opt->pathspec);
7e1ec0d4
EN
128 if (show == 2)
129 *all_interesting = 1;
5d865017
LT
130 if (!show) {
131 update_tree_entry(t);
132 continue;
133 }
134 /* Skip it all? */
135 if (show < 0)
136 t->size = 0;
137 return;
138 }
139}
140
48932677
NTND
141int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
142 const char *base_str, struct diff_options *opt)
ac1b3d12 143{
48932677
NTND
144 struct strbuf base;
145 int baselen = strlen(base_str);
7e1ec0d4
EN
146 int all_t1_interesting = 0;
147 int all_t2_interesting = 0;
304de2d2 148
bc96cc87
NTND
149 /* Enable recursion indefinitely */
150 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
151 opt->pathspec.max_depth = -1;
152
48932677
NTND
153 strbuf_init(&base, PATH_MAX);
154 strbuf_add(&base, base_str, baselen);
155
5d865017 156 for (;;) {
90b19941 157 if (DIFF_OPT_TST(opt, QUICK) &&
f245194f 158 DIFF_OPT_TST(opt, HAS_CHANGES))
822cac01 159 break;
66f13625 160 if (opt->pathspec.nr) {
7e1ec0d4 161 if (!all_t1_interesting)
48932677 162 skip_uninteresting(t1, &base, opt, &all_t1_interesting);
7e1ec0d4 163 if (!all_t2_interesting)
48932677 164 skip_uninteresting(t2, &base, opt, &all_t2_interesting);
ac1b3d12
LT
165 }
166 if (!t1->size) {
5d865017
LT
167 if (!t2->size)
168 break;
48932677 169 show_entry(opt, "+", t2, &base);
ac1b3d12
LT
170 update_tree_entry(t2);
171 continue;
172 }
173 if (!t2->size) {
48932677 174 show_entry(opt, "-", t1, &base);
ac1b3d12
LT
175 update_tree_entry(t1);
176 continue;
177 }
48932677 178 switch (compare_tree_entry(t1, t2, &base, opt)) {
ac1b3d12
LT
179 case -1:
180 update_tree_entry(t1);
181 continue;
182 case 0:
183 update_tree_entry(t1);
184 /* Fallthrough */
185 case 1:
186 update_tree_entry(t2);
187 continue;
188 }
7e44c935 189 die("git diff-tree: internal error");
ac1b3d12 190 }
48932677
NTND
191
192 strbuf_release(&base);
ac1b3d12
LT
193 return 0;
194}
195
750f7b66
LT
196/*
197 * Does it look like the resulting diff might be due to a rename?
198 * - single entry
199 * - not a valid previous file
200 */
201static inline int diff_might_be_rename(void)
202{
203 return diff_queued_diff.nr == 1 &&
204 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
205}
206
207static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
208{
209 struct diff_options diff_opts;
9f38e1ef
LT
210 struct diff_queue_struct *q = &diff_queued_diff;
211 struct diff_filepair *choice;
212 const char *paths[1];
750f7b66
LT
213 int i;
214
9f38e1ef
LT
215 /* Remove the file creation entry from the diff queue, and remember it */
216 choice = q->queue[0];
217 q->nr = 0;
218
750f7b66 219 diff_setup(&diff_opts);
8f67f8ae 220 DIFF_OPT_SET(&diff_opts, RECURSIVE);
0cdca133 221 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
750f7b66 222 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
66f13625 223 diff_opts.single_follow = opt->pathspec.raw[0];
6dd4b66f 224 diff_opts.break_opt = opt->break_opt;
750f7b66
LT
225 paths[0] = NULL;
226 diff_tree_setup_paths(paths, &diff_opts);
227 if (diff_setup_done(&diff_opts) < 0)
228 die("unable to set up diff options to follow renames");
229 diff_tree(t1, t2, base, &diff_opts);
230 diffcore_std(&diff_opts);
03b69c76 231 diff_tree_release_paths(&diff_opts);
750f7b66 232
9f38e1ef 233 /* Go through the new set of filepairing, and see if we find a more interesting one */
44c48a90 234 opt->found_follow = 0;
9f38e1ef
LT
235 for (i = 0; i < q->nr; i++) {
236 struct diff_filepair *p = q->queue[i];
750f7b66
LT
237
238 /*
239 * Found a source? Not only do we use that for the new
9f38e1ef 240 * diff_queued_diff, we will also use that as the path in
750f7b66
LT
241 * the future!
242 */
66f13625
NTND
243 if ((p->status == 'R' || p->status == 'C') &&
244 !strcmp(p->two->path, opt->pathspec.raw[0])) {
9f38e1ef
LT
245 /* Switch the file-pairs around */
246 q->queue[i] = choice;
247 choice = p;
248
249 /* Update the path we use from now on.. */
03b69c76 250 diff_tree_release_paths(opt);
66f13625
NTND
251 opt->pathspec.raw[0] = xstrdup(p->one->path);
252 diff_tree_setup_paths(opt->pathspec.raw, opt);
44c48a90
JH
253
254 /*
255 * The caller expects us to return a set of vanilla
256 * filepairs to let a later call to diffcore_std()
257 * it makes to sort the renames out (among other
258 * things), but we already have found renames
259 * ourselves; signal diffcore_std() not to muck with
260 * rename information.
261 */
262 opt->found_follow = 1;
750f7b66
LT
263 break;
264 }
265 }
266
267 /*
3ea3c215 268 * Then, discard all the non-relevant file pairs...
9f38e1ef
LT
269 */
270 for (i = 0; i < q->nr; i++) {
271 struct diff_filepair *p = q->queue[i];
272 diff_free_filepair(p);
273 }
274
275 /*
276 * .. and re-instate the one we want (which might be either the
277 * original one, or the rename/copy we found)
750f7b66 278 */
9f38e1ef
LT
279 q->queue[0] = choice;
280 q->nr = 1;
750f7b66
LT
281}
282
ac1b3d12
LT
283int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
284{
285 void *tree1, *tree2;
286 struct tree_desc t1, t2;
6fda5e51 287 unsigned long size1, size2;
ac1b3d12
LT
288 int retval;
289
6fda5e51 290 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
ac1b3d12
LT
291 if (!tree1)
292 die("unable to read source tree (%s)", sha1_to_hex(old));
6fda5e51 293 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
ac1b3d12
LT
294 if (!tree2)
295 die("unable to read destination tree (%s)", sha1_to_hex(new));
6fda5e51
LT
296 init_tree_desc(&t1, tree1, size1);
297 init_tree_desc(&t2, tree2, size2);
ac1b3d12 298 retval = diff_tree(&t1, &t2, base, opt);
39f75d26 299 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
750f7b66
LT
300 init_tree_desc(&t1, tree1, size1);
301 init_tree_desc(&t2, tree2, size2);
302 try_to_follow_renames(&t1, &t2, base, opt);
303 }
ac1b3d12
LT
304 free(tree1);
305 free(tree2);
306 return retval;
307}
308
2b60356d
RS
309int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
310{
311 int retval;
312 void *tree;
6fda5e51 313 unsigned long size;
2b60356d
RS
314 struct tree_desc empty, real;
315
6fda5e51 316 tree = read_object_with_reference(new, tree_type, &size, NULL);
2b60356d
RS
317 if (!tree)
318 die("unable to read root tree (%s)", sha1_to_hex(new));
6fda5e51 319 init_tree_desc(&real, tree, size);
2b60356d 320
6fda5e51 321 init_tree_desc(&empty, "", 0);
2b60356d
RS
322 retval = diff_tree(&empty, &real, base, opt);
323 free(tree);
324 return retval;
325}
326
a8baa7b9 327void diff_tree_release_paths(struct diff_options *opt)
ac1b3d12 328{
66f13625 329 free_pathspec(&opt->pathspec);
a8baa7b9
JH
330}
331
332void diff_tree_setup_paths(const char **p, struct diff_options *opt)
333{
66f13625 334 init_pathspec(&opt->pathspec, p);
ac1b3d12 335}