From f4c1a9140f8b2700d9910d35cbab62b2de1fc857 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Wed, 24 Apr 2019 11:52:12 +0200 Subject: Code cleanup, split into more files for clarity Signed-off-by: Filip Navara --- plumbing/object/commitnode.go | 205 ----------------------------------- plumbing/object/commitnode_graph.go | 121 +++++++++++++++++++++ plumbing/object/commitnode_object.go | 79 ++++++++++++++ 3 files changed, 200 insertions(+), 205 deletions(-) create mode 100644 plumbing/object/commitnode_graph.go create mode 100644 plumbing/object/commitnode_object.go (limited to 'plumbing') diff --git a/plumbing/object/commitnode.go b/plumbing/object/commitnode.go index 0717a65..62c0aef 100644 --- a/plumbing/object/commitnode.go +++ b/plumbing/object/commitnode.go @@ -1,12 +1,10 @@ package object import ( - "fmt" "io" "time" "gopkg.in/src-d/go-git.v4/plumbing" - "gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph" "gopkg.in/src-d/go-git.v4/plumbing/storer" ) @@ -45,209 +43,6 @@ type CommitNodeIter interface { Close() } -// graphCommitNode is a reduced representation of Commit as presented in the commit -// graph file (commitgraph.Node). It is merely useful as an optimization for walking -// the commit graphs. -// -// graphCommitNode implements the CommitNode interface. -type graphCommitNode struct { - // Hash for the Commit object - hash plumbing.Hash - // Index of the node in the commit graph file - index int - - node *commitgraph.Node - gci *graphCommitNodeIndex -} - -// graphCommitNodeIndex is an index that can load CommitNode objects from both the commit -// graph files and the object store. -// -// graphCommitNodeIndex implements the CommitNodeIndex interface -type graphCommitNodeIndex struct { - commitGraph commitgraph.Index - s storer.EncodedObjectStorer -} - -// 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 -} - -// 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 -} - -// ID returns the Commit object id referenced by the commit graph node. -func (c *graphCommitNode) ID() plumbing.Hash { - return c.hash -} - -// Tree returns the Tree referenced by the commit graph node. -func (c *graphCommitNode) Tree() (*Tree, error) { - return GetTree(c.gci.s, c.node.TreeHash) -} - -// CommitTime returns the Commiter.When time of the Commit referenced by the commit graph node. -func (c *graphCommitNode) CommitTime() time.Time { - return c.node.When -} - -// NumParents returns the number of parents in a commit. -func (c *graphCommitNode) NumParents() int { - return len(c.node.ParentIndexes) -} - -// ParentNodes return a CommitNodeIter for parents of specified node. -func (c *graphCommitNode) ParentNodes() CommitNodeIter { - return newParentgraphCommitNodeIter(c) -} - -// ParentNode returns the ith parent of a commit. -func (c *graphCommitNode) ParentNode(i int) (CommitNode, error) { - if i < 0 || i >= len(c.node.ParentIndexes) { - return nil, ErrParentNotFound - } - - parent, err := c.gci.commitGraph.GetNodeByIndex(c.node.ParentIndexes[i]) - if err != nil { - return nil, err - } - - return &graphCommitNode{ - hash: c.node.ParentHashes[i], - index: c.node.ParentIndexes[i], - node: parent, - gci: c.gci, - }, nil -} - -// ParentHashes returns hashes of the parent commits for a specified node -func (c *graphCommitNode) ParentHashes() []plumbing.Hash { - return c.node.ParentHashes -} - -// Commit returns the full Commit object representing the commit graph node. -func (c *graphCommitNode) Commit() (*Commit, error) { - return GetCommit(c.gci.s, c.hash) -} - -func (c *graphCommitNode) String() string { - return fmt.Sprintf( - "%s %s\nDate: %s", - plumbing.CommitObject, c.ID(), - c.CommitTime().Format(DateFormat), - ) -} - -func NewGraphCommitNodeIndex(commitGraph commitgraph.Index, s storer.EncodedObjectStorer) CommitNodeIndex { - return &graphCommitNodeIndex{commitGraph, s} -} - -// NodeFromHash looks up a commit node by it's object hash -func (gci *graphCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) { - // Check the commit graph first - parentIndex, err := gci.commitGraph.GetIndexByHash(hash) - if err == nil { - parent, err := gci.commitGraph.GetNodeByIndex(parentIndex) - if err != nil { - return nil, err - } - - return &graphCommitNode{ - hash: hash, - index: parentIndex, - node: parent, - gci: gci, - }, nil - } - - // Fallback to loading full commit object - commit, err := GetCommit(gci.s, hash) - if err != nil { - return nil, err - } - - return &objectCommitNode{ - nodeIndex: gci, - commit: commit, - }, nil -} - -// CommitTime returns the time when the commit was performed. -func (c *objectCommitNode) CommitTime() time.Time { - return c.commit.Committer.When -} - -// ID returns the Commit object id referenced by the node. -func (c *objectCommitNode) ID() plumbing.Hash { - return c.commit.ID() -} - -// Tree returns the Tree referenced by the node. -func (c *objectCommitNode) Tree() (*Tree, error) { - return c.commit.Tree() -} - -// NumParents returns the number of parents in a commit. -func (c *objectCommitNode) NumParents() int { - return c.commit.NumParents() -} - -// ParentNodes return a CommitNodeIter for parents of specified node. -func (c *objectCommitNode) ParentNodes() CommitNodeIter { - return newParentgraphCommitNodeIter(c) -} - -// ParentNode returns the ith parent of a commit. -func (c *objectCommitNode) ParentNode(i int) (CommitNode, error) { - if i < 0 || i >= len(c.commit.ParentHashes) { - return nil, ErrParentNotFound - } - - return c.nodeIndex.Get(c.commit.ParentHashes[i]) -} - -// ParentHashes returns hashes of the parent commits for a specified node -func (c *objectCommitNode) ParentHashes() []plumbing.Hash { - return c.commit.ParentHashes -} - -// Commit returns the full Commit object representing the commit graph node. -func (c *objectCommitNode) Commit() (*Commit, error) { - return c.commit, nil -} - -func NewObjectCommitNodeIndex(s storer.EncodedObjectStorer) CommitNodeIndex { - return &objectCommitNodeIndex{s} -} - -// NodeFromHash looks up a commit node by it's object hash -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 -} - -// Commit returns the full Commit object representing the commit graph node. -func (oci *objectCommitNodeIndex) Commit(node CommitNode) (*Commit, error) { - co := node.(*objectCommitNode) - return co.commit, nil -} - // parentCommitNodeIter provides an iterator for parent commits from associated CommitNodeIndex. type parentCommitNodeIter struct { node CommitNode diff --git a/plumbing/object/commitnode_graph.go b/plumbing/object/commitnode_graph.go new file mode 100644 index 0000000..c57a258 --- /dev/null +++ b/plumbing/object/commitnode_graph.go @@ -0,0 +1,121 @@ +package object + +import ( + "fmt" + "time" + + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph" + "gopkg.in/src-d/go-git.v4/plumbing/storer" +) + +// graphCommitNode is a reduced representation of Commit as presented in the commit +// graph file (commitgraph.Node). It is merely useful as an optimization for walking +// the commit graphs. +// +// graphCommitNode implements the CommitNode interface. +type graphCommitNode struct { + // Hash for the Commit object + hash plumbing.Hash + // Index of the node in the commit graph file + index int + + node *commitgraph.Node + gci *graphCommitNodeIndex +} + +// graphCommitNodeIndex is an index that can load CommitNode objects from both the commit +// graph files and the object store. +// +// graphCommitNodeIndex implements the CommitNodeIndex interface +type graphCommitNodeIndex struct { + commitGraph commitgraph.Index + s storer.EncodedObjectStorer +} + +func NewGraphCommitNodeIndex(commitGraph commitgraph.Index, s storer.EncodedObjectStorer) CommitNodeIndex { + return &graphCommitNodeIndex{commitGraph, s} +} + +func (gci *graphCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) { + // Check the commit graph first + parentIndex, err := gci.commitGraph.GetIndexByHash(hash) + if err == nil { + parent, err := gci.commitGraph.GetNodeByIndex(parentIndex) + if err != nil { + return nil, err + } + + return &graphCommitNode{ + hash: hash, + index: parentIndex, + node: parent, + gci: gci, + }, nil + } + + // Fallback to loading full commit object + commit, err := GetCommit(gci.s, hash) + if err != nil { + return nil, err + } + + return &objectCommitNode{ + nodeIndex: gci, + commit: commit, + }, nil +} + +func (c *graphCommitNode) ID() plumbing.Hash { + return c.hash +} + +func (c *graphCommitNode) Tree() (*Tree, error) { + return GetTree(c.gci.s, c.node.TreeHash) +} + +func (c *graphCommitNode) CommitTime() time.Time { + return c.node.When +} + +func (c *graphCommitNode) NumParents() int { + return len(c.node.ParentIndexes) +} + +func (c *graphCommitNode) ParentNodes() CommitNodeIter { + return newParentgraphCommitNodeIter(c) +} + +func (c *graphCommitNode) ParentNode(i int) (CommitNode, error) { + if i < 0 || i >= len(c.node.ParentIndexes) { + return nil, ErrParentNotFound + } + + parent, err := c.gci.commitGraph.GetNodeByIndex(c.node.ParentIndexes[i]) + if err != nil { + return nil, err + } + + return &graphCommitNode{ + hash: c.node.ParentHashes[i], + index: c.node.ParentIndexes[i], + node: parent, + gci: c.gci, + }, nil +} + +func (c *graphCommitNode) ParentHashes() []plumbing.Hash { + return c.node.ParentHashes +} + +func (c *graphCommitNode) Commit() (*Commit, error) { + return GetCommit(c.gci.s, c.hash) +} + +func (c *graphCommitNode) String() string { + return fmt.Sprintf( + "%s %s\nDate: %s", + plumbing.CommitObject, c.ID(), + c.CommitTime().Format(DateFormat), + ) +} 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 +} -- cgit