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