aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object/commit_walker.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2017-03-27 18:32:09 +0200
committerGitHub <noreply@github.com>2017-03-27 18:32:09 +0200
commit62ad629b9a4213fdb8d33bcc7e0bea66d043fc41 (patch)
treefa1776b5eff4c57515402a7b94fd6391f4bbaaaa /plumbing/object/commit_walker.go
parentcfbd64f09f0d068d593f3dc3beb4ea7e62719e34 (diff)
parent33db30d79702b717324574a34bd262fc655234ef (diff)
downloadgo-git-62ad629b9a4213fdb8d33bcc7e0bea66d043fc41.tar.gz
Merge pull request #305 from mvdan/history-reverse
plumbing/object: add WalkCommitHistoryPost func
Diffstat (limited to 'plumbing/object/commit_walker.go')
-rw-r--r--plumbing/object/commit_walker.go25
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
+}