git
/
tree-diff.c
744 строки · 18.7 Кб
1/*
2* Helper functions for tree diff generation
3*/
4#include "git-compat-util.h"5#include "diff.h"6#include "diffcore.h"7#include "hash.h"8#include "tree.h"9#include "tree-walk.h"10#include "environment.h"11#include "repository.h"12
13/*
14* Some mode bits are also used internally for computations.
15*
16* They *must* not overlap with any valid modes, and they *must* not be emitted
17* to outside world - i.e. appear on disk or network. In other words, it's just
18* temporary fields, which we internally use, but they have to stay in-house.
19*
20* ( such approach is valid, as standard S_IF* fits into 16 bits, and in Git
21* codebase mode is `unsigned int` which is assumed to be at least 32 bits )
22*/
23
24#define S_DIFFTREE_IFXMIN_NEQ 0x8000000025
26/*
27* internal mode marker, saying a tree entry != entry of tp[imin]
28* (see ll_diff_tree_paths for what it means there)
29*
30* we will update/use/emit entry for diff only with it unset.
31*/
32#define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ33
34#define FAST_ARRAY_ALLOC(x, nr) do { \35if ((nr) <= 2) \36(x) = xalloca((nr) * sizeof(*(x))); \37else \38ALLOC_ARRAY((x), nr); \39} while(0)40#define FAST_ARRAY_FREE(x, nr) do { \41if ((nr) <= 2) \42xalloca_free((x)); \43else \44free((x)); \45} while(0)46
47static struct combine_diff_path *ll_diff_tree_paths(48struct combine_diff_path *p, const struct object_id *oid,49const struct object_id **parents_oid, int nparent,50struct strbuf *base, struct diff_options *opt,51int depth);52static void ll_diff_tree_oid(const struct object_id *old_oid,53const struct object_id *new_oid,54struct strbuf *base, struct diff_options *opt);55
56/*
57* Compare two tree entries, taking into account only path/S_ISDIR(mode),
58* but not their sha1's.
59*
60* NOTE files and directories *always* compare differently, even when having
61* the same name - thanks to base_name_compare().
62*
63* NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
64* so that they sort *after* valid tree entries.
65*
66* Due to this convention, if trees are scanned in sorted order, all
67* non-empty descriptors will be processed first.
68*/
69static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2)70{
71struct name_entry *e1, *e2;72int cmp;73
74/* empty descriptors sort after valid tree entries */75if (!t1->size)76return t2->size ? 1 : 0;77else if (!t2->size)78return -1;79
80e1 = &t1->entry;81e2 = &t2->entry;82cmp = base_name_compare(e1->path, tree_entry_len(e1), e1->mode,83e2->path, tree_entry_len(e2), e2->mode);84return cmp;85}
86
87
88/*
89* convert path -> opt->diff_*() callbacks
90*
91* emits diff to first parent only, and tells diff tree-walker that we are done
92* with p and it can be freed.
93*/
94static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_diff_path *p)95{
96struct combine_diff_parent *p0 = &p->parent[0];97if (p->mode && p0->mode) {98opt->change(opt, p0->mode, p->mode, &p0->oid, &p->oid,991, 1, p->path, 0, 0);100}101else {102const struct object_id *oid;103unsigned int mode;104int addremove;105
106if (p->mode) {107addremove = '+';108oid = &p->oid;109mode = p->mode;110} else {111addremove = '-';112oid = &p0->oid;113mode = p0->mode;114}115
116opt->add_remove(opt, addremove, mode, oid, 1, p->path, 0);117}118
119return 0; /* we are done with p */120}
121
122
123/*
124* Make a new combine_diff_path from path/mode/sha1
125* and append it to paths list tail.
126*
127* Memory for created elements could be reused:
128*
129* - if last->next == NULL, the memory is allocated;
130*
131* - if last->next != NULL, it is assumed that p=last->next was returned
132* earlier by this function, and p->next was *not* modified.
133* The memory is then reused from p.
134*
135* so for clients,
136*
137* - if you do need to keep the element
138*
139* p = path_appendnew(p, ...);
140* process(p);
141* p->next = NULL;
142*
143* - if you don't need to keep the element after processing
144*
145* pprev = p;
146* p = path_appendnew(p, ...);
147* process(p);
148* p = pprev;
149* ; don't forget to free tail->next in the end
150*
151* p->parent[] remains uninitialized.
152*/
153static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,154int nparent, const struct strbuf *base, const char *path, int pathlen,155unsigned mode, const struct object_id *oid)156{
157struct combine_diff_path *p;158size_t len = st_add(base->len, pathlen);159size_t alloclen = combine_diff_path_size(nparent, len);160
161/* if last->next is !NULL - it is a pre-allocated memory, we can reuse */162p = last->next;163if (p && (alloclen > (intptr_t)p->next)) {164FREE_AND_NULL(p);165}166
167if (!p) {168p = xmalloc(alloclen);169
170/*171* until we go to it next round, .next holds how many bytes we
172* allocated (for faster realloc - we don't need copying old data).
173*/
174p->next = (struct combine_diff_path *)(intptr_t)alloclen;175}176
177last->next = p;178
179p->path = (char *)&(p->parent[nparent]);180memcpy(p->path, base->buf, base->len);181memcpy(p->path + base->len, path, pathlen);182p->path[len] = 0;183p->mode = mode;184oidcpy(&p->oid, oid ? oid : null_oid());185
186return p;187}
188
189/*
190* new path should be added to combine diff
191*
192* 3 cases on how/when it should be called and behaves:
193*
194* t, !tp -> path added, all parents lack it
195* !t, tp -> path removed from all parents
196* t, tp -> path modified/added
197* (M for tp[i]=tp[imin], A otherwise)
198*/
199static struct combine_diff_path *emit_path(struct combine_diff_path *p,200struct strbuf *base, struct diff_options *opt, int nparent,201struct tree_desc *t, struct tree_desc *tp,202int imin, int depth)203{
204unsigned short mode;205const char *path;206const struct object_id *oid;207int pathlen;208int old_baselen = base->len;209int i, isdir, recurse = 0, emitthis = 1;210
211/* at least something has to be valid */212assert(t || tp);213
214if (t) {215/* path present in resulting tree */216oid = tree_entry_extract(t, &path, &mode);217pathlen = tree_entry_len(&t->entry);218isdir = S_ISDIR(mode);219} else {220/*221* a path was removed - take path from imin parent. Also take
222* mode from that parent, to decide on recursion(1).
223*
224* 1) all modes for tp[i]=tp[imin] should be the same wrt
225* S_ISDIR, thanks to base_name_compare().
226*/
227tree_entry_extract(&tp[imin], &path, &mode);228pathlen = tree_entry_len(&tp[imin].entry);229
230isdir = S_ISDIR(mode);231oid = NULL;232mode = 0;233}234
235if (opt->flags.recursive && isdir) {236recurse = 1;237emitthis = opt->flags.tree_in_recursive;238}239
240if (emitthis) {241int keep;242struct combine_diff_path *pprev = p;243p = path_appendnew(p, nparent, base, path, pathlen, mode, oid);244
245for (i = 0; i < nparent; ++i) {246/*247* tp[i] is valid, if present and if tp[i]==tp[imin] -
248* otherwise, we should ignore it.
249*/
250int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);251
252const struct object_id *oid_i;253unsigned mode_i;254
255p->parent[i].status =256!t ? DIFF_STATUS_DELETED :257tpi_valid ?258DIFF_STATUS_MODIFIED :259DIFF_STATUS_ADDED;260
261if (tpi_valid) {262oid_i = &tp[i].entry.oid;263mode_i = tp[i].entry.mode;264}265else {266oid_i = null_oid();267mode_i = 0;268}269
270p->parent[i].mode = mode_i;271oidcpy(&p->parent[i].oid, oid_i);272}273
274keep = 1;275if (opt->pathchange)276keep = opt->pathchange(opt, p);277
278/*279* If a path was filtered or consumed - we don't need to add it
280* to the list and can reuse its memory, leaving it as
281* pre-allocated element on the tail.
282*
283* On the other hand, if path needs to be kept, we need to
284* correct its .next to NULL, as it was pre-initialized to how
285* much memory was allocated.
286*
287* see path_appendnew() for details.
288*/
289if (!keep)290p = pprev;291else292p->next = NULL;293}294
295if (recurse) {296const struct object_id **parents_oid;297
298FAST_ARRAY_ALLOC(parents_oid, nparent);299for (i = 0; i < nparent; ++i) {300/* same rule as in emitthis */301int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);302
303parents_oid[i] = tpi_valid ? &tp[i].entry.oid : NULL;304}305
306strbuf_add(base, path, pathlen);307strbuf_addch(base, '/');308p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt,309depth + 1);310FAST_ARRAY_FREE(parents_oid, nparent);311}312
313strbuf_setlen(base, old_baselen);314return p;315}
316
317static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,318struct diff_options *opt)319{
320enum interesting match;321
322while (t->size) {323match = tree_entry_interesting(opt->repo->index, &t->entry,324base, &opt->pathspec);325if (match) {326if (match == all_entries_not_interesting)327t->size = 0;328break;329}330update_tree_entry(t);331}332}
333
334
335/*
336* generate paths for combined diff D(sha1,parents_oid[])
337*
338* Resulting paths are appended to combine_diff_path linked list, and also, are
339* emitted on the go via opt->pathchange() callback, so it is possible to
340* process the result as batch or incrementally.
341*
342* The paths are generated scanning new tree and all parents trees
343* simultaneously, similarly to what diff_tree() was doing for 2 trees.
344* The theory behind such scan is as follows:
345*
346*
347* D(T,P1...Pn) calculation scheme
348* -------------------------------
349*
350* D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set)
351*
352* D(T,Pj) - diff between T..Pj
353* D(T,P1...Pn) - combined diff from T to parents P1,...,Pn
354*
355*
356* We start from all trees, which are sorted, and compare their entries in
357* lock-step:
358*
359* T P1 Pn
360* - - -
361* |t| |p1| |pn|
362* |-| |--| ... |--| imin = argmin(p1...pn)
363* | | | | | |
364* |-| |--| |--|
365* |.| |. | |. |
366* . . .
367* . . .
368*
369* at any time there could be 3 cases:
370*
371* 1) t < p[imin];
372* 2) t > p[imin];
373* 3) t = p[imin].
374*
375* Schematic deduction of what every case means, and what to do, follows:
376*
377* 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓
378*
379* 2) t > p[imin]
380*
381* 2.1) ∃j: pj > p[imin] -> "-p[imin]" ∉ D(T,Pj) -> D += ø; ∀ pi=p[imin] pi↓
382* 2.2) ∀i pi = p[imin] -> pi ∉ T -> "-pi" ∈ D(T,Pi) -> D += "-p[imin]"; ∀i pi↓
383*
384* 3) t = p[imin]
385*
386* 3.1) ∃j: pj > p[imin] -> "+t" ∈ D(T,Pj) -> only pi=p[imin] remains to investigate
387* 3.2) pi = p[imin] -> investigate δ(t,pi)
388* |
389* |
390* v
391*
392* 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø ->
393*
394* ⎧δ(t,pi) - if pi=p[imin]
395* -> D += ⎨
396* ⎩"+t" - if pi>p[imin]
397*
398*
399* in any case t↓ ∀ pi=p[imin] pi↓
400*
401*
402* ~~~~~~~~
403*
404* NOTE
405*
406* Usual diff D(A,B) is by definition the same as combined diff D(A,[B]),
407* so this diff paths generator can, and is used, for plain diffs
408* generation too.
409*
410* Please keep attention to the common D(A,[B]) case when working on the
411* code, in order not to slow it down.
412*
413* NOTE
414* nparent must be > 0.
415*/
416
417
418/* ∀ pi=p[imin] pi↓ */
419static inline void update_tp_entries(struct tree_desc *tp, int nparent)420{
421int i;422for (i = 0; i < nparent; ++i)423if (!(tp[i].entry.mode & S_IFXMIN_NEQ))424update_tree_entry(&tp[i]);425}
426
427static struct combine_diff_path *ll_diff_tree_paths(428struct combine_diff_path *p, const struct object_id *oid,429const struct object_id **parents_oid, int nparent,430struct strbuf *base, struct diff_options *opt,431int depth)432{
433struct tree_desc t, *tp;434void *ttree, **tptree;435int i;436
437if (depth > max_allowed_tree_depth)438die("exceeded maximum allowed tree depth");439
440FAST_ARRAY_ALLOC(tp, nparent);441FAST_ARRAY_ALLOC(tptree, nparent);442
443/*444* load parents first, as they are probably already cached.
445*
446* ( log_tree_diff() parses commit->parent before calling here via
447* diff_tree_oid(parent, commit) )
448*/
449for (i = 0; i < nparent; ++i)450tptree[i] = fill_tree_descriptor(opt->repo, &tp[i], parents_oid[i]);451ttree = fill_tree_descriptor(opt->repo, &t, oid);452
453/* Enable recursion indefinitely */454opt->pathspec.recursive = opt->flags.recursive;455
456for (;;) {457int imin, cmp;458
459if (diff_can_quit_early(opt))460break;461
462if (opt->max_changes && diff_queued_diff.nr > opt->max_changes)463break;464
465if (opt->pathspec.nr) {466skip_uninteresting(&t, base, opt);467for (i = 0; i < nparent; i++)468skip_uninteresting(&tp[i], base, opt);469}470
471/* comparing is finished when all trees are done */472if (!t.size) {473int done = 1;474for (i = 0; i < nparent; ++i)475if (tp[i].size) {476done = 0;477break;478}479if (done)480break;481}482
483/*484* lookup imin = argmin(p1...pn),
485* mark entries whether they =p[imin] along the way
486*/
487imin = 0;488tp[0].entry.mode &= ~S_IFXMIN_NEQ;489
490for (i = 1; i < nparent; ++i) {491cmp = tree_entry_pathcmp(&tp[i], &tp[imin]);492if (cmp < 0) {493imin = i;494tp[i].entry.mode &= ~S_IFXMIN_NEQ;495}496else if (cmp == 0) {497tp[i].entry.mode &= ~S_IFXMIN_NEQ;498}499else {500tp[i].entry.mode |= S_IFXMIN_NEQ;501}502}503
504/* fixup markings for entries before imin */505for (i = 0; i < imin; ++i)506tp[i].entry.mode |= S_IFXMIN_NEQ; /* pi > p[imin] */507
508
509
510/* compare t vs p[imin] */511cmp = tree_entry_pathcmp(&t, &tp[imin]);512
513/* t = p[imin] */514if (cmp == 0) {515/* are either pi > p[imin] or diff(t,pi) != ø ? */516if (!opt->flags.find_copies_harder) {517for (i = 0; i < nparent; ++i) {518/* p[i] > p[imin] */519if (tp[i].entry.mode & S_IFXMIN_NEQ)520continue;521
522/* diff(t,pi) != ø */523if (!oideq(&t.entry.oid, &tp[i].entry.oid) ||524(t.entry.mode != tp[i].entry.mode))525continue;526
527goto skip_emit_t_tp;528}529}530
531/* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */532p = emit_path(p, base, opt, nparent,533&t, tp, imin, depth);534
535skip_emit_t_tp:536/* t↓, ∀ pi=p[imin] pi↓ */537update_tree_entry(&t);538update_tp_entries(tp, nparent);539}540
541/* t < p[imin] */542else if (cmp < 0) {543/* D += "+t" */544p = emit_path(p, base, opt, nparent,545&t, /*tp=*/NULL, -1, depth);546
547/* t↓ */548update_tree_entry(&t);549}550
551/* t > p[imin] */552else {553/* ∀i pi=p[imin] -> D += "-p[imin]" */554if (!opt->flags.find_copies_harder) {555for (i = 0; i < nparent; ++i)556if (tp[i].entry.mode & S_IFXMIN_NEQ)557goto skip_emit_tp;558}559
560p = emit_path(p, base, opt, nparent,561/*t=*/NULL, tp, imin, depth);562
563skip_emit_tp:564/* ∀ pi=p[imin] pi↓ */565update_tp_entries(tp, nparent);566}567}568
569free(ttree);570for (i = nparent-1; i >= 0; i--)571free(tptree[i]);572FAST_ARRAY_FREE(tptree, nparent);573FAST_ARRAY_FREE(tp, nparent);574
575return p;576}
577
578struct combine_diff_path *diff_tree_paths(579struct combine_diff_path *p, const struct object_id *oid,580const struct object_id **parents_oid, int nparent,581struct strbuf *base, struct diff_options *opt)582{
583p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt, 0);584
585/*586* free pre-allocated last element, if any
587* (see path_appendnew() for details about why)
588*/
589FREE_AND_NULL(p->next);590
591return p;592}
593
594/*
595* Does it look like the resulting diff might be due to a rename?
596* - single entry
597* - not a valid previous file
598*/
599static inline int diff_might_be_rename(void)600{
601return diff_queued_diff.nr == 1 &&602!DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);603}
604
605static void try_to_follow_renames(const struct object_id *old_oid,606const struct object_id *new_oid,607struct strbuf *base, struct diff_options *opt)608{
609struct diff_options diff_opts;610struct diff_queue_struct *q = &diff_queued_diff;611struct diff_filepair *choice;612int i;613
614/*615* follow-rename code is very specific, we need exactly one
616* path. Magic that matches more than one path is not
617* supported.
618*/
619GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);620#if 0621/*622* We should reject wildcards as well. Unfortunately we
623* haven't got a reliable way to detect that 'foo\*bar' in
624* fact has no wildcards. nowildcard_len is merely a hint for
625* optimization. Let it slip for now until wildmatch is taught
626* about dry-run mode and returns wildcard info.
627*/
628if (opt->pathspec.has_wildcard)629BUG("wildcards are not supported");630#endif631
632/* Remove the file creation entry from the diff queue, and remember it */633choice = q->queue[0];634q->nr = 0;635
636repo_diff_setup(opt->repo, &diff_opts);637diff_opts.flags.recursive = 1;638diff_opts.flags.find_copies_harder = 1;639diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;640diff_opts.single_follow = opt->pathspec.items[0].match;641diff_opts.break_opt = opt->break_opt;642diff_opts.rename_score = opt->rename_score;643diff_setup_done(&diff_opts);644ll_diff_tree_oid(old_oid, new_oid, base, &diff_opts);645diffcore_std(&diff_opts);646clear_pathspec(&diff_opts.pathspec);647
648/* Go through the new set of filepairing, and see if we find a more interesting one */649opt->found_follow = 0;650for (i = 0; i < q->nr; i++) {651struct diff_filepair *p = q->queue[i];652
653/*654* Found a source? Not only do we use that for the new
655* diff_queued_diff, we will also use that as the path in
656* the future!
657*/
658if ((p->status == 'R' || p->status == 'C') &&659!strcmp(p->two->path, opt->pathspec.items[0].match)) {660const char *path[2];661
662/* Switch the file-pairs around */663q->queue[i] = choice;664choice = p;665
666/* Update the path we use from now on.. */667path[0] = p->one->path;668path[1] = NULL;669clear_pathspec(&opt->pathspec);670parse_pathspec(&opt->pathspec,671PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,672PATHSPEC_LITERAL_PATH, "", path);673
674/*675* The caller expects us to return a set of vanilla
676* filepairs to let a later call to diffcore_std()
677* it makes to sort the renames out (among other
678* things), but we already have found renames
679* ourselves; signal diffcore_std() not to muck with
680* rename information.
681*/
682opt->found_follow = 1;683break;684}685}686
687/*688* Then, discard all the non-relevant file pairs...
689*/
690for (i = 0; i < q->nr; i++) {691struct diff_filepair *p = q->queue[i];692diff_free_filepair(p);693}694
695/*696* .. and re-instate the one we want (which might be either the
697* original one, or the rename/copy we found)
698*/
699q->queue[0] = choice;700q->nr = 1;701}
702
703static void ll_diff_tree_oid(const struct object_id *old_oid,704const struct object_id *new_oid,705struct strbuf *base, struct diff_options *opt)706{
707struct combine_diff_path phead, *p;708pathchange_fn_t pathchange_old = opt->pathchange;709
710phead.next = NULL;711opt->pathchange = emit_diff_first_parent_only;712diff_tree_paths(&phead, new_oid, &old_oid, 1, base, opt);713
714for (p = phead.next; p;) {715struct combine_diff_path *pprev = p;716p = p->next;717free(pprev);718}719
720opt->pathchange = pathchange_old;721}
722
723void diff_tree_oid(const struct object_id *old_oid,724const struct object_id *new_oid,725const char *base_str, struct diff_options *opt)726{
727struct strbuf base;728
729strbuf_init(&base, PATH_MAX);730strbuf_addstr(&base, base_str);731
732ll_diff_tree_oid(old_oid, new_oid, &base, opt);733if (!*base_str && opt->flags.follow_renames && diff_might_be_rename())734try_to_follow_renames(old_oid, new_oid, &base, opt);735
736strbuf_release(&base);737}
738
739void diff_root_tree_oid(const struct object_id *new_oid,740const char *base,741struct diff_options *opt)742{
743diff_tree_oid(NULL, new_oid, base, opt);744}
745