]>
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 | |
72441af7 KS |
9 | /* |
10 | * internal mode marker, saying a tree entry != entry of tp[imin] | |
11 | * (see ll_diff_tree_paths for what it means there) | |
12 | * | |
13 | * we will update/use/emit entry for diff only with it unset. | |
14 | */ | |
15 | #define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ | |
16 | ||
b8ba412b JK |
17 | #define FAST_ARRAY_ALLOC(x, nr) do { \ |
18 | if ((nr) <= 2) \ | |
19 | (x) = xalloca((nr) * sizeof(*(x))); \ | |
20 | else \ | |
21 | ALLOC_ARRAY((x), nr); \ | |
22 | } while(0) | |
23 | #define FAST_ARRAY_FREE(x, nr) do { \ | |
24 | if ((nr) > 2) \ | |
25 | free((x)); \ | |
26 | } while(0) | |
b9081a65 | 27 | |
72441af7 KS |
28 | static struct combine_diff_path *ll_diff_tree_paths( |
29 | struct combine_diff_path *p, const unsigned char *sha1, | |
30 | const unsigned char **parents_sha1, int nparent, | |
31 | struct strbuf *base, struct diff_options *opt); | |
b9081a65 | 32 | static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new, |
12cd8174 | 33 | struct strbuf *base, struct diff_options *opt); |
b9081a65 | 34 | |
9bc06196 KS |
35 | /* |
36 | * Compare two tree entries, taking into account only path/S_ISDIR(mode), | |
37 | * but not their sha1's. | |
38 | * | |
39 | * NOTE files and directories *always* compare differently, even when having | |
40 | * the same name - thanks to base_name_compare(). | |
6ca844e9 KS |
41 | * |
42 | * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty, | |
43 | * so that they sort *after* valid tree entries. | |
44 | * | |
45 | * Due to this convention, if trees are scanned in sorted order, all | |
46 | * non-empty descriptors will be processed first. | |
9bc06196 KS |
47 | */ |
48 | static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) | |
ac1b3d12 | 49 | { |
1a27a154 KS |
50 | struct name_entry *e1, *e2; |
51 | int cmp; | |
ac1b3d12 | 52 | |
6ca844e9 KS |
53 | /* empty descriptors sort after valid tree entries */ |
54 | if (!t1->size) | |
55 | return t2->size ? 1 : 0; | |
56 | else if (!t2->size) | |
57 | return -1; | |
58 | ||
1a27a154 KS |
59 | e1 = &t1->entry; |
60 | e2 = &t2->entry; | |
61 | cmp = base_name_compare(e1->path, tree_entry_len(e1), e1->mode, | |
62 | e2->path, tree_entry_len(e2), e2->mode); | |
903bba68 | 63 | return cmp; |
d00e980c KS |
64 | } |
65 | ||
66 | ||
72441af7 KS |
67 | /* |
68 | * convert path -> opt->diff_*() callbacks | |
69 | * | |
70 | * emits diff to first parent only, and tells diff tree-walker that we are done | |
71 | * with p and it can be freed. | |
72 | */ | |
73 | static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_diff_path *p) | |
d00e980c | 74 | { |
72441af7 KS |
75 | struct combine_diff_parent *p0 = &p->parent[0]; |
76 | if (p->mode && p0->mode) { | |
1ff57c13 | 77 | opt->change(opt, p0->mode, p->mode, p0->oid.hash, p->oid.hash, |
72441af7 | 78 | 1, 1, p->path, 0, 0); |
d00e980c KS |
79 | } |
80 | else { | |
81 | const unsigned char *sha1; | |
82 | unsigned int mode; | |
83 | int addremove; | |
84 | ||
72441af7 | 85 | if (p->mode) { |
d00e980c | 86 | addremove = '+'; |
1ff57c13 | 87 | sha1 = p->oid.hash; |
72441af7 | 88 | mode = p->mode; |
d00e980c KS |
89 | } else { |
90 | addremove = '-'; | |
1ff57c13 | 91 | sha1 = p0->oid.hash; |
72441af7 | 92 | mode = p0->mode; |
fd55a19e | 93 | } |
d00e980c | 94 | |
72441af7 | 95 | opt->add_remove(opt, addremove, mode, sha1, 1, p->path, 0); |
ac1b3d12 | 96 | } |
72441af7 KS |
97 | |
98 | return 0; /* we are done with p */ | |
ac1b3d12 LT |
99 | } |
100 | ||
d00e980c | 101 | |
72441af7 KS |
102 | /* |
103 | * Make a new combine_diff_path from path/mode/sha1 | |
104 | * and append it to paths list tail. | |
105 | * | |
106 | * Memory for created elements could be reused: | |
107 | * | |
108 | * - if last->next == NULL, the memory is allocated; | |
109 | * | |
110 | * - if last->next != NULL, it is assumed that p=last->next was returned | |
111 | * earlier by this function, and p->next was *not* modified. | |
112 | * The memory is then reused from p. | |
113 | * | |
114 | * so for clients, | |
115 | * | |
116 | * - if you do need to keep the element | |
117 | * | |
118 | * p = path_appendnew(p, ...); | |
119 | * process(p); | |
120 | * p->next = NULL; | |
121 | * | |
122 | * - if you don't need to keep the element after processing | |
123 | * | |
124 | * pprev = p; | |
125 | * p = path_appendnew(p, ...); | |
126 | * process(p); | |
127 | * p = pprev; | |
128 | * ; don't forget to free tail->next in the end | |
129 | * | |
130 | * p->parent[] remains uninitialized. | |
131 | */ | |
132 | static struct combine_diff_path *path_appendnew(struct combine_diff_path *last, | |
133 | int nparent, const struct strbuf *base, const char *path, int pathlen, | |
134 | unsigned mode, const unsigned char *sha1) | |
135 | { | |
136 | struct combine_diff_path *p; | |
5b442c4f JK |
137 | size_t len = st_add(base->len, pathlen); |
138 | size_t alloclen = combine_diff_path_size(nparent, len); | |
72441af7 KS |
139 | |
140 | /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */ | |
141 | p = last->next; | |
142 | if (p && (alloclen > (intptr_t)p->next)) { | |
143 | free(p); | |
144 | p = NULL; | |
145 | } | |
146 | ||
147 | if (!p) { | |
148 | p = xmalloc(alloclen); | |
149 | ||
150 | /* | |
151 | * until we go to it next round, .next holds how many bytes we | |
152 | * allocated (for faster realloc - we don't need copying old data). | |
153 | */ | |
154 | p->next = (struct combine_diff_path *)(intptr_t)alloclen; | |
155 | } | |
156 | ||
157 | last->next = p; | |
158 | ||
159 | p->path = (char *)&(p->parent[nparent]); | |
160 | memcpy(p->path, base->buf, base->len); | |
161 | memcpy(p->path + base->len, path, pathlen); | |
162 | p->path[len] = 0; | |
163 | p->mode = mode; | |
1ff57c13 | 164 | hashcpy(p->oid.hash, sha1 ? sha1 : null_sha1); |
72441af7 KS |
165 | |
166 | return p; | |
167 | } | |
168 | ||
169 | /* | |
170 | * new path should be added to combine diff | |
d00e980c KS |
171 | * |
172 | * 3 cases on how/when it should be called and behaves: | |
173 | * | |
72441af7 KS |
174 | * t, !tp -> path added, all parents lack it |
175 | * !t, tp -> path removed from all parents | |
176 | * t, tp -> path modified/added | |
177 | * (M for tp[i]=tp[imin], A otherwise) | |
d00e980c | 178 | */ |
72441af7 KS |
179 | static struct combine_diff_path *emit_path(struct combine_diff_path *p, |
180 | struct strbuf *base, struct diff_options *opt, int nparent, | |
181 | struct tree_desc *t, struct tree_desc *tp, | |
182 | int imin) | |
ac1b3d12 LT |
183 | { |
184 | unsigned mode; | |
185 | const char *path; | |
72441af7 | 186 | const unsigned char *sha1; |
d00e980c | 187 | int pathlen; |
48932677 | 188 | int old_baselen = base->len; |
72441af7 | 189 | int i, isdir, recurse = 0, emitthis = 1; |
d00e980c KS |
190 | |
191 | /* at least something has to be valid */ | |
72441af7 | 192 | assert(t || tp); |
d00e980c | 193 | |
72441af7 | 194 | if (t) { |
d00e980c | 195 | /* path present in resulting tree */ |
ce6663a9 | 196 | sha1 = tree_entry_extract(t, &path, &mode)->hash; |
72441af7 | 197 | pathlen = tree_entry_len(&t->entry); |
d00e980c KS |
198 | isdir = S_ISDIR(mode); |
199 | } else { | |
200 | /* | |
72441af7 KS |
201 | * a path was removed - take path from imin parent. Also take |
202 | * mode from that parent, to decide on recursion(1). | |
203 | * | |
204 | * 1) all modes for tp[i]=tp[imin] should be the same wrt | |
205 | * S_ISDIR, thanks to base_name_compare(). | |
d00e980c | 206 | */ |
72441af7 KS |
207 | tree_entry_extract(&tp[imin], &path, &mode); |
208 | pathlen = tree_entry_len(&tp[imin].entry); | |
d00e980c KS |
209 | |
210 | isdir = S_ISDIR(mode); | |
72441af7 | 211 | sha1 = NULL; |
d00e980c KS |
212 | mode = 0; |
213 | } | |
214 | ||
215 | if (DIFF_OPT_TST(opt, RECURSIVE) && isdir) { | |
216 | recurse = 1; | |
217 | emitthis = DIFF_OPT_TST(opt, TREE_IN_RECURSIVE); | |
218 | } | |
ac1b3d12 | 219 | |
72441af7 KS |
220 | if (emitthis) { |
221 | int keep; | |
222 | struct combine_diff_path *pprev = p; | |
223 | p = path_appendnew(p, nparent, base, path, pathlen, mode, sha1); | |
224 | ||
225 | for (i = 0; i < nparent; ++i) { | |
226 | /* | |
227 | * tp[i] is valid, if present and if tp[i]==tp[imin] - | |
228 | * otherwise, we should ignore it. | |
229 | */ | |
230 | int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ); | |
231 | ||
232 | const unsigned char *sha1_i; | |
233 | unsigned mode_i; | |
234 | ||
235 | p->parent[i].status = | |
236 | !t ? DIFF_STATUS_DELETED : | |
237 | tpi_valid ? | |
238 | DIFF_STATUS_MODIFIED : | |
239 | DIFF_STATUS_ADDED; | |
240 | ||
241 | if (tpi_valid) { | |
7d924c91 | 242 | sha1_i = tp[i].entry.oid->hash; |
72441af7 KS |
243 | mode_i = tp[i].entry.mode; |
244 | } | |
245 | else { | |
246 | sha1_i = NULL; | |
247 | mode_i = 0; | |
248 | } | |
249 | ||
250 | p->parent[i].mode = mode_i; | |
1ff57c13 | 251 | hashcpy(p->parent[i].oid.hash, sha1_i ? sha1_i : null_sha1); |
72441af7 | 252 | } |
df533f34 | 253 | |
72441af7 KS |
254 | keep = 1; |
255 | if (opt->pathchange) | |
256 | keep = opt->pathchange(opt, p); | |
257 | ||
258 | /* | |
259 | * If a path was filtered or consumed - we don't need to add it | |
260 | * to the list and can reuse its memory, leaving it as | |
261 | * pre-allocated element on the tail. | |
262 | * | |
263 | * On the other hand, if path needs to be kept, we need to | |
264 | * correct its .next to NULL, as it was pre-initialized to how | |
265 | * much memory was allocated. | |
266 | * | |
267 | * see path_appendnew() for details. | |
268 | */ | |
269 | if (!keep) | |
270 | p = pprev; | |
271 | else | |
272 | p->next = NULL; | |
273 | } | |
d00e980c KS |
274 | |
275 | if (recurse) { | |
72441af7 KS |
276 | const unsigned char **parents_sha1; |
277 | ||
b8ba412b | 278 | FAST_ARRAY_ALLOC(parents_sha1, nparent); |
72441af7 KS |
279 | for (i = 0; i < nparent; ++i) { |
280 | /* same rule as in emitthis */ | |
281 | int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ); | |
282 | ||
7d924c91 | 283 | parents_sha1[i] = tpi_valid ? tp[i].entry.oid->hash |
72441af7 KS |
284 | : NULL; |
285 | } | |
286 | ||
287 | strbuf_add(base, path, pathlen); | |
48932677 | 288 | strbuf_addch(base, '/'); |
72441af7 | 289 | p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt); |
b8ba412b | 290 | FAST_ARRAY_FREE(parents_sha1, nparent); |
d00e980c | 291 | } |
48932677 NTND |
292 | |
293 | strbuf_setlen(base, old_baselen); | |
72441af7 | 294 | return p; |
ac1b3d12 LT |
295 | } |
296 | ||
48932677 | 297 | static void skip_uninteresting(struct tree_desc *t, struct strbuf *base, |
e9066121 | 298 | struct diff_options *opt) |
5d865017 | 299 | { |
e9066121 KS |
300 | enum interesting match; |
301 | ||
5d865017 | 302 | while (t->size) { |
e9066121 KS |
303 | match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec); |
304 | if (match) { | |
305 | if (match == all_entries_not_interesting) | |
97d0b74a NTND |
306 | t->size = 0; |
307 | break; | |
5d865017 | 308 | } |
97d0b74a | 309 | update_tree_entry(t); |
5d865017 LT |
310 | } |
311 | } | |
312 | ||
72441af7 KS |
313 | |
314 | /* | |
315 | * generate paths for combined diff D(sha1,parents_sha1[]) | |
316 | * | |
317 | * Resulting paths are appended to combine_diff_path linked list, and also, are | |
318 | * emitted on the go via opt->pathchange() callback, so it is possible to | |
319 | * process the result as batch or incrementally. | |
320 | * | |
321 | * The paths are generated scanning new tree and all parents trees | |
322 | * simultaneously, similarly to what diff_tree() was doing for 2 trees. | |
323 | * The theory behind such scan is as follows: | |
324 | * | |
325 | * | |
326 | * D(T,P1...Pn) calculation scheme | |
327 | * ------------------------------- | |
328 | * | |
329 | * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set) | |
330 | * | |
331 | * D(T,Pj) - diff between T..Pj | |
332 | * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn | |
333 | * | |
334 | * | |
335 | * We start from all trees, which are sorted, and compare their entries in | |
336 | * lock-step: | |
337 | * | |
338 | * T P1 Pn | |
339 | * - - - | |
340 | * |t| |p1| |pn| | |
341 | * |-| |--| ... |--| imin = argmin(p1...pn) | |
342 | * | | | | | | | |
343 | * |-| |--| |--| | |
344 | * |.| |. | |. | | |
345 | * . . . | |
346 | * . . . | |
347 | * | |
348 | * at any time there could be 3 cases: | |
349 | * | |
350 | * 1) t < p[imin]; | |
351 | * 2) t > p[imin]; | |
352 | * 3) t = p[imin]. | |
353 | * | |
354 | * Schematic deduction of what every case means, and what to do, follows: | |
355 | * | |
356 | * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓ | |
357 | * | |
358 | * 2) t > p[imin] | |
359 | * | |
360 | * 2.1) ∃j: pj > p[imin] -> "-p[imin]" ∉ D(T,Pj) -> D += ø; ∀ pi=p[imin] pi↓ | |
361 | * 2.2) ∀i pi = p[imin] -> pi ∉ T -> "-pi" ∈ D(T,Pi) -> D += "-p[imin]"; ∀i pi↓ | |
362 | * | |
363 | * 3) t = p[imin] | |
364 | * | |
365 | * 3.1) ∃j: pj > p[imin] -> "+t" ∈ D(T,Pj) -> only pi=p[imin] remains to investigate | |
366 | * 3.2) pi = p[imin] -> investigate δ(t,pi) | |
367 | * | | |
368 | * | | |
369 | * v | |
370 | * | |
371 | * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø -> | |
372 | * | |
373 | * ⎧δ(t,pi) - if pi=p[imin] | |
374 | * -> D += ⎨ | |
375 | * ⎩"+t" - if pi>p[imin] | |
376 | * | |
377 | * | |
378 | * in any case t↓ ∀ pi=p[imin] pi↓ | |
379 | * | |
380 | * | |
381 | * ~~~~~~~~ | |
382 | * | |
383 | * NOTE | |
384 | * | |
385 | * Usual diff D(A,B) is by definition the same as combined diff D(A,[B]), | |
386 | * so this diff paths generator can, and is used, for plain diffs | |
387 | * generation too. | |
388 | * | |
389 | * Please keep attention to the common D(A,[B]) case when working on the | |
390 | * code, in order not to slow it down. | |
391 | * | |
392 | * NOTE | |
393 | * nparent must be > 0. | |
394 | */ | |
395 | ||
396 | ||
397 | /* ∀ pi=p[imin] pi↓ */ | |
398 | static inline void update_tp_entries(struct tree_desc *tp, int nparent) | |
ac1b3d12 | 399 | { |
72441af7 KS |
400 | int i; |
401 | for (i = 0; i < nparent; ++i) | |
402 | if (!(tp[i].entry.mode & S_IFXMIN_NEQ)) | |
403 | update_tree_entry(&tp[i]); | |
404 | } | |
304de2d2 | 405 | |
72441af7 KS |
406 | static struct combine_diff_path *ll_diff_tree_paths( |
407 | struct combine_diff_path *p, const unsigned char *sha1, | |
408 | const unsigned char **parents_sha1, int nparent, | |
409 | struct strbuf *base, struct diff_options *opt) | |
410 | { | |
411 | struct tree_desc t, *tp; | |
412 | void *ttree, **tptree; | |
413 | int i; | |
414 | ||
b8ba412b JK |
415 | FAST_ARRAY_ALLOC(tp, nparent); |
416 | FAST_ARRAY_ALLOC(tptree, nparent); | |
72441af7 KS |
417 | |
418 | /* | |
419 | * load parents first, as they are probably already cached. | |
420 | * | |
421 | * ( log_tree_diff() parses commit->parent before calling here via | |
422 | * diff_tree_sha1(parent, commit) ) | |
423 | */ | |
424 | for (i = 0; i < nparent; ++i) | |
425 | tptree[i] = fill_tree_descriptor(&tp[i], parents_sha1[i]); | |
426 | ttree = fill_tree_descriptor(&t, sha1); | |
52894e70 | 427 | |
bc96cc87 NTND |
428 | /* Enable recursion indefinitely */ |
429 | opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE); | |
bc96cc87 | 430 | |
5d865017 | 431 | for (;;) { |
72441af7 | 432 | int imin, cmp; |
903bba68 | 433 | |
28b9264d | 434 | if (diff_can_quit_early(opt)) |
822cac01 | 435 | break; |
72441af7 | 436 | |
66f13625 | 437 | if (opt->pathspec.nr) { |
72441af7 KS |
438 | skip_uninteresting(&t, base, opt); |
439 | for (i = 0; i < nparent; i++) | |
440 | skip_uninteresting(&tp[i], base, opt); | |
441 | } | |
442 | ||
443 | /* comparing is finished when all trees are done */ | |
444 | if (!t.size) { | |
445 | int done = 1; | |
446 | for (i = 0; i < nparent; ++i) | |
447 | if (tp[i].size) { | |
448 | done = 0; | |
449 | break; | |
450 | } | |
451 | if (done) | |
452 | break; | |
453 | } | |
454 | ||
455 | /* | |
456 | * lookup imin = argmin(p1...pn), | |
457 | * mark entries whether they =p[imin] along the way | |
458 | */ | |
459 | imin = 0; | |
460 | tp[0].entry.mode &= ~S_IFXMIN_NEQ; | |
461 | ||
462 | for (i = 1; i < nparent; ++i) { | |
463 | cmp = tree_entry_pathcmp(&tp[i], &tp[imin]); | |
464 | if (cmp < 0) { | |
465 | imin = i; | |
466 | tp[i].entry.mode &= ~S_IFXMIN_NEQ; | |
467 | } | |
468 | else if (cmp == 0) { | |
469 | tp[i].entry.mode &= ~S_IFXMIN_NEQ; | |
470 | } | |
471 | else { | |
472 | tp[i].entry.mode |= S_IFXMIN_NEQ; | |
473 | } | |
ac1b3d12 | 474 | } |
5dfb2bbd | 475 | |
72441af7 KS |
476 | /* fixup markings for entries before imin */ |
477 | for (i = 0; i < imin; ++i) | |
478 | tp[i].entry.mode |= S_IFXMIN_NEQ; /* pi > p[imin] */ | |
5dfb2bbd | 479 | |
903bba68 | 480 | |
72441af7 KS |
481 | |
482 | /* compare t vs p[imin] */ | |
483 | cmp = tree_entry_pathcmp(&t, &tp[imin]); | |
484 | ||
485 | /* t = p[imin] */ | |
486 | if (cmp == 0) { | |
487 | /* are either pi > p[imin] or diff(t,pi) != ø ? */ | |
488 | if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) { | |
489 | for (i = 0; i < nparent; ++i) { | |
490 | /* p[i] > p[imin] */ | |
491 | if (tp[i].entry.mode & S_IFXMIN_NEQ) | |
492 | continue; | |
493 | ||
494 | /* diff(t,pi) != ø */ | |
7d924c91 | 495 | if (oidcmp(t.entry.oid, tp[i].entry.oid) || |
72441af7 KS |
496 | (t.entry.mode != tp[i].entry.mode)) |
497 | continue; | |
498 | ||
499 | goto skip_emit_t_tp; | |
500 | } | |
501 | } | |
502 | ||
503 | /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */ | |
504 | p = emit_path(p, base, opt, nparent, | |
505 | &t, tp, imin); | |
506 | ||
507 | skip_emit_t_tp: | |
508 | /* t↓, ∀ pi=p[imin] pi↓ */ | |
509 | update_tree_entry(&t); | |
510 | update_tp_entries(tp, nparent); | |
5dfb2bbd KS |
511 | } |
512 | ||
72441af7 | 513 | /* t < p[imin] */ |
5dfb2bbd | 514 | else if (cmp < 0) { |
72441af7 KS |
515 | /* D += "+t" */ |
516 | p = emit_path(p, base, opt, nparent, | |
517 | &t, /*tp=*/NULL, -1); | |
518 | ||
519 | /* t↓ */ | |
520 | update_tree_entry(&t); | |
5dfb2bbd KS |
521 | } |
522 | ||
72441af7 | 523 | /* t > p[imin] */ |
5dfb2bbd | 524 | else { |
72441af7 KS |
525 | /* ∀i pi=p[imin] -> D += "-p[imin]" */ |
526 | if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) { | |
527 | for (i = 0; i < nparent; ++i) | |
528 | if (tp[i].entry.mode & S_IFXMIN_NEQ) | |
529 | goto skip_emit_tp; | |
530 | } | |
531 | ||
532 | p = emit_path(p, base, opt, nparent, | |
533 | /*t=*/NULL, tp, imin); | |
534 | ||
535 | skip_emit_tp: | |
536 | /* ∀ pi=p[imin] pi↓ */ | |
537 | update_tp_entries(tp, nparent); | |
ac1b3d12 | 538 | } |
ac1b3d12 | 539 | } |
48932677 | 540 | |
72441af7 KS |
541 | free(ttree); |
542 | for (i = nparent-1; i >= 0; i--) | |
543 | free(tptree[i]); | |
b8ba412b JK |
544 | FAST_ARRAY_FREE(tptree, nparent); |
545 | FAST_ARRAY_FREE(tp, nparent); | |
72441af7 KS |
546 | |
547 | return p; | |
548 | } | |
549 | ||
550 | struct combine_diff_path *diff_tree_paths( | |
551 | struct combine_diff_path *p, const unsigned char *sha1, | |
552 | const unsigned char **parents_sha1, int nparent, | |
553 | struct strbuf *base, struct diff_options *opt) | |
554 | { | |
555 | p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt); | |
556 | ||
557 | /* | |
558 | * free pre-allocated last element, if any | |
559 | * (see path_appendnew() for details about why) | |
560 | */ | |
561 | if (p->next) { | |
562 | free(p->next); | |
563 | p->next = NULL; | |
564 | } | |
565 | ||
566 | return p; | |
ac1b3d12 LT |
567 | } |
568 | ||
750f7b66 LT |
569 | /* |
570 | * Does it look like the resulting diff might be due to a rename? | |
571 | * - single entry | |
572 | * - not a valid previous file | |
573 | */ | |
574 | static inline int diff_might_be_rename(void) | |
575 | { | |
576 | return diff_queued_diff.nr == 1 && | |
577 | !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one); | |
578 | } | |
579 | ||
12cd8174 | 580 | static void try_to_follow_renames(const unsigned char *old, const unsigned char *new, struct strbuf *base, struct diff_options *opt) |
750f7b66 LT |
581 | { |
582 | struct diff_options diff_opts; | |
9f38e1ef LT |
583 | struct diff_queue_struct *q = &diff_queued_diff; |
584 | struct diff_filepair *choice; | |
750f7b66 LT |
585 | int i; |
586 | ||
8f4f8f45 NTND |
587 | /* |
588 | * follow-rename code is very specific, we need exactly one | |
589 | * path. Magic that matches more than one path is not | |
590 | * supported. | |
591 | */ | |
5c6933d2 | 592 | GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL); |
8f4f8f45 NTND |
593 | #if 0 |
594 | /* | |
595 | * We should reject wildcards as well. Unfortunately we | |
596 | * haven't got a reliable way to detect that 'foo\*bar' in | |
597 | * fact has no wildcards. nowildcard_len is merely a hint for | |
598 | * optimization. Let it slip for now until wildmatch is taught | |
599 | * about dry-run mode and returns wildcard info. | |
600 | */ | |
601 | if (opt->pathspec.has_wildcard) | |
602 | die("BUG:%s:%d: wildcards are not supported", | |
603 | __FILE__, __LINE__); | |
604 | #endif | |
605 | ||
9f38e1ef LT |
606 | /* Remove the file creation entry from the diff queue, and remember it */ |
607 | choice = q->queue[0]; | |
608 | q->nr = 0; | |
609 | ||
750f7b66 | 610 | diff_setup(&diff_opts); |
8f67f8ae | 611 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
0cdca133 | 612 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
750f7b66 | 613 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
61588ccf | 614 | diff_opts.single_follow = opt->pathspec.items[0].match; |
6dd4b66f | 615 | diff_opts.break_opt = opt->break_opt; |
dd98d88b | 616 | diff_opts.rename_score = opt->rename_score; |
28452655 | 617 | diff_setup_done(&diff_opts); |
52894e70 | 618 | ll_diff_tree_sha1(old, new, base, &diff_opts); |
750f7b66 | 619 | diffcore_std(&diff_opts); |
ed6e8038 | 620 | clear_pathspec(&diff_opts.pathspec); |
750f7b66 | 621 | |
9f38e1ef | 622 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
44c48a90 | 623 | opt->found_follow = 0; |
9f38e1ef LT |
624 | for (i = 0; i < q->nr; i++) { |
625 | struct diff_filepair *p = q->queue[i]; | |
750f7b66 LT |
626 | |
627 | /* | |
628 | * Found a source? Not only do we use that for the new | |
9f38e1ef | 629 | * diff_queued_diff, we will also use that as the path in |
750f7b66 LT |
630 | * the future! |
631 | */ | |
66f13625 | 632 | if ((p->status == 'R' || p->status == 'C') && |
61588ccf | 633 | !strcmp(p->two->path, opt->pathspec.items[0].match)) { |
9a087274 NTND |
634 | const char *path[2]; |
635 | ||
9f38e1ef LT |
636 | /* Switch the file-pairs around */ |
637 | q->queue[i] = choice; | |
638 | choice = p; | |
639 | ||
640 | /* Update the path we use from now on.. */ | |
9a087274 NTND |
641 | path[0] = p->one->path; |
642 | path[1] = NULL; | |
ed6e8038 | 643 | clear_pathspec(&opt->pathspec); |
4a2d5ae2 NTND |
644 | parse_pathspec(&opt->pathspec, |
645 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
646 | PATHSPEC_LITERAL_PATH, "", path); | |
44c48a90 JH |
647 | |
648 | /* | |
649 | * The caller expects us to return a set of vanilla | |
650 | * filepairs to let a later call to diffcore_std() | |
651 | * it makes to sort the renames out (among other | |
652 | * things), but we already have found renames | |
653 | * ourselves; signal diffcore_std() not to muck with | |
654 | * rename information. | |
655 | */ | |
656 | opt->found_follow = 1; | |
750f7b66 LT |
657 | break; |
658 | } | |
659 | } | |
660 | ||
661 | /* | |
3ea3c215 | 662 | * Then, discard all the non-relevant file pairs... |
9f38e1ef LT |
663 | */ |
664 | for (i = 0; i < q->nr; i++) { | |
665 | struct diff_filepair *p = q->queue[i]; | |
666 | diff_free_filepair(p); | |
667 | } | |
668 | ||
669 | /* | |
670 | * .. and re-instate the one we want (which might be either the | |
671 | * original one, or the rename/copy we found) | |
750f7b66 | 672 | */ |
9f38e1ef LT |
673 | q->queue[0] = choice; |
674 | q->nr = 1; | |
750f7b66 LT |
675 | } |
676 | ||
72441af7 KS |
677 | static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new, |
678 | struct strbuf *base, struct diff_options *opt) | |
679 | { | |
680 | struct combine_diff_path phead, *p; | |
681 | pathchange_fn_t pathchange_old = opt->pathchange; | |
682 | ||
683 | phead.next = NULL; | |
684 | opt->pathchange = emit_diff_first_parent_only; | |
685 | diff_tree_paths(&phead, new, &old, 1, base, opt); | |
686 | ||
687 | for (p = phead.next; p;) { | |
688 | struct combine_diff_path *pprev = p; | |
689 | p = p->next; | |
690 | free(pprev); | |
691 | } | |
692 | ||
693 | opt->pathchange = pathchange_old; | |
694 | return 0; | |
695 | } | |
696 | ||
12cd8174 | 697 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base_str, struct diff_options *opt) |
ac1b3d12 | 698 | { |
12cd8174 | 699 | struct strbuf base; |
ac1b3d12 LT |
700 | int retval; |
701 | ||
12cd8174 KS |
702 | strbuf_init(&base, PATH_MAX); |
703 | strbuf_addstr(&base, base_str); | |
704 | ||
705 | retval = ll_diff_tree_sha1(old, new, &base, opt); | |
706 | if (!*base_str && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) | |
707 | try_to_follow_renames(old, new, &base, opt); | |
708 | ||
709 | strbuf_release(&base); | |
52894e70 | 710 | |
ac1b3d12 LT |
711 | return retval; |
712 | } | |
713 | ||
2b60356d RS |
714 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
715 | { | |
0b707c33 | 716 | return diff_tree_sha1(NULL, new, base, opt); |
2b60356d | 717 | } |