Debian Codegraph @

plumbing/object/difftree.go

raw
1 package object
2
3 import (
4 "bytes"
5 "context"
6
7 "github.com/go-git/go-git/v5/utils/merkletrie"
8 "github.com/go-git/go-git/v5/utils/merkletrie/noder"
9 )
10
11 // DiffTree compares the content and mode of the blobs found via two
12 // tree objects.
13 // DiffTree does not perform rename detection, use DiffTreeWithOptions
14 // instead to detect renames.
15 func DiffTree(a, b *Tree) (Changes, error) {
16 return DiffTreeContext(context.Background(), a, b)
17 }
18
19 // DiffTreeContext compares the content and mode of the blobs found via two
20 // tree objects. Provided context must be non-nil.
21 // An error will be returned if context expires.
22 func DiffTreeContext(ctx context.Context, a, b *Tree) (Changes, error) {
23 return DiffTreeWithOptions(ctx, a, b, nil)
24 }
25
26 // DiffTreeOptions are the configurable options when performing a diff tree.
27 type DiffTreeOptions struct {
28 // DetectRenames is whether the diff tree will use rename detection.
29 DetectRenames bool
30 // RenameScore is the threshold to of similarity between files to consider
31 // that a pair of delete and insert are a rename. The number must be
32 // exactly between 0 and 100.
33 RenameScore uint
34 // RenameLimit is the maximum amount of files that can be compared when
35 // detecting renames. The number of comparisons that have to be performed
36 // is equal to the number of deleted files * the number of added files.
37 // That means, that if 100 files were deleted and 50 files were added, 5000
38 // file comparisons may be needed. So, if the rename limit is 50, the number
39 // of both deleted and added needs to be equal or less than 50.
40 // A value of 0 means no limit.
41 RenameLimit uint
42 // OnlyExactRenames performs only detection of exact renames and will not perform
43 // any detection of renames based on file similarity.
44 OnlyExactRenames bool
45 }
46
47 // DefaultDiffTreeOptions are the default and recommended options for the
48 // diff tree.
49 var DefaultDiffTreeOptions = &DiffTreeOptions{
50 DetectRenames: true,
51 RenameScore: 60,
52 RenameLimit: 0,
53 OnlyExactRenames: false,
54 }
55
56 // DiffTreeWithOptions compares the content and mode of the blobs found
57 // via two tree objects with the given options. The provided context
58 // must be non-nil.
59 // If no options are passed, no rename detection will be performed. The
60 // recommended options are DefaultDiffTreeOptions.
61 // An error will be returned if the context expires.
62 // This function will be deprecated and removed in v6 so the default
63 // behaviour of DiffTree is to detect renames.
64 func DiffTreeWithOptions(
65 ctx context.Context,
66 a, b *Tree,
67 opts *DiffTreeOptions,
68 ) (Changes, error) {
69 from := NewTreeRootNode(a)
70 to := NewTreeRootNode(b)
71
72 hashEqual := func(a, b noder.Hasher) bool {
73 return bytes.Equal(a.Hash(), b.Hash())
74 }
75
76 merkletrieChanges, err := merkletrie.DiffTreeContext(ctx, from, to, hashEqual)
77 if err != nil {
78 if err == merkletrie.ErrCanceled {
79 return nil, ErrCanceled
80 }
81 return nil, err
82 }
83
84 changes, err := newChanges(merkletrieChanges)
85 if err != nil {
86 return nil, err
87 }
88
89 if opts == nil {
90 opts = new(DiffTreeOptions)
91 }
92
93 if opts.DetectRenames {
94 return DetectRenames(changes, opts)
95 }
96
97 return changes, nil
98 }