2 * Helper functions for tree diff generation
4 #include "git-compat-util.h"
12 * Some mode bits are also used internally for computations.
14 * They *must* not overlap with any valid modes, and they *must* not be emitted
15 * to outside world - i.e. appear on disk or network. In other words, it's just
16 * temporary fields, which we internally use, but they have to stay in-house.
18 * ( such approach is valid, as standard S_IF* fits into 16 bits, and in Git
19 * codebase mode is `unsigned int` which is assumed to be at least 32 bits )
22 #define S_DIFFTREE_IFXMIN_NEQ 0x80000000
25 * internal mode marker, saying a tree entry != entry of tp[imin]
26 * (see ll_diff_tree_paths for what it means there)
28 * we will update/use/emit entry for diff only with it unset.
30 #define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ
32 #define FAST_ARRAY_ALLOC(x, nr) do { \
34 (x) = xalloca((nr) * sizeof(*(x))); \
36 ALLOC_ARRAY((x), nr); \
38 #define FAST_ARRAY_FREE(x, nr) do { \
45 static struct combine_diff_path
*ll_diff_tree_paths(
46 struct combine_diff_path
*p
, const struct object_id
*oid
,
47 const struct object_id
**parents_oid
, int nparent
,
48 struct strbuf
*base
, struct diff_options
*opt
);
49 static void ll_diff_tree_oid(const struct object_id
*old_oid
,
50 const struct object_id
*new_oid
,
51 struct strbuf
*base
, struct diff_options
*opt
);
54 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
55 * but not their sha1's.
57 * NOTE files and directories *always* compare differently, even when having
58 * the same name - thanks to base_name_compare().
60 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
61 * so that they sort *after* valid tree entries.
63 * Due to this convention, if trees are scanned in sorted order, all
64 * non-empty descriptors will be processed first.
66 static int tree_entry_pathcmp(struct tree_desc
*t1
, struct tree_desc
*t2
)
68 struct name_entry
*e1
, *e2
;
71 /* empty descriptors sort after valid tree entries */
73 return t2
->size
? 1 : 0;
79 cmp
= base_name_compare(e1
->path
, tree_entry_len(e1
), e1
->mode
,
80 e2
->path
, tree_entry_len(e2
), e2
->mode
);
86 * convert path -> opt->diff_*() callbacks
88 * emits diff to first parent only, and tells diff tree-walker that we are done
89 * with p and it can be freed.
91 static int emit_diff_first_parent_only(struct diff_options
*opt
, struct combine_diff_path
*p
)
93 struct combine_diff_parent
*p0
= &p
->parent
[0];
94 if (p
->mode
&& p0
->mode
) {
95 opt
->change(opt
, p0
->mode
, p
->mode
, &p0
->oid
, &p
->oid
,
99 const struct object_id
*oid
;
113 opt
->add_remove(opt
, addremove
, mode
, oid
, 1, p
->path
, 0);
116 return 0; /* we are done with p */
121 * Make a new combine_diff_path from path/mode/sha1
122 * and append it to paths list tail.
124 * Memory for created elements could be reused:
126 * - if last->next == NULL, the memory is allocated;
128 * - if last->next != NULL, it is assumed that p=last->next was returned
129 * earlier by this function, and p->next was *not* modified.
130 * The memory is then reused from p.
134 * - if you do need to keep the element
136 * p = path_appendnew(p, ...);
140 * - if you don't need to keep the element after processing
143 * p = path_appendnew(p, ...);
146 * ; don't forget to free tail->next in the end
148 * p->parent[] remains uninitialized.
150 static struct combine_diff_path
*path_appendnew(struct combine_diff_path
*last
,
151 int nparent
, const struct strbuf
*base
, const char *path
, int pathlen
,
152 unsigned mode
, const struct object_id
*oid
)
154 struct combine_diff_path
*p
;
155 size_t len
= st_add(base
->len
, pathlen
);
156 size_t alloclen
= combine_diff_path_size(nparent
, len
);
158 /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
160 if (p
&& (alloclen
> (intptr_t)p
->next
)) {
165 p
= xmalloc(alloclen
);
168 * until we go to it next round, .next holds how many bytes we
169 * allocated (for faster realloc - we don't need copying old data).
171 p
->next
= (struct combine_diff_path
*)(intptr_t)alloclen
;
176 p
->path
= (char *)&(p
->parent
[nparent
]);
177 memcpy(p
->path
, base
->buf
, base
->len
);
178 memcpy(p
->path
+ base
->len
, path
, pathlen
);
181 oidcpy(&p
->oid
, oid
? oid
: null_oid());
187 * new path should be added to combine diff
189 * 3 cases on how/when it should be called and behaves:
191 * t, !tp -> path added, all parents lack it
192 * !t, tp -> path removed from all parents
193 * t, tp -> path modified/added
194 * (M for tp[i]=tp[imin], A otherwise)
196 static struct combine_diff_path
*emit_path(struct combine_diff_path
*p
,
197 struct strbuf
*base
, struct diff_options
*opt
, int nparent
,
198 struct tree_desc
*t
, struct tree_desc
*tp
,
203 const struct object_id
*oid
;
205 int old_baselen
= base
->len
;
206 int i
, isdir
, recurse
= 0, emitthis
= 1;
208 /* at least something has to be valid */
212 /* path present in resulting tree */
213 oid
= tree_entry_extract(t
, &path
, &mode
);
214 pathlen
= tree_entry_len(&t
->entry
);
215 isdir
= S_ISDIR(mode
);
218 * a path was removed - take path from imin parent. Also take
219 * mode from that parent, to decide on recursion(1).
221 * 1) all modes for tp[i]=tp[imin] should be the same wrt
222 * S_ISDIR, thanks to base_name_compare().
224 tree_entry_extract(&tp
[imin
], &path
, &mode
);
225 pathlen
= tree_entry_len(&tp
[imin
].entry
);
227 isdir
= S_ISDIR(mode
);
232 if (opt
->flags
.recursive
&& isdir
) {
234 emitthis
= opt
->flags
.tree_in_recursive
;
239 struct combine_diff_path
*pprev
= p
;
240 p
= path_appendnew(p
, nparent
, base
, path
, pathlen
, mode
, oid
);
242 for (i
= 0; i
< nparent
; ++i
) {
244 * tp[i] is valid, if present and if tp[i]==tp[imin] -
245 * otherwise, we should ignore it.
247 int tpi_valid
= tp
&& !(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
);
249 const struct object_id
*oid_i
;
252 p
->parent
[i
].status
=
253 !t
? DIFF_STATUS_DELETED
:
255 DIFF_STATUS_MODIFIED
:
259 oid_i
= &tp
[i
].entry
.oid
;
260 mode_i
= tp
[i
].entry
.mode
;
267 p
->parent
[i
].mode
= mode_i
;
268 oidcpy(&p
->parent
[i
].oid
, oid_i
);
273 keep
= opt
->pathchange(opt
, p
);
276 * If a path was filtered or consumed - we don't need to add it
277 * to the list and can reuse its memory, leaving it as
278 * pre-allocated element on the tail.
280 * On the other hand, if path needs to be kept, we need to
281 * correct its .next to NULL, as it was pre-initialized to how
282 * much memory was allocated.
284 * see path_appendnew() for details.
293 const struct object_id
**parents_oid
;
295 FAST_ARRAY_ALLOC(parents_oid
, nparent
);
296 for (i
= 0; i
< nparent
; ++i
) {
297 /* same rule as in emitthis */
298 int tpi_valid
= tp
&& !(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
);
300 parents_oid
[i
] = tpi_valid
? &tp
[i
].entry
.oid
: NULL
;
303 strbuf_add(base
, path
, pathlen
);
304 strbuf_addch(base
, '/');
305 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
);
306 FAST_ARRAY_FREE(parents_oid
, nparent
);
309 strbuf_setlen(base
, old_baselen
);
313 static void skip_uninteresting(struct tree_desc
*t
, struct strbuf
*base
,
314 struct diff_options
*opt
)
316 enum interesting match
;
319 match
= tree_entry_interesting(opt
->repo
->index
, &t
->entry
,
320 base
, &opt
->pathspec
);
322 if (match
== all_entries_not_interesting
)
326 update_tree_entry(t
);
332 * generate paths for combined diff D(sha1,parents_oid[])
334 * Resulting paths are appended to combine_diff_path linked list, and also, are
335 * emitted on the go via opt->pathchange() callback, so it is possible to
336 * process the result as batch or incrementally.
338 * The paths are generated scanning new tree and all parents trees
339 * simultaneously, similarly to what diff_tree() was doing for 2 trees.
340 * The theory behind such scan is as follows:
343 * D(T,P1...Pn) calculation scheme
344 * -------------------------------
346 * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set)
348 * D(T,Pj) - diff between T..Pj
349 * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn
352 * We start from all trees, which are sorted, and compare their entries in
358 * |-| |--| ... |--| imin = argmin(p1...pn)
365 * at any time there could be 3 cases:
371 * Schematic deduction of what every case means, and what to do, follows:
373 * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓
377 * 2.1) ∃j: pj > p[imin] -> "-p[imin]" ∉ D(T,Pj) -> D += ø; ∀ pi=p[imin] pi↓
378 * 2.2) ∀i pi = p[imin] -> pi ∉ T -> "-pi" ∈ D(T,Pi) -> D += "-p[imin]"; ∀i pi↓
382 * 3.1) ∃j: pj > p[imin] -> "+t" ∈ D(T,Pj) -> only pi=p[imin] remains to investigate
383 * 3.2) pi = p[imin] -> investigate δ(t,pi)
388 * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø ->
390 * ⎧δ(t,pi) - if pi=p[imin]
392 * ⎩"+t" - if pi>p[imin]
395 * in any case t↓ ∀ pi=p[imin] pi↓
402 * Usual diff D(A,B) is by definition the same as combined diff D(A,[B]),
403 * so this diff paths generator can, and is used, for plain diffs
406 * Please keep attention to the common D(A,[B]) case when working on the
407 * code, in order not to slow it down.
410 * nparent must be > 0.
414 /* ∀ pi=p[imin] pi↓ */
415 static inline void update_tp_entries(struct tree_desc
*tp
, int nparent
)
418 for (i
= 0; i
< nparent
; ++i
)
419 if (!(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
))
420 update_tree_entry(&tp
[i
]);
423 static struct combine_diff_path
*ll_diff_tree_paths(
424 struct combine_diff_path
*p
, const struct object_id
*oid
,
425 const struct object_id
**parents_oid
, int nparent
,
426 struct strbuf
*base
, struct diff_options
*opt
)
428 struct tree_desc t
, *tp
;
429 void *ttree
, **tptree
;
432 FAST_ARRAY_ALLOC(tp
, nparent
);
433 FAST_ARRAY_ALLOC(tptree
, nparent
);
436 * load parents first, as they are probably already cached.
438 * ( log_tree_diff() parses commit->parent before calling here via
439 * diff_tree_oid(parent, commit) )
441 for (i
= 0; i
< nparent
; ++i
)
442 tptree
[i
] = fill_tree_descriptor(opt
->repo
, &tp
[i
], parents_oid
[i
]);
443 ttree
= fill_tree_descriptor(opt
->repo
, &t
, oid
);
445 /* Enable recursion indefinitely */
446 opt
->pathspec
.recursive
= opt
->flags
.recursive
;
451 if (diff_can_quit_early(opt
))
454 if (opt
->max_changes
&& diff_queued_diff
.nr
> opt
->max_changes
)
457 if (opt
->pathspec
.nr
) {
458 skip_uninteresting(&t
, base
, opt
);
459 for (i
= 0; i
< nparent
; i
++)
460 skip_uninteresting(&tp
[i
], base
, opt
);
463 /* comparing is finished when all trees are done */
466 for (i
= 0; i
< nparent
; ++i
)
476 * lookup imin = argmin(p1...pn),
477 * mark entries whether they =p[imin] along the way
480 tp
[0].entry
.mode
&= ~S_IFXMIN_NEQ
;
482 for (i
= 1; i
< nparent
; ++i
) {
483 cmp
= tree_entry_pathcmp(&tp
[i
], &tp
[imin
]);
486 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
489 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
492 tp
[i
].entry
.mode
|= S_IFXMIN_NEQ
;
496 /* fixup markings for entries before imin */
497 for (i
= 0; i
< imin
; ++i
)
498 tp
[i
].entry
.mode
|= S_IFXMIN_NEQ
; /* pi > p[imin] */
502 /* compare t vs p[imin] */
503 cmp
= tree_entry_pathcmp(&t
, &tp
[imin
]);
507 /* are either pi > p[imin] or diff(t,pi) != ø ? */
508 if (!opt
->flags
.find_copies_harder
) {
509 for (i
= 0; i
< nparent
; ++i
) {
511 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
514 /* diff(t,pi) != ø */
515 if (!oideq(&t
.entry
.oid
, &tp
[i
].entry
.oid
) ||
516 (t
.entry
.mode
!= tp
[i
].entry
.mode
))
523 /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */
524 p
= emit_path(p
, base
, opt
, nparent
,
528 /* t↓, ∀ pi=p[imin] pi↓ */
529 update_tree_entry(&t
);
530 update_tp_entries(tp
, nparent
);
536 p
= emit_path(p
, base
, opt
, nparent
,
537 &t
, /*tp=*/NULL
, -1);
540 update_tree_entry(&t
);
545 /* ∀i pi=p[imin] -> D += "-p[imin]" */
546 if (!opt
->flags
.find_copies_harder
) {
547 for (i
= 0; i
< nparent
; ++i
)
548 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
552 p
= emit_path(p
, base
, opt
, nparent
,
553 /*t=*/NULL
, tp
, imin
);
556 /* ∀ pi=p[imin] pi↓ */
557 update_tp_entries(tp
, nparent
);
562 for (i
= nparent
-1; i
>= 0; i
--)
564 FAST_ARRAY_FREE(tptree
, nparent
);
565 FAST_ARRAY_FREE(tp
, nparent
);
570 struct combine_diff_path
*diff_tree_paths(
571 struct combine_diff_path
*p
, const struct object_id
*oid
,
572 const struct object_id
**parents_oid
, int nparent
,
573 struct strbuf
*base
, struct diff_options
*opt
)
575 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
);
578 * free pre-allocated last element, if any
579 * (see path_appendnew() for details about why)
581 FREE_AND_NULL(p
->next
);
587 * Does it look like the resulting diff might be due to a rename?
589 * - not a valid previous file
591 static inline int diff_might_be_rename(void)
593 return diff_queued_diff
.nr
== 1 &&
594 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
597 static void try_to_follow_renames(const struct object_id
*old_oid
,
598 const struct object_id
*new_oid
,
599 struct strbuf
*base
, struct diff_options
*opt
)
601 struct diff_options diff_opts
;
602 struct diff_queue_struct
*q
= &diff_queued_diff
;
603 struct diff_filepair
*choice
;
607 * follow-rename code is very specific, we need exactly one
608 * path. Magic that matches more than one path is not
611 GUARD_PATHSPEC(&opt
->pathspec
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
614 * We should reject wildcards as well. Unfortunately we
615 * haven't got a reliable way to detect that 'foo\*bar' in
616 * fact has no wildcards. nowildcard_len is merely a hint for
617 * optimization. Let it slip for now until wildmatch is taught
618 * about dry-run mode and returns wildcard info.
620 if (opt
->pathspec
.has_wildcard
)
621 BUG("wildcards are not supported");
624 /* Remove the file creation entry from the diff queue, and remember it */
625 choice
= q
->queue
[0];
628 repo_diff_setup(opt
->repo
, &diff_opts
);
629 diff_opts
.flags
.recursive
= 1;
630 diff_opts
.flags
.find_copies_harder
= 1;
631 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
632 diff_opts
.single_follow
= opt
->pathspec
.items
[0].match
;
633 diff_opts
.break_opt
= opt
->break_opt
;
634 diff_opts
.rename_score
= opt
->rename_score
;
635 diff_setup_done(&diff_opts
);
636 ll_diff_tree_oid(old_oid
, new_oid
, base
, &diff_opts
);
637 diffcore_std(&diff_opts
);
638 clear_pathspec(&diff_opts
.pathspec
);
640 /* Go through the new set of filepairing, and see if we find a more interesting one */
641 opt
->found_follow
= 0;
642 for (i
= 0; i
< q
->nr
; i
++) {
643 struct diff_filepair
*p
= q
->queue
[i
];
646 * Found a source? Not only do we use that for the new
647 * diff_queued_diff, we will also use that as the path in
650 if ((p
->status
== 'R' || p
->status
== 'C') &&
651 !strcmp(p
->two
->path
, opt
->pathspec
.items
[0].match
)) {
654 /* Switch the file-pairs around */
655 q
->queue
[i
] = choice
;
658 /* Update the path we use from now on.. */
659 path
[0] = p
->one
->path
;
661 clear_pathspec(&opt
->pathspec
);
662 parse_pathspec(&opt
->pathspec
,
663 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
664 PATHSPEC_LITERAL_PATH
, "", path
);
667 * The caller expects us to return a set of vanilla
668 * filepairs to let a later call to diffcore_std()
669 * it makes to sort the renames out (among other
670 * things), but we already have found renames
671 * ourselves; signal diffcore_std() not to muck with
672 * rename information.
674 opt
->found_follow
= 1;
680 * Then, discard all the non-relevant file pairs...
682 for (i
= 0; i
< q
->nr
; i
++) {
683 struct diff_filepair
*p
= q
->queue
[i
];
684 diff_free_filepair(p
);
688 * .. and re-instate the one we want (which might be either the
689 * original one, or the rename/copy we found)
691 q
->queue
[0] = choice
;
695 static void ll_diff_tree_oid(const struct object_id
*old_oid
,
696 const struct object_id
*new_oid
,
697 struct strbuf
*base
, struct diff_options
*opt
)
699 struct combine_diff_path phead
, *p
;
700 pathchange_fn_t pathchange_old
= opt
->pathchange
;
703 opt
->pathchange
= emit_diff_first_parent_only
;
704 diff_tree_paths(&phead
, new_oid
, &old_oid
, 1, base
, opt
);
706 for (p
= phead
.next
; p
;) {
707 struct combine_diff_path
*pprev
= p
;
712 opt
->pathchange
= pathchange_old
;
715 void diff_tree_oid(const struct object_id
*old_oid
,
716 const struct object_id
*new_oid
,
717 const char *base_str
, struct diff_options
*opt
)
721 strbuf_init(&base
, PATH_MAX
);
722 strbuf_addstr(&base
, base_str
);
724 ll_diff_tree_oid(old_oid
, new_oid
, &base
, opt
);
725 if (!*base_str
&& opt
->flags
.follow_renames
&& diff_might_be_rename())
726 try_to_follow_renames(old_oid
, new_oid
, &base
, opt
);
728 strbuf_release(&base
);
731 void diff_root_tree_oid(const struct object_id
*new_oid
,
733 struct diff_options
*opt
)
735 diff_tree_oid(NULL
, new_oid
, base
, opt
);