|
1 | 1 | package object |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "io" |
6 | 5 | "time" |
7 | 6 |
|
8 | 7 | "gopkg.in/src-d/go-git.v4/plumbing" |
9 | | - "gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph" |
10 | 8 | "gopkg.in/src-d/go-git.v4/plumbing/storer" |
11 | 9 | ) |
12 | 10 |
|
@@ -45,209 +43,6 @@ type CommitNodeIter interface { |
45 | 43 | Close() |
46 | 44 | } |
47 | 45 |
|
48 | | -// graphCommitNode is a reduced representation of Commit as presented in the commit |
49 | | -// graph file (commitgraph.Node). It is merely useful as an optimization for walking |
50 | | -// the commit graphs. |
51 | | -// |
52 | | -// graphCommitNode implements the CommitNode interface. |
53 | | -type graphCommitNode struct { |
54 | | - // Hash for the Commit object |
55 | | - hash plumbing.Hash |
56 | | - // Index of the node in the commit graph file |
57 | | - index int |
58 | | - |
59 | | - node *commitgraph.Node |
60 | | - gci *graphCommitNodeIndex |
61 | | -} |
62 | | - |
63 | | -// graphCommitNodeIndex is an index that can load CommitNode objects from both the commit |
64 | | -// graph files and the object store. |
65 | | -// |
66 | | -// graphCommitNodeIndex implements the CommitNodeIndex interface |
67 | | -type graphCommitNodeIndex struct { |
68 | | - commitGraph commitgraph.Index |
69 | | - s storer.EncodedObjectStorer |
70 | | -} |
71 | | - |
72 | | -// objectCommitNode is a representation of Commit as presented in the GIT object format. |
73 | | -// |
74 | | -// objectCommitNode implements the CommitNode interface. |
75 | | -type objectCommitNode struct { |
76 | | - nodeIndex CommitNodeIndex |
77 | | - commit *Commit |
78 | | -} |
79 | | - |
80 | | -// objectCommitNodeIndex is an index that can load CommitNode objects only from the |
81 | | -// object store. |
82 | | -// |
83 | | -// objectCommitNodeIndex implements the CommitNodeIndex interface |
84 | | -type objectCommitNodeIndex struct { |
85 | | - s storer.EncodedObjectStorer |
86 | | -} |
87 | | - |
88 | | -// ID returns the Commit object id referenced by the commit graph node. |
89 | | -func (c *graphCommitNode) ID() plumbing.Hash { |
90 | | - return c.hash |
91 | | -} |
92 | | - |
93 | | -// Tree returns the Tree referenced by the commit graph node. |
94 | | -func (c *graphCommitNode) Tree() (*Tree, error) { |
95 | | - return GetTree(c.gci.s, c.node.TreeHash) |
96 | | -} |
97 | | - |
98 | | -// CommitTime returns the Commiter.When time of the Commit referenced by the commit graph node. |
99 | | -func (c *graphCommitNode) CommitTime() time.Time { |
100 | | - return c.node.When |
101 | | -} |
102 | | - |
103 | | -// NumParents returns the number of parents in a commit. |
104 | | -func (c *graphCommitNode) NumParents() int { |
105 | | - return len(c.node.ParentIndexes) |
106 | | -} |
107 | | - |
108 | | -// ParentNodes return a CommitNodeIter for parents of specified node. |
109 | | -func (c *graphCommitNode) ParentNodes() CommitNodeIter { |
110 | | - return newParentgraphCommitNodeIter(c) |
111 | | -} |
112 | | - |
113 | | -// ParentNode returns the ith parent of a commit. |
114 | | -func (c *graphCommitNode) ParentNode(i int) (CommitNode, error) { |
115 | | - if i < 0 || i >= len(c.node.ParentIndexes) { |
116 | | - return nil, ErrParentNotFound |
117 | | - } |
118 | | - |
119 | | - parent, err := c.gci.commitGraph.GetNodeByIndex(c.node.ParentIndexes[i]) |
120 | | - if err != nil { |
121 | | - return nil, err |
122 | | - } |
123 | | - |
124 | | - return &graphCommitNode{ |
125 | | - hash: c.node.ParentHashes[i], |
126 | | - index: c.node.ParentIndexes[i], |
127 | | - node: parent, |
128 | | - gci: c.gci, |
129 | | - }, nil |
130 | | -} |
131 | | - |
132 | | -// ParentHashes returns hashes of the parent commits for a specified node |
133 | | -func (c *graphCommitNode) ParentHashes() []plumbing.Hash { |
134 | | - return c.node.ParentHashes |
135 | | -} |
136 | | - |
137 | | -// Commit returns the full Commit object representing the commit graph node. |
138 | | -func (c *graphCommitNode) Commit() (*Commit, error) { |
139 | | - return GetCommit(c.gci.s, c.hash) |
140 | | -} |
141 | | - |
142 | | -func (c *graphCommitNode) String() string { |
143 | | - return fmt.Sprintf( |
144 | | - "%s %s\nDate: %s", |
145 | | - plumbing.CommitObject, c.ID(), |
146 | | - c.CommitTime().Format(DateFormat), |
147 | | - ) |
148 | | -} |
149 | | - |
150 | | -func NewGraphCommitNodeIndex(commitGraph commitgraph.Index, s storer.EncodedObjectStorer) CommitNodeIndex { |
151 | | - return &graphCommitNodeIndex{commitGraph, s} |
152 | | -} |
153 | | - |
154 | | -// NodeFromHash looks up a commit node by it's object hash |
155 | | -func (gci *graphCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) { |
156 | | - // Check the commit graph first |
157 | | - parentIndex, err := gci.commitGraph.GetIndexByHash(hash) |
158 | | - if err == nil { |
159 | | - parent, err := gci.commitGraph.GetNodeByIndex(parentIndex) |
160 | | - if err != nil { |
161 | | - return nil, err |
162 | | - } |
163 | | - |
164 | | - return &graphCommitNode{ |
165 | | - hash: hash, |
166 | | - index: parentIndex, |
167 | | - node: parent, |
168 | | - gci: gci, |
169 | | - }, nil |
170 | | - } |
171 | | - |
172 | | - // Fallback to loading full commit object |
173 | | - commit, err := GetCommit(gci.s, hash) |
174 | | - if err != nil { |
175 | | - return nil, err |
176 | | - } |
177 | | - |
178 | | - return &objectCommitNode{ |
179 | | - nodeIndex: gci, |
180 | | - commit: commit, |
181 | | - }, nil |
182 | | -} |
183 | | - |
184 | | -// CommitTime returns the time when the commit was performed. |
185 | | -func (c *objectCommitNode) CommitTime() time.Time { |
186 | | - return c.commit.Committer.When |
187 | | -} |
188 | | - |
189 | | -// ID returns the Commit object id referenced by the node. |
190 | | -func (c *objectCommitNode) ID() plumbing.Hash { |
191 | | - return c.commit.ID() |
192 | | -} |
193 | | - |
194 | | -// Tree returns the Tree referenced by the node. |
195 | | -func (c *objectCommitNode) Tree() (*Tree, error) { |
196 | | - return c.commit.Tree() |
197 | | -} |
198 | | - |
199 | | -// NumParents returns the number of parents in a commit. |
200 | | -func (c *objectCommitNode) NumParents() int { |
201 | | - return c.commit.NumParents() |
202 | | -} |
203 | | - |
204 | | -// ParentNodes return a CommitNodeIter for parents of specified node. |
205 | | -func (c *objectCommitNode) ParentNodes() CommitNodeIter { |
206 | | - return newParentgraphCommitNodeIter(c) |
207 | | -} |
208 | | - |
209 | | -// ParentNode returns the ith parent of a commit. |
210 | | -func (c *objectCommitNode) ParentNode(i int) (CommitNode, error) { |
211 | | - if i < 0 || i >= len(c.commit.ParentHashes) { |
212 | | - return nil, ErrParentNotFound |
213 | | - } |
214 | | - |
215 | | - return c.nodeIndex.Get(c.commit.ParentHashes[i]) |
216 | | -} |
217 | | - |
218 | | -// ParentHashes returns hashes of the parent commits for a specified node |
219 | | -func (c *objectCommitNode) ParentHashes() []plumbing.Hash { |
220 | | - return c.commit.ParentHashes |
221 | | -} |
222 | | - |
223 | | -// Commit returns the full Commit object representing the commit graph node. |
224 | | -func (c *objectCommitNode) Commit() (*Commit, error) { |
225 | | - return c.commit, nil |
226 | | -} |
227 | | - |
228 | | -func NewObjectCommitNodeIndex(s storer.EncodedObjectStorer) CommitNodeIndex { |
229 | | - return &objectCommitNodeIndex{s} |
230 | | -} |
231 | | - |
232 | | -// NodeFromHash looks up a commit node by it's object hash |
233 | | -func (oci *objectCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) { |
234 | | - commit, err := GetCommit(oci.s, hash) |
235 | | - if err != nil { |
236 | | - return nil, err |
237 | | - } |
238 | | - |
239 | | - return &objectCommitNode{ |
240 | | - nodeIndex: oci, |
241 | | - commit: commit, |
242 | | - }, nil |
243 | | -} |
244 | | - |
245 | | -// Commit returns the full Commit object representing the commit graph node. |
246 | | -func (oci *objectCommitNodeIndex) Commit(node CommitNode) (*Commit, error) { |
247 | | - co := node.(*objectCommitNode) |
248 | | - return co.commit, nil |
249 | | -} |
250 | | - |
251 | 46 | // parentCommitNodeIter provides an iterator for parent commits from associated CommitNodeIndex. |
252 | 47 | type parentCommitNodeIter struct { |
253 | 48 | node CommitNode |
|
0 commit comments