2 * Helper functions for tree diff generation
10 * internal mode marker, saying a tree entry != entry of tp[imin]
11 * (see ll_diff_tree_paths for what it means there)
13 * we will update/use/emit entry for diff only with it unset.
15 #define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ
17 #define FAST_ARRAY_ALLOC(x, nr) do { \
19 (x) = xalloca((nr) * sizeof(*(x))); \
21 ALLOC_ARRAY((x), nr); \
23 #define FAST_ARRAY_FREE(x, nr) do { \
30 static struct combine_diff_path
*ll_diff_tree_paths(
31 struct combine_diff_path
*p
, const struct object_id
*oid
,
32 const struct object_id
**parents_oid
, int nparent
,
33 struct strbuf
*base
, struct diff_options
*opt
);
34 static 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
);
39 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
40 * but not their sha1's.
42 * NOTE files and directories *always* compare differently, even when having
43 * the same name - thanks to base_name_compare().
45 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
46 * so that they sort *after* valid tree entries.
48 * Due to this convention, if trees are scanned in sorted order, all
49 * non-empty descriptors will be processed first.
51 static int tree_entry_pathcmp(struct tree_desc
*t1
, struct tree_desc
*t2
)
53 struct name_entry
*e1
, *e2
;
56 /* empty descriptors sort after valid tree entries */
58 return t2
->size
? 1 : 0;
64 cmp
= base_name_compare(e1
->path
, tree_entry_len(e1
), e1
->mode
,
65 e2
->path
, tree_entry_len(e2
), e2
->mode
);
71 * convert path -> opt->diff_*() callbacks
73 * emits diff to first parent only, and tells diff tree-walker that we are done
74 * with p and it can be freed.
76 static int emit_diff_first_parent_only(struct diff_options
*opt
, struct combine_diff_path
*p
)
78 struct combine_diff_parent
*p0
= &p
->parent
[0];
79 if (p
->mode
&& p0
->mode
) {
80 opt
->change(opt
, p0
->mode
, p
->mode
, &p0
->oid
, &p
->oid
,
84 const struct object_id
*oid
;
98 opt
->add_remove(opt
, addremove
, mode
, oid
, 1, p
->path
, 0);
101 return 0; /* we are done with p */
106 * Make a new combine_diff_path from path/mode/sha1
107 * and append it to paths list tail.
109 * Memory for created elements could be reused:
111 * - if last->next == NULL, the memory is allocated;
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.
119 * - if you do need to keep the element
121 * p = path_appendnew(p, ...);
125 * - if you don't need to keep the element after processing
128 * p = path_appendnew(p, ...);
131 * ; don't forget to free tail->next in the end
133 * p->parent[] remains uninitialized.
135 static struct combine_diff_path
*path_appendnew(struct combine_diff_path
*last
,
136 int nparent
, const struct strbuf
*base
, const char *path
, int pathlen
,
137 unsigned mode
, const struct object_id
*oid
)
139 struct combine_diff_path
*p
;
140 size_t len
= st_add(base
->len
, pathlen
);
141 size_t alloclen
= combine_diff_path_size(nparent
, len
);
143 /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
145 if (p
&& (alloclen
> (intptr_t)p
->next
)) {
150 p
= xmalloc(alloclen
);
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).
156 p
->next
= (struct combine_diff_path
*)(intptr_t)alloclen
;
161 p
->path
= (char *)&(p
->parent
[nparent
]);
162 memcpy(p
->path
, base
->buf
, base
->len
);
163 memcpy(p
->path
+ base
->len
, path
, pathlen
);
166 oidcpy(&p
->oid
, oid
? oid
: null_oid());
172 * new path should be added to combine diff
174 * 3 cases on how/when it should be called and behaves:
176 * 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)
181 static 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
,
188 const struct object_id
*oid
;
190 int old_baselen
= base
->len
;
191 int i
, isdir
, recurse
= 0, emitthis
= 1;
193 /* at least something has to be valid */
197 /* path present in resulting tree */
198 oid
= tree_entry_extract(t
, &path
, &mode
);
199 pathlen
= tree_entry_len(&t
->entry
);
200 isdir
= S_ISDIR(mode
);
203 * a path was removed - take path from imin parent. Also take
204 * mode from that parent, to decide on recursion(1).
206 * 1) all modes for tp[i]=tp[imin] should be the same wrt
207 * S_ISDIR, thanks to base_name_compare().
209 tree_entry_extract(&tp
[imin
], &path
, &mode
);
210 pathlen
= tree_entry_len(&tp
[imin
].entry
);
212 isdir
= S_ISDIR(mode
);
217 if (opt
->flags
.recursive
&& isdir
) {
219 emitthis
= opt
->flags
.tree_in_recursive
;
224 struct combine_diff_path
*pprev
= p
;
225 p
= path_appendnew(p
, nparent
, base
, path
, pathlen
, mode
, oid
);
227 for (i
= 0; i
< nparent
; ++i
) {
229 * tp[i] is valid, if present and if tp[i]==tp[imin] -
230 * otherwise, we should ignore it.
232 int tpi_valid
= tp
&& !(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
);
234 const struct object_id
*oid_i
;
237 p
->parent
[i
].status
=
238 !t
? DIFF_STATUS_DELETED
:
240 DIFF_STATUS_MODIFIED
:
244 oid_i
= &tp
[i
].entry
.oid
;
245 mode_i
= tp
[i
].entry
.mode
;
252 p
->parent
[i
].mode
= mode_i
;
253 oidcpy(&p
->parent
[i
].oid
, oid_i
);
258 keep
= opt
->pathchange(opt
, p
);
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.
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.
269 * see path_appendnew() for details.
278 const struct object_id
**parents_oid
;
280 FAST_ARRAY_ALLOC(parents_oid
, nparent
);
281 for (i
= 0; i
< nparent
; ++i
) {
282 /* same rule as in emitthis */
283 int tpi_valid
= tp
&& !(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
);
285 parents_oid
[i
] = tpi_valid
? &tp
[i
].entry
.oid
: NULL
;
288 strbuf_add(base
, path
, pathlen
);
289 strbuf_addch(base
, '/');
290 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
);
291 FAST_ARRAY_FREE(parents_oid
, nparent
);
294 strbuf_setlen(base
, old_baselen
);
298 static void skip_uninteresting(struct tree_desc
*t
, struct strbuf
*base
,
299 struct diff_options
*opt
)
301 enum interesting match
;
304 match
= tree_entry_interesting(opt
->repo
->index
, &t
->entry
,
305 base
, 0, &opt
->pathspec
);
307 if (match
== all_entries_not_interesting
)
311 update_tree_entry(t
);
317 * generate paths for combined diff D(sha1,parents_oid[])
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.
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:
328 * D(T,P1...Pn) calculation scheme
329 * -------------------------------
331 * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set)
333 * D(T,Pj) - diff between T..Pj
334 * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn
337 * We start from all trees, which are sorted, and compare their entries in
343 * |-| |--| ... |--| imin = argmin(p1...pn)
350 * at any time there could be 3 cases:
356 * Schematic deduction of what every case means, and what to do, follows:
358 * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓
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↓
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)
373 * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø ->
375 * ⎧δ(t,pi) - if pi=p[imin]
377 * ⎩"+t" - if pi>p[imin]
380 * in any case t↓ ∀ pi=p[imin] pi↓
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
391 * Please keep attention to the common D(A,[B]) case when working on the
392 * code, in order not to slow it down.
395 * nparent must be > 0.
399 /* ∀ pi=p[imin] pi↓ */
400 static inline void update_tp_entries(struct tree_desc
*tp
, int nparent
)
403 for (i
= 0; i
< nparent
; ++i
)
404 if (!(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
))
405 update_tree_entry(&tp
[i
]);
408 static struct combine_diff_path
*ll_diff_tree_paths(
409 struct combine_diff_path
*p
, const struct object_id
*oid
,
410 const struct object_id
**parents_oid
, int nparent
,
411 struct strbuf
*base
, struct diff_options
*opt
)
413 struct tree_desc t
, *tp
;
414 void *ttree
, **tptree
;
417 FAST_ARRAY_ALLOC(tp
, nparent
);
418 FAST_ARRAY_ALLOC(tptree
, nparent
);
421 * load parents first, as they are probably already cached.
423 * ( log_tree_diff() parses commit->parent before calling here via
424 * diff_tree_oid(parent, commit) )
426 for (i
= 0; i
< nparent
; ++i
)
427 tptree
[i
] = fill_tree_descriptor(opt
->repo
, &tp
[i
], parents_oid
[i
]);
428 ttree
= fill_tree_descriptor(opt
->repo
, &t
, oid
);
430 /* Enable recursion indefinitely */
431 opt
->pathspec
.recursive
= opt
->flags
.recursive
;
436 if (diff_can_quit_early(opt
))
439 if (opt
->max_changes
&& diff_queued_diff
.nr
> opt
->max_changes
)
442 if (opt
->pathspec
.nr
) {
443 skip_uninteresting(&t
, base
, opt
);
444 for (i
= 0; i
< nparent
; i
++)
445 skip_uninteresting(&tp
[i
], base
, opt
);
448 /* comparing is finished when all trees are done */
451 for (i
= 0; i
< nparent
; ++i
)
461 * lookup imin = argmin(p1...pn),
462 * mark entries whether they =p[imin] along the way
465 tp
[0].entry
.mode
&= ~S_IFXMIN_NEQ
;
467 for (i
= 1; i
< nparent
; ++i
) {
468 cmp
= tree_entry_pathcmp(&tp
[i
], &tp
[imin
]);
471 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
474 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
477 tp
[i
].entry
.mode
|= S_IFXMIN_NEQ
;
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] */
487 /* compare t vs p[imin] */
488 cmp
= tree_entry_pathcmp(&t
, &tp
[imin
]);
492 /* are either pi > p[imin] or diff(t,pi) != ø ? */
493 if (!opt
->flags
.find_copies_harder
) {
494 for (i
= 0; i
< nparent
; ++i
) {
496 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
499 /* diff(t,pi) != ø */
500 if (!oideq(&t
.entry
.oid
, &tp
[i
].entry
.oid
) ||
501 (t
.entry
.mode
!= tp
[i
].entry
.mode
))
508 /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */
509 p
= emit_path(p
, base
, opt
, nparent
,
513 /* t↓, ∀ pi=p[imin] pi↓ */
514 update_tree_entry(&t
);
515 update_tp_entries(tp
, nparent
);
521 p
= emit_path(p
, base
, opt
, nparent
,
522 &t
, /*tp=*/NULL
, -1);
525 update_tree_entry(&t
);
530 /* ∀i pi=p[imin] -> D += "-p[imin]" */
531 if (!opt
->flags
.find_copies_harder
) {
532 for (i
= 0; i
< nparent
; ++i
)
533 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
537 p
= emit_path(p
, base
, opt
, nparent
,
538 /*t=*/NULL
, tp
, imin
);
541 /* ∀ pi=p[imin] pi↓ */
542 update_tp_entries(tp
, nparent
);
547 for (i
= nparent
-1; i
>= 0; i
--)
549 FAST_ARRAY_FREE(tptree
, nparent
);
550 FAST_ARRAY_FREE(tp
, nparent
);
555 struct combine_diff_path
*diff_tree_paths(
556 struct combine_diff_path
*p
, const struct object_id
*oid
,
557 const struct object_id
**parents_oid
, int nparent
,
558 struct strbuf
*base
, struct diff_options
*opt
)
560 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
);
563 * free pre-allocated last element, if any
564 * (see path_appendnew() for details about why)
566 FREE_AND_NULL(p
->next
);
572 * Does it look like the resulting diff might be due to a rename?
574 * - not a valid previous file
576 static inline int diff_might_be_rename(void)
578 return diff_queued_diff
.nr
== 1 &&
579 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
582 static 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
)
586 struct diff_options diff_opts
;
587 struct diff_queue_struct
*q
= &diff_queued_diff
;
588 struct diff_filepair
*choice
;
592 * follow-rename code is very specific, we need exactly one
593 * path. Magic that matches more than one path is not
596 GUARD_PATHSPEC(&opt
->pathspec
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
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.
605 if (opt
->pathspec
.has_wildcard
)
606 BUG("wildcards are not supported");
609 /* Remove the file creation entry from the diff queue, and remember it */
610 choice
= q
->queue
[0];
613 repo_diff_setup(opt
->repo
, &diff_opts
);
614 diff_opts
.flags
.recursive
= 1;
615 diff_opts
.flags
.find_copies_harder
= 1;
616 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
617 diff_opts
.single_follow
= opt
->pathspec
.items
[0].match
;
618 diff_opts
.break_opt
= opt
->break_opt
;
619 diff_opts
.rename_score
= opt
->rename_score
;
620 diff_setup_done(&diff_opts
);
621 ll_diff_tree_oid(old_oid
, new_oid
, base
, &diff_opts
);
622 diffcore_std(&diff_opts
);
623 clear_pathspec(&diff_opts
.pathspec
);
625 /* Go through the new set of filepairing, and see if we find a more interesting one */
626 opt
->found_follow
= 0;
627 for (i
= 0; i
< q
->nr
; i
++) {
628 struct diff_filepair
*p
= q
->queue
[i
];
631 * Found a source? Not only do we use that for the new
632 * diff_queued_diff, we will also use that as the path in
635 if ((p
->status
== 'R' || p
->status
== 'C') &&
636 !strcmp(p
->two
->path
, opt
->pathspec
.items
[0].match
)) {
639 /* Switch the file-pairs around */
640 q
->queue
[i
] = choice
;
643 /* Update the path we use from now on.. */
644 path
[0] = p
->one
->path
;
646 clear_pathspec(&opt
->pathspec
);
647 parse_pathspec(&opt
->pathspec
,
648 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
649 PATHSPEC_LITERAL_PATH
, "", path
);
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.
659 opt
->found_follow
= 1;
665 * Then, discard all the non-relevant file pairs...
667 for (i
= 0; i
< q
->nr
; i
++) {
668 struct diff_filepair
*p
= q
->queue
[i
];
669 diff_free_filepair(p
);
673 * .. and re-instate the one we want (which might be either the
674 * original one, or the rename/copy we found)
676 q
->queue
[0] = choice
;
680 static 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
)
684 struct combine_diff_path phead
, *p
;
685 pathchange_fn_t pathchange_old
= opt
->pathchange
;
688 opt
->pathchange
= emit_diff_first_parent_only
;
689 diff_tree_paths(&phead
, new_oid
, &old_oid
, 1, base
, opt
);
691 for (p
= phead
.next
; p
;) {
692 struct combine_diff_path
*pprev
= p
;
697 opt
->pathchange
= pathchange_old
;
700 void 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
)
706 strbuf_init(&base
, PATH_MAX
);
707 strbuf_addstr(&base
, base_str
);
709 ll_diff_tree_oid(old_oid
, new_oid
, &base
, opt
);
710 if (!*base_str
&& opt
->flags
.follow_renames
&& diff_might_be_rename())
711 try_to_follow_renames(old_oid
, new_oid
, &base
, opt
);
713 strbuf_release(&base
);
716 void diff_root_tree_oid(const struct object_id
*new_oid
,
718 struct diff_options
*opt
)
720 diff_tree_oid(NULL
, new_oid
, base
, opt
);