]>
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(). | |
15 | */ | |
16 | static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) | |
ac1b3d12 | 17 | { |
1a27a154 KS |
18 | struct name_entry *e1, *e2; |
19 | int cmp; | |
ac1b3d12 | 20 | |
1a27a154 KS |
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); | |
903bba68 | 25 | return cmp; |
d00e980c KS |
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; | |
fd55a19e | 53 | } |
d00e980c KS |
54 | |
55 | opt->add_remove(opt, addremove, mode, sha1, 1, path->buf, 0); | |
ac1b3d12 | 56 | } |
ac1b3d12 LT |
57 | } |
58 | ||
d00e980c KS |
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) | |
ac1b3d12 LT |
70 | { |
71 | unsigned mode; | |
72 | const char *path; | |
d00e980c | 73 | int pathlen; |
48932677 | 74 | int old_baselen = base->len; |
d00e980c KS |
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 | } | |
ac1b3d12 | 101 | |
48932677 | 102 | strbuf_add(base, path, pathlen); |
df533f34 | 103 | |
d00e980c KS |
104 | if (emitthis) |
105 | emit_diff(opt, base, t1, t2); | |
106 | ||
107 | if (recurse) { | |
48932677 | 108 | strbuf_addch(base, '/'); |
d00e980c KS |
109 | diff_tree_sha1(t1 ? t1->entry.sha1 : NULL, |
110 | t2 ? t2->entry.sha1 : NULL, base->buf, opt); | |
111 | } | |
48932677 NTND |
112 | |
113 | strbuf_setlen(base, old_baselen); | |
ac1b3d12 LT |
114 | } |
115 | ||
48932677 | 116 | static void skip_uninteresting(struct tree_desc *t, struct strbuf *base, |
e9066121 | 117 | struct diff_options *opt) |
5d865017 | 118 | { |
e9066121 KS |
119 | enum interesting match; |
120 | ||
5d865017 | 121 | while (t->size) { |
e9066121 KS |
122 | match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec); |
123 | if (match) { | |
124 | if (match == all_entries_not_interesting) | |
97d0b74a NTND |
125 | t->size = 0; |
126 | break; | |
5d865017 | 127 | } |
97d0b74a | 128 | update_tree_entry(t); |
5d865017 LT |
129 | } |
130 | } | |
131 | ||
48932677 NTND |
132 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, |
133 | const char *base_str, struct diff_options *opt) | |
ac1b3d12 | 134 | { |
48932677 NTND |
135 | struct strbuf base; |
136 | int baselen = strlen(base_str); | |
304de2d2 | 137 | |
bc96cc87 NTND |
138 | /* Enable recursion indefinitely */ |
139 | opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE); | |
bc96cc87 | 140 | |
48932677 NTND |
141 | strbuf_init(&base, PATH_MAX); |
142 | strbuf_add(&base, base_str, baselen); | |
143 | ||
5d865017 | 144 | for (;;) { |
903bba68 KS |
145 | int cmp; |
146 | ||
28b9264d | 147 | if (diff_can_quit_early(opt)) |
822cac01 | 148 | break; |
66f13625 | 149 | if (opt->pathspec.nr) { |
e9066121 KS |
150 | skip_uninteresting(t1, &base, opt); |
151 | skip_uninteresting(t2, &base, opt); | |
ac1b3d12 LT |
152 | } |
153 | if (!t1->size) { | |
5d865017 LT |
154 | if (!t2->size) |
155 | break; | |
d00e980c | 156 | show_path(&base, opt, /*t1=*/NULL, t2); |
ac1b3d12 LT |
157 | update_tree_entry(t2); |
158 | continue; | |
159 | } | |
160 | if (!t2->size) { | |
d00e980c | 161 | show_path(&base, opt, t1, /*t2=*/NULL); |
ac1b3d12 LT |
162 | update_tree_entry(t1); |
163 | continue; | |
164 | } | |
5dfb2bbd | 165 | |
9bc06196 | 166 | cmp = tree_entry_pathcmp(t1, t2); |
5dfb2bbd KS |
167 | |
168 | /* t1 = t2 */ | |
169 | if (cmp == 0) { | |
903bba68 KS |
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 | ||
ac1b3d12 | 175 | update_tree_entry(t1); |
5dfb2bbd KS |
176 | update_tree_entry(t2); |
177 | } | |
178 | ||
179 | /* t1 < t2 */ | |
180 | else if (cmp < 0) { | |
903bba68 | 181 | show_path(&base, opt, t1, /*t2=*/NULL); |
ac1b3d12 | 182 | update_tree_entry(t1); |
5dfb2bbd KS |
183 | } |
184 | ||
185 | /* t1 > t2 */ | |
186 | else { | |
903bba68 | 187 | show_path(&base, opt, /*t1=*/NULL, t2); |
ac1b3d12 | 188 | update_tree_entry(t2); |
ac1b3d12 | 189 | } |
ac1b3d12 | 190 | } |
48932677 NTND |
191 | |
192 | strbuf_release(&base); | |
ac1b3d12 LT |
193 | return 0; |
194 | } | |
195 | ||
750f7b66 LT |
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; | |
9f38e1ef LT |
210 | struct diff_queue_struct *q = &diff_queued_diff; |
211 | struct diff_filepair *choice; | |
750f7b66 LT |
212 | int i; |
213 | ||
8f4f8f45 NTND |
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 | */ | |
5c6933d2 | 219 | GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL); |
8f4f8f45 NTND |
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 | ||
9f38e1ef LT |
233 | /* Remove the file creation entry from the diff queue, and remember it */ |
234 | choice = q->queue[0]; | |
235 | q->nr = 0; | |
236 | ||
750f7b66 | 237 | diff_setup(&diff_opts); |
8f67f8ae | 238 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
0cdca133 | 239 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
750f7b66 | 240 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
61588ccf | 241 | diff_opts.single_follow = opt->pathspec.items[0].match; |
6dd4b66f | 242 | diff_opts.break_opt = opt->break_opt; |
dd98d88b | 243 | diff_opts.rename_score = opt->rename_score; |
28452655 | 244 | diff_setup_done(&diff_opts); |
750f7b66 LT |
245 | diff_tree(t1, t2, base, &diff_opts); |
246 | diffcore_std(&diff_opts); | |
bd1928df | 247 | free_pathspec(&diff_opts.pathspec); |
750f7b66 | 248 | |
9f38e1ef | 249 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
44c48a90 | 250 | opt->found_follow = 0; |
9f38e1ef LT |
251 | for (i = 0; i < q->nr; i++) { |
252 | struct diff_filepair *p = q->queue[i]; | |
750f7b66 LT |
253 | |
254 | /* | |
255 | * Found a source? Not only do we use that for the new | |
9f38e1ef | 256 | * diff_queued_diff, we will also use that as the path in |
750f7b66 LT |
257 | * the future! |
258 | */ | |
66f13625 | 259 | if ((p->status == 'R' || p->status == 'C') && |
61588ccf | 260 | !strcmp(p->two->path, opt->pathspec.items[0].match)) { |
9a087274 NTND |
261 | const char *path[2]; |
262 | ||
9f38e1ef LT |
263 | /* Switch the file-pairs around */ |
264 | q->queue[i] = choice; | |
265 | choice = p; | |
266 | ||
267 | /* Update the path we use from now on.. */ | |
9a087274 NTND |
268 | path[0] = p->one->path; |
269 | path[1] = NULL; | |
bd1928df | 270 | free_pathspec(&opt->pathspec); |
4a2d5ae2 NTND |
271 | parse_pathspec(&opt->pathspec, |
272 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
273 | PATHSPEC_LITERAL_PATH, "", path); | |
44c48a90 JH |
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; | |
750f7b66 LT |
284 | break; |
285 | } | |
286 | } | |
287 | ||
288 | /* | |
3ea3c215 | 289 | * Then, discard all the non-relevant file pairs... |
9f38e1ef LT |
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) | |
750f7b66 | 299 | */ |
9f38e1ef LT |
300 | q->queue[0] = choice; |
301 | q->nr = 1; | |
750f7b66 LT |
302 | } |
303 | ||
ac1b3d12 LT |
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; | |
6fda5e51 | 308 | unsigned long size1, size2; |
ac1b3d12 LT |
309 | int retval; |
310 | ||
79130328 KS |
311 | tree1 = fill_tree_descriptor(&t1, old); |
312 | tree2 = fill_tree_descriptor(&t2, new); | |
313 | size1 = t1.size; | |
314 | size2 = t2.size; | |
ac1b3d12 | 315 | retval = diff_tree(&t1, &t2, base, opt); |
39f75d26 | 316 | if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) { |
750f7b66 LT |
317 | init_tree_desc(&t1, tree1, size1); |
318 | init_tree_desc(&t2, tree2, size2); | |
319 | try_to_follow_renames(&t1, &t2, base, opt); | |
320 | } | |
ac1b3d12 LT |
321 | free(tree1); |
322 | free(tree2); | |
323 | return retval; | |
324 | } | |
325 | ||
2b60356d RS |
326 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
327 | { | |
0b707c33 | 328 | return diff_tree_sha1(NULL, new, base, opt); |
2b60356d | 329 | } |