blob: 69031d7cbae6a88c1fca3242e974d9cd435a4c78 [file] [log] [blame]
Linus Torvaldsac1b3d12005-10-20 21:05:05 -07001/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
Linus Torvalds750f7b62007-06-19 14:22:46 -07006#include "diffcore.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02007#include "tree.h"
Linus Torvaldsac1b3d12005-10-20 21:05:05 -07008
Kirill Smelkov72441af2014-04-07 01:46:26 +04009/*
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
Kirill Smelkovb9081a62014-03-27 18:21:29 +040016
Jeff Kingb8ba4122016-06-07 18:53:00 -040017#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 { \
Carlo Marcelo Arenas Belón637799b2021-09-16 01:55:22 -070024 if ((nr) <= 2) \
25 xalloca_free((x)); \
26 else \
Jeff Kingb8ba4122016-06-07 18:53:00 -040027 free((x)); \
28} while(0)
Kirill Smelkov72441af2014-04-07 01:46:26 +040029
30static struct combine_diff_path *ll_diff_tree_paths(
Brandon Williamsfda94b42017-05-30 10:31:06 -070031 struct combine_diff_path *p, const struct object_id *oid,
32 const struct object_id **parents_oid, int nparent,
Kirill Smelkov72441af2014-04-07 01:46:26 +040033 struct strbuf *base, struct diff_options *opt);
SZEDER Gábor0ee3cb82020-06-05 13:00:27 +000034static void ll_diff_tree_oid(const struct object_id *old_oid,
35 const struct object_id *new_oid,
36 struct strbuf *base, struct diff_options *opt);
Kirill Smelkovb9081a62014-03-27 18:21:29 +040037
Kirill Smelkov9bc06192014-02-24 20:21:41 +040038/*
39 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
40 * but not their sha1's.
41 *
42 * NOTE files and directories *always* compare differently, even when having
43 * the same name - thanks to base_name_compare().
Kirill Smelkov6ca844e2014-02-24 20:21:44 +040044 *
45 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
46 * so that they sort *after* valid tree entries.
47 *
48 * Due to this convention, if trees are scanned in sorted order, all
49 * non-empty descriptors will be processed first.
Kirill Smelkov9bc06192014-02-24 20:21:41 +040050 */
51static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070052{
Kirill Smelkov1a27a152014-02-24 20:21:43 +040053 struct name_entry *e1, *e2;
54 int cmp;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070055
Kirill Smelkov6ca844e2014-02-24 20:21:44 +040056 /* empty descriptors sort after valid tree entries */
57 if (!t1->size)
58 return t2->size ? 1 : 0;
59 else if (!t2->size)
60 return -1;
61
Kirill Smelkov1a27a152014-02-24 20:21:43 +040062 e1 = &t1->entry;
63 e2 = &t2->entry;
64 cmp = base_name_compare(e1->path, tree_entry_len(e1), e1->mode,
65 e2->path, tree_entry_len(e2), e2->mode);
Kirill Smelkov903bba62014-02-24 20:21:40 +040066 return cmp;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070067}
68
Kirill Smelkovd00e9802014-02-24 20:21:38 +040069
Kirill Smelkov72441af2014-04-07 01:46:26 +040070/*
71 * convert path -> opt->diff_*() callbacks
72 *
73 * emits diff to first parent only, and tells diff tree-walker that we are done
74 * with p and it can be freed.
75 */
76static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_diff_path *p)
Kirill Smelkovd00e9802014-02-24 20:21:38 +040077{
Kirill Smelkov72441af2014-04-07 01:46:26 +040078 struct combine_diff_parent *p0 = &p->parent[0];
79 if (p->mode && p0->mode) {
Brandon Williams94a00972017-05-30 10:30:49 -070080 opt->change(opt, p0->mode, p->mode, &p0->oid, &p->oid,
Kirill Smelkov72441af2014-04-07 01:46:26 +040081 1, 1, p->path, 0, 0);
Kirill Smelkovd00e9802014-02-24 20:21:38 +040082 }
83 else {
Brandon Williamsc26022e2017-05-30 10:30:47 -070084 const struct object_id *oid;
Kirill Smelkovd00e9802014-02-24 20:21:38 +040085 unsigned int mode;
86 int addremove;
87
Kirill Smelkov72441af2014-04-07 01:46:26 +040088 if (p->mode) {
Kirill Smelkovd00e9802014-02-24 20:21:38 +040089 addremove = '+';
Brandon Williamsc26022e2017-05-30 10:30:47 -070090 oid = &p->oid;
Kirill Smelkov72441af2014-04-07 01:46:26 +040091 mode = p->mode;
Kirill Smelkovd00e9802014-02-24 20:21:38 +040092 } else {
93 addremove = '-';
Brandon Williamsc26022e2017-05-30 10:30:47 -070094 oid = &p0->oid;
Kirill Smelkov72441af2014-04-07 01:46:26 +040095 mode = p0->mode;
Kirill Smelkovd00e9802014-02-24 20:21:38 +040096 }
97
Brandon Williamsc26022e2017-05-30 10:30:47 -070098 opt->add_remove(opt, addremove, mode, oid, 1, p->path, 0);
Kirill Smelkovd00e9802014-02-24 20:21:38 +040099 }
Kirill Smelkov72441af2014-04-07 01:46:26 +0400100
101 return 0; /* we are done with p */
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400102}
103
104
Kirill Smelkov72441af2014-04-07 01:46:26 +0400105/*
106 * Make a new combine_diff_path from path/mode/sha1
107 * and append it to paths list tail.
108 *
109 * Memory for created elements could be reused:
110 *
111 * - if last->next == NULL, the memory is allocated;
112 *
113 * - if last->next != NULL, it is assumed that p=last->next was returned
114 * earlier by this function, and p->next was *not* modified.
115 * The memory is then reused from p.
116 *
117 * so for clients,
118 *
119 * - if you do need to keep the element
120 *
121 * p = path_appendnew(p, ...);
122 * process(p);
123 * p->next = NULL;
124 *
125 * - if you don't need to keep the element after processing
126 *
127 * pprev = p;
128 * p = path_appendnew(p, ...);
129 * process(p);
130 * p = pprev;
131 * ; don't forget to free tail->next in the end
132 *
133 * p->parent[] remains uninitialized.
134 */
135static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
136 int nparent, const struct strbuf *base, const char *path, int pathlen,
Brandon Williams0e724622017-05-30 10:31:07 -0700137 unsigned mode, const struct object_id *oid)
Kirill Smelkov72441af2014-04-07 01:46:26 +0400138{
139 struct combine_diff_path *p;
Jeff King5b442c42016-02-19 06:21:30 -0500140 size_t len = st_add(base->len, pathlen);
141 size_t alloclen = combine_diff_path_size(nparent, len);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400142
143 /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
144 p = last->next;
145 if (p && (alloclen > (intptr_t)p->next)) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000146 FREE_AND_NULL(p);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400147 }
148
149 if (!p) {
150 p = xmalloc(alloclen);
151
152 /*
153 * until we go to it next round, .next holds how many bytes we
154 * allocated (for faster realloc - we don't need copying old data).
155 */
156 p->next = (struct combine_diff_path *)(intptr_t)alloclen;
157 }
158
159 last->next = p;
160
161 p->path = (char *)&(p->parent[nparent]);
162 memcpy(p->path, base->buf, base->len);
163 memcpy(p->path + base->len, path, pathlen);
164 p->path[len] = 0;
165 p->mode = mode;
brian m. carlson14228442021-04-26 01:02:56 +0000166 oidcpy(&p->oid, oid ? oid : null_oid());
Kirill Smelkov72441af2014-04-07 01:46:26 +0400167
168 return p;
169}
170
171/*
172 * new path should be added to combine diff
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400173 *
174 * 3 cases on how/when it should be called and behaves:
175 *
Kirill Smelkov72441af2014-04-07 01:46:26 +0400176 * t, !tp -> path added, all parents lack it
177 * !t, tp -> path removed from all parents
178 * t, tp -> path modified/added
179 * (M for tp[i]=tp[imin], A otherwise)
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400180 */
Kirill Smelkov72441af2014-04-07 01:46:26 +0400181static struct combine_diff_path *emit_path(struct combine_diff_path *p,
182 struct strbuf *base, struct diff_options *opt, int nparent,
183 struct tree_desc *t, struct tree_desc *tp,
184 int imin)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700185{
Elijah Newren5ec1e722019-04-05 08:00:12 -0700186 unsigned short mode;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700187 const char *path;
Brandon Williamsfda94b42017-05-30 10:31:06 -0700188 const struct object_id *oid;
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400189 int pathlen;
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700190 int old_baselen = base->len;
Kirill Smelkov72441af2014-04-07 01:46:26 +0400191 int i, isdir, recurse = 0, emitthis = 1;
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400192
193 /* at least something has to be valid */
Kirill Smelkov72441af2014-04-07 01:46:26 +0400194 assert(t || tp);
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400195
Kirill Smelkov72441af2014-04-07 01:46:26 +0400196 if (t) {
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400197 /* path present in resulting tree */
Brandon Williamsfda94b42017-05-30 10:31:06 -0700198 oid = tree_entry_extract(t, &path, &mode);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400199 pathlen = tree_entry_len(&t->entry);
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400200 isdir = S_ISDIR(mode);
201 } else {
202 /*
Kirill Smelkov72441af2014-04-07 01:46:26 +0400203 * a path was removed - take path from imin parent. Also take
204 * mode from that parent, to decide on recursion(1).
205 *
206 * 1) all modes for tp[i]=tp[imin] should be the same wrt
207 * S_ISDIR, thanks to base_name_compare().
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400208 */
Kirill Smelkov72441af2014-04-07 01:46:26 +0400209 tree_entry_extract(&tp[imin], &path, &mode);
210 pathlen = tree_entry_len(&tp[imin].entry);
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400211
212 isdir = S_ISDIR(mode);
Brandon Williamsfda94b42017-05-30 10:31:06 -0700213 oid = NULL;
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400214 mode = 0;
215 }
216
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700217 if (opt->flags.recursive && isdir) {
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400218 recurse = 1;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700219 emitthis = opt->flags.tree_in_recursive;
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400220 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700221
Kirill Smelkov72441af2014-04-07 01:46:26 +0400222 if (emitthis) {
223 int keep;
224 struct combine_diff_path *pprev = p;
Brandon Williams0e724622017-05-30 10:31:07 -0700225 p = path_appendnew(p, nparent, base, path, pathlen, mode, oid);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700226
Kirill Smelkov72441af2014-04-07 01:46:26 +0400227 for (i = 0; i < nparent; ++i) {
228 /*
229 * tp[i] is valid, if present and if tp[i]==tp[imin] -
230 * otherwise, we should ignore it.
231 */
232 int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
233
Brandon Williamsfda94b42017-05-30 10:31:06 -0700234 const struct object_id *oid_i;
Kirill Smelkov72441af2014-04-07 01:46:26 +0400235 unsigned mode_i;
236
237 p->parent[i].status =
238 !t ? DIFF_STATUS_DELETED :
239 tpi_valid ?
240 DIFF_STATUS_MODIFIED :
241 DIFF_STATUS_ADDED;
242
243 if (tpi_valid) {
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000244 oid_i = &tp[i].entry.oid;
Kirill Smelkov72441af2014-04-07 01:46:26 +0400245 mode_i = tp[i].entry.mode;
246 }
247 else {
brian m. carlson14228442021-04-26 01:02:56 +0000248 oid_i = null_oid();
Kirill Smelkov72441af2014-04-07 01:46:26 +0400249 mode_i = 0;
250 }
251
252 p->parent[i].mode = mode_i;
Brandon Williamsfda94b42017-05-30 10:31:06 -0700253 oidcpy(&p->parent[i].oid, oid_i);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400254 }
255
256 keep = 1;
257 if (opt->pathchange)
258 keep = opt->pathchange(opt, p);
259
260 /*
261 * If a path was filtered or consumed - we don't need to add it
262 * to the list and can reuse its memory, leaving it as
263 * pre-allocated element on the tail.
264 *
265 * On the other hand, if path needs to be kept, we need to
266 * correct its .next to NULL, as it was pre-initialized to how
267 * much memory was allocated.
268 *
269 * see path_appendnew() for details.
270 */
271 if (!keep)
272 p = pprev;
273 else
274 p->next = NULL;
275 }
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400276
277 if (recurse) {
Brandon Williamsfda94b42017-05-30 10:31:06 -0700278 const struct object_id **parents_oid;
Kirill Smelkov72441af2014-04-07 01:46:26 +0400279
Brandon Williamsfda94b42017-05-30 10:31:06 -0700280 FAST_ARRAY_ALLOC(parents_oid, nparent);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400281 for (i = 0; i < nparent; ++i) {
282 /* same rule as in emitthis */
283 int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
284
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000285 parents_oid[i] = tpi_valid ? &tp[i].entry.oid : NULL;
Kirill Smelkov72441af2014-04-07 01:46:26 +0400286 }
287
288 strbuf_add(base, path, pathlen);
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700289 strbuf_addch(base, '/');
Brandon Williamsfda94b42017-05-30 10:31:06 -0700290 p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt);
291 FAST_ARRAY_FREE(parents_oid, nparent);
Kirill Smelkovd00e9802014-02-24 20:21:38 +0400292 }
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700293
294 strbuf_setlen(base, old_baselen);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400295 return p;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700296}
297
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700298static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
Kirill Smelkove9066122014-02-03 16:47:18 +0400299 struct diff_options *opt)
Linus Torvalds5d865012007-03-18 15:18:30 -0700300{
Kirill Smelkove9066122014-02-03 16:47:18 +0400301 enum interesting match;
302
Linus Torvalds5d865012007-03-18 15:18:30 -0700303 while (t->size) {
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100304 match = tree_entry_interesting(opt->repo->index, &t->entry,
305 base, 0, &opt->pathspec);
Kirill Smelkove9066122014-02-03 16:47:18 +0400306 if (match) {
307 if (match == all_entries_not_interesting)
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +0700308 t->size = 0;
309 break;
Linus Torvalds5d865012007-03-18 15:18:30 -0700310 }
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +0700311 update_tree_entry(t);
Linus Torvalds5d865012007-03-18 15:18:30 -0700312 }
313}
314
Linus Torvalds304de2d2007-03-17 20:06:24 -0700315
Kirill Smelkov72441af2014-04-07 01:46:26 +0400316/*
Brandon Williamsfda94b42017-05-30 10:31:06 -0700317 * generate paths for combined diff D(sha1,parents_oid[])
Kirill Smelkov72441af2014-04-07 01:46:26 +0400318 *
319 * Resulting paths are appended to combine_diff_path linked list, and also, are
320 * emitted on the go via opt->pathchange() callback, so it is possible to
321 * process the result as batch or incrementally.
322 *
323 * The paths are generated scanning new tree and all parents trees
324 * simultaneously, similarly to what diff_tree() was doing for 2 trees.
325 * The theory behind such scan is as follows:
326 *
327 *
328 * D(T,P1...Pn) calculation scheme
329 * -------------------------------
330 *
331 * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set)
332 *
333 * D(T,Pj) - diff between T..Pj
334 * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn
335 *
336 *
337 * We start from all trees, which are sorted, and compare their entries in
338 * lock-step:
339 *
340 * T P1 Pn
341 * - - -
342 * |t| |p1| |pn|
343 * |-| |--| ... |--| imin = argmin(p1...pn)
344 * | | | | | |
345 * |-| |--| |--|
346 * |.| |. | |. |
347 * . . .
348 * . . .
349 *
350 * at any time there could be 3 cases:
351 *
352 * 1) t < p[imin];
353 * 2) t > p[imin];
354 * 3) t = p[imin].
355 *
356 * Schematic deduction of what every case means, and what to do, follows:
357 *
358 * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓
359 *
360 * 2) t > p[imin]
361 *
362 * 2.1) ∃j: pj > p[imin] -> "-p[imin]" ∉ D(T,Pj) -> D += ø; ∀ pi=p[imin] pi↓
363 * 2.2) ∀i pi = p[imin] -> pi ∉ T -> "-pi" ∈ D(T,Pi) -> D += "-p[imin]"; ∀i pi↓
364 *
365 * 3) t = p[imin]
366 *
367 * 3.1) ∃j: pj > p[imin] -> "+t" ∈ D(T,Pj) -> only pi=p[imin] remains to investigate
368 * 3.2) pi = p[imin] -> investigate δ(t,pi)
369 * |
370 * |
371 * v
372 *
373 * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø ->
374 *
375 * ⎧δ(t,pi) - if pi=p[imin]
376 * -> D += ⎨
377 * ⎩"+t" - if pi>p[imin]
378 *
379 *
380 * in any case t↓ ∀ pi=p[imin] pi↓
381 *
382 *
383 * ~~~~~~~~
384 *
385 * NOTE
386 *
387 * Usual diff D(A,B) is by definition the same as combined diff D(A,[B]),
388 * so this diff paths generator can, and is used, for plain diffs
389 * generation too.
390 *
391 * Please keep attention to the common D(A,[B]) case when working on the
392 * code, in order not to slow it down.
393 *
394 * NOTE
395 * nparent must be > 0.
396 */
397
398
399/* ∀ pi=p[imin] pi↓ */
400static inline void update_tp_entries(struct tree_desc *tp, int nparent)
401{
402 int i;
403 for (i = 0; i < nparent; ++i)
404 if (!(tp[i].entry.mode & S_IFXMIN_NEQ))
405 update_tree_entry(&tp[i]);
406}
407
408static struct combine_diff_path *ll_diff_tree_paths(
Brandon Williamsfda94b42017-05-30 10:31:06 -0700409 struct combine_diff_path *p, const struct object_id *oid,
410 const struct object_id **parents_oid, int nparent,
Kirill Smelkov72441af2014-04-07 01:46:26 +0400411 struct strbuf *base, struct diff_options *opt)
412{
413 struct tree_desc t, *tp;
414 void *ttree, **tptree;
415 int i;
416
Jeff Kingb8ba4122016-06-07 18:53:00 -0400417 FAST_ARRAY_ALLOC(tp, nparent);
418 FAST_ARRAY_ALLOC(tptree, nparent);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400419
420 /*
421 * load parents first, as they are probably already cached.
422 *
423 * ( log_tree_diff() parses commit->parent before calling here via
Brandon Williams66f414f2017-05-30 10:31:03 -0700424 * diff_tree_oid(parent, commit) )
Kirill Smelkov72441af2014-04-07 01:46:26 +0400425 */
426 for (i = 0; i < nparent; ++i)
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700427 tptree[i] = fill_tree_descriptor(opt->repo, &tp[i], parents_oid[i]);
428 ttree = fill_tree_descriptor(opt->repo, &t, oid);
Kirill Smelkov52894e72014-03-27 18:24:38 +0400429
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +0700430 /* Enable recursion indefinitely */
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700431 opt->pathspec.recursive = opt->flags.recursive;
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +0700432
Linus Torvalds5d865012007-03-18 15:18:30 -0700433 for (;;) {
Kirill Smelkov72441af2014-04-07 01:46:26 +0400434 int imin, cmp;
Kirill Smelkov903bba62014-02-24 20:21:40 +0400435
Junio C Hamano28b92642011-05-31 09:14:17 -0700436 if (diff_can_quit_early(opt))
Junio C Hamano822cac02007-03-14 11:12:51 -0700437 break;
Kirill Smelkov72441af2014-04-07 01:46:26 +0400438
Derrick Stoleeb16a8272020-09-16 14:07:52 -0400439 if (opt->max_changes && diff_queued_diff.nr > opt->max_changes)
Derrick Stoleee3696982020-03-30 00:31:27 +0000440 break;
441
Nguyễn Thái Ngọc Duy66f13622010-12-15 22:02:38 +0700442 if (opt->pathspec.nr) {
Kirill Smelkov72441af2014-04-07 01:46:26 +0400443 skip_uninteresting(&t, base, opt);
444 for (i = 0; i < nparent; i++)
445 skip_uninteresting(&tp[i], base, opt);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700446 }
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400447
Kirill Smelkov72441af2014-04-07 01:46:26 +0400448 /* comparing is finished when all trees are done */
449 if (!t.size) {
450 int done = 1;
451 for (i = 0; i < nparent; ++i)
452 if (tp[i].size) {
453 done = 0;
454 break;
455 }
456 if (done)
457 break;
458 }
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400459
Kirill Smelkov72441af2014-04-07 01:46:26 +0400460 /*
461 * lookup imin = argmin(p1...pn),
462 * mark entries whether they =p[imin] along the way
463 */
464 imin = 0;
465 tp[0].entry.mode &= ~S_IFXMIN_NEQ;
466
467 for (i = 1; i < nparent; ++i) {
468 cmp = tree_entry_pathcmp(&tp[i], &tp[imin]);
469 if (cmp < 0) {
470 imin = i;
471 tp[i].entry.mode &= ~S_IFXMIN_NEQ;
472 }
473 else if (cmp == 0) {
474 tp[i].entry.mode &= ~S_IFXMIN_NEQ;
475 }
476 else {
477 tp[i].entry.mode |= S_IFXMIN_NEQ;
478 }
479 }
480
481 /* fixup markings for entries before imin */
482 for (i = 0; i < imin; ++i)
483 tp[i].entry.mode |= S_IFXMIN_NEQ; /* pi > p[imin] */
484
485
486
487 /* compare t vs p[imin] */
488 cmp = tree_entry_pathcmp(&t, &tp[imin]);
489
490 /* t = p[imin] */
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400491 if (cmp == 0) {
Kirill Smelkov72441af2014-04-07 01:46:26 +0400492 /* are either pi > p[imin] or diff(t,pi) != ø ? */
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700493 if (!opt->flags.find_copies_harder) {
Kirill Smelkov72441af2014-04-07 01:46:26 +0400494 for (i = 0; i < nparent; ++i) {
495 /* p[i] > p[imin] */
496 if (tp[i].entry.mode & S_IFXMIN_NEQ)
497 continue;
Kirill Smelkov903bba62014-02-24 20:21:40 +0400498
Kirill Smelkov72441af2014-04-07 01:46:26 +0400499 /* diff(t,pi) != ø */
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000500 if (!oideq(&t.entry.oid, &tp[i].entry.oid) ||
Kirill Smelkov72441af2014-04-07 01:46:26 +0400501 (t.entry.mode != tp[i].entry.mode))
502 continue;
503
504 goto skip_emit_t_tp;
505 }
506 }
507
508 /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */
509 p = emit_path(p, base, opt, nparent,
510 &t, tp, imin);
511
512 skip_emit_t_tp:
513 /* t↓, ∀ pi=p[imin] pi↓ */
514 update_tree_entry(&t);
515 update_tp_entries(tp, nparent);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700516 }
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400517
Kirill Smelkov72441af2014-04-07 01:46:26 +0400518 /* t < p[imin] */
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400519 else if (cmp < 0) {
Kirill Smelkov72441af2014-04-07 01:46:26 +0400520 /* D += "+t" */
521 p = emit_path(p, base, opt, nparent,
522 &t, /*tp=*/NULL, -1);
523
524 /* t↓ */
525 update_tree_entry(&t);
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400526 }
527
Kirill Smelkov72441af2014-04-07 01:46:26 +0400528 /* t > p[imin] */
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400529 else {
Kirill Smelkov72441af2014-04-07 01:46:26 +0400530 /* ∀i pi=p[imin] -> D += "-p[imin]" */
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700531 if (!opt->flags.find_copies_harder) {
Kirill Smelkov72441af2014-04-07 01:46:26 +0400532 for (i = 0; i < nparent; ++i)
533 if (tp[i].entry.mode & S_IFXMIN_NEQ)
534 goto skip_emit_tp;
535 }
536
537 p = emit_path(p, base, opt, nparent,
538 /*t=*/NULL, tp, imin);
539
540 skip_emit_tp:
541 /* ∀ pi=p[imin] pi↓ */
542 update_tp_entries(tp, nparent);
Kirill Smelkov5dfb2bb2014-02-24 20:21:39 +0400543 }
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700544 }
Nguyễn Thái Ngọc Duy48932672010-12-15 22:02:42 +0700545
Kirill Smelkov72441af2014-04-07 01:46:26 +0400546 free(ttree);
547 for (i = nparent-1; i >= 0; i--)
548 free(tptree[i]);
Jeff Kingb8ba4122016-06-07 18:53:00 -0400549 FAST_ARRAY_FREE(tptree, nparent);
550 FAST_ARRAY_FREE(tp, nparent);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400551
552 return p;
553}
554
555struct combine_diff_path *diff_tree_paths(
Brandon Williamsfda94b42017-05-30 10:31:06 -0700556 struct combine_diff_path *p, const struct object_id *oid,
557 const struct object_id **parents_oid, int nparent,
Kirill Smelkov72441af2014-04-07 01:46:26 +0400558 struct strbuf *base, struct diff_options *opt)
559{
Brandon Williamsfda94b42017-05-30 10:31:06 -0700560 p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400561
562 /*
563 * free pre-allocated last element, if any
564 * (see path_appendnew() for details about why)
565 */
Ævar Arnfjörð Bjarmasonce528de2018-08-17 13:02:50 +0000566 FREE_AND_NULL(p->next);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400567
568 return p;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700569}
570
Linus Torvalds750f7b62007-06-19 14:22:46 -0700571/*
572 * Does it look like the resulting diff might be due to a rename?
573 * - single entry
574 * - not a valid previous file
575 */
576static inline int diff_might_be_rename(void)
577{
578 return diff_queued_diff.nr == 1 &&
579 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
580}
581
Brandon Williams128be872017-05-30 10:31:05 -0700582static void try_to_follow_renames(const struct object_id *old_oid,
583 const struct object_id *new_oid,
584 struct strbuf *base, struct diff_options *opt)
Linus Torvalds750f7b62007-06-19 14:22:46 -0700585{
586 struct diff_options diff_opts;
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700587 struct diff_queue_struct *q = &diff_queued_diff;
588 struct diff_filepair *choice;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700589 int i;
590
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +0700591 /*
592 * follow-rename code is very specific, we need exactly one
593 * path. Magic that matches more than one path is not
594 * supported.
595 */
Nguyễn Thái Ngọc Duy5c6933d2013-07-14 15:36:06 +0700596 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +0700597#if 0
598 /*
599 * We should reject wildcards as well. Unfortunately we
600 * haven't got a reliable way to detect that 'foo\*bar' in
601 * fact has no wildcards. nowildcard_len is merely a hint for
602 * optimization. Let it slip for now until wildmatch is taught
603 * about dry-run mode and returns wildcard info.
604 */
605 if (opt->pathspec.has_wildcard)
Ævar Arnfjörð Bjarmasona78537a2021-12-07 12:05:53 +0100606 BUG("wildcards are not supported");
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +0700607#endif
608
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700609 /* Remove the file creation entry from the diff queue, and remember it */
610 choice = q->queue[0];
611 q->nr = 0;
612
Nguyễn Thái Ngọc Duydcf42862018-09-21 17:57:36 +0200613 repo_diff_setup(opt->repo, &diff_opts);
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700614 diff_opts.flags.recursive = 1;
615 diff_opts.flags.find_copies_harder = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700616 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
Nguyễn Thái Ngọc Duy61588cc2013-07-14 15:36:01 +0700617 diff_opts.single_follow = opt->pathspec.items[0].match;
Linus Torvalds6dd4b662007-10-20 12:31:31 -0700618 diff_opts.break_opt = opt->break_opt;
Jeff Kingdd98d882011-12-16 06:27:50 -0500619 diff_opts.rename_score = opt->rename_score;
Thomas Rast28452652012-08-03 14:16:24 +0200620 diff_setup_done(&diff_opts);
Brandon Williamsfda94b42017-05-30 10:31:06 -0700621 ll_diff_tree_oid(old_oid, new_oid, base, &diff_opts);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700622 diffcore_std(&diff_opts);
Junio C Hamanoed6e8032016-06-02 14:09:22 -0700623 clear_pathspec(&diff_opts.pathspec);
Linus Torvalds750f7b62007-06-19 14:22:46 -0700624
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700625 /* Go through the new set of filepairing, and see if we find a more interesting one */
Junio C Hamano44c48a92010-08-13 12:17:45 -0700626 opt->found_follow = 0;
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700627 for (i = 0; i < q->nr; i++) {
628 struct diff_filepair *p = q->queue[i];
Linus Torvalds750f7b62007-06-19 14:22:46 -0700629
630 /*
631 * Found a source? Not only do we use that for the new
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700632 * diff_queued_diff, we will also use that as the path in
Linus Torvalds750f7b62007-06-19 14:22:46 -0700633 * the future!
634 */
Nguyễn Thái Ngọc Duy66f13622010-12-15 22:02:38 +0700635 if ((p->status == 'R' || p->status == 'C') &&
Nguyễn Thái Ngọc Duy61588cc2013-07-14 15:36:01 +0700636 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700637 const char *path[2];
638
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700639 /* Switch the file-pairs around */
640 q->queue[i] = choice;
641 choice = p;
642
643 /* Update the path we use from now on.. */
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700644 path[0] = p->one->path;
645 path[1] = NULL;
Junio C Hamanoed6e8032016-06-02 14:09:22 -0700646 clear_pathspec(&opt->pathspec);
Nguyễn Thái Ngọc Duy4a2d5ae2013-10-26 09:09:20 +0700647 parse_pathspec(&opt->pathspec,
648 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
649 PATHSPEC_LITERAL_PATH, "", path);
Junio C Hamano44c48a92010-08-13 12:17:45 -0700650
651 /*
652 * The caller expects us to return a set of vanilla
653 * filepairs to let a later call to diffcore_std()
654 * it makes to sort the renames out (among other
655 * things), but we already have found renames
656 * ourselves; signal diffcore_std() not to muck with
657 * rename information.
658 */
659 opt->found_follow = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700660 break;
661 }
662 }
663
664 /*
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100665 * Then, discard all the non-relevant file pairs...
Linus Torvalds750f7b62007-06-19 14:22:46 -0700666 */
Linus Torvalds9f38e1e2007-06-21 10:22:59 -0700667 for (i = 0; i < q->nr; i++) {
668 struct diff_filepair *p = q->queue[i];
669 diff_free_filepair(p);
670 }
671
672 /*
673 * .. and re-instate the one we want (which might be either the
674 * original one, or the rename/copy we found)
675 */
676 q->queue[0] = choice;
677 q->nr = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -0700678}
679
SZEDER Gábor0ee3cb82020-06-05 13:00:27 +0000680static void ll_diff_tree_oid(const struct object_id *old_oid,
681 const struct object_id *new_oid,
682 struct strbuf *base, struct diff_options *opt)
Kirill Smelkov72441af2014-04-07 01:46:26 +0400683{
684 struct combine_diff_path phead, *p;
685 pathchange_fn_t pathchange_old = opt->pathchange;
686
687 phead.next = NULL;
688 opt->pathchange = emit_diff_first_parent_only;
Brandon Williamsfda94b42017-05-30 10:31:06 -0700689 diff_tree_paths(&phead, new_oid, &old_oid, 1, base, opt);
Kirill Smelkov72441af2014-04-07 01:46:26 +0400690
691 for (p = phead.next; p;) {
692 struct combine_diff_path *pprev = p;
693 p = p->next;
694 free(pprev);
695 }
696
697 opt->pathchange = pathchange_old;
Kirill Smelkov72441af2014-04-07 01:46:26 +0400698}
699
SZEDER Gábor0ee3cb82020-06-05 13:00:27 +0000700void diff_tree_oid(const struct object_id *old_oid,
701 const struct object_id *new_oid,
702 const char *base_str, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700703{
Kirill Smelkov12cd8172014-03-27 18:22:07 +0400704 struct strbuf base;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700705
Kirill Smelkov12cd8172014-03-27 18:22:07 +0400706 strbuf_init(&base, PATH_MAX);
707 strbuf_addstr(&base, base_str);
708
SZEDER Gábor0ee3cb82020-06-05 13:00:27 +0000709 ll_diff_tree_oid(old_oid, new_oid, &base, opt);
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700710 if (!*base_str && opt->flags.follow_renames && diff_might_be_rename())
Brandon Williams128be872017-05-30 10:31:05 -0700711 try_to_follow_renames(old_oid, new_oid, &base, opt);
Kirill Smelkov12cd8172014-03-27 18:22:07 +0400712
713 strbuf_release(&base);
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700714}
715
SZEDER Gábor0ee3cb82020-06-05 13:00:27 +0000716void diff_root_tree_oid(const struct object_id *new_oid,
717 const char *base,
718 struct diff_options *opt)
Rene Scharfe2b603562006-10-26 18:52:39 +0200719{
SZEDER Gábor0ee3cb82020-06-05 13:00:27 +0000720 diff_tree_oid(NULL, new_oid, base, opt);
Rene Scharfe2b603562006-10-26 18:52:39 +0200721}