|
| 1 | +package merge_base |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + |
| 6 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 7 | + "gopkg.in/src-d/go-git.v4/plumbing/object" |
| 8 | + "gopkg.in/src-d/go-git.v4/plumbing/storer" |
| 9 | +) |
| 10 | + |
| 11 | +// NewFilterCommitIter returns a CommitIter that walks the commit history, |
| 12 | +// starting at the passed commit and visiting its parents in Breadth-first order. |
| 13 | +// The commits returned by the CommitIter will validate the passed CommitFilter. |
| 14 | +// The history won't be transversed beyond a commit if isLimit is true for it. |
| 15 | +// Each commit will be visited only once. |
| 16 | +// If the commit history can not be traversed, or the Close() method is called, |
| 17 | +// the CommitIter won't return more commits. |
| 18 | +// If no isValid is passed, all ancestors of from commit will be valid. |
| 19 | +// If no isLimit is limmit, all ancestors of all commits will be visited. |
| 20 | +func NewFilterCommitIter( |
| 21 | + // REVIEWER: store argument wouldn't be needed if this were part of go-git/plumbing/object package |
| 22 | + store storer.EncodedObjectStorer, |
| 23 | + from *object.Commit, |
| 24 | + isValid *CommitFilter, |
| 25 | + isLimit *CommitFilter, |
| 26 | +) object.CommitIter { |
| 27 | + var validFilter CommitFilter |
| 28 | + if isValid == nil { |
| 29 | + validFilter = func(_ *object.Commit) bool { |
| 30 | + return true |
| 31 | + } |
| 32 | + } else { |
| 33 | + validFilter = *isValid |
| 34 | + } |
| 35 | + |
| 36 | + var limitFilter CommitFilter |
| 37 | + if isLimit == nil { |
| 38 | + limitFilter = func(_ *object.Commit) bool { |
| 39 | + return false |
| 40 | + } |
| 41 | + } else { |
| 42 | + limitFilter = *isLimit |
| 43 | + } |
| 44 | + |
| 45 | + return &filterCommitIter{ |
| 46 | + // REVIEWER: store wouldn't be needed if this were part of go-git/plumbing/object package |
| 47 | + store: store, |
| 48 | + isValid: validFilter, |
| 49 | + isLimit: limitFilter, |
| 50 | + visited: map[plumbing.Hash]bool{}, |
| 51 | + queue: []*object.Commit{from}, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// CommitFilter returns a boolean for the passed Commit |
| 56 | +type CommitFilter func(*object.Commit) bool |
| 57 | + |
| 58 | +// filterCommitIter implments object.CommitIter |
| 59 | +type filterCommitIter struct { |
| 60 | + // REVIEWER: store wouldn't be needed if this were part of go-git/plumbing/object package |
| 61 | + store storer.EncodedObjectStorer |
| 62 | + isValid CommitFilter |
| 63 | + isLimit CommitFilter |
| 64 | + visited map[plumbing.Hash]bool |
| 65 | + queue []*object.Commit |
| 66 | + lastErr error |
| 67 | +} |
| 68 | + |
| 69 | +// Next returns the next commit of the CommitIter. |
| 70 | +// It will return io.EOF if there are no more commits to visit, |
| 71 | +// or an error if the history could not be traversed. |
| 72 | +func (w *filterCommitIter) Next() (*object.Commit, error) { |
| 73 | + var commit *object.Commit |
| 74 | + var err error |
| 75 | + for { |
| 76 | + commit, err = w.popNewFromQueue() |
| 77 | + if err != nil { |
| 78 | + return nil, w.close(err) |
| 79 | + } |
| 80 | + |
| 81 | + w.visited[commit.Hash] = true |
| 82 | + |
| 83 | + if !w.isLimit(commit) { |
| 84 | + // REVIEWER: first argument would be commit.s if this were part of object.Commit |
| 85 | + err = w.addToQueue(w.store, commit.ParentHashes...) |
| 86 | + if err != nil { |
| 87 | + return nil, w.close(err) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if w.isValid(commit) { |
| 92 | + return commit, nil |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// ForEach runs the passed callback over each Commit returned by the CommitIter |
| 98 | +// until the callback returns an error or there is no more commits to traverse. |
| 99 | +func (w *filterCommitIter) ForEach(cb func(*object.Commit) error) error { |
| 100 | + for { |
| 101 | + commit, err := w.Next() |
| 102 | + if err == io.EOF { |
| 103 | + break |
| 104 | + } |
| 105 | + |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + if err := cb(commit); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +// Error returns the error that caused that the CommitIter is no longer returning commits |
| 119 | +func (w *filterCommitIter) Error() error { |
| 120 | + return w.lastErr |
| 121 | +} |
| 122 | + |
| 123 | +// Close closes the CommitIter |
| 124 | +func (w *filterCommitIter) Close() { |
| 125 | + w.visited = map[plumbing.Hash]bool{} |
| 126 | + w.queue = []*object.Commit{} |
| 127 | + w.isLimit = nil |
| 128 | + w.isValid = nil |
| 129 | +} |
| 130 | + |
| 131 | +// close closes the CommitIter with an error |
| 132 | +func (w *filterCommitIter) close(err error) error { |
| 133 | + w.Close() |
| 134 | + w.lastErr = err |
| 135 | + return err |
| 136 | +} |
| 137 | + |
| 138 | +// popNewFromQueue returns the first new commit from the internal fifo queue, or |
| 139 | +// an io.EOF error if the queue is empty |
| 140 | +func (w *filterCommitIter) popNewFromQueue() (*object.Commit, error) { |
| 141 | + var first *object.Commit |
| 142 | + for { |
| 143 | + if len(w.queue) == 0 { |
| 144 | + if w.lastErr != nil { |
| 145 | + return nil, w.lastErr |
| 146 | + } |
| 147 | + |
| 148 | + return nil, io.EOF |
| 149 | + } |
| 150 | + |
| 151 | + first = w.queue[0] |
| 152 | + w.queue = w.queue[1:] |
| 153 | + if w.visited[first.Hash] { |
| 154 | + continue |
| 155 | + } |
| 156 | + |
| 157 | + return first, nil |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// addToQueue adds the passed commits to the internal fifo queue if they weren'r already seen |
| 162 | +// or returns an error if the passed hashes could not be used to get valid commits |
| 163 | +func (w *filterCommitIter) addToQueue( |
| 164 | + store storer.EncodedObjectStorer, |
| 165 | + hashes ...plumbing.Hash, |
| 166 | +) error { |
| 167 | + for _, hash := range hashes { |
| 168 | + if w.visited[hash] { |
| 169 | + continue |
| 170 | + } |
| 171 | + |
| 172 | + commit, err := object.GetCommit(store, hash) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + w.queue = append(w.queue, commit) |
| 178 | + } |
| 179 | + |
| 180 | + return nil |
| 181 | +} |
0 commit comments