]>
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: | |
73 | * - positive for yes | |
74 | * - zero for no | |
75 | * - negative for "no, and no subsequent entries will be either" | |
76 | */ | |
77 | static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt) | |
ac1b3d12 LT |
78 | { |
79 | const char *path; | |
304de2d2 | 80 | const unsigned char *sha1; |
ac1b3d12 LT |
81 | unsigned mode; |
82 | int i; | |
304de2d2 | 83 | int pathlen; |
7d2f667b | 84 | int never_interesting = -1; |
ac1b3d12 | 85 | |
a8baa7b9 | 86 | if (!opt->nr_paths) |
ac1b3d12 LT |
87 | return 1; |
88 | ||
304de2d2 | 89 | sha1 = tree_entry_extract(desc, &path, &mode); |
ac1b3d12 | 90 | |
304de2d2 | 91 | pathlen = tree_entry_len(path, sha1); |
ac1b3d12 | 92 | |
7d2f667b | 93 | for (i = 0; i < opt->nr_paths; i++) { |
a8baa7b9 JH |
94 | const char *match = opt->paths[i]; |
95 | int matchlen = opt->pathlens[i]; | |
7d2f667b | 96 | int m; |
ac1b3d12 LT |
97 | |
98 | if (baselen >= matchlen) { | |
99 | /* If it doesn't match, move along... */ | |
100 | if (strncmp(base, match, matchlen)) | |
101 | continue; | |
102 | ||
103 | /* The base is a subdirectory of a path which was specified. */ | |
104 | return 1; | |
105 | } | |
106 | ||
107 | /* Does the base match? */ | |
108 | if (strncmp(base, match, baselen)) | |
109 | continue; | |
110 | ||
111 | match += baselen; | |
112 | matchlen -= baselen; | |
113 | ||
7d2f667b JH |
114 | /* |
115 | * Does match sort strictly earlier than path with their | |
116 | * common parts? | |
117 | */ | |
118 | m = strncmp(match, path, | |
119 | (matchlen < pathlen) ? matchlen : pathlen); | |
120 | if (m < 0) | |
121 | continue; | |
122 | ||
123 | /* | |
124 | * If we come here even once, that means there is at | |
125 | * least one pathspec that would sort equal to or | |
126 | * later than the path we are currently looking at. | |
127 | * In other words, if we have never reached this point | |
128 | * after iterating all pathspecs, it means all | |
129 | * pathspecs are either outside of base, or inside the | |
130 | * base but sorts strictly earlier than the current | |
131 | * one. In either case, they will never match the | |
132 | * subsequent entries. In such a case, we initialized | |
133 | * the variable to -1 and that is what will be | |
134 | * returned, allowing the caller to terminate early. | |
135 | */ | |
136 | never_interesting = 0; | |
137 | ||
ac1b3d12 LT |
138 | if (pathlen > matchlen) |
139 | continue; | |
140 | ||
141 | if (matchlen > pathlen) { | |
142 | if (match[pathlen] != '/') | |
143 | continue; | |
144 | if (!S_ISDIR(mode)) | |
145 | continue; | |
146 | } | |
147 | ||
7d2f667b JH |
148 | /* |
149 | * If common part matched earlier then it is a hit, | |
150 | * because we rejected the case where path is not a | |
151 | * leading directory and is shorter than match. | |
152 | */ | |
153 | if (!m) | |
154 | return 1; | |
ac1b3d12 | 155 | } |
7d2f667b | 156 | return never_interesting; /* No matches */ |
ac1b3d12 LT |
157 | } |
158 | ||
159 | /* A whole sub-tree went away or appeared */ | |
304de2d2 | 160 | static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen) |
ac1b3d12 LT |
161 | { |
162 | while (desc->size) { | |
5d865017 LT |
163 | int show = tree_entry_interesting(desc, base, baselen, opt); |
164 | if (show < 0) | |
165 | break; | |
166 | if (show) | |
304de2d2 | 167 | show_entry(opt, prefix, desc, base, baselen); |
ac1b3d12 LT |
168 | update_tree_entry(desc); |
169 | } | |
170 | } | |
171 | ||
172 | /* A file entry went away or appeared */ | |
cf995ede | 173 | static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, |
304de2d2 | 174 | const char *base, int baselen) |
ac1b3d12 LT |
175 | { |
176 | unsigned mode; | |
177 | const char *path; | |
50f9a858 | 178 | const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); |
ac1b3d12 LT |
179 | |
180 | if (opt->recursive && S_ISDIR(mode)) { | |
21666f1a | 181 | enum object_type type; |
304de2d2 LT |
182 | int pathlen = tree_entry_len(path, sha1); |
183 | char *newbase = malloc_base(base, baselen, path, pathlen); | |
ac1b3d12 LT |
184 | struct tree_desc inner; |
185 | void *tree; | |
6fda5e51 | 186 | unsigned long size; |
ac1b3d12 | 187 | |
6fda5e51 | 188 | tree = read_sha1_file(sha1, &type, &size); |
21666f1a | 189 | if (!tree || type != OBJ_TREE) |
ac1b3d12 LT |
190 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
191 | ||
6fda5e51 | 192 | init_tree_desc(&inner, tree, size); |
304de2d2 | 193 | show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen); |
ac1b3d12 LT |
194 | |
195 | free(tree); | |
196 | free(newbase); | |
cf995ede DR |
197 | } else { |
198 | opt->add_remove(opt, prefix[0], mode, sha1, base, path); | |
ac1b3d12 | 199 | } |
ac1b3d12 LT |
200 | } |
201 | ||
5d865017 LT |
202 | static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt) |
203 | { | |
204 | while (t->size) { | |
205 | int show = tree_entry_interesting(t, base, baselen, opt); | |
206 | if (!show) { | |
207 | update_tree_entry(t); | |
208 | continue; | |
209 | } | |
210 | /* Skip it all? */ | |
211 | if (show < 0) | |
212 | t->size = 0; | |
213 | return; | |
214 | } | |
215 | } | |
216 | ||
ac1b3d12 LT |
217 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) |
218 | { | |
304de2d2 LT |
219 | int baselen = strlen(base); |
220 | ||
5d865017 | 221 | for (;;) { |
822cac01 JH |
222 | if (opt->quiet && opt->has_changes) |
223 | break; | |
5d865017 LT |
224 | if (opt->nr_paths) { |
225 | skip_uninteresting(t1, base, baselen, opt); | |
226 | skip_uninteresting(t2, base, baselen, opt); | |
ac1b3d12 LT |
227 | } |
228 | if (!t1->size) { | |
5d865017 LT |
229 | if (!t2->size) |
230 | break; | |
304de2d2 | 231 | show_entry(opt, "+", t2, base, baselen); |
ac1b3d12 LT |
232 | update_tree_entry(t2); |
233 | continue; | |
234 | } | |
235 | if (!t2->size) { | |
304de2d2 | 236 | show_entry(opt, "-", t1, base, baselen); |
ac1b3d12 LT |
237 | update_tree_entry(t1); |
238 | continue; | |
239 | } | |
304de2d2 | 240 | switch (compare_tree_entry(t1, t2, base, baselen, opt)) { |
ac1b3d12 LT |
241 | case -1: |
242 | update_tree_entry(t1); | |
243 | continue; | |
244 | case 0: | |
245 | update_tree_entry(t1); | |
246 | /* Fallthrough */ | |
247 | case 1: | |
248 | update_tree_entry(t2); | |
249 | continue; | |
250 | } | |
251 | die("git-diff-tree: internal error"); | |
252 | } | |
253 | return 0; | |
254 | } | |
255 | ||
256 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) | |
257 | { | |
258 | void *tree1, *tree2; | |
259 | struct tree_desc t1, t2; | |
6fda5e51 | 260 | unsigned long size1, size2; |
ac1b3d12 LT |
261 | int retval; |
262 | ||
6fda5e51 | 263 | tree1 = read_object_with_reference(old, tree_type, &size1, NULL); |
ac1b3d12 LT |
264 | if (!tree1) |
265 | die("unable to read source tree (%s)", sha1_to_hex(old)); | |
6fda5e51 | 266 | tree2 = read_object_with_reference(new, tree_type, &size2, NULL); |
ac1b3d12 LT |
267 | if (!tree2) |
268 | die("unable to read destination tree (%s)", sha1_to_hex(new)); | |
6fda5e51 LT |
269 | init_tree_desc(&t1, tree1, size1); |
270 | init_tree_desc(&t2, tree2, size2); | |
ac1b3d12 LT |
271 | retval = diff_tree(&t1, &t2, base, opt); |
272 | free(tree1); | |
273 | free(tree2); | |
274 | return retval; | |
275 | } | |
276 | ||
2b60356d RS |
277 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
278 | { | |
279 | int retval; | |
280 | void *tree; | |
6fda5e51 | 281 | unsigned long size; |
2b60356d RS |
282 | struct tree_desc empty, real; |
283 | ||
6fda5e51 | 284 | tree = read_object_with_reference(new, tree_type, &size, NULL); |
2b60356d RS |
285 | if (!tree) |
286 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
6fda5e51 | 287 | init_tree_desc(&real, tree, size); |
2b60356d | 288 | |
6fda5e51 | 289 | init_tree_desc(&empty, "", 0); |
2b60356d RS |
290 | retval = diff_tree(&empty, &real, base, opt); |
291 | free(tree); | |
292 | return retval; | |
293 | } | |
294 | ||
ac1b3d12 LT |
295 | static int count_paths(const char **paths) |
296 | { | |
297 | int i = 0; | |
298 | while (*paths++) | |
299 | i++; | |
300 | return i; | |
301 | } | |
302 | ||
a8baa7b9 | 303 | void diff_tree_release_paths(struct diff_options *opt) |
ac1b3d12 | 304 | { |
a8baa7b9 JH |
305 | free(opt->pathlens); |
306 | } | |
307 | ||
308 | void diff_tree_setup_paths(const char **p, struct diff_options *opt) | |
309 | { | |
310 | opt->nr_paths = 0; | |
311 | opt->pathlens = NULL; | |
312 | opt->paths = NULL; | |
313 | ||
ac1b3d12 LT |
314 | if (p) { |
315 | int i; | |
316 | ||
a8baa7b9 JH |
317 | opt->paths = p; |
318 | opt->nr_paths = count_paths(p); | |
319 | if (opt->nr_paths == 0) { | |
320 | opt->pathlens = NULL; | |
7e4a2a84 JH |
321 | return; |
322 | } | |
a8baa7b9 JH |
323 | opt->pathlens = xmalloc(opt->nr_paths * sizeof(int)); |
324 | for (i=0; i < opt->nr_paths; i++) | |
325 | opt->pathlens[i] = strlen(p[i]); | |
ac1b3d12 LT |
326 | } |
327 | } |