]>
Commit | Line | Data |
---|---|---|
ac1b3d12 LT |
1 | /* |
2 | * Helper functions for tree diff generation | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "diff.h" | |
8e440259 | 6 | #include "tree.h" |
ac1b3d12 | 7 | |
304de2d2 | 8 | static char *malloc_base(const char *base, int baselen, const char *path, int pathlen) |
ac1b3d12 | 9 | { |
ac1b3d12 LT |
10 | char *newbase = xmalloc(baselen + pathlen + 2); |
11 | memcpy(newbase, base, baselen); | |
12 | memcpy(newbase + baselen, path, pathlen); | |
13 | memcpy(newbase + baselen + pathlen, "/", 2); | |
14 | return newbase; | |
15 | } | |
16 | ||
cf995ede | 17 | static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, |
304de2d2 | 18 | const char *base, int baselen); |
ac1b3d12 | 19 | |
304de2d2 | 20 | static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt) |
ac1b3d12 LT |
21 | { |
22 | unsigned mode1, mode2; | |
23 | const char *path1, *path2; | |
24 | const unsigned char *sha1, *sha2; | |
25 | int cmp, pathlen1, pathlen2; | |
26 | ||
50f9a858 LT |
27 | sha1 = tree_entry_extract(t1, &path1, &mode1); |
28 | sha2 = tree_entry_extract(t2, &path2, &mode2); | |
ac1b3d12 | 29 | |
304de2d2 LT |
30 | pathlen1 = tree_entry_len(path1, sha1); |
31 | pathlen2 = tree_entry_len(path2, sha2); | |
ac1b3d12 LT |
32 | cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); |
33 | if (cmp < 0) { | |
304de2d2 | 34 | show_entry(opt, "-", t1, base, baselen); |
ac1b3d12 LT |
35 | return -1; |
36 | } | |
37 | if (cmp > 0) { | |
304de2d2 | 38 | show_entry(opt, "+", t2, base, baselen); |
ac1b3d12 LT |
39 | return 1; |
40 | } | |
a89fccd2 | 41 | if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2) |
ac1b3d12 LT |
42 | return 0; |
43 | ||
44 | /* | |
45 | * If the filemode has changed to/from a directory from/to a regular | |
46 | * file, we need to consider it a remove and an add. | |
47 | */ | |
48 | if (S_ISDIR(mode1) != S_ISDIR(mode2)) { | |
304de2d2 LT |
49 | show_entry(opt, "-", t1, base, baselen); |
50 | show_entry(opt, "+", t2, base, baselen); | |
ac1b3d12 LT |
51 | return 0; |
52 | } | |
53 | ||
54 | if (opt->recursive && S_ISDIR(mode1)) { | |
55 | int retval; | |
304de2d2 | 56 | char *newbase = malloc_base(base, baselen, path1, pathlen1); |
ac1b3d12 LT |
57 | if (opt->tree_in_recursive) |
58 | opt->change(opt, mode1, mode2, | |
59 | sha1, sha2, base, path1); | |
60 | retval = diff_tree_sha1(sha1, sha2, newbase, opt); | |
61 | free(newbase); | |
62 | return retval; | |
63 | } | |
64 | ||
65 | opt->change(opt, mode1, mode2, sha1, sha2, base, path1); | |
66 | return 0; | |
67 | } | |
68 | ||
5d865017 LT |
69 | /* |
70 | * Is a tree entry interesting given the pathspec we have? | |
71 | * | |
72 | * Return: | |
1d848f64 JH |
73 | * - 2 for "yes, and all subsequent entries will be" |
74 | * - 1 for yes | |
5d865017 LT |
75 | * - zero for no |
76 | * - negative for "no, and no subsequent entries will be either" | |
77 | */ | |
78 | static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt) | |
ac1b3d12 LT |
79 | { |
80 | const char *path; | |
304de2d2 | 81 | const unsigned char *sha1; |
ac1b3d12 LT |
82 | unsigned mode; |
83 | int i; | |
304de2d2 | 84 | int pathlen; |
7d2f667b | 85 | int never_interesting = -1; |
ac1b3d12 | 86 | |
a8baa7b9 | 87 | if (!opt->nr_paths) |
ac1b3d12 LT |
88 | return 1; |
89 | ||
304de2d2 | 90 | sha1 = tree_entry_extract(desc, &path, &mode); |
ac1b3d12 | 91 | |
304de2d2 | 92 | pathlen = tree_entry_len(path, sha1); |
ac1b3d12 | 93 | |
7d2f667b | 94 | for (i = 0; i < opt->nr_paths; i++) { |
a8baa7b9 JH |
95 | const char *match = opt->paths[i]; |
96 | int matchlen = opt->pathlens[i]; | |
ccc744ab | 97 | int m = -1; /* signals that we haven't called strncmp() */ |
ac1b3d12 LT |
98 | |
99 | if (baselen >= matchlen) { | |
100 | /* If it doesn't match, move along... */ | |
101 | if (strncmp(base, match, matchlen)) | |
102 | continue; | |
103 | ||
1d848f64 JH |
104 | /* |
105 | * The base is a subdirectory of a path which | |
106 | * was specified, so all of them are interesting. | |
107 | */ | |
108 | return 2; | |
ac1b3d12 LT |
109 | } |
110 | ||
111 | /* Does the base match? */ | |
112 | if (strncmp(base, match, baselen)) | |
113 | continue; | |
114 | ||
115 | match += baselen; | |
116 | matchlen -= baselen; | |
117 | ||
ccc744ab JH |
118 | if (never_interesting) { |
119 | /* | |
120 | * We have not seen any match that sorts later | |
121 | * than the current path. | |
122 | */ | |
123 | ||
124 | /* | |
125 | * Does match sort strictly earlier than path | |
126 | * with their common parts? | |
127 | */ | |
128 | m = strncmp(match, path, | |
129 | (matchlen < pathlen) ? matchlen : pathlen); | |
130 | if (m < 0) | |
131 | continue; | |
7d2f667b | 132 | |
ccc744ab JH |
133 | /* |
134 | * If we come here even once, that means there is at | |
135 | * least one pathspec that would sort equal to or | |
136 | * later than the path we are currently looking at. | |
137 | * In other words, if we have never reached this point | |
138 | * after iterating all pathspecs, it means all | |
139 | * pathspecs are either outside of base, or inside the | |
140 | * base but sorts strictly earlier than the current | |
141 | * one. In either case, they will never match the | |
142 | * subsequent entries. In such a case, we initialized | |
143 | * the variable to -1 and that is what will be | |
144 | * returned, allowing the caller to terminate early. | |
145 | */ | |
146 | never_interesting = 0; | |
147 | } | |
7d2f667b | 148 | |
ac1b3d12 LT |
149 | if (pathlen > matchlen) |
150 | continue; | |
151 | ||
152 | if (matchlen > pathlen) { | |
153 | if (match[pathlen] != '/') | |
154 | continue; | |
155 | if (!S_ISDIR(mode)) | |
156 | continue; | |
157 | } | |
158 | ||
ccc744ab JH |
159 | if (m == -1) |
160 | /* | |
161 | * we cheated and did not do strncmp(), so we do | |
162 | * that here. | |
163 | */ | |
164 | m = strncmp(match, path, pathlen); | |
165 | ||
7d2f667b JH |
166 | /* |
167 | * If common part matched earlier then it is a hit, | |
168 | * because we rejected the case where path is not a | |
169 | * leading directory and is shorter than match. | |
170 | */ | |
171 | if (!m) | |
172 | return 1; | |
ac1b3d12 | 173 | } |
7d2f667b | 174 | return never_interesting; /* No matches */ |
ac1b3d12 LT |
175 | } |
176 | ||
177 | /* A whole sub-tree went away or appeared */ | |
304de2d2 | 178 | static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen) |
ac1b3d12 | 179 | { |
1d848f64 | 180 | int all_interesting = 0; |
ac1b3d12 | 181 | while (desc->size) { |
1d848f64 JH |
182 | int show; |
183 | ||
184 | if (all_interesting) | |
185 | show = 1; | |
186 | else { | |
187 | show = tree_entry_interesting(desc, base, baselen, | |
188 | opt); | |
189 | if (show == 2) | |
190 | all_interesting = 1; | |
191 | } | |
5d865017 LT |
192 | if (show < 0) |
193 | break; | |
194 | if (show) | |
304de2d2 | 195 | show_entry(opt, prefix, desc, base, baselen); |
ac1b3d12 LT |
196 | update_tree_entry(desc); |
197 | } | |
198 | } | |
199 | ||
200 | /* A file entry went away or appeared */ | |
cf995ede | 201 | static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, |
304de2d2 | 202 | const char *base, int baselen) |
ac1b3d12 LT |
203 | { |
204 | unsigned mode; | |
205 | const char *path; | |
50f9a858 | 206 | const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); |
ac1b3d12 LT |
207 | |
208 | if (opt->recursive && S_ISDIR(mode)) { | |
21666f1a | 209 | enum object_type type; |
304de2d2 LT |
210 | int pathlen = tree_entry_len(path, sha1); |
211 | char *newbase = malloc_base(base, baselen, path, pathlen); | |
ac1b3d12 LT |
212 | struct tree_desc inner; |
213 | void *tree; | |
6fda5e51 | 214 | unsigned long size; |
ac1b3d12 | 215 | |
6fda5e51 | 216 | tree = read_sha1_file(sha1, &type, &size); |
21666f1a | 217 | if (!tree || type != OBJ_TREE) |
ac1b3d12 LT |
218 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
219 | ||
6fda5e51 | 220 | init_tree_desc(&inner, tree, size); |
304de2d2 | 221 | show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen); |
ac1b3d12 LT |
222 | |
223 | free(tree); | |
224 | free(newbase); | |
cf995ede DR |
225 | } else { |
226 | opt->add_remove(opt, prefix[0], mode, sha1, base, path); | |
ac1b3d12 | 227 | } |
ac1b3d12 LT |
228 | } |
229 | ||
5d865017 LT |
230 | static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt) |
231 | { | |
1d848f64 | 232 | int all_interesting = 0; |
5d865017 | 233 | while (t->size) { |
1d848f64 JH |
234 | int show; |
235 | ||
236 | if (all_interesting) | |
237 | show = 1; | |
238 | else { | |
239 | show = tree_entry_interesting(t, base, baselen, opt); | |
240 | if (show == 2) | |
241 | all_interesting = 1; | |
242 | } | |
5d865017 LT |
243 | if (!show) { |
244 | update_tree_entry(t); | |
245 | continue; | |
246 | } | |
247 | /* Skip it all? */ | |
248 | if (show < 0) | |
249 | t->size = 0; | |
250 | return; | |
251 | } | |
252 | } | |
253 | ||
ac1b3d12 LT |
254 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) |
255 | { | |
304de2d2 LT |
256 | int baselen = strlen(base); |
257 | ||
5d865017 | 258 | for (;;) { |
822cac01 JH |
259 | if (opt->quiet && opt->has_changes) |
260 | break; | |
5d865017 LT |
261 | if (opt->nr_paths) { |
262 | skip_uninteresting(t1, base, baselen, opt); | |
263 | skip_uninteresting(t2, base, baselen, opt); | |
ac1b3d12 LT |
264 | } |
265 | if (!t1->size) { | |
5d865017 LT |
266 | if (!t2->size) |
267 | break; | |
304de2d2 | 268 | show_entry(opt, "+", t2, base, baselen); |
ac1b3d12 LT |
269 | update_tree_entry(t2); |
270 | continue; | |
271 | } | |
272 | if (!t2->size) { | |
304de2d2 | 273 | show_entry(opt, "-", t1, base, baselen); |
ac1b3d12 LT |
274 | update_tree_entry(t1); |
275 | continue; | |
276 | } | |
304de2d2 | 277 | switch (compare_tree_entry(t1, t2, base, baselen, opt)) { |
ac1b3d12 LT |
278 | case -1: |
279 | update_tree_entry(t1); | |
280 | continue; | |
281 | case 0: | |
282 | update_tree_entry(t1); | |
283 | /* Fallthrough */ | |
284 | case 1: | |
285 | update_tree_entry(t2); | |
286 | continue; | |
287 | } | |
288 | die("git-diff-tree: internal error"); | |
289 | } | |
290 | return 0; | |
291 | } | |
292 | ||
293 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) | |
294 | { | |
295 | void *tree1, *tree2; | |
296 | struct tree_desc t1, t2; | |
6fda5e51 | 297 | unsigned long size1, size2; |
ac1b3d12 LT |
298 | int retval; |
299 | ||
6fda5e51 | 300 | tree1 = read_object_with_reference(old, tree_type, &size1, NULL); |
ac1b3d12 LT |
301 | if (!tree1) |
302 | die("unable to read source tree (%s)", sha1_to_hex(old)); | |
6fda5e51 | 303 | tree2 = read_object_with_reference(new, tree_type, &size2, NULL); |
ac1b3d12 LT |
304 | if (!tree2) |
305 | die("unable to read destination tree (%s)", sha1_to_hex(new)); | |
6fda5e51 LT |
306 | init_tree_desc(&t1, tree1, size1); |
307 | init_tree_desc(&t2, tree2, size2); | |
ac1b3d12 LT |
308 | retval = diff_tree(&t1, &t2, base, opt); |
309 | free(tree1); | |
310 | free(tree2); | |
311 | return retval; | |
312 | } | |
313 | ||
2b60356d RS |
314 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
315 | { | |
316 | int retval; | |
317 | void *tree; | |
6fda5e51 | 318 | unsigned long size; |
2b60356d RS |
319 | struct tree_desc empty, real; |
320 | ||
6fda5e51 | 321 | tree = read_object_with_reference(new, tree_type, &size, NULL); |
2b60356d RS |
322 | if (!tree) |
323 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
6fda5e51 | 324 | init_tree_desc(&real, tree, size); |
2b60356d | 325 | |
6fda5e51 | 326 | init_tree_desc(&empty, "", 0); |
2b60356d RS |
327 | retval = diff_tree(&empty, &real, base, opt); |
328 | free(tree); | |
329 | return retval; | |
330 | } | |
331 | ||
ac1b3d12 LT |
332 | static int count_paths(const char **paths) |
333 | { | |
334 | int i = 0; | |
335 | while (*paths++) | |
336 | i++; | |
337 | return i; | |
338 | } | |
339 | ||
a8baa7b9 | 340 | void diff_tree_release_paths(struct diff_options *opt) |
ac1b3d12 | 341 | { |
a8baa7b9 JH |
342 | free(opt->pathlens); |
343 | } | |
344 | ||
345 | void diff_tree_setup_paths(const char **p, struct diff_options *opt) | |
346 | { | |
347 | opt->nr_paths = 0; | |
348 | opt->pathlens = NULL; | |
349 | opt->paths = NULL; | |
350 | ||
ac1b3d12 LT |
351 | if (p) { |
352 | int i; | |
353 | ||
a8baa7b9 JH |
354 | opt->paths = p; |
355 | opt->nr_paths = count_paths(p); | |
356 | if (opt->nr_paths == 0) { | |
357 | opt->pathlens = NULL; | |
7e4a2a84 JH |
358 | return; |
359 | } | |
a8baa7b9 JH |
360 | opt->pathlens = xmalloc(opt->nr_paths * sizeof(int)); |
361 | for (i=0; i < opt->nr_paths; i++) | |
362 | opt->pathlens[i] = strlen(p[i]); | |
ac1b3d12 LT |
363 | } |
364 | } |