1package commitgraph
2
3import (
4 "io"
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// CommitNode is generic interface encapsulating a lightweight commit object retrieved
13// from CommitNodeIndex
14type CommitNode interface {
15 // ID returns the Commit object id referenced by the commit graph node.
16 ID() plumbing.Hash
17 // Tree returns the Tree referenced by the commit graph node.
18 Tree() (*object.Tree, error)
19 // CommitTime returns the Committer.When time of the Commit referenced by the commit graph node.
20 CommitTime() time.Time
21 // NumParents returns the number of parents in a commit.
22 NumParents() int
23 // ParentNodes return a CommitNodeIter for parents of specified node.
24 ParentNodes() CommitNodeIter
25 // ParentNode returns the ith parent of a commit.
26 ParentNode(i int) (CommitNode, error)
27 // ParentHashes returns hashes of the parent commits for a specified node
28 ParentHashes() []plumbing.Hash
29 // Generation returns the generation of the commit for reachability analysis.
30 // Objects with newer generation are not reachable from objects of older generation.
31 Generation() uint64
32 // GenerationV2 stores the corrected commit date for the commits
33 // It combines the contents of the GDA2 and GDO2 sections of the commit-graph
34 // with the commit time portion of the CDAT section.
35 GenerationV2() uint64
36 // Commit returns the full commit object from the node
37 Commit() (*object.Commit, error)
38}
39
40// CommitNodeIndex is generic interface encapsulating an index of CommitNode objects
41type CommitNodeIndex interface {
42 // Get returns a commit node from a commit hash
43 Get(hash plumbing.Hash) (CommitNode, error)
44}
45
46// CommitNodeIter is a generic closable interface for iterating over commit nodes.
47type CommitNodeIter interface {
48 Next() (CommitNode, error)
49 ForEach(func(CommitNode) error) error
50 Close()
51}
52
53// parentCommitNodeIter provides an iterator for parent commits from associated CommitNodeIndex.
54type parentCommitNodeIter struct {
55 node CommitNode
56 i int
57}
58
59func newParentgraphCommitNodeIter(node CommitNode) CommitNodeIter {
60 return &parentCommitNodeIter{node, 0}
61}
62
63// Next moves the iterator to the next commit and returns a pointer to it. If
64// there are no more commits, it returns io.EOF.
65func (iter *parentCommitNodeIter) Next() (CommitNode, error) {
66 obj, err := iter.node.ParentNode(iter.i)
67 if err == object.ErrParentNotFound {
68 return nil, io.EOF
69 }
70 if err == nil {
71 iter.i++
72 }
73
74 return obj, err
75}
76
77// ForEach call the cb function for each commit contained on this iter until
78// an error appends or the end of the iter is reached. If ErrStop is sent
79// the iteration is stopped but no error is returned. The iterator is closed.
80func (iter *parentCommitNodeIter) ForEach(cb func(CommitNode) error) error {
81 for {
82 obj, err := iter.Next()
83 if err != nil {
84 if err == io.EOF {
85 return nil
86 }
87
88 return err
89 }
90
91 if err := cb(obj); err != nil {
92 if err == storer.ErrStop {
93 return nil
94 }
95
96 return err
97 }
98 }
99}
100
101func (iter *parentCommitNodeIter) Close() {
102}