Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit f4c1a91

Browse files
committed
Code cleanup, split into more files for clarity
Signed-off-by: Filip Navara <filip.navara@gmail.com>
1 parent 58c7314 commit f4c1a91

File tree

3 files changed

+200
-205
lines changed

3 files changed

+200
-205
lines changed

plumbing/object/commitnode.go

Lines changed: 0 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package object
22

33
import (
4-
"fmt"
54
"io"
65
"time"
76

87
"gopkg.in/src-d/go-git.v4/plumbing"
9-
"gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph"
108
"gopkg.in/src-d/go-git.v4/plumbing/storer"
119
)
1210

@@ -45,209 +43,6 @@ type CommitNodeIter interface {
4543
Close()
4644
}
4745

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-
25146
// parentCommitNodeIter provides an iterator for parent commits from associated CommitNodeIndex.
25247
type parentCommitNodeIter struct {
25348
node CommitNode
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package object
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"gopkg.in/src-d/go-git.v4/plumbing"
8+
"gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph"
9+
"gopkg.in/src-d/go-git.v4/plumbing/storer"
10+
)
11+
12+
// graphCommitNode is a reduced representation of Commit as presented in the commit
13+
// graph file (commitgraph.Node). It is merely useful as an optimization for walking
14+
// the commit graphs.
15+
//
16+
// graphCommitNode implements the CommitNode interface.
17+
type graphCommitNode struct {
18+
// Hash for the Commit object
19+
hash plumbing.Hash
20+
// Index of the node in the commit graph file
21+
index int
22+
23+
node *commitgraph.Node
24+
gci *graphCommitNodeIndex
25+
}
26+
27+
// graphCommitNodeIndex is an index that can load CommitNode objects from both the commit
28+
// graph files and the object store.
29+
//
30+
// graphCommitNodeIndex implements the CommitNodeIndex interface
31+
type graphCommitNodeIndex struct {
32+
commitGraph commitgraph.Index
33+
s storer.EncodedObjectStorer
34+
}
35+
36+
func NewGraphCommitNodeIndex(commitGraph commitgraph.Index, s storer.EncodedObjectStorer) CommitNodeIndex {
37+
return &graphCommitNodeIndex{commitGraph, s}
38+
}
39+
40+
func (gci *graphCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) {
41+
// Check the commit graph first
42+
parentIndex, err := gci.commitGraph.GetIndexByHash(hash)
43+
if err == nil {
44+
parent, err := gci.commitGraph.GetNodeByIndex(parentIndex)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
return &graphCommitNode{
50+
hash: hash,
51+
index: parentIndex,
52+
node: parent,
53+
gci: gci,
54+
}, nil
55+
}
56+
57+
// Fallback to loading full commit object
58+
commit, err := GetCommit(gci.s, hash)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
return &objectCommitNode{
64+
nodeIndex: gci,
65+
commit: commit,
66+
}, nil
67+
}
68+
69+
func (c *graphCommitNode) ID() plumbing.Hash {
70+
return c.hash
71+
}
72+
73+
func (c *graphCommitNode) Tree() (*Tree, error) {
74+
return GetTree(c.gci.s, c.node.TreeHash)
75+
}
76+
77+
func (c *graphCommitNode) CommitTime() time.Time {
78+
return c.node.When
79+
}
80+
81+
func (c *graphCommitNode) NumParents() int {
82+
return len(c.node.ParentIndexes)
83+
}
84+
85+
func (c *graphCommitNode) ParentNodes() CommitNodeIter {
86+
return newParentgraphCommitNodeIter(c)
87+
}
88+
89+
func (c *graphCommitNode) ParentNode(i int) (CommitNode, error) {
90+
if i < 0 || i >= len(c.node.ParentIndexes) {
91+
return nil, ErrParentNotFound
92+
}
93+
94+
parent, err := c.gci.commitGraph.GetNodeByIndex(c.node.ParentIndexes[i])
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
return &graphCommitNode{
100+
hash: c.node.ParentHashes[i],
101+
index: c.node.ParentIndexes[i],
102+
node: parent,
103+
gci: c.gci,
104+
}, nil
105+
}
106+
107+
func (c *graphCommitNode) ParentHashes() []plumbing.Hash {
108+
return c.node.ParentHashes
109+
}
110+
111+
func (c *graphCommitNode) Commit() (*Commit, error) {
112+
return GetCommit(c.gci.s, c.hash)
113+
}
114+
115+
func (c *graphCommitNode) String() string {
116+
return fmt.Sprintf(
117+
"%s %s\nDate: %s",
118+
plumbing.CommitObject, c.ID(),
119+
c.CommitTime().Format(DateFormat),
120+
)
121+
}

0 commit comments

Comments
 (0)