]>
Commit | Line | Data |
---|---|---|
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 | |
9bc06196 KS |
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(). | |
6ca844e9 KS |
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. | |
9bc06196 KS |
21 | */ |
22 | static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) | |
ac1b3d12 | 23 | { |
1a27a154 KS |
24 | struct name_entry *e1, *e2; |
25 | int cmp; | |
ac1b3d12 | 26 | |
6ca844e9 KS |
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 | ||
1a27a154 KS |
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); | |
903bba68 | 37 | return cmp; |
d00e980c KS |
38 | } |
39 | ||
40 | ||
41 | /* convert path, t1/t2 -> opt->diff_*() callbacks */ | |
42 | static 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; | |
fd55a19e | 65 | } |
d00e980c KS |
66 | |
67 | opt->add_remove(opt, addremove, mode, sha1, 1, path->buf, 0); | |
ac1b3d12 | 68 | } |
ac1b3d12 LT |
69 | } |
70 | ||
d00e980c KS |
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 | */ | |
80 | static void show_path(struct strbuf *base, struct diff_options *opt, | |
81 | struct tree_desc *t1, struct tree_desc *t2) | |
ac1b3d12 LT |
82 | { |
83 | unsigned mode; | |
84 | const char *path; | |
d00e980c | 85 | int pathlen; |
48932677 | 86 | int old_baselen = base->len; |
d00e980c KS |
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 | } | |
ac1b3d12 | 113 | |
48932677 | 114 | strbuf_add(base, path, pathlen); |
df533f34 | 115 | |
d00e980c KS |
116 | if (emitthis) |
117 | emit_diff(opt, base, t1, t2); | |
118 | ||
119 | if (recurse) { | |
48932677 | 120 | strbuf_addch(base, '/'); |
d00e980c KS |
121 | diff_tree_sha1(t1 ? t1->entry.sha1 : NULL, |
122 | t2 ? t2->entry.sha1 : NULL, base->buf, opt); | |
123 | } | |
48932677 NTND |
124 | |
125 | strbuf_setlen(base, old_baselen); | |
ac1b3d12 LT |
126 | } |
127 | ||
48932677 | 128 | static void skip_uninteresting(struct tree_desc *t, struct strbuf *base, |
e9066121 | 129 | struct diff_options *opt) |
5d865017 | 130 | { |
e9066121 KS |
131 | enum interesting match; |
132 | ||
5d865017 | 133 | while (t->size) { |
e9066121 KS |
134 | match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec); |
135 | if (match) { | |
136 | if (match == all_entries_not_interesting) | |
97d0b74a NTND |
137 | t->size = 0; |
138 | break; | |
5d865017 | 139 | } |
97d0b74a | 140 | update_tree_entry(t); |
5d865017 LT |
141 | } |
142 | } | |
143 | ||
ad6f3cc7 KS |
144 | static int diff_tree(struct tree_desc *t1, struct tree_desc *t2, |
145 | const char *base_str, struct diff_options *opt) | |
ac1b3d12 | 146 | { |
48932677 NTND |
147 | struct strbuf base; |
148 | int baselen = strlen(base_str); | |
304de2d2 | 149 | |
bc96cc87 NTND |
150 | /* Enable recursion indefinitely */ |
151 | opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE); | |
bc96cc87 | 152 | |
48932677 NTND |
153 | strbuf_init(&base, PATH_MAX); |
154 | strbuf_add(&base, base_str, baselen); | |
155 | ||
5d865017 | 156 | for (;;) { |
903bba68 KS |
157 | int cmp; |
158 | ||
28b9264d | 159 | if (diff_can_quit_early(opt)) |
822cac01 | 160 | break; |
66f13625 | 161 | if (opt->pathspec.nr) { |
e9066121 KS |
162 | skip_uninteresting(t1, &base, opt); |
163 | skip_uninteresting(t2, &base, opt); | |
ac1b3d12 | 164 | } |
6ca844e9 KS |
165 | if (!t1->size && !t2->size) |
166 | break; | |
5dfb2bbd | 167 | |
9bc06196 | 168 | cmp = tree_entry_pathcmp(t1, t2); |
5dfb2bbd KS |
169 | |
170 | /* t1 = t2 */ | |
171 | if (cmp == 0) { | |
903bba68 KS |
172 | if (DIFF_OPT_TST(opt, FIND_COPIES_HARDER) || |
173 | hashcmp(t1->entry.sha1, t2->entry.sha1) || | |
174 | (t1->entry.mode != t2->entry.mode)) | |
175 | show_path(&base, opt, t1, t2); | |
176 | ||
ac1b3d12 | 177 | update_tree_entry(t1); |
5dfb2bbd KS |
178 | update_tree_entry(t2); |
179 | } | |
180 | ||
181 | /* t1 < t2 */ | |
182 | else if (cmp < 0) { | |
903bba68 | 183 | show_path(&base, opt, t1, /*t2=*/NULL); |
ac1b3d12 | 184 | update_tree_entry(t1); |
5dfb2bbd KS |
185 | } |
186 | ||
187 | /* t1 > t2 */ | |
188 | else { | |
903bba68 | 189 | show_path(&base, opt, /*t1=*/NULL, t2); |
ac1b3d12 | 190 | update_tree_entry(t2); |
ac1b3d12 | 191 | } |
ac1b3d12 | 192 | } |
48932677 NTND |
193 | |
194 | strbuf_release(&base); | |
ac1b3d12 LT |
195 | return 0; |
196 | } | |
197 | ||
750f7b66 LT |
198 | /* |
199 | * Does it look like the resulting diff might be due to a rename? | |
200 | * - single entry | |
201 | * - not a valid previous file | |
202 | */ | |
203 | static inline int diff_might_be_rename(void) | |
204 | { | |
205 | return diff_queued_diff.nr == 1 && | |
206 | !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one); | |
207 | } | |
208 | ||
209 | static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) | |
210 | { | |
211 | struct diff_options diff_opts; | |
9f38e1ef LT |
212 | struct diff_queue_struct *q = &diff_queued_diff; |
213 | struct diff_filepair *choice; | |
750f7b66 LT |
214 | int i; |
215 | ||
8f4f8f45 NTND |
216 | /* |
217 | * follow-rename code is very specific, we need exactly one | |
218 | * path. Magic that matches more than one path is not | |
219 | * supported. | |
220 | */ | |
5c6933d2 | 221 | GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL); |
8f4f8f45 NTND |
222 | #if 0 |
223 | /* | |
224 | * We should reject wildcards as well. Unfortunately we | |
225 | * haven't got a reliable way to detect that 'foo\*bar' in | |
226 | * fact has no wildcards. nowildcard_len is merely a hint for | |
227 | * optimization. Let it slip for now until wildmatch is taught | |
228 | * about dry-run mode and returns wildcard info. | |
229 | */ | |
230 | if (opt->pathspec.has_wildcard) | |
231 | die("BUG:%s:%d: wildcards are not supported", | |
232 | __FILE__, __LINE__); | |
233 | #endif | |
234 | ||
9f38e1ef LT |
235 | /* Remove the file creation entry from the diff queue, and remember it */ |
236 | choice = q->queue[0]; | |
237 | q->nr = 0; | |
238 | ||
750f7b66 | 239 | diff_setup(&diff_opts); |
8f67f8ae | 240 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
0cdca133 | 241 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
750f7b66 | 242 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
61588ccf | 243 | diff_opts.single_follow = opt->pathspec.items[0].match; |
6dd4b66f | 244 | diff_opts.break_opt = opt->break_opt; |
dd98d88b | 245 | diff_opts.rename_score = opt->rename_score; |
28452655 | 246 | diff_setup_done(&diff_opts); |
750f7b66 LT |
247 | diff_tree(t1, t2, base, &diff_opts); |
248 | diffcore_std(&diff_opts); | |
bd1928df | 249 | free_pathspec(&diff_opts.pathspec); |
750f7b66 | 250 | |
9f38e1ef | 251 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
44c48a90 | 252 | opt->found_follow = 0; |
9f38e1ef LT |
253 | for (i = 0; i < q->nr; i++) { |
254 | struct diff_filepair *p = q->queue[i]; | |
750f7b66 LT |
255 | |
256 | /* | |
257 | * Found a source? Not only do we use that for the new | |
9f38e1ef | 258 | * diff_queued_diff, we will also use that as the path in |
750f7b66 LT |
259 | * the future! |
260 | */ | |
66f13625 | 261 | if ((p->status == 'R' || p->status == 'C') && |
61588ccf | 262 | !strcmp(p->two->path, opt->pathspec.items[0].match)) { |
9a087274 NTND |
263 | const char *path[2]; |
264 | ||
9f38e1ef LT |
265 | /* Switch the file-pairs around */ |
266 | q->queue[i] = choice; | |
267 | choice = p; | |
268 | ||
269 | /* Update the path we use from now on.. */ | |
9a087274 NTND |
270 | path[0] = p->one->path; |
271 | path[1] = NULL; | |
bd1928df | 272 | free_pathspec(&opt->pathspec); |
4a2d5ae2 NTND |
273 | parse_pathspec(&opt->pathspec, |
274 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
275 | PATHSPEC_LITERAL_PATH, "", path); | |
44c48a90 JH |
276 | |
277 | /* | |
278 | * The caller expects us to return a set of vanilla | |
279 | * filepairs to let a later call to diffcore_std() | |
280 | * it makes to sort the renames out (among other | |
281 | * things), but we already have found renames | |
282 | * ourselves; signal diffcore_std() not to muck with | |
283 | * rename information. | |
284 | */ | |
285 | opt->found_follow = 1; | |
750f7b66 LT |
286 | break; |
287 | } | |
288 | } | |
289 | ||
290 | /* | |
3ea3c215 | 291 | * Then, discard all the non-relevant file pairs... |
9f38e1ef LT |
292 | */ |
293 | for (i = 0; i < q->nr; i++) { | |
294 | struct diff_filepair *p = q->queue[i]; | |
295 | diff_free_filepair(p); | |
296 | } | |
297 | ||
298 | /* | |
299 | * .. and re-instate the one we want (which might be either the | |
300 | * original one, or the rename/copy we found) | |
750f7b66 | 301 | */ |
9f38e1ef LT |
302 | q->queue[0] = choice; |
303 | q->nr = 1; | |
750f7b66 LT |
304 | } |
305 | ||
ac1b3d12 LT |
306 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) |
307 | { | |
308 | void *tree1, *tree2; | |
309 | struct tree_desc t1, t2; | |
6fda5e51 | 310 | unsigned long size1, size2; |
ac1b3d12 LT |
311 | int retval; |
312 | ||
79130328 KS |
313 | tree1 = fill_tree_descriptor(&t1, old); |
314 | tree2 = fill_tree_descriptor(&t2, new); | |
315 | size1 = t1.size; | |
316 | size2 = t2.size; | |
ac1b3d12 | 317 | retval = diff_tree(&t1, &t2, base, opt); |
39f75d26 | 318 | if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) { |
750f7b66 LT |
319 | init_tree_desc(&t1, tree1, size1); |
320 | init_tree_desc(&t2, tree2, size2); | |
321 | try_to_follow_renames(&t1, &t2, base, opt); | |
322 | } | |
ac1b3d12 LT |
323 | free(tree1); |
324 | free(tree2); | |
325 | return retval; | |
326 | } | |
327 | ||
2b60356d RS |
328 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
329 | { | |
0b707c33 | 330 | return diff_tree_sha1(NULL, new, base, opt); |
2b60356d | 331 | } |