]>
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 | |
48932677 NTND |
9 | static void show_entry(struct diff_options *opt, const char *prefix, |
10 | struct tree_desc *desc, struct strbuf *base); | |
ac1b3d12 | 11 | |
48932677 NTND |
12 | static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, |
13 | struct strbuf *base, struct diff_options *opt) | |
ac1b3d12 LT |
14 | { |
15 | unsigned mode1, mode2; | |
16 | const char *path1, *path2; | |
17 | const unsigned char *sha1, *sha2; | |
18 | int cmp, pathlen1, pathlen2; | |
48932677 | 19 | int old_baselen = base->len; |
ac1b3d12 | 20 | |
50f9a858 LT |
21 | sha1 = tree_entry_extract(t1, &path1, &mode1); |
22 | sha2 = tree_entry_extract(t2, &path2, &mode2); | |
ac1b3d12 | 23 | |
0de16337 NTND |
24 | pathlen1 = tree_entry_len(&t1->entry); |
25 | pathlen2 = tree_entry_len(&t2->entry); | |
ac1b3d12 LT |
26 | cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); |
27 | if (cmp < 0) { | |
48932677 | 28 | show_entry(opt, "-", t1, base); |
ac1b3d12 LT |
29 | return -1; |
30 | } | |
31 | if (cmp > 0) { | |
48932677 | 32 | show_entry(opt, "+", t2, base); |
ac1b3d12 LT |
33 | return 1; |
34 | } | |
8f67f8ae | 35 | if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2) |
ac1b3d12 LT |
36 | return 0; |
37 | ||
38 | /* | |
39 | * If the filemode has changed to/from a directory from/to a regular | |
40 | * file, we need to consider it a remove and an add. | |
41 | */ | |
42 | if (S_ISDIR(mode1) != S_ISDIR(mode2)) { | |
48932677 NTND |
43 | show_entry(opt, "-", t1, base); |
44 | show_entry(opt, "+", t2, base); | |
ac1b3d12 LT |
45 | return 0; |
46 | } | |
47 | ||
48932677 | 48 | strbuf_add(base, path1, pathlen1); |
8f67f8ae | 49 | if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) { |
fd55a19e | 50 | if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) { |
ac1b3d12 | 51 | opt->change(opt, mode1, mode2, |
e5450100 | 52 | sha1, sha2, 1, 1, base->buf, 0, 0); |
fd55a19e | 53 | } |
48932677 | 54 | strbuf_addch(base, '/'); |
c0aa335c | 55 | diff_tree_sha1(sha1, sha2, base->buf, opt); |
48932677 | 56 | } else { |
e5450100 | 57 | opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0); |
ac1b3d12 | 58 | } |
48932677 | 59 | strbuf_setlen(base, old_baselen); |
ac1b3d12 LT |
60 | return 0; |
61 | } | |
62 | ||
ac1b3d12 | 63 | /* A whole sub-tree went away or appeared */ |
48932677 NTND |
64 | static void show_tree(struct diff_options *opt, const char *prefix, |
65 | struct tree_desc *desc, struct strbuf *base) | |
ac1b3d12 | 66 | { |
d688cf07 | 67 | enum interesting match = entry_not_interesting; |
97d0b74a | 68 | for (; desc->size; update_tree_entry(desc)) { |
d688cf07 | 69 | if (match != all_entries_interesting) { |
97d0b74a NTND |
70 | match = tree_entry_interesting(&desc->entry, base, 0, |
71 | &opt->pathspec); | |
d688cf07 | 72 | if (match == all_entries_not_interesting) |
97d0b74a | 73 | break; |
d688cf07 | 74 | if (match == entry_not_interesting) |
97d0b74a | 75 | continue; |
1d848f64 | 76 | } |
97d0b74a | 77 | show_entry(opt, prefix, desc, base); |
ac1b3d12 LT |
78 | } |
79 | } | |
80 | ||
81 | /* A file entry went away or appeared */ | |
48932677 NTND |
82 | static void show_entry(struct diff_options *opt, const char *prefix, |
83 | struct tree_desc *desc, struct strbuf *base) | |
ac1b3d12 LT |
84 | { |
85 | unsigned mode; | |
86 | const char *path; | |
50f9a858 | 87 | const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); |
0de16337 | 88 | int pathlen = tree_entry_len(&desc->entry); |
48932677 | 89 | int old_baselen = base->len; |
ac1b3d12 | 90 | |
48932677 | 91 | strbuf_add(base, path, pathlen); |
8f67f8ae | 92 | if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) { |
21666f1a | 93 | enum object_type type; |
ac1b3d12 LT |
94 | struct tree_desc inner; |
95 | void *tree; | |
6fda5e51 | 96 | unsigned long size; |
ac1b3d12 | 97 | |
6fda5e51 | 98 | tree = read_sha1_file(sha1, &type, &size); |
21666f1a | 99 | if (!tree || type != OBJ_TREE) |
ac1b3d12 LT |
100 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
101 | ||
48932677 | 102 | if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) |
e5450100 | 103 | opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0); |
df533f34 | 104 | |
48932677 | 105 | strbuf_addch(base, '/'); |
ac1b3d12 | 106 | |
48932677 NTND |
107 | init_tree_desc(&inner, tree, size); |
108 | show_tree(opt, prefix, &inner, base); | |
ac1b3d12 | 109 | free(tree); |
48932677 | 110 | } else |
e5450100 | 111 | opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0); |
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, |
d688cf07 NTND |
117 | struct diff_options *opt, |
118 | enum interesting *match) | |
5d865017 LT |
119 | { |
120 | while (t->size) { | |
97d0b74a NTND |
121 | *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec); |
122 | if (*match) { | |
d688cf07 | 123 | if (*match == all_entries_not_interesting) |
97d0b74a NTND |
124 | t->size = 0; |
125 | break; | |
5d865017 | 126 | } |
97d0b74a | 127 | update_tree_entry(t); |
5d865017 LT |
128 | } |
129 | } | |
130 | ||
48932677 NTND |
131 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, |
132 | const char *base_str, struct diff_options *opt) | |
ac1b3d12 | 133 | { |
48932677 NTND |
134 | struct strbuf base; |
135 | int baselen = strlen(base_str); | |
d688cf07 NTND |
136 | enum interesting t1_match = entry_not_interesting; |
137 | enum interesting t2_match = entry_not_interesting; | |
304de2d2 | 138 | |
bc96cc87 NTND |
139 | /* Enable recursion indefinitely */ |
140 | opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE); | |
bc96cc87 | 141 | |
48932677 NTND |
142 | strbuf_init(&base, PATH_MAX); |
143 | strbuf_add(&base, base_str, baselen); | |
144 | ||
5d865017 | 145 | for (;;) { |
28b9264d | 146 | if (diff_can_quit_early(opt)) |
822cac01 | 147 | break; |
66f13625 | 148 | if (opt->pathspec.nr) { |
97d0b74a NTND |
149 | skip_uninteresting(t1, &base, opt, &t1_match); |
150 | skip_uninteresting(t2, &base, opt, &t2_match); | |
ac1b3d12 LT |
151 | } |
152 | if (!t1->size) { | |
5d865017 LT |
153 | if (!t2->size) |
154 | break; | |
48932677 | 155 | show_entry(opt, "+", t2, &base); |
ac1b3d12 LT |
156 | update_tree_entry(t2); |
157 | continue; | |
158 | } | |
159 | if (!t2->size) { | |
48932677 | 160 | show_entry(opt, "-", t1, &base); |
ac1b3d12 LT |
161 | update_tree_entry(t1); |
162 | continue; | |
163 | } | |
48932677 | 164 | switch (compare_tree_entry(t1, t2, &base, opt)) { |
ac1b3d12 LT |
165 | case -1: |
166 | update_tree_entry(t1); | |
167 | continue; | |
168 | case 0: | |
169 | update_tree_entry(t1); | |
170 | /* Fallthrough */ | |
171 | case 1: | |
172 | update_tree_entry(t2); | |
173 | continue; | |
174 | } | |
7e44c935 | 175 | die("git diff-tree: internal error"); |
ac1b3d12 | 176 | } |
48932677 NTND |
177 | |
178 | strbuf_release(&base); | |
ac1b3d12 LT |
179 | return 0; |
180 | } | |
181 | ||
750f7b66 LT |
182 | /* |
183 | * Does it look like the resulting diff might be due to a rename? | |
184 | * - single entry | |
185 | * - not a valid previous file | |
186 | */ | |
187 | static inline int diff_might_be_rename(void) | |
188 | { | |
189 | return diff_queued_diff.nr == 1 && | |
190 | !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one); | |
191 | } | |
192 | ||
193 | static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) | |
194 | { | |
195 | struct diff_options diff_opts; | |
9f38e1ef LT |
196 | struct diff_queue_struct *q = &diff_queued_diff; |
197 | struct diff_filepair *choice; | |
750f7b66 LT |
198 | int i; |
199 | ||
8f4f8f45 NTND |
200 | /* |
201 | * follow-rename code is very specific, we need exactly one | |
202 | * path. Magic that matches more than one path is not | |
203 | * supported. | |
204 | */ | |
5c6933d2 | 205 | GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL); |
8f4f8f45 NTND |
206 | #if 0 |
207 | /* | |
208 | * We should reject wildcards as well. Unfortunately we | |
209 | * haven't got a reliable way to detect that 'foo\*bar' in | |
210 | * fact has no wildcards. nowildcard_len is merely a hint for | |
211 | * optimization. Let it slip for now until wildmatch is taught | |
212 | * about dry-run mode and returns wildcard info. | |
213 | */ | |
214 | if (opt->pathspec.has_wildcard) | |
215 | die("BUG:%s:%d: wildcards are not supported", | |
216 | __FILE__, __LINE__); | |
217 | #endif | |
218 | ||
9f38e1ef LT |
219 | /* Remove the file creation entry from the diff queue, and remember it */ |
220 | choice = q->queue[0]; | |
221 | q->nr = 0; | |
222 | ||
750f7b66 | 223 | diff_setup(&diff_opts); |
8f67f8ae | 224 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
0cdca133 | 225 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
750f7b66 | 226 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
61588ccf | 227 | diff_opts.single_follow = opt->pathspec.items[0].match; |
6dd4b66f | 228 | diff_opts.break_opt = opt->break_opt; |
dd98d88b | 229 | diff_opts.rename_score = opt->rename_score; |
28452655 | 230 | diff_setup_done(&diff_opts); |
750f7b66 LT |
231 | diff_tree(t1, t2, base, &diff_opts); |
232 | diffcore_std(&diff_opts); | |
bd1928df | 233 | free_pathspec(&diff_opts.pathspec); |
750f7b66 | 234 | |
9f38e1ef | 235 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
44c48a90 | 236 | opt->found_follow = 0; |
9f38e1ef LT |
237 | for (i = 0; i < q->nr; i++) { |
238 | struct diff_filepair *p = q->queue[i]; | |
750f7b66 LT |
239 | |
240 | /* | |
241 | * Found a source? Not only do we use that for the new | |
9f38e1ef | 242 | * diff_queued_diff, we will also use that as the path in |
750f7b66 LT |
243 | * the future! |
244 | */ | |
66f13625 | 245 | if ((p->status == 'R' || p->status == 'C') && |
61588ccf | 246 | !strcmp(p->two->path, opt->pathspec.items[0].match)) { |
9a087274 NTND |
247 | const char *path[2]; |
248 | ||
9f38e1ef LT |
249 | /* Switch the file-pairs around */ |
250 | q->queue[i] = choice; | |
251 | choice = p; | |
252 | ||
253 | /* Update the path we use from now on.. */ | |
9a087274 NTND |
254 | path[0] = p->one->path; |
255 | path[1] = NULL; | |
bd1928df | 256 | free_pathspec(&opt->pathspec); |
4a2d5ae2 NTND |
257 | parse_pathspec(&opt->pathspec, |
258 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
259 | PATHSPEC_LITERAL_PATH, "", path); | |
44c48a90 JH |
260 | |
261 | /* | |
262 | * The caller expects us to return a set of vanilla | |
263 | * filepairs to let a later call to diffcore_std() | |
264 | * it makes to sort the renames out (among other | |
265 | * things), but we already have found renames | |
266 | * ourselves; signal diffcore_std() not to muck with | |
267 | * rename information. | |
268 | */ | |
269 | opt->found_follow = 1; | |
750f7b66 LT |
270 | break; |
271 | } | |
272 | } | |
273 | ||
274 | /* | |
3ea3c215 | 275 | * Then, discard all the non-relevant file pairs... |
9f38e1ef LT |
276 | */ |
277 | for (i = 0; i < q->nr; i++) { | |
278 | struct diff_filepair *p = q->queue[i]; | |
279 | diff_free_filepair(p); | |
280 | } | |
281 | ||
282 | /* | |
283 | * .. and re-instate the one we want (which might be either the | |
284 | * original one, or the rename/copy we found) | |
750f7b66 | 285 | */ |
9f38e1ef LT |
286 | q->queue[0] = choice; |
287 | q->nr = 1; | |
750f7b66 LT |
288 | } |
289 | ||
ac1b3d12 LT |
290 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) |
291 | { | |
292 | void *tree1, *tree2; | |
293 | struct tree_desc t1, t2; | |
6fda5e51 | 294 | unsigned long size1, size2; |
ac1b3d12 LT |
295 | int retval; |
296 | ||
6fda5e51 | 297 | tree1 = read_object_with_reference(old, tree_type, &size1, NULL); |
ac1b3d12 LT |
298 | if (!tree1) |
299 | die("unable to read source tree (%s)", sha1_to_hex(old)); | |
6fda5e51 | 300 | tree2 = read_object_with_reference(new, tree_type, &size2, NULL); |
ac1b3d12 LT |
301 | if (!tree2) |
302 | die("unable to read destination tree (%s)", sha1_to_hex(new)); | |
6fda5e51 LT |
303 | init_tree_desc(&t1, tree1, size1); |
304 | init_tree_desc(&t2, tree2, size2); | |
ac1b3d12 | 305 | retval = diff_tree(&t1, &t2, base, opt); |
39f75d26 | 306 | if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) { |
750f7b66 LT |
307 | init_tree_desc(&t1, tree1, size1); |
308 | init_tree_desc(&t2, tree2, size2); | |
309 | try_to_follow_renames(&t1, &t2, base, opt); | |
310 | } | |
ac1b3d12 LT |
311 | free(tree1); |
312 | free(tree2); | |
313 | return retval; | |
314 | } | |
315 | ||
2b60356d RS |
316 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
317 | { | |
318 | int retval; | |
319 | void *tree; | |
6fda5e51 | 320 | unsigned long size; |
2b60356d RS |
321 | struct tree_desc empty, real; |
322 | ||
6fda5e51 | 323 | tree = read_object_with_reference(new, tree_type, &size, NULL); |
2b60356d RS |
324 | if (!tree) |
325 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
6fda5e51 | 326 | init_tree_desc(&real, tree, size); |
2b60356d | 327 | |
6fda5e51 | 328 | init_tree_desc(&empty, "", 0); |
2b60356d RS |
329 | retval = diff_tree(&empty, &real, base, opt); |
330 | free(tree); | |
331 | return retval; | |
332 | } |