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