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