2 * Helper functions for tree diff generation
4 #include "git-compat-util.h"
11 * Some mode bits are also used internally for computations.
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.
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 )
21 #define S_DIFFTREE_IFXMIN_NEQ 0x80000000
24 * internal mode marker, saying a tree entry != entry of tp[imin]
25 * (see ll_diff_tree_paths for what it means there)
27 * we will update/use/emit entry for diff only with it unset.
29 #define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ
31 #define FAST_ARRAY_ALLOC(x, nr) do { \
33 (x) = xalloca((nr) * sizeof(*(x))); \
35 ALLOC_ARRAY((x), nr); \
37 #define FAST_ARRAY_FREE(x, nr) do { \
44 static struct combine_diff_path
*ll_diff_tree_paths(
45 struct combine_diff_path
*p
, const struct object_id
*oid
,
46 const struct object_id
**parents_oid
, int nparent
,
47 struct strbuf
*base
, struct diff_options
*opt
);
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
);
53 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
54 * but not their sha1's.
56 * NOTE files and directories *always* compare differently, even when having
57 * the same name - thanks to base_name_compare().
59 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
60 * so that they sort *after* valid tree entries.
62 * Due to this convention, if trees are scanned in sorted order, all
63 * non-empty descriptors will be processed first.
65 static int tree_entry_pathcmp(struct tree_desc
*t1
, struct tree_desc
*t2
)
67 struct name_entry
*e1
, *e2
;
70 /* empty descriptors sort after valid tree entries */
72 return t2
->size
? 1 : 0;
78 cmp
= base_name_compare(e1
->path
, tree_entry_len(e1
), e1
->mode
,
79 e2
->path
, tree_entry_len(e2
), e2
->mode
);
85 * convert path -> opt->diff_*() callbacks
87 * emits diff to first parent only, and tells diff tree-walker that we are done
88 * with p and it can be freed.
90 static int emit_diff_first_parent_only(struct diff_options
*opt
, struct combine_diff_path
*p
)
92 struct combine_diff_parent
*p0
= &p
->parent
[0];
93 if (p
->mode
&& p0
->mode
) {
94 opt
->change(opt
, p0
->mode
, p
->mode
, &p0
->oid
, &p
->oid
,
98 const struct object_id
*oid
;
112 opt
->add_remove(opt
, addremove
, mode
, oid
, 1, p
->path
, 0);
115 return 0; /* we are done with p */
120 * Make a new combine_diff_path from path/mode/sha1
121 * and append it to paths list tail.
123 * Memory for created elements could be reused:
125 * - if last->next == NULL, the memory is allocated;
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.
133 * - if you do need to keep the element
135 * p = path_appendnew(p, ...);
139 * - if you don't need to keep the element after processing
142 * p = path_appendnew(p, ...);
145 * ; don't forget to free tail->next in the end
147 * p->parent[] remains uninitialized.
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
,
151 unsigned mode
, const struct object_id
*oid
)
153 struct combine_diff_path
*p
;
154 size_t len
= st_add(base
->len
, pathlen
);
155 size_t alloclen
= combine_diff_path_size(nparent
, len
);
157 /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
159 if (p
&& (alloclen
> (intptr_t)p
->next
)) {
164 p
= xmalloc(alloclen
);
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).
170 p
->next
= (struct combine_diff_path
*)(intptr_t)alloclen
;
175 p
->path
= (char *)&(p
->parent
[nparent
]);
176 memcpy(p
->path
, base
->buf
, base
->len
);
177 memcpy(p
->path
+ base
->len
, path
, pathlen
);
180 oidcpy(&p
->oid
, oid
? oid
: null_oid());
186 * new path should be added to combine diff
188 * 3 cases on how/when it should be called and behaves:
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)
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
,
202 const struct object_id
*oid
;
204 int old_baselen
= base
->len
;
205 int i
, isdir
, recurse
= 0, emitthis
= 1;
207 /* at least something has to be valid */
211 /* path present in resulting tree */
212 oid
= tree_entry_extract(t
, &path
, &mode
);
213 pathlen
= tree_entry_len(&t
->entry
);
214 isdir
= S_ISDIR(mode
);
217 * a path was removed - take path from imin parent. Also take
218 * mode from that parent, to decide on recursion(1).
220 * 1) all modes for tp[i]=tp[imin] should be the same wrt
221 * S_ISDIR, thanks to base_name_compare().
223 tree_entry_extract(&tp
[imin
], &path
, &mode
);
224 pathlen
= tree_entry_len(&tp
[imin
].entry
);
226 isdir
= S_ISDIR(mode
);
231 if (opt
->flags
.recursive
&& isdir
) {
233 emitthis
= opt
->flags
.tree_in_recursive
;
238 struct combine_diff_path
*pprev
= p
;
239 p
= path_appendnew(p
, nparent
, base
, path
, pathlen
, mode
, oid
);
241 for (i
= 0; i
< nparent
; ++i
) {
243 * tp[i] is valid, if present and if tp[i]==tp[imin] -
244 * otherwise, we should ignore it.
246 int tpi_valid
= tp
&& !(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
);
248 const struct object_id
*oid_i
;
251 p
->parent
[i
].status
=
252 !t
? DIFF_STATUS_DELETED
:
254 DIFF_STATUS_MODIFIED
:
258 oid_i
= &tp
[i
].entry
.oid
;
259 mode_i
= tp
[i
].entry
.mode
;
266 p
->parent
[i
].mode
= mode_i
;
267 oidcpy(&p
->parent
[i
].oid
, oid_i
);
272 keep
= opt
->pathchange(opt
, p
);
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.
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.
283 * see path_appendnew() for details.
292 const struct object_id
**parents_oid
;
294 FAST_ARRAY_ALLOC(parents_oid
, nparent
);
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
);
299 parents_oid
[i
] = tpi_valid
? &tp
[i
].entry
.oid
: NULL
;
302 strbuf_add(base
, path
, pathlen
);
303 strbuf_addch(base
, '/');
304 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
);
305 FAST_ARRAY_FREE(parents_oid
, nparent
);
308 strbuf_setlen(base
, old_baselen
);
312 static void skip_uninteresting(struct tree_desc
*t
, struct strbuf
*base
,
313 struct diff_options
*opt
)
315 enum interesting match
;
318 match
= tree_entry_interesting(opt
->repo
->index
, &t
->entry
,
319 base
, 0, &opt
->pathspec
);
321 if (match
== all_entries_not_interesting
)
325 update_tree_entry(t
);
331 * generate paths for combined diff D(sha1,parents_oid[])
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.
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:
342 * D(T,P1...Pn) calculation scheme
343 * -------------------------------
345 * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set)
347 * D(T,Pj) - diff between T..Pj
348 * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn
351 * We start from all trees, which are sorted, and compare their entries in
357 * |-| |--| ... |--| imin = argmin(p1...pn)
364 * at any time there could be 3 cases:
370 * Schematic deduction of what every case means, and what to do, follows:
372 * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓
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↓
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)
387 * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø ->
389 * ⎧δ(t,pi) - if pi=p[imin]
391 * ⎩"+t" - if pi>p[imin]
394 * in any case t↓ ∀ pi=p[imin] pi↓
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
405 * Please keep attention to the common D(A,[B]) case when working on the
406 * code, in order not to slow it down.
409 * nparent must be > 0.
413 /* ∀ pi=p[imin] pi↓ */
414 static inline void update_tp_entries(struct tree_desc
*tp
, int nparent
)
417 for (i
= 0; i
< nparent
; ++i
)
418 if (!(tp
[i
].entry
.mode
& S_IFXMIN_NEQ
))
419 update_tree_entry(&tp
[i
]);
422 static struct combine_diff_path
*ll_diff_tree_paths(
423 struct combine_diff_path
*p
, const struct object_id
*oid
,
424 const struct object_id
**parents_oid
, int nparent
,
425 struct strbuf
*base
, struct diff_options
*opt
)
427 struct tree_desc t
, *tp
;
428 void *ttree
, **tptree
;
431 FAST_ARRAY_ALLOC(tp
, nparent
);
432 FAST_ARRAY_ALLOC(tptree
, nparent
);
435 * load parents first, as they are probably already cached.
437 * ( log_tree_diff() parses commit->parent before calling here via
438 * diff_tree_oid(parent, commit) )
440 for (i
= 0; i
< nparent
; ++i
)
441 tptree
[i
] = fill_tree_descriptor(opt
->repo
, &tp
[i
], parents_oid
[i
]);
442 ttree
= fill_tree_descriptor(opt
->repo
, &t
, oid
);
444 /* Enable recursion indefinitely */
445 opt
->pathspec
.recursive
= opt
->flags
.recursive
;
450 if (diff_can_quit_early(opt
))
453 if (opt
->max_changes
&& diff_queued_diff
.nr
> opt
->max_changes
)
456 if (opt
->pathspec
.nr
) {
457 skip_uninteresting(&t
, base
, opt
);
458 for (i
= 0; i
< nparent
; i
++)
459 skip_uninteresting(&tp
[i
], base
, opt
);
462 /* comparing is finished when all trees are done */
465 for (i
= 0; i
< nparent
; ++i
)
475 * lookup imin = argmin(p1...pn),
476 * mark entries whether they =p[imin] along the way
479 tp
[0].entry
.mode
&= ~S_IFXMIN_NEQ
;
481 for (i
= 1; i
< nparent
; ++i
) {
482 cmp
= tree_entry_pathcmp(&tp
[i
], &tp
[imin
]);
485 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
488 tp
[i
].entry
.mode
&= ~S_IFXMIN_NEQ
;
491 tp
[i
].entry
.mode
|= S_IFXMIN_NEQ
;
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] */
501 /* compare t vs p[imin] */
502 cmp
= tree_entry_pathcmp(&t
, &tp
[imin
]);
506 /* are either pi > p[imin] or diff(t,pi) != ø ? */
507 if (!opt
->flags
.find_copies_harder
) {
508 for (i
= 0; i
< nparent
; ++i
) {
510 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
513 /* diff(t,pi) != ø */
514 if (!oideq(&t
.entry
.oid
, &tp
[i
].entry
.oid
) ||
515 (t
.entry
.mode
!= tp
[i
].entry
.mode
))
522 /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */
523 p
= emit_path(p
, base
, opt
, nparent
,
527 /* t↓, ∀ pi=p[imin] pi↓ */
528 update_tree_entry(&t
);
529 update_tp_entries(tp
, nparent
);
535 p
= emit_path(p
, base
, opt
, nparent
,
536 &t
, /*tp=*/NULL
, -1);
539 update_tree_entry(&t
);
544 /* ∀i pi=p[imin] -> D += "-p[imin]" */
545 if (!opt
->flags
.find_copies_harder
) {
546 for (i
= 0; i
< nparent
; ++i
)
547 if (tp
[i
].entry
.mode
& S_IFXMIN_NEQ
)
551 p
= emit_path(p
, base
, opt
, nparent
,
552 /*t=*/NULL
, tp
, imin
);
555 /* ∀ pi=p[imin] pi↓ */
556 update_tp_entries(tp
, nparent
);
561 for (i
= nparent
-1; i
>= 0; i
--)
563 FAST_ARRAY_FREE(tptree
, nparent
);
564 FAST_ARRAY_FREE(tp
, nparent
);
569 struct combine_diff_path
*diff_tree_paths(
570 struct combine_diff_path
*p
, const struct object_id
*oid
,
571 const struct object_id
**parents_oid
, int nparent
,
572 struct strbuf
*base
, struct diff_options
*opt
)
574 p
= ll_diff_tree_paths(p
, oid
, parents_oid
, nparent
, base
, opt
);
577 * free pre-allocated last element, if any
578 * (see path_appendnew() for details about why)
580 FREE_AND_NULL(p
->next
);
586 * Does it look like the resulting diff might be due to a rename?
588 * - not a valid previous file
590 static inline int diff_might_be_rename(void)
592 return diff_queued_diff
.nr
== 1 &&
593 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
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
)
600 struct diff_options diff_opts
;
601 struct diff_queue_struct
*q
= &diff_queued_diff
;
602 struct diff_filepair
*choice
;
606 * follow-rename code is very specific, we need exactly one
607 * path. Magic that matches more than one path is not
610 GUARD_PATHSPEC(&opt
->pathspec
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
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.
619 if (opt
->pathspec
.has_wildcard
)
620 BUG("wildcards are not supported");
623 /* Remove the file creation entry from the diff queue, and remember it */
624 choice
= q
->queue
[0];
627 repo_diff_setup(opt
->repo
, &diff_opts
);
628 diff_opts
.flags
.recursive
= 1;
629 diff_opts
.flags
.find_copies_harder
= 1;
630 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
631 diff_opts
.single_follow
= opt
->pathspec
.items
[0].match
;
632 diff_opts
.break_opt
= opt
->break_opt
;
633 diff_opts
.rename_score
= opt
->rename_score
;
634 diff_setup_done(&diff_opts
);
635 ll_diff_tree_oid(old_oid
, new_oid
, base
, &diff_opts
);
636 diffcore_std(&diff_opts
);
637 clear_pathspec(&diff_opts
.pathspec
);
639 /* Go through the new set of filepairing, and see if we find a more interesting one */
640 opt
->found_follow
= 0;
641 for (i
= 0; i
< q
->nr
; i
++) {
642 struct diff_filepair
*p
= q
->queue
[i
];
645 * Found a source? Not only do we use that for the new
646 * diff_queued_diff, we will also use that as the path in
649 if ((p
->status
== 'R' || p
->status
== 'C') &&
650 !strcmp(p
->two
->path
, opt
->pathspec
.items
[0].match
)) {
653 /* Switch the file-pairs around */
654 q
->queue
[i
] = choice
;
657 /* Update the path we use from now on.. */
658 path
[0] = p
->one
->path
;
660 clear_pathspec(&opt
->pathspec
);
661 parse_pathspec(&opt
->pathspec
,
662 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
663 PATHSPEC_LITERAL_PATH
, "", path
);
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.
673 opt
->found_follow
= 1;
679 * Then, discard all the non-relevant file pairs...
681 for (i
= 0; i
< q
->nr
; i
++) {
682 struct diff_filepair
*p
= q
->queue
[i
];
683 diff_free_filepair(p
);
687 * .. and re-instate the one we want (which might be either the
688 * original one, or the rename/copy we found)
690 q
->queue
[0] = choice
;
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
)
698 struct combine_diff_path phead
, *p
;
699 pathchange_fn_t pathchange_old
= opt
->pathchange
;
702 opt
->pathchange
= emit_diff_first_parent_only
;
703 diff_tree_paths(&phead
, new_oid
, &old_oid
, 1, base
, opt
);
705 for (p
= phead
.next
; p
;) {
706 struct combine_diff_path
*pprev
= p
;
711 opt
->pathchange
= pathchange_old
;
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
)
720 strbuf_init(&base
, PATH_MAX
);
721 strbuf_addstr(&base
, base_str
);
723 ll_diff_tree_oid(old_oid
, new_oid
, &base
, opt
);
724 if (!*base_str
&& opt
->flags
.follow_renames
&& diff_might_be_rename())
725 try_to_follow_renames(old_oid
, new_oid
, &base
, opt
);
727 strbuf_release(&base
);
730 void diff_root_tree_oid(const struct object_id
*new_oid
,
732 struct diff_options
*opt
)
734 diff_tree_oid(NULL
, new_oid
, base
, opt
);