]>
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 | |
304de2d2 LT |
24 | pathlen1 = tree_entry_len(path1, sha1); |
25 | pathlen2 = tree_entry_len(path2, sha2); | |
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, |
48932677 | 52 | sha1, sha2, base->buf, 0, 0); |
fd55a19e | 53 | } |
48932677 | 54 | strbuf_addch(base, '/'); |
c0aa335c | 55 | diff_tree_sha1(sha1, sha2, base->buf, opt); |
48932677 NTND |
56 | } else { |
57 | opt->change(opt, mode1, mode2, sha1, sha2, 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 | { |
1d848f64 | 67 | int all_interesting = 0; |
ac1b3d12 | 68 | while (desc->size) { |
1d848f64 JH |
69 | int show; |
70 | ||
71 | if (all_interesting) | |
72 | show = 1; | |
73 | else { | |
1376e507 | 74 | show = tree_entry_interesting(&desc->entry, base, 0, |
48932677 | 75 | &opt->pathspec); |
1d848f64 JH |
76 | if (show == 2) |
77 | all_interesting = 1; | |
78 | } | |
5d865017 LT |
79 | if (show < 0) |
80 | break; | |
81 | if (show) | |
48932677 | 82 | show_entry(opt, prefix, desc, base); |
ac1b3d12 LT |
83 | update_tree_entry(desc); |
84 | } | |
85 | } | |
86 | ||
87 | /* A file entry went away or appeared */ | |
48932677 NTND |
88 | static void show_entry(struct diff_options *opt, const char *prefix, |
89 | struct tree_desc *desc, struct strbuf *base) | |
ac1b3d12 LT |
90 | { |
91 | unsigned mode; | |
92 | const char *path; | |
50f9a858 | 93 | const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); |
fd55a19e | 94 | int pathlen = tree_entry_len(path, sha1); |
48932677 | 95 | int old_baselen = base->len; |
ac1b3d12 | 96 | |
48932677 | 97 | strbuf_add(base, path, pathlen); |
8f67f8ae | 98 | if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) { |
21666f1a | 99 | enum object_type type; |
ac1b3d12 LT |
100 | struct tree_desc inner; |
101 | void *tree; | |
6fda5e51 | 102 | unsigned long size; |
ac1b3d12 | 103 | |
6fda5e51 | 104 | tree = read_sha1_file(sha1, &type, &size); |
21666f1a | 105 | if (!tree || type != OBJ_TREE) |
ac1b3d12 LT |
106 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
107 | ||
48932677 NTND |
108 | if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) |
109 | opt->add_remove(opt, *prefix, mode, sha1, base->buf, 0); | |
df533f34 | 110 | |
48932677 | 111 | strbuf_addch(base, '/'); |
ac1b3d12 | 112 | |
48932677 NTND |
113 | init_tree_desc(&inner, tree, size); |
114 | show_tree(opt, prefix, &inner, base); | |
ac1b3d12 | 115 | free(tree); |
48932677 NTND |
116 | } else |
117 | opt->add_remove(opt, prefix[0], mode, sha1, base->buf, 0); | |
118 | ||
119 | strbuf_setlen(base, old_baselen); | |
ac1b3d12 LT |
120 | } |
121 | ||
48932677 NTND |
122 | static void skip_uninteresting(struct tree_desc *t, struct strbuf *base, |
123 | struct diff_options *opt, int *all_interesting) | |
5d865017 LT |
124 | { |
125 | while (t->size) { | |
1376e507 | 126 | int show = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec); |
7e1ec0d4 EN |
127 | if (show == 2) |
128 | *all_interesting = 1; | |
5d865017 LT |
129 | if (!show) { |
130 | update_tree_entry(t); | |
131 | continue; | |
132 | } | |
133 | /* Skip it all? */ | |
134 | if (show < 0) | |
135 | t->size = 0; | |
136 | return; | |
137 | } | |
138 | } | |
139 | ||
48932677 NTND |
140 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, |
141 | const char *base_str, struct diff_options *opt) | |
ac1b3d12 | 142 | { |
48932677 NTND |
143 | struct strbuf base; |
144 | int baselen = strlen(base_str); | |
7e1ec0d4 EN |
145 | int all_t1_interesting = 0; |
146 | int all_t2_interesting = 0; | |
304de2d2 | 147 | |
bc96cc87 NTND |
148 | /* Enable recursion indefinitely */ |
149 | opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE); | |
150 | opt->pathspec.max_depth = -1; | |
151 | ||
48932677 NTND |
152 | strbuf_init(&base, PATH_MAX); |
153 | strbuf_add(&base, base_str, baselen); | |
154 | ||
5d865017 | 155 | for (;;) { |
90b19941 | 156 | if (DIFF_OPT_TST(opt, QUICK) && |
f245194f | 157 | DIFF_OPT_TST(opt, HAS_CHANGES)) |
822cac01 | 158 | break; |
66f13625 | 159 | if (opt->pathspec.nr) { |
7e1ec0d4 | 160 | if (!all_t1_interesting) |
48932677 | 161 | skip_uninteresting(t1, &base, opt, &all_t1_interesting); |
7e1ec0d4 | 162 | if (!all_t2_interesting) |
48932677 | 163 | skip_uninteresting(t2, &base, opt, &all_t2_interesting); |
ac1b3d12 LT |
164 | } |
165 | if (!t1->size) { | |
5d865017 LT |
166 | if (!t2->size) |
167 | break; | |
48932677 | 168 | show_entry(opt, "+", t2, &base); |
ac1b3d12 LT |
169 | update_tree_entry(t2); |
170 | continue; | |
171 | } | |
172 | if (!t2->size) { | |
48932677 | 173 | show_entry(opt, "-", t1, &base); |
ac1b3d12 LT |
174 | update_tree_entry(t1); |
175 | continue; | |
176 | } | |
48932677 | 177 | switch (compare_tree_entry(t1, t2, &base, opt)) { |
ac1b3d12 LT |
178 | case -1: |
179 | update_tree_entry(t1); | |
180 | continue; | |
181 | case 0: | |
182 | update_tree_entry(t1); | |
183 | /* Fallthrough */ | |
184 | case 1: | |
185 | update_tree_entry(t2); | |
186 | continue; | |
187 | } | |
7e44c935 | 188 | die("git diff-tree: internal error"); |
ac1b3d12 | 189 | } |
48932677 NTND |
190 | |
191 | strbuf_release(&base); | |
ac1b3d12 LT |
192 | return 0; |
193 | } | |
194 | ||
750f7b66 LT |
195 | /* |
196 | * Does it look like the resulting diff might be due to a rename? | |
197 | * - single entry | |
198 | * - not a valid previous file | |
199 | */ | |
200 | static inline int diff_might_be_rename(void) | |
201 | { | |
202 | return diff_queued_diff.nr == 1 && | |
203 | !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one); | |
204 | } | |
205 | ||
206 | static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) | |
207 | { | |
208 | struct diff_options diff_opts; | |
9f38e1ef LT |
209 | struct diff_queue_struct *q = &diff_queued_diff; |
210 | struct diff_filepair *choice; | |
211 | const char *paths[1]; | |
750f7b66 LT |
212 | int i; |
213 | ||
9f38e1ef LT |
214 | /* Remove the file creation entry from the diff queue, and remember it */ |
215 | choice = q->queue[0]; | |
216 | q->nr = 0; | |
217 | ||
750f7b66 | 218 | diff_setup(&diff_opts); |
8f67f8ae | 219 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
0cdca133 | 220 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
750f7b66 | 221 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
66f13625 | 222 | diff_opts.single_follow = opt->pathspec.raw[0]; |
6dd4b66f | 223 | diff_opts.break_opt = opt->break_opt; |
750f7b66 LT |
224 | paths[0] = NULL; |
225 | diff_tree_setup_paths(paths, &diff_opts); | |
226 | if (diff_setup_done(&diff_opts) < 0) | |
227 | die("unable to set up diff options to follow renames"); | |
228 | diff_tree(t1, t2, base, &diff_opts); | |
229 | diffcore_std(&diff_opts); | |
03b69c76 | 230 | diff_tree_release_paths(&diff_opts); |
750f7b66 | 231 | |
9f38e1ef | 232 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
44c48a90 | 233 | opt->found_follow = 0; |
9f38e1ef LT |
234 | for (i = 0; i < q->nr; i++) { |
235 | struct diff_filepair *p = q->queue[i]; | |
750f7b66 LT |
236 | |
237 | /* | |
238 | * Found a source? Not only do we use that for the new | |
9f38e1ef | 239 | * diff_queued_diff, we will also use that as the path in |
750f7b66 LT |
240 | * the future! |
241 | */ | |
66f13625 NTND |
242 | if ((p->status == 'R' || p->status == 'C') && |
243 | !strcmp(p->two->path, opt->pathspec.raw[0])) { | |
9f38e1ef LT |
244 | /* Switch the file-pairs around */ |
245 | q->queue[i] = choice; | |
246 | choice = p; | |
247 | ||
248 | /* Update the path we use from now on.. */ | |
03b69c76 | 249 | diff_tree_release_paths(opt); |
66f13625 NTND |
250 | opt->pathspec.raw[0] = xstrdup(p->one->path); |
251 | diff_tree_setup_paths(opt->pathspec.raw, opt); | |
44c48a90 JH |
252 | |
253 | /* | |
254 | * The caller expects us to return a set of vanilla | |
255 | * filepairs to let a later call to diffcore_std() | |
256 | * it makes to sort the renames out (among other | |
257 | * things), but we already have found renames | |
258 | * ourselves; signal diffcore_std() not to muck with | |
259 | * rename information. | |
260 | */ | |
261 | opt->found_follow = 1; | |
750f7b66 LT |
262 | break; |
263 | } | |
264 | } | |
265 | ||
266 | /* | |
3ea3c215 | 267 | * Then, discard all the non-relevant file pairs... |
9f38e1ef LT |
268 | */ |
269 | for (i = 0; i < q->nr; i++) { | |
270 | struct diff_filepair *p = q->queue[i]; | |
271 | diff_free_filepair(p); | |
272 | } | |
273 | ||
274 | /* | |
275 | * .. and re-instate the one we want (which might be either the | |
276 | * original one, or the rename/copy we found) | |
750f7b66 | 277 | */ |
9f38e1ef LT |
278 | q->queue[0] = choice; |
279 | q->nr = 1; | |
750f7b66 LT |
280 | } |
281 | ||
ac1b3d12 LT |
282 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) |
283 | { | |
284 | void *tree1, *tree2; | |
285 | struct tree_desc t1, t2; | |
6fda5e51 | 286 | unsigned long size1, size2; |
ac1b3d12 LT |
287 | int retval; |
288 | ||
6fda5e51 | 289 | tree1 = read_object_with_reference(old, tree_type, &size1, NULL); |
ac1b3d12 LT |
290 | if (!tree1) |
291 | die("unable to read source tree (%s)", sha1_to_hex(old)); | |
6fda5e51 | 292 | tree2 = read_object_with_reference(new, tree_type, &size2, NULL); |
ac1b3d12 LT |
293 | if (!tree2) |
294 | die("unable to read destination tree (%s)", sha1_to_hex(new)); | |
6fda5e51 LT |
295 | init_tree_desc(&t1, tree1, size1); |
296 | init_tree_desc(&t2, tree2, size2); | |
ac1b3d12 | 297 | retval = diff_tree(&t1, &t2, base, opt); |
39f75d26 | 298 | if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) { |
750f7b66 LT |
299 | init_tree_desc(&t1, tree1, size1); |
300 | init_tree_desc(&t2, tree2, size2); | |
301 | try_to_follow_renames(&t1, &t2, base, opt); | |
302 | } | |
ac1b3d12 LT |
303 | free(tree1); |
304 | free(tree2); | |
305 | return retval; | |
306 | } | |
307 | ||
2b60356d RS |
308 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
309 | { | |
310 | int retval; | |
311 | void *tree; | |
6fda5e51 | 312 | unsigned long size; |
2b60356d RS |
313 | struct tree_desc empty, real; |
314 | ||
6fda5e51 | 315 | tree = read_object_with_reference(new, tree_type, &size, NULL); |
2b60356d RS |
316 | if (!tree) |
317 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
6fda5e51 | 318 | init_tree_desc(&real, tree, size); |
2b60356d | 319 | |
6fda5e51 | 320 | init_tree_desc(&empty, "", 0); |
2b60356d RS |
321 | retval = diff_tree(&empty, &real, base, opt); |
322 | free(tree); | |
323 | return retval; | |
324 | } | |
325 | ||
a8baa7b9 | 326 | void diff_tree_release_paths(struct diff_options *opt) |
ac1b3d12 | 327 | { |
66f13625 | 328 | free_pathspec(&opt->pathspec); |
a8baa7b9 JH |
329 | } |
330 | ||
331 | void diff_tree_setup_paths(const char **p, struct diff_options *opt) | |
332 | { | |
66f13625 | 333 | init_pathspec(&opt->pathspec, p); |
ac1b3d12 | 334 | } |