diff options
Diffstat (limited to 'plumbing/object/commit_walker.go')
-rw-r--r-- | plumbing/object/commit_walker.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/plumbing/object/commit_walker.go b/plumbing/object/commit_walker.go index b986067..681ea5e 100644 --- a/plumbing/object/commit_walker.go +++ b/plumbing/object/commit_walker.go @@ -69,3 +69,28 @@ func (w *commitWalker) walk() error { return w.walk() } + +// WalkCommitHistoryPost walks the commit history like WalkCommitHistory +// but in post-order. This means that after walking a merge commit, the +// merged commit will be walked before the base it was merged on. This +// can be useful if you wish to see the history in chronological order. +func WalkCommitHistoryPost(c *Commit, cb func(*Commit) error) error { + stack := []*Commit{c} + seen := make(map[plumbing.Hash]bool) + for len(stack) > 0 { + c := stack[len(stack)-1] + stack = stack[:len(stack)-1] + if seen[c.Hash] { + continue + } + seen[c.Hash] = true + if err := cb(c); err != nil { + return err + } + c.Parents().ForEach(func(pcm *Commit) error { + stack = append(stack, pcm) + return nil + }) + } + return nil +} |