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