1/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
7#include "tree.h"
8
9static void show_entry(struct diff_options *opt, const char *prefix,
10 struct tree_desc *desc, struct strbuf *base);
11
12static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
13 struct strbuf *base, struct diff_options *opt)
14{
15 unsigned mode1, mode2;
16 const char *path1, *path2;
17 const unsigned char *sha1, *sha2;
18 int cmp, pathlen1, pathlen2;
19 int old_baselen = base->len;
20
21 sha1 = tree_entry_extract(t1, &path1, &mode1);
22 sha2 = tree_entry_extract(t2, &path2, &mode2);
23
24 pathlen1 = tree_entry_len(&t1->entry);
25 pathlen2 = tree_entry_len(&t2->entry);
26
27 /*
28 * NOTE files and directories *always* compare differently,
29 * even when having the same name.
30 */
31 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
32 if (cmp < 0) {
33 show_entry(opt, "-", t1, base);
34 return -1;
35 }
36 if (cmp > 0) {
37 show_entry(opt, "+", t2, base);
38 return 1;
39 }
40 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
41 return 0;
42
43 strbuf_add(base, path1, pathlen1);
44 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
45 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
46 opt->change(opt, mode1, mode2,
47 sha1, sha2, 1, 1, base->buf, 0, 0);
48 }
49 strbuf_addch(base, '/');
50 diff_tree_sha1(sha1, sha2, base->buf, opt);
51 } else {
52 opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0);
53 }
54 strbuf_setlen(base, old_baselen);
55 return 0;
56}
57
58/* A whole sub-tree went away or appeared */
59static void show_tree(struct diff_options *opt, const char *prefix,
60 struct tree_desc *desc, struct strbuf *base)
61{
62 enum interesting match = entry_not_interesting;
63 for (; desc->size; update_tree_entry(desc)) {
64 if (match != all_entries_interesting) {
65 match = tree_entry_interesting(&desc->entry, base, 0,
66 &opt->pathspec);
67 if (match == all_entries_not_interesting)
68 break;
69 if (match == entry_not_interesting)
70 continue;
71 }
72 show_entry(opt, prefix, desc, base);
73 }
74}
75
76/* A file entry went away or appeared */
77static void show_entry(struct diff_options *opt, const char *prefix,
78 struct tree_desc *desc, struct strbuf *base)
79{
80 unsigned mode;
81 const char *path;
82 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
83 int pathlen = tree_entry_len(&desc->entry);
84 int old_baselen = base->len;
85
86 strbuf_add(base, path, pathlen);
87 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
88 enum object_type type;
89 struct tree_desc inner;
90 void *tree;
91 unsigned long size;
92
93 tree = read_sha1_file(sha1, &type, &size);
94 if (!tree || type != OBJ_TREE)
95 die("corrupt tree sha %s", sha1_to_hex(sha1));
96
97 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
98 opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0);
99
100 strbuf_addch(base, '/');
101
102 init_tree_desc(&inner, tree, size);
103 show_tree(opt, prefix, &inner, base);
104 free(tree);
105 } else
106 opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0);
107
108 strbuf_setlen(base, old_baselen);
109}
110
111static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
112 struct diff_options *opt,
113 enum interesting *match)
114{
115 while (t->size) {
116 *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
117 if (*match) {
118 if (*match == all_entries_not_interesting)
119 t->size = 0;
120 break;
121 }
122 update_tree_entry(t);
123 }
124}
125
126int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
127 const char *base_str, struct diff_options *opt)
128{
129 struct strbuf base;
130 int baselen = strlen(base_str);
131 enum interesting t1_match = entry_not_interesting;
132 enum interesting t2_match = entry_not_interesting;
133
134 /* Enable recursion indefinitely */
135 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
136
137 strbuf_init(&base, PATH_MAX);
138 strbuf_add(&base, base_str, baselen);
139
140 for (;;) {
141 if (diff_can_quit_early(opt))
142 break;
143 if (opt->pathspec.nr) {
144 skip_uninteresting(t1, &base, opt, &t1_match);
145 skip_uninteresting(t2, &base, opt, &t2_match);
146 }
147 if (!t1->size) {
148 if (!t2->size)
149 break;
150 show_entry(opt, "+", t2, &base);
151 update_tree_entry(t2);
152 continue;
153 }
154 if (!t2->size) {
155 show_entry(opt, "-", t1, &base);
156 update_tree_entry(t1);
157 continue;
158 }
159 switch (compare_tree_entry(t1, t2, &base, opt)) {
160 case -1:
161 update_tree_entry(t1);
162 continue;
163 case 0:
164 update_tree_entry(t1);
165 /* Fallthrough */
166 case 1:
167 update_tree_entry(t2);
168 continue;
169 }
170 die("git diff-tree: internal error");
171 }
172
173 strbuf_release(&base);
174 return 0;
175}
176
177/*
178 * Does it look like the resulting diff might be due to a rename?
179 * - single entry
180 * - not a valid previous file
181 */
182static inline int diff_might_be_rename(void)
183{
184 return diff_queued_diff.nr == 1 &&
185 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
186}
187
188static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
189{
190 struct diff_options diff_opts;
191 struct diff_queue_struct *q = &diff_queued_diff;
192 struct diff_filepair *choice;
193 int i;
194
195 /*
196 * follow-rename code is very specific, we need exactly one
197 * path. Magic that matches more than one path is not
198 * supported.
199 */
200 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
201#if 0
202 /*
203 * We should reject wildcards as well. Unfortunately we
204 * haven't got a reliable way to detect that 'foo\*bar' in
205 * fact has no wildcards. nowildcard_len is merely a hint for
206 * optimization. Let it slip for now until wildmatch is taught
207 * about dry-run mode and returns wildcard info.
208 */
209 if (opt->pathspec.has_wildcard)
210 die("BUG:%s:%d: wildcards are not supported",
211 __FILE__, __LINE__);
212#endif
213
214 /* Remove the file creation entry from the diff queue, and remember it */
215 choice = q->queue[0];
216 q->nr = 0;
217
218 diff_setup(&diff_opts);
219 DIFF_OPT_SET(&diff_opts, RECURSIVE);
220 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
221 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
222 diff_opts.single_follow = opt->pathspec.items[0].match;
223 diff_opts.break_opt = opt->break_opt;
224 diff_opts.rename_score = opt->rename_score;
225 diff_setup_done(&diff_opts);
226 diff_tree(t1, t2, base, &diff_opts);
227 diffcore_std(&diff_opts);
228 free_pathspec(&diff_opts.pathspec);
229
230 /* Go through the new set of filepairing, and see if we find a more interesting one */
231 opt->found_follow = 0;
232 for (i = 0; i < q->nr; i++) {
233 struct diff_filepair *p = q->queue[i];
234
235 /*
236 * Found a source? Not only do we use that for the new
237 * diff_queued_diff, we will also use that as the path in
238 * the future!
239 */
240 if ((p->status == 'R' || p->status == 'C') &&
241 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
242 const char *path[2];
243
244 /* Switch the file-pairs around */
245 q->queue[i] = choice;
246 choice = p;
247
248 /* Update the path we use from now on.. */
249 path[0] = p->one->path;
250 path[1] = NULL;
251 free_pathspec(&opt->pathspec);
252 parse_pathspec(&opt->pathspec,
253 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
254 PATHSPEC_LITERAL_PATH, "", path);
255
256 /*
257 * The caller expects us to return a set of vanilla
258 * filepairs to let a later call to diffcore_std()
259 * it makes to sort the renames out (among other
260 * things), but we already have found renames
261 * ourselves; signal diffcore_std() not to muck with
262 * rename information.
263 */
264 opt->found_follow = 1;
265 break;
266 }
267 }
268
269 /*
270 * Then, discard all the non-relevant file pairs...
271 */
272 for (i = 0; i < q->nr; i++) {
273 struct diff_filepair *p = q->queue[i];
274 diff_free_filepair(p);
275 }
276
277 /*
278 * .. and re-instate the one we want (which might be either the
279 * original one, or the rename/copy we found)
280 */
281 q->queue[0] = choice;
282 q->nr = 1;
283}
284
285int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
286{
287 void *tree1, *tree2;
288 struct tree_desc t1, t2;
289 unsigned long size1, size2;
290 int retval;
291
292 tree1 = fill_tree_descriptor(&t1, old);
293 tree2 = fill_tree_descriptor(&t2, new);
294 size1 = t1.size;
295 size2 = t2.size;
296 retval = diff_tree(&t1, &t2, base, opt);
297 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
298 init_tree_desc(&t1, tree1, size1);
299 init_tree_desc(&t2, tree2, size2);
300 try_to_follow_renames(&t1, &t2, base, opt);
301 }
302 free(tree1);
303 free(tree2);
304 return retval;
305}
306
307int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
308{
309 return diff_tree_sha1(NULL, new, base, opt);
310}