| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"container/list" |
| 5 |
"io" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing" |
| 8 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 9 |
"github.com/go-git/go-git/v5/storage" |
| 10 |
) |
| 11 |
|
| 12 |
type commitPreIterator struct { |
| 13 |
seenExternal map[plumbing.Hash]bool |
| 14 |
seen map[plumbing.Hash]bool |
| 15 |
stack []CommitIter |
| 16 |
start *Commit |
| 17 |
} |
| 18 |
|
| 19 |
// NewCommitPreorderIter returns a CommitIter that walks the commit history, |
| 20 |
// starting at the given commit and visiting its parents in pre-order. |
| 21 |
// The given callback will be called for each visited commit. Each commit will |
| 22 |
// be visited only once. If the callback returns an error, walking will stop |
| 23 |
// and will return the error. Other errors might be returned if the history |
| 24 |
// cannot be traversed (e.g. missing objects). Ignore allows to skip some |
| 25 |
// commits from being iterated. |
| 26 |
func NewCommitPreorderIter( |
| 27 |
c *Commit, |
| 28 |
seenExternal map[plumbing.Hash]bool, |
| 29 |
ignore []plumbing.Hash, |
| 30 |
) CommitIter { |
| 31 |
seen := make(map[plumbing.Hash]bool) |
| 32 |
for _, h := range ignore { |
| 33 |
seen[h] = true |
| 34 |
} |
| 35 |
|
| 36 |
return &commitPreIterator{ |
| 37 |
seenExternal: seenExternal, |
| 38 |
seen: seen, |
| 39 |
stack: make([]CommitIter, 0), |
| 40 |
start: c, |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func (w *commitPreIterator) Next() (*Commit, error) { |
| 45 |
var c *Commit |
| 46 |
for { |
| 47 |
if w.start != nil { |
| 48 |
c = w.start |
| 49 |
w.start = nil |
| 50 |
} else { |
| 51 |
current := len(w.stack) - 1 |
| 52 |
if current < 0 { |
| 53 |
return nil, io.EOF |
| 54 |
} |
| 55 |
|
| 56 |
var err error |
| 57 |
c, err = w.stack[current].Next() |
| 58 |
if err == io.EOF { |
| 59 |
w.stack = w.stack[:current] |
| 60 |
continue |
| 61 |
} |
| 62 |
|
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if w.seen[c.Hash] || w.seenExternal[c.Hash] { |
| 69 |
continue |
| 70 |
} |
| 71 |
|
| 72 |
w.seen[c.Hash] = true |
| 73 |
|
| 74 |
if c.NumParents() > 0 { |
| 75 |
w.stack = append(w.stack, filteredParentIter(c, w.seen)) |
| 76 |
} |
| 77 |
|
| 78 |
return c, nil |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func filteredParentIter(c *Commit, seen map[plumbing.Hash]bool) CommitIter { |
| 83 |
var hashes []plumbing.Hash |
| 84 |
for _, h := range c.ParentHashes { |
| 85 |
if !seen[h] { |
| 86 |
hashes = append(hashes, h) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return NewCommitIter(c.s, |
| 91 |
storer.NewEncodedObjectLookupIter(c.s, plumbing.CommitObject, hashes), |
| 92 |
) |
| 93 |
} |
| 94 |
|
| 95 |
func (w *commitPreIterator) ForEach(cb func(*Commit) error) error { |
| 96 |
for { |
| 97 |
c, err := w.Next() |
| 98 |
if err == io.EOF { |
| 99 |
break |
| 100 |
} |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
|
| 105 |
err = cb(c) |
| 106 |
if err == storer.ErrStop { |
| 107 |
break |
| 108 |
} |
| 109 |
if err != nil { |
| 110 |
return err |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
func (w *commitPreIterator) Close() {} |
| 118 |
|
| 119 |
type commitPostIterator struct { |
| 120 |
stack []*Commit |
| 121 |
seen map[plumbing.Hash]bool |
| 122 |
} |
| 123 |
|
| 124 |
// NewCommitPostorderIter returns a CommitIter that walks the commit |
| 125 |
// history like WalkCommitHistory but in post-order. This means that after |
| 126 |
// walking a merge commit, the merged commit will be walked before the base |
| 127 |
// it was merged on. This can be useful if you wish to see the history in |
| 128 |
// chronological order. Ignore allows to skip some commits from being iterated. |
| 129 |
func NewCommitPostorderIter(c *Commit, ignore []plumbing.Hash) CommitIter { |
| 130 |
seen := make(map[plumbing.Hash]bool) |
| 131 |
for _, h := range ignore { |
| 132 |
seen[h] = true |
| 133 |
} |
| 134 |
|
| 135 |
return &commitPostIterator{ |
| 136 |
stack: []*Commit{c}, |
| 137 |
seen: seen, |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func (w *commitPostIterator) Next() (*Commit, error) { |
| 142 |
for { |
| 143 |
if len(w.stack) == 0 { |
| 144 |
return nil, io.EOF |
| 145 |
} |
| 146 |
|
| 147 |
c := w.stack[len(w.stack)-1] |
| 148 |
w.stack = w.stack[:len(w.stack)-1] |
| 149 |
|
| 150 |
if w.seen[c.Hash] { |
| 151 |
continue |
| 152 |
} |
| 153 |
|
| 154 |
w.seen[c.Hash] = true |
| 155 |
|
| 156 |
return c, c.Parents().ForEach(func(p *Commit) error { |
| 157 |
w.stack = append(w.stack, p) |
| 158 |
return nil |
| 159 |
}) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func (w *commitPostIterator) ForEach(cb func(*Commit) error) error { |
| 164 |
for { |
| 165 |
c, err := w.Next() |
| 166 |
if err == io.EOF { |
| 167 |
break |
| 168 |
} |
| 169 |
if err != nil { |
| 170 |
return err |
| 171 |
} |
| 172 |
|
| 173 |
err = cb(c) |
| 174 |
if err == storer.ErrStop { |
| 175 |
break |
| 176 |
} |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return nil |
| 183 |
} |
| 184 |
|
| 185 |
func (w *commitPostIterator) Close() {} |
| 186 |
|
| 187 |
// commitAllIterator stands for commit iterator for all refs. |
| 188 |
type commitAllIterator struct { |
| 189 |
// currCommit points to the current commit. |
| 190 |
currCommit *list.Element |
| 191 |
} |
| 192 |
|
| 193 |
// NewCommitAllIter returns a new commit iterator for all refs. |
| 194 |
// repoStorer is a repo Storer used to get commits and references. |
| 195 |
// commitIterFunc is a commit iterator function, used to iterate through ref commits in chosen order |
| 196 |
func NewCommitAllIter(repoStorer storage.Storer, commitIterFunc func(*Commit) CommitIter) (CommitIter, error) { |
| 197 |
commitsPath := list.New() |
| 198 |
commitsLookup := make(map[plumbing.Hash]*list.Element) |
| 199 |
head, err := storer.ResolveReference(repoStorer, plumbing.HEAD) |
| 200 |
if err == nil { |
| 201 |
err = addReference(repoStorer, commitIterFunc, head, commitsPath, commitsLookup) |
| 202 |
} |
| 203 |
|
| 204 |
if err != nil && err != plumbing.ErrReferenceNotFound { |
| 205 |
return nil, err |
| 206 |
} |
| 207 |
|
| 208 |
// add all references along with the HEAD |
| 209 |
refIter, err := repoStorer.IterReferences() |
| 210 |
if err != nil { |
| 211 |
return nil, err |
| 212 |
} |
| 213 |
defer refIter.Close() |
| 214 |
|
| 215 |
for { |
| 216 |
ref, err := refIter.Next() |
| 217 |
if err == io.EOF { |
| 218 |
break |
| 219 |
} |
| 220 |
|
| 221 |
if err == plumbing.ErrReferenceNotFound { |
| 222 |
continue |
| 223 |
} |
| 224 |
|
| 225 |
if err != nil { |
| 226 |
return nil, err |
| 227 |
} |
| 228 |
|
| 229 |
if err = addReference(repoStorer, commitIterFunc, ref, commitsPath, commitsLookup); err != nil { |
| 230 |
return nil, err |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return &commitAllIterator{commitsPath.Front()}, nil |
| 235 |
} |
| 236 |
|
| 237 |
func addReference( |
| 238 |
repoStorer storage.Storer, |
| 239 |
commitIterFunc func(*Commit) CommitIter, |
| 240 |
ref *plumbing.Reference, |
| 241 |
commitsPath *list.List, |
| 242 |
commitsLookup map[plumbing.Hash]*list.Element) error { |
| 243 |
|
| 244 |
_, exists := commitsLookup[ref.Hash()] |
| 245 |
if exists { |
| 246 |
// we already have it - skip the reference. |
| 247 |
return nil |
| 248 |
} |
| 249 |
|
| 250 |
refCommit, _ := GetCommit(repoStorer, ref.Hash()) |
| 251 |
if refCommit == nil { |
| 252 |
// if it's not a commit - skip it. |
| 253 |
return nil |
| 254 |
} |
| 255 |
|
| 256 |
var ( |
| 257 |
refCommits []*Commit |
| 258 |
parent *list.Element |
| 259 |
) |
| 260 |
// collect all ref commits to add |
| 261 |
commitIter := commitIterFunc(refCommit) |
| 262 |
for c, e := commitIter.Next(); e == nil; { |
| 263 |
parent, exists = commitsLookup[c.Hash] |
| 264 |
if exists { |
| 265 |
break |
| 266 |
} |
| 267 |
refCommits = append(refCommits, c) |
| 268 |
c, e = commitIter.Next() |
| 269 |
} |
| 270 |
commitIter.Close() |
| 271 |
|
| 272 |
if parent == nil { |
| 273 |
// common parent - not found |
| 274 |
// add all commits to the path from this ref (maybe it's a HEAD and we don't have anything, yet) |
| 275 |
for _, c := range refCommits { |
| 276 |
parent = commitsPath.PushBack(c) |
| 277 |
commitsLookup[c.Hash] = parent |
| 278 |
} |
| 279 |
} else { |
| 280 |
// add ref's commits to the path in reverse order (from the latest) |
| 281 |
for i := len(refCommits) - 1; i >= 0; i-- { |
| 282 |
c := refCommits[i] |
| 283 |
// insert before found common parent |
| 284 |
parent = commitsPath.InsertBefore(c, parent) |
| 285 |
commitsLookup[c.Hash] = parent |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return nil |
| 290 |
} |
| 291 |
|
| 292 |
func (it *commitAllIterator) Next() (*Commit, error) { |
| 293 |
if it.currCommit == nil { |
| 294 |
return nil, io.EOF |
| 295 |
} |
| 296 |
|
| 297 |
c := it.currCommit.Value.(*Commit) |
| 298 |
it.currCommit = it.currCommit.Next() |
| 299 |
|
| 300 |
return c, nil |
| 301 |
} |
| 302 |
|
| 303 |
func (it *commitAllIterator) ForEach(cb func(*Commit) error) error { |
| 304 |
for { |
| 305 |
c, err := it.Next() |
| 306 |
if err == io.EOF { |
| 307 |
break |
| 308 |
} |
| 309 |
if err != nil { |
| 310 |
return err |
| 311 |
} |
| 312 |
|
| 313 |
err = cb(c) |
| 314 |
if err == storer.ErrStop { |
| 315 |
break |
| 316 |
} |
| 317 |
if err != nil { |
| 318 |
return err |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
return nil |
| 323 |
} |
| 324 |
|
| 325 |
func (it *commitAllIterator) Close() { |
| 326 |
it.currCommit = nil |
| 327 |
} |