本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9KB

  1. package commitgraph
  2. import (
  3. "io"
  4. "time"
  5. "gopkg.in/src-d/go-git.v4/plumbing"
  6. "gopkg.in/src-d/go-git.v4/plumbing/object"
  7. "gopkg.in/src-d/go-git.v4/plumbing/storer"
  8. )
  9. // CommitNode is generic interface encapsulating a lightweight commit object retrieved
  10. // from CommitNodeIndex
  11. type CommitNode interface {
  12. // ID returns the Commit object id referenced by the commit graph node.
  13. ID() plumbing.Hash
  14. // Tree returns the Tree referenced by the commit graph node.
  15. Tree() (*object.Tree, error)
  16. // CommitTime returns the Commiter.When time of the Commit referenced by the commit graph node.
  17. CommitTime() time.Time
  18. // NumParents returns the number of parents in a commit.
  19. NumParents() int
  20. // ParentNodes return a CommitNodeIter for parents of specified node.
  21. ParentNodes() CommitNodeIter
  22. // ParentNode returns the ith parent of a commit.
  23. ParentNode(i int) (CommitNode, error)
  24. // ParentHashes returns hashes of the parent commits for a specified node
  25. ParentHashes() []plumbing.Hash
  26. // Generation returns the generation of the commit for reachability analysis.
  27. // Objects with newer generation are not reachable from objects of older generation.
  28. Generation() uint64
  29. // Commit returns the full commit object from the node
  30. Commit() (*object.Commit, error)
  31. }
  32. // CommitNodeIndex is generic interface encapsulating an index of CommitNode objects
  33. type CommitNodeIndex interface {
  34. // Get returns a commit node from a commit hash
  35. Get(hash plumbing.Hash) (CommitNode, error)
  36. }
  37. // CommitNodeIter is a generic closable interface for iterating over commit nodes.
  38. type CommitNodeIter interface {
  39. Next() (CommitNode, error)
  40. ForEach(func(CommitNode) error) error
  41. Close()
  42. }
  43. // parentCommitNodeIter provides an iterator for parent commits from associated CommitNodeIndex.
  44. type parentCommitNodeIter struct {
  45. node CommitNode
  46. i int
  47. }
  48. func newParentgraphCommitNodeIter(node CommitNode) CommitNodeIter {
  49. return &parentCommitNodeIter{node, 0}
  50. }
  51. // Next moves the iterator to the next commit and returns a pointer to it. If
  52. // there are no more commits, it returns io.EOF.
  53. func (iter *parentCommitNodeIter) Next() (CommitNode, error) {
  54. obj, err := iter.node.ParentNode(iter.i)
  55. if err == object.ErrParentNotFound {
  56. return nil, io.EOF
  57. }
  58. if err == nil {
  59. iter.i++
  60. }
  61. return obj, err
  62. }
  63. // ForEach call the cb function for each commit contained on this iter until
  64. // an error appends or the end of the iter is reached. If ErrStop is sent
  65. // the iteration is stopped but no error is returned. The iterator is closed.
  66. func (iter *parentCommitNodeIter) ForEach(cb func(CommitNode) error) error {
  67. for {
  68. obj, err := iter.Next()
  69. if err != nil {
  70. if err == io.EOF {
  71. return nil
  72. }
  73. return err
  74. }
  75. if err := cb(obj); err != nil {
  76. if err == storer.ErrStop {
  77. return nil
  78. }
  79. return err
  80. }
  81. }
  82. }
  83. func (iter *parentCommitNodeIter) Close() {
  84. }
上海开阖软件有限公司 沪ICP备12045867号-1