aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object/commitnode_object.go
diff options
context:
space:
mode:
authorFilip Navara <filip.navara@gmail.com>2019-04-24 11:52:12 +0200
committerFilip Navara <filip.navara@gmail.com>2019-04-24 11:52:12 +0200
commitf4c1a9140f8b2700d9910d35cbab62b2de1fc857 (patch)
tree6ad62c9673ac633957c15f36c0e0d4fd0ba6eed3 /plumbing/object/commitnode_object.go
parent58c731411944090126f86208bcf0419d6ba84122 (diff)
downloadgo-git-f4c1a9140f8b2700d9910d35cbab62b2de1fc857.tar.gz
Code cleanup, split into more files for clarity
Signed-off-by: Filip Navara <filip.navara@gmail.com>
Diffstat (limited to 'plumbing/object/commitnode_object.go')
-rw-r--r--plumbing/object/commitnode_object.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/plumbing/object/commitnode_object.go b/plumbing/object/commitnode_object.go
new file mode 100644
index 0000000..08d8c0f
--- /dev/null
+++ b/plumbing/object/commitnode_object.go
@@ -0,0 +1,79 @@
+package object
+
+import (
+ "time"
+
+ "gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/storer"
+)
+
+// objectCommitNode is a representation of Commit as presented in the GIT object format.
+//
+// objectCommitNode implements the CommitNode interface.
+type objectCommitNode struct {
+ nodeIndex CommitNodeIndex
+ commit *Commit
+}
+
+func NewObjectCommitNodeIndex(s storer.EncodedObjectStorer) CommitNodeIndex {
+ return &objectCommitNodeIndex{s}
+}
+
+func (oci *objectCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) {
+ commit, err := GetCommit(oci.s, hash)
+ if err != nil {
+ return nil, err
+ }
+
+ return &objectCommitNode{
+ nodeIndex: oci,
+ commit: commit,
+ }, nil
+}
+
+// objectCommitNodeIndex is an index that can load CommitNode objects only from the
+// object store.
+//
+// objectCommitNodeIndex implements the CommitNodeIndex interface
+type objectCommitNodeIndex struct {
+ s storer.EncodedObjectStorer
+}
+
+func (c *objectCommitNode) CommitTime() time.Time {
+ return c.commit.Committer.When
+}
+
+func (c *objectCommitNode) ID() plumbing.Hash {
+ return c.commit.ID()
+}
+
+func (c *objectCommitNode) Tree() (*Tree, error) {
+ return c.commit.Tree()
+}
+
+func (c *objectCommitNode) NumParents() int {
+ return c.commit.NumParents()
+}
+
+func (c *objectCommitNode) ParentNodes() CommitNodeIter {
+ return newParentgraphCommitNodeIter(c)
+}
+
+func (c *objectCommitNode) ParentNode(i int) (CommitNode, error) {
+ if i < 0 || i >= len(c.commit.ParentHashes) {
+ return nil, ErrParentNotFound
+ }
+
+ // Note: It's necessary to go through CommitNodeIndex here to ensure
+ // that if the commit-graph file covers only part of the history we
+ // start using it when that part is reached.
+ return c.nodeIndex.Get(c.commit.ParentHashes[i])
+}
+
+func (c *objectCommitNode) ParentHashes() []plumbing.Hash {
+ return c.commit.ParentHashes
+}
+
+func (c *objectCommitNode) Commit() (*Commit, error) {
+ return c.commit, nil
+}