Debian Codegraph @

plumbing/object/treenoder.go

raw
1 package object
2
3 import (
4 "io"
5
6 "github.com/go-git/go-git/v5/plumbing"
7 "github.com/go-git/go-git/v5/plumbing/filemode"
8 "github.com/go-git/go-git/v5/utils/merkletrie/noder"
9 )
10
11 // A treenoder is a helper type that wraps git trees into merkletrie
12 // noders.
13 //
14 // As a merkletrie noder doesn't understand the concept of modes (e.g.
15 // file permissions), the treenoder includes the mode of the git tree in
16 // the hash, so changes in the modes will be detected as modifications
17 // to the file contents by the merkletrie difftree algorithm. This is
18 // consistent with how the "git diff-tree" command works.
19 type treeNoder struct {
20 parent *Tree // the root node is its own parent
21 name string // empty string for the root node
22 mode filemode.FileMode
23 hash plumbing.Hash
24 children []noder.Noder // memoized
25 }
26
27 // NewTreeRootNode returns the root node of a Tree
28 func NewTreeRootNode(t *Tree) noder.Noder {
29 if t == nil {
30 return &treeNoder{}
31 }
32
33 return &treeNoder{
34 parent: t,
35 name: "",
36 mode: filemode.Dir,
37 hash: t.Hash,
38 }
39 }
40
41 func (t *treeNoder) Skip() bool {
42 return false
43 }
44
45 func (t *treeNoder) isRoot() bool {
46 return t.name == ""
47 }
48
49 func (t *treeNoder) String() string {
50 return "treeNoder <" + t.name + ">"
51 }
52
53 func (t *treeNoder) Hash() []byte {
54 if t.mode == filemode.Deprecated {
55 return append(t.hash[:], filemode.Regular.Bytes()...)
56 }
57 return append(t.hash[:], t.mode.Bytes()...)
58 }
59
60 func (t *treeNoder) Name() string {
61 return t.name
62 }
63
64 func (t *treeNoder) IsDir() bool {
65 return t.mode == filemode.Dir
66 }
67
68 // Children will return the children of a treenoder as treenoders,
69 // building them from the children of the wrapped git tree.
70 func (t *treeNoder) Children() ([]noder.Noder, error) {
71 if t.mode != filemode.Dir {
72 return noder.NoChildren, nil
73 }
74
75 // children are memoized for efficiency
76 if t.children != nil {
77 return t.children, nil
78 }
79
80 // the parent of the returned children will be ourself as a tree if
81 // we are a not the root treenoder. The root is special as it
82 // is is own parent.
83 parent := t.parent
84 if !t.isRoot() {
85 var err error
86 if parent, err = t.parent.Tree(t.name); err != nil {
87 return nil, err
88 }
89 }
90
91 var err error
92 t.children, err = transformChildren(parent)
93 return t.children, err
94 }
95
96 // Returns the children of a tree as treenoders.
97 // Efficiency is key here.
98 func transformChildren(t *Tree) ([]noder.Noder, error) {
99 var err error
100 var e TreeEntry
101
102 // there will be more tree entries than children in the tree,
103 // due to submodules and empty directories, but I think it is still
104 // worth it to pre-allocate the whole array now, even if sometimes
105 // is bigger than needed.
106 ret := make([]noder.Noder, 0, len(t.Entries))
107
108 walker := NewTreeWalker(t, false, nil) // don't recurse
109 // don't defer walker.Close() for efficiency reasons.
110 for {
111 _, e, err = walker.Next()
112 if err == io.EOF {
113 break
114 }
115 if err != nil {
116 walker.Close()
117 return nil, err
118 }
119
120 ret = append(ret, &treeNoder{
121 parent: t,
122 name: e.Name,
123 mode: e.Mode,
124 hash: e.Hash,
125 })
126 }
127 walker.Close()
128
129 return ret, nil
130 }
131
132 // len(t.tree.Entries) != the number of elements walked by treewalker
133 // for some reason because of empty directories, submodules, etc, so we
134 // have to walk here.
135 func (t *treeNoder) NumChildren() (int, error) {
136 children, err := t.Children()
137 if err != nil {
138 return 0, err
139 }
140
141 return len(children), nil
142 }