| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
|
| 6 |
"github.com/go-git/go-git/v5/plumbing" |
| 7 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 8 |
) |
| 9 |
|
| 10 |
type bfsCommitIterator struct { |
| 11 |
seenExternal map[plumbing.Hash]bool |
| 12 |
seen map[plumbing.Hash]bool |
| 13 |
queue []*Commit |
| 14 |
} |
| 15 |
|
| 16 |
// NewCommitIterBSF returns a CommitIter that walks the commit history, |
| 17 |
// starting at the given commit and visiting its parents in pre-order. |
| 18 |
// The given callback will be called for each visited commit. Each commit will |
| 19 |
// be visited only once. If the callback returns an error, walking will stop |
| 20 |
// and will return the error. Other errors might be returned if the history |
| 21 |
// cannot be traversed (e.g. missing objects). Ignore allows to skip some |
| 22 |
// commits from being iterated. |
| 23 |
func NewCommitIterBSF( |
| 24 |
c *Commit, |
| 25 |
seenExternal map[plumbing.Hash]bool, |
| 26 |
ignore []plumbing.Hash, |
| 27 |
) CommitIter { |
| 28 |
seen := make(map[plumbing.Hash]bool) |
| 29 |
for _, h := range ignore { |
| 30 |
seen[h] = true |
| 31 |
} |
| 32 |
|
| 33 |
return &bfsCommitIterator{ |
| 34 |
seenExternal: seenExternal, |
| 35 |
seen: seen, |
| 36 |
queue: []*Commit{c}, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func (w *bfsCommitIterator) appendHash(store storer.EncodedObjectStorer, h plumbing.Hash) error { |
| 41 |
if w.seen[h] || w.seenExternal[h] { |
| 42 |
return nil |
| 43 |
} |
| 44 |
c, err := GetCommit(store, h) |
| 45 |
if err != nil { |
| 46 |
return err |
| 47 |
} |
| 48 |
w.queue = append(w.queue, c) |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
func (w *bfsCommitIterator) Next() (*Commit, error) { |
| 53 |
var c *Commit |
| 54 |
for { |
| 55 |
if len(w.queue) == 0 { |
| 56 |
return nil, io.EOF |
| 57 |
} |
| 58 |
c = w.queue[0] |
| 59 |
w.queue = w.queue[1:] |
| 60 |
|
| 61 |
if w.seen[c.Hash] || w.seenExternal[c.Hash] { |
| 62 |
continue |
| 63 |
} |
| 64 |
|
| 65 |
w.seen[c.Hash] = true |
| 66 |
|
| 67 |
for _, h := range c.ParentHashes { |
| 68 |
err := w.appendHash(c.s, h) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return c, nil |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func (w *bfsCommitIterator) ForEach(cb func(*Commit) error) error { |
| 79 |
for { |
| 80 |
c, err := w.Next() |
| 81 |
if err == io.EOF { |
| 82 |
break |
| 83 |
} |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
|
| 88 |
err = cb(c) |
| 89 |
if err == storer.ErrStop { |
| 90 |
break |
| 91 |
} |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
func (w *bfsCommitIterator) Close() {} |