2 * Helper functions for tree diff generation
4 #include "git-compat-util.h"
10 #include "environment.h"
13 * Some mode bits are also used internally for computations.
15 * They *must* not overlap with any valid modes, and they *must* not be emitted
16 * to outside world - i.e. appear on disk or network. In other words, it's just
17 * temporary fields, which we internally use, but they have to stay in-house.
19 * ( such approach is valid, as standard S_IF* fits into 16 bits, and in Git
20 * codebase mode is `unsigned int` which is assumed to be at least 32 bits )
23 #define S_DIFFTREE_IFXMIN_NEQ 0x80000000
26 * internal mode marker, saying a tree entry != entry of tp[imin]
27 * (see ll_diff_tree_paths for what it means there)
29 * we will update/use/emit entry for diff only with it unset.
31 #define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ
33 #define FAST_ARRAY_ALLOC(x, nr) do { \
35 (x) = xalloca((nr) * sizeof(*(x))); \
37 ALLOC_ARRAY((x), nr); \
39 #define FAST_ARRAY_FREE(x, nr) do { \
46 static struct combine_diff_path
*ll_diff_tree_paths(
47 struct combine_diff_path
*p
, const struct object_id
*oid
,
48 const struct object_id
**parents_oid
, int nparent
,
49 struct strbuf
*base
, struct diff_options
*opt
,
51 static void ll_diff_tree_oid(const struct object_id
*old_oid
,
52 const struct object_id
*new_oid
,
53 struct strbuf
*base
, struct diff_options
*opt
);
56 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
57 * but not their sha1's.
59 * NOTE files and directories *always* compare differently, even when having
60 * the same name - thanks to base_name_compare().
62 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
63 * so that they sort *after* valid tree entries.
65 * Due to this convention, if trees are scanned in sorted order, all
66 * non-empty descriptors will be processed first.
68 static int tree_entry_pathcmp(struct tree_desc
*t1
, struct tree_desc
*t2
)
70 struct name_entry
*e1
, *e2
;
73 /* empty descriptors sort after valid tree entries */
75 return t2
->size
? 1 : 0;
81 cmp
= base_name_compare(e1
->path
, tree_entry_len(e1
), e1
->mode
,
82 e2
->path
, tree_entry_len(e2
), e2
->mode
);
88 * convert path -> opt->diff_*() callbacks
90 * emits diff to first parent only, and tells diff tree-walker that we are done
91 * with p and it can be freed.
93 static int emit_diff_first_parent_only(struct diff_options
*opt
, struct combine_diff_path
*p
)
95 struct combine_diff_parent
*p0
= &p
->parent
[0];
96 if (p
->mode
&& p0
->mode
) {
97 opt
->change(opt
, p0
->mode
, p
->mode
, &p0
->oid
, &p
->oid
,
101 const struct object_id
*oid
;
115 opt
->add_remove(opt
, addremove
, mode
, oid
, 1, p
->path
, 0);
118 return 0; /* we are done with p */
123 * Make a new combine_diff_path from path/mode/sha1
124 * and append it to paths list tail.
126 * Memory for created elements could be reused:
128 * - if last->next == NULL, the memory is allocated;
130 * - if last->next != NULL, it is assumed that p=last->next was returned
131 * earlier by this function, and p->next was *not* modified.
132 * The memory is then reused from p.
136 * - if you do need to keep the element
138 * p = path_appendnew(p, ...);
142 * - if you don't need to keep the element after processing
145 * p = path_appendnew(p, ...);
148 * ; don't forget to free tail->next in the end
150 * p->parent[] remains uninitialized.
152 static struct combine_diff_path
*path_appendnew(struct combine_diff_path
*last
,
153 int nparent
, const struct strbuf
*base
, const char *path
, int pathlen
,
154 unsigned mode
, const struct object_id
*oid
)
156 struct combine_diff_path
*p
;
157 size_t len
= st_add(base
->len
, pathlen
);
158 size_t alloclen
= combine_diff_path_size(nparent
, len
);
160 /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
162 if (p
&& (alloclen
> (intptr_t)p
->next
)) {
167 p
= xmalloc(alloclen
);
170 * until we go to it next round, .next holds how many bytes we
171 * allocated (for faster realloc - we don't need copying old data).
173 p
->next
= (struct combine_diff_path
*)(intptr_t)alloclen
;
178 p
->path
= (char *)&(p
->parent
[nparent
]);
179 memcpy(p
->path
, base
->buf
, base
->len
);
180 memcpy(p
->path
+ base
->len
, path
, pathlen
);
183 oidcpy(&p
->oid
, oid
? oid
: null_oid());
189 * new path should be added to combine diff
191 * 3 cases on how/when it should be called and behaves:
193 * t, !tp -> path added, all parents lack it
194 * !t, tp -> path removed from all parents
195 * t, tp -> path modified/added
196 * (M for tp[i]=tp[imin], A otherwise)
198 static struct combine_diff_path
*emit_path(struct combine_diff_path
*p
,
199 struct strbuf
*base
, struct diff_options
*opt
, int nparent
,
200 struct tree_desc
*t
, struct tree_desc
*tp
,
205 const struct object_id
*oid
;
207 int old_baselen
= base
->len
;
208 int i
, isdir
, recurse
= 0, emitthis
= 1;
210 /* at least something has to be valid */
214 /* path present in resulting tree */
215 oid
= tree_entry_extract(t
, &path
, &mode
);
216 pathlen
= tree_entry_len(&t
->entry
);
217 isdir
= S_ISDIR(mode
);
220 * a path was removed - take path from imin parent. Also take
221 * mode from that parent, to decide on recursion(1).
223 * 1) all modes for tp[i]=tp[imin] should be the same wrt
224 * S_ISDIR, thanks to base_name_compare().
226 tree_entry_extract(&tp
[imin
], &path
, &mode
);
227 pathlen
= tree_entry_len(&tp
[imin
].entry
);
229 isdir
= S_ISDIR(mode
);
234 if (opt
->flags
.recursive
&& isdir
) {
236 emitthis
= opt
->flags
.tree_in_recursive
;
241 struct combine_diff_path
*pprev
= p
;
242 p
= path_appendnew(p
, nparent
, base
, path
, pathlen
, mode
, oid
);
244 for (i
= 0; i
< nparent
; ++i
) {
246 * tp[i] is valid, if present and if tp[i]==tp[imin] -
247 * otherwise, we should ignore it.
249 int tpi_valid
= tp
&& !(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
);
251 const struct object_id
*oid_i
;
254 p
->parent
[i
].status
=
255 !t
? DIFF_STATUS_DELETED
:
257 DIFF_STATUS_MODIFIED
:
261 oid_i
= &tp
[i
].entry
.oid
;
262 mode_i
= tp
[i
].entry
.mode
;
269 p
->parent
[i
].mode
= mode_i
;
270 oidcpy(&p
->parent
[i
].oid
, oid_i
);
275 keep
= opt
->pathchange(opt
, p
);
278 * If a path was filtered or consumed - we don't need to add it
279 * to the list and can reuse its memory, leaving it as
280 * pre-allocated element on the tail.
282 * On the other hand, if path needs to be kept, we need to
283 * correct its .next to NULL, as it was pre-initialized to how
284 * much memory was allocated.
286 * see path_appendnew() for details.
295 const struct object_id
**parents_oid
;
297 FAST_ARRAY_ALLOC(parents_oid
, nparent
);
298 for (i
= 0; i
< nparent
; ++i
) {
299 /* same rule as in emitthis */
300 int tpi_valid
= tp
&& !(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
);
302 parents_oid
[i
] = tpi_valid
? &tp
[i
].entry
.oid
: NULL
;
305 strbuf_add(base
, path
, pathlen
);
306 strbuf_addch(base
, '/');
307 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
,
309 FAST_ARRAY_FREE(parents_oid
, nparent
);
312 strbuf_setlen(base
, old_baselen
);
316 static void skip_uninteresting(struct tree_desc
*t
, struct strbuf
*base
,
317 struct diff_options
*opt
)
319 enum interesting match
;
322 match
= tree_entry_interesting(opt
->repo
->index
, &t
->entry
,
323 base
, &opt
->pathspec
);
325 if (match
== all_entries_not_interesting
)
329 update_tree_entry(t
);
335 * generate paths for combined diff D(sha1,parents_oid[])
337 * Resulting paths are appended to combine_diff_path linked list, and also, are
338 * emitted on the go via opt->pathchange() callback, so it is possible to
339 * process the result as batch or incrementally.
341 * The paths are generated scanning new tree and all parents trees
342 * simultaneously, similarly to what diff_tree() was doing for 2 trees.
343 * The theory behind such scan is as follows:
346 * D(T,P1...Pn) calculation scheme
347 * -------------------------------
349 * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set)
351 * D(T,Pj) - diff between T..Pj
352 * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn
355 * We start from all trees, which are sorted, and compare their entries in
361 * |-| |--| ... |--| imin = argmin(p1...pn)
368 * at any time there could be 3 cases:
374 * Schematic deduction of what every case means, and what to do, follows:
376 * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓
380 * 2.1) ∃j: pj > p[imin] -> "-p[imin]" ∉ D(T,Pj) -> D += ø; ∀ pi=p[imin] pi↓
381 * 2.2) ∀i pi = p[imin] -> pi ∉ T -> "-pi" ∈ D(T,Pi) -> D += "-p[imin]"; ∀i pi↓
385 * 3.1) ∃j: pj > p[imin] -> "+t" ∈ D(T,Pj) -> only pi=p[imin] remains to investigate
386 * 3.2) pi = p[imin] -> investigate δ(t,pi)
391 * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø ->
393 * ⎧δ(t,pi) - if pi=p[imin]
395 * ⎩"+t" - if pi>p[imin]
398 * in any case t↓ ∀ pi=p[imin] pi↓
405 * Usual diff D(A,B) is by definition the same as combined diff D(A,[B]),
406 * so this diff paths generator can, and is used, for plain diffs
409 * Please keep attention to the common D(A,[B]) case when working on the
410 * code, in order not to slow it down.
413 * nparent must be > 0.
417 /* ∀ pi=p[imin] pi↓ */
418 static inline void update_tp_entries(struct tree_desc
*tp
, int nparent
)
421 for (i
= 0; i
< nparent
; ++i
)
422 if (!(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
))
423 update_tree_entry(&tp
[i
]);
426 static struct combine_diff_path
*ll_diff_tree_paths(
427 struct combine_diff_path
*p
, const struct object_id
*oid
,
428 const struct object_id
**parents_oid
, int nparent
,
429 struct strbuf
*base
, struct diff_options
*opt
,
432 struct tree_desc t
, *tp
;
433 void *ttree
, **tptree
;
436 if (depth
> max_allowed_tree_depth
)
437 die("exceeded maximum allowed tree depth");
439 FAST_ARRAY_ALLOC(tp
, nparent
);
440 FAST_ARRAY_ALLOC(tptree
, nparent
);
443 * load parents first, as they are probably already cached.
445 * ( log_tree_diff() parses commit->parent before calling here via
446 * diff_tree_oid(parent, commit) )
448 for (i
= 0; i
< nparent
; ++i
)
449 tptree
[i
] = fill_tree_descriptor(opt
->repo
, &tp
[i
], parents_oid
[i
]);
450 ttree
= fill_tree_descriptor(opt
->repo
, &t
, oid
);
452 /* Enable recursion indefinitely */
453 opt
->pathspec
.recursive
= opt
->flags
.recursive
;
458 if (diff_can_quit_early(opt
))
461 if (opt
->max_changes
&& diff_queued_diff
.nr
> opt
->max_changes
)
464 if (opt
->pathspec
.nr
) {
465 skip_uninteresting(&t
, base
, opt
);
466 for (i
= 0; i
< nparent
; i
++)
467 skip_uninteresting(&tp
[i
], base
, opt
);
470 /* comparing is finished when all trees are done */
473 for (i
= 0; i
< nparent
; ++i
)
483 * lookup imin = argmin(p1...pn),
484 * mark entries whether they =p[imin] along the way
487 tp
[0].entry
.mode
&= ~S_IFXMIN_NEQ
;
489 for (i
= 1; i
< nparent
; ++i
) {
490 cmp
= tree_entry_pathcmp(&tp
[i
], &tp
[imin
]);
493 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
496 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
499 tp
[i
].entry
.mode
|= S_IFXMIN_NEQ
;
503 /* fixup markings for entries before imin */
504 for (i
= 0; i
< imin
; ++i
)
505 tp
[i
].entry
.mode
|= S_IFXMIN_NEQ
; /* pi > p[imin] */
509 /* compare t vs p[imin] */
510 cmp
= tree_entry_pathcmp(&t
, &tp
[imin
]);
514 /* are either pi > p[imin] or diff(t,pi) != ø ? */
515 if (!opt
->flags
.find_copies_harder
) {
516 for (i
= 0; i
< nparent
; ++i
) {
518 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
521 /* diff(t,pi) != ø */
522 if (!oideq(&t
.entry
.oid
, &tp
[i
].entry
.oid
) ||
523 (t
.entry
.mode
!= tp
[i
].entry
.mode
))
530 /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */
531 p
= emit_path(p
, base
, opt
, nparent
,
532 &t
, tp
, imin
, depth
);
535 /* t↓, ∀ pi=p[imin] pi↓ */
536 update_tree_entry(&t
);
537 update_tp_entries(tp
, nparent
);
543 p
= emit_path(p
, base
, opt
, nparent
,
544 &t
, /*tp=*/NULL
, -1, depth
);
547 update_tree_entry(&t
);
552 /* ∀i pi=p[imin] -> D += "-p[imin]" */
553 if (!opt
->flags
.find_copies_harder
) {
554 for (i
= 0; i
< nparent
; ++i
)
555 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
559 p
= emit_path(p
, base
, opt
, nparent
,
560 /*t=*/NULL
, tp
, imin
, depth
);
563 /* ∀ pi=p[imin] pi↓ */
564 update_tp_entries(tp
, nparent
);
569 for (i
= nparent
-1; i
>= 0; i
--)
571 FAST_ARRAY_FREE(tptree
, nparent
);
572 FAST_ARRAY_FREE(tp
, nparent
);
577 struct combine_diff_path
*diff_tree_paths(
578 struct combine_diff_path
*p
, const struct object_id
*oid
,
579 const struct object_id
**parents_oid
, int nparent
,
580 struct strbuf
*base
, struct diff_options
*opt
)
582 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
, 0);
585 * free pre-allocated last element, if any
586 * (see path_appendnew() for details about why)
588 FREE_AND_NULL(p
->next
);
594 * Does it look like the resulting diff might be due to a rename?
596 * - not a valid previous file
598 static inline int diff_might_be_rename(void)
600 return diff_queued_diff
.nr
== 1 &&
601 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
604 static void try_to_follow_renames(const struct object_id
*old_oid
,
605 const struct object_id
*new_oid
,
606 struct strbuf
*base
, struct diff_options
*opt
)
608 struct diff_options diff_opts
;
609 struct diff_queue_struct
*q
= &diff_queued_diff
;
610 struct diff_filepair
*choice
;
614 * follow-rename code is very specific, we need exactly one
615 * path. Magic that matches more than one path is not
618 GUARD_PATHSPEC(&opt
->pathspec
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
621 * We should reject wildcards as well. Unfortunately we
622 * haven't got a reliable way to detect that 'foo\*bar' in
623 * fact has no wildcards. nowildcard_len is merely a hint for
624 * optimization. Let it slip for now until wildmatch is taught
625 * about dry-run mode and returns wildcard info.
627 if (opt
->pathspec
.has_wildcard
)
628 BUG("wildcards are not supported");
631 /* Remove the file creation entry from the diff queue, and remember it */
632 choice
= q
->queue
[0];
635 repo_diff_setup(opt
->repo
, &diff_opts
);
636 diff_opts
.flags
.recursive
= 1;
637 diff_opts
.flags
.find_copies_harder
= 1;
638 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
639 diff_opts
.single_follow
= opt
->pathspec
.items
[0].match
;
640 diff_opts
.break_opt
= opt
->break_opt
;
641 diff_opts
.rename_score
= opt
->rename_score
;
642 diff_setup_done(&diff_opts
);
643 ll_diff_tree_oid(old_oid
, new_oid
, base
, &diff_opts
);
644 diffcore_std(&diff_opts
);
645 clear_pathspec(&diff_opts
.pathspec
);
647 /* Go through the new set of filepairing, and see if we find a more interesting one */
648 opt
->found_follow
= 0;
649 for (i
= 0; i
< q
->nr
; i
++) {
650 struct diff_filepair
*p
= q
->queue
[i
];
653 * Found a source? Not only do we use that for the new
654 * diff_queued_diff, we will also use that as the path in
657 if ((p
->status
== 'R' || p
->status
== 'C') &&
658 !strcmp(p
->two
->path
, opt
->pathspec
.items
[0].match
)) {
661 /* Switch the file-pairs around */
662 q
->queue
[i
] = choice
;
665 /* Update the path we use from now on.. */
666 path
[0] = p
->one
->path
;
668 clear_pathspec(&opt
->pathspec
);
669 parse_pathspec(&opt
->pathspec
,
670 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
671 PATHSPEC_LITERAL_PATH
, "", path
);
674 * The caller expects us to return a set of vanilla
675 * filepairs to let a later call to diffcore_std()
676 * it makes to sort the renames out (among other
677 * things), but we already have found renames
678 * ourselves; signal diffcore_std() not to muck with
679 * rename information.
681 opt
->found_follow
= 1;
687 * Then, discard all the non-relevant file pairs...
689 for (i
= 0; i
< q
->nr
; i
++) {
690 struct diff_filepair
*p
= q
->queue
[i
];
691 diff_free_filepair(p
);
695 * .. and re-instate the one we want (which might be either the
696 * original one, or the rename/copy we found)
698 q
->queue
[0] = choice
;
702 static void ll_diff_tree_oid(const struct object_id
*old_oid
,
703 const struct object_id
*new_oid
,
704 struct strbuf
*base
, struct diff_options
*opt
)
706 struct combine_diff_path phead
, *p
;
707 pathchange_fn_t pathchange_old
= opt
->pathchange
;
710 opt
->pathchange
= emit_diff_first_parent_only
;
711 diff_tree_paths(&phead
, new_oid
, &old_oid
, 1, base
, opt
);
713 for (p
= phead
.next
; p
;) {
714 struct combine_diff_path
*pprev
= p
;
719 opt
->pathchange
= pathchange_old
;
722 void diff_tree_oid(const struct object_id
*old_oid
,
723 const struct object_id
*new_oid
,
724 const char *base_str
, struct diff_options
*opt
)
728 strbuf_init(&base
, PATH_MAX
);
729 strbuf_addstr(&base
, base_str
);
731 ll_diff_tree_oid(old_oid
, new_oid
, &base
, opt
);
732 if (!*base_str
&& opt
->flags
.follow_renames
&& diff_might_be_rename())
733 try_to_follow_renames(old_oid
, new_oid
, &base
, opt
);
735 strbuf_release(&base
);
738 void diff_root_tree_oid(const struct object_id
*new_oid
,
740 struct diff_options
*opt
)
742 diff_tree_oid(NULL
, new_oid
, base
, opt
);