Debian Codegraph @

plumbing/object/commitgraph/commitnode_object.go

raw
1 package commitgraph
2
3 import (
4 "math"
5 "time"
6
7 "github.com/go-git/go-git/v5/plumbing"
8 "github.com/go-git/go-git/v5/plumbing/object"
9 "github.com/go-git/go-git/v5/plumbing/storer"
10 )
11
12 // objectCommitNode is a representation of Commit as presented in the GIT object format.
13 //
14 // objectCommitNode implements the CommitNode interface.
15 type objectCommitNode struct {
16 nodeIndex CommitNodeIndex
17 commit *object.Commit
18 }
19
20 // NewObjectCommitNodeIndex returns CommitNodeIndex implementation that uses
21 // only object storage to load the nodes
22 func NewObjectCommitNodeIndex(s storer.EncodedObjectStorer) CommitNodeIndex {
23 return &objectCommitNodeIndex{s}
24 }
25
26 func (oci *objectCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) {
27 commit, err := object.GetCommit(oci.s, hash)
28 if err != nil {
29 return nil, err
30 }
31
32 return &objectCommitNode{
33 nodeIndex: oci,
34 commit: commit,
35 }, nil
36 }
37
38 // objectCommitNodeIndex is an index that can load CommitNode objects only from the
39 // object store.
40 //
41 // objectCommitNodeIndex implements the CommitNodeIndex interface
42 type objectCommitNodeIndex struct {
43 s storer.EncodedObjectStorer
44 }
45
46 func (c *objectCommitNode) CommitTime() time.Time {
47 return c.commit.Committer.When
48 }
49
50 func (c *objectCommitNode) ID() plumbing.Hash {
51 return c.commit.ID()
52 }
53
54 func (c *objectCommitNode) Tree() (*object.Tree, error) {
55 return c.commit.Tree()
56 }
57
58 func (c *objectCommitNode) NumParents() int {
59 return c.commit.NumParents()
60 }
61
62 func (c *objectCommitNode) ParentNodes() CommitNodeIter {
63 return newParentgraphCommitNodeIter(c)
64 }
65
66 func (c *objectCommitNode) ParentNode(i int) (CommitNode, error) {
67 if i < 0 || i >= len(c.commit.ParentHashes) {
68 return nil, object.ErrParentNotFound
69 }
70
71 // Note: It's necessary to go through CommitNodeIndex here to ensure
72 // that if the commit-graph file covers only part of the history we
73 // start using it when that part is reached.
74 return c.nodeIndex.Get(c.commit.ParentHashes[i])
75 }
76
77 func (c *objectCommitNode) ParentHashes() []plumbing.Hash {
78 return c.commit.ParentHashes
79 }
80
81 func (c *objectCommitNode) Generation() uint64 {
82 // Commit nodes representing objects outside of the commit graph can never
83 // be reached by objects from the commit-graph thus we return the highest
84 // possible value.
85 return math.MaxUint64
86 }
87
88 func (c *objectCommitNode) GenerationV2() uint64 {
89 // Commit nodes representing objects outside of the commit graph can never
90 // be reached by objects from the commit-graph thus we return the highest
91 // possible value.
92 return math.MaxUint64
93 }
94
95 func (c *objectCommitNode) Commit() (*object.Commit, error) {
96 return c.commit, nil
97 }