Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 33db30d

Browse files
committed
plumbing/object: add WalkCommitHistoryPost func
Also add a test. Make both the pre-order and post-order tests not sort commits, to actually test the order in which the commit history is walked. Fixes #223.
1 parent 4eef16a commit 33db30d

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

plumbing/object/commit_walker.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,28 @@ func (w *commitWalker) walk() error {
6969

7070
return w.walk()
7171
}
72+
73+
// WalkCommitHistoryPost walks the commit history like WalkCommitHistory
74+
// but in post-order. This means that after walking a merge commit, the
75+
// merged commit will be walked before the base it was merged on. This
76+
// can be useful if you wish to see the history in chronological order.
77+
func WalkCommitHistoryPost(c *Commit, cb func(*Commit) error) error {
78+
stack := []*Commit{c}
79+
seen := make(map[plumbing.Hash]bool)
80+
for len(stack) > 0 {
81+
c := stack[len(stack)-1]
82+
stack = stack[:len(stack)-1]
83+
if seen[c.Hash] {
84+
continue
85+
}
86+
seen[c.Hash] = true
87+
if err := cb(c); err != nil {
88+
return err
89+
}
90+
c.Parents().ForEach(func(pcm *Commit) error {
91+
stack = append(stack, pcm)
92+
return nil
93+
})
94+
}
95+
return nil
96+
}

plumbing/object/commit_walker_test.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,53 @@ type CommitWalkerSuite struct {
88

99
var _ = Suite(&CommitWalkerSuite{})
1010

11-
func (s *CommitWalkerSuite) TestWalkerNext(c *C) {
11+
func (s *CommitWalkerSuite) TestWalkHistory(c *C) {
1212
commit := s.commit(c, s.Fixture.Head)
1313

1414
var commits []*Commit
15-
1615
WalkCommitHistory(commit, func(c *Commit) error {
1716
commits = append(commits, c)
1817
return nil
1918
})
2019

21-
SortCommits(commits)
2220
c.Assert(commits, HasLen, 8)
2321

2422
expected := []string{
25-
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "b8e471f58bcbca63b07bda20e428190409c2db47",
26-
"35e85108805c84807bc66a02d91535e1e24b38b9", "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
27-
"1669dce138d9b841a518c64b10914d88f5e488ea", "af2d6a6954d532f8ffb47615169c8fdf9d383a1a",
28-
"918c48b83bd081e863dbe1b80f8998f058cd8294", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
23+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
24+
"918c48b83bd081e863dbe1b80f8998f058cd8294",
25+
"af2d6a6954d532f8ffb47615169c8fdf9d383a1a",
26+
"1669dce138d9b841a518c64b10914d88f5e488ea",
27+
"35e85108805c84807bc66a02d91535e1e24b38b9",
28+
"b029517f6300c2da0f4b651b8642506cd6aaf45d",
29+
"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
30+
"b8e471f58bcbca63b07bda20e428190409c2db47",
31+
}
32+
for i, commit := range commits {
33+
c.Assert(commit.Hash.String(), Equals, expected[i])
2934
}
35+
}
36+
37+
func (s *CommitWalkerSuite) TestWalkHistoryPost(c *C) {
38+
commit := s.commit(c, s.Fixture.Head)
39+
40+
var commits []*Commit
41+
WalkCommitHistoryPost(commit, func(c *Commit) error {
42+
commits = append(commits, c)
43+
return nil
44+
})
3045

46+
c.Assert(commits, HasLen, 8)
47+
48+
expected := []string{
49+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
50+
"918c48b83bd081e863dbe1b80f8998f058cd8294",
51+
"af2d6a6954d532f8ffb47615169c8fdf9d383a1a",
52+
"1669dce138d9b841a518c64b10914d88f5e488ea",
53+
"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
54+
"b8e471f58bcbca63b07bda20e428190409c2db47",
55+
"b029517f6300c2da0f4b651b8642506cd6aaf45d",
56+
"35e85108805c84807bc66a02d91535e1e24b38b9",
57+
}
3158
for i, commit := range commits {
3259
c.Assert(commit.Hash.String(), Equals, expected[i])
3360
}

0 commit comments

Comments
 (0)