1package object
2
3import (
4	"github.com/go-git/go-git/v5/plumbing"
5
6	. "gopkg.in/check.v1"
7)
8
9type CommitWalkerSuite struct {
10	BaseObjectsSuite
11}
12
13var _ = Suite(&CommitWalkerSuite{})
14
15func (s *CommitWalkerSuite) TestCommitPreIterator(c *C) {
16	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
17
18	var commits []*Commit
19	NewCommitPreorderIter(commit, nil, nil).ForEach(func(c *Commit) error {
20		commits = append(commits, c)
21		return nil
22	})
23
24	c.Assert(commits, HasLen, 8)
25
26	expected := []string{
27		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
28		"918c48b83bd081e863dbe1b80f8998f058cd8294",
29		"af2d6a6954d532f8ffb47615169c8fdf9d383a1a",
30		"1669dce138d9b841a518c64b10914d88f5e488ea",
31		"35e85108805c84807bc66a02d91535e1e24b38b9",
32		"b029517f6300c2da0f4b651b8642506cd6aaf45d",
33		"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
34		"b8e471f58bcbca63b07bda20e428190409c2db47",
35	}
36	for i, commit := range commits {
37		c.Assert(commit.Hash.String(), Equals, expected[i])
38	}
39}
40
41func (s *CommitWalkerSuite) TestCommitPreIteratorWithIgnore(c *C) {
42	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
43
44	var commits []*Commit
45	NewCommitPreorderIter(commit, nil, []plumbing.Hash{
46		plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"),
47	}).ForEach(func(c *Commit) error {
48		commits = append(commits, c)
49		return nil
50	})
51
52	c.Assert(commits, HasLen, 2)
53
54	expected := []string{
55		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
56		"918c48b83bd081e863dbe1b80f8998f058cd8294",
57	}
58	for i, commit := range commits {
59		c.Assert(commit.Hash.String(), Equals, expected[i])
60	}
61}
62
63func (s *CommitWalkerSuite) TestCommitPreIteratorWithSeenExternal(c *C) {
64	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
65
66	var commits []*Commit
67	seenExternal := map[plumbing.Hash]bool{
68		plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"): true,
69	}
70	NewCommitPreorderIter(commit, seenExternal, nil).
71		ForEach(func(c *Commit) error {
72			commits = append(commits, c)
73			return nil
74		})
75
76	c.Assert(commits, HasLen, 2)
77
78	expected := []string{
79		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
80		"918c48b83bd081e863dbe1b80f8998f058cd8294",
81	}
82	for i, commit := range commits {
83		c.Assert(commit.Hash.String(), Equals, expected[i])
84	}
85}
86
87func (s *CommitWalkerSuite) TestCommitPostIterator(c *C) {
88	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
89
90	var commits []*Commit
91	NewCommitPostorderIter(commit, nil).ForEach(func(c *Commit) error {
92		commits = append(commits, c)
93		return nil
94	})
95
96	c.Assert(commits, HasLen, 8)
97
98	expected := []string{
99		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
100		"918c48b83bd081e863dbe1b80f8998f058cd8294",
101		"af2d6a6954d532f8ffb47615169c8fdf9d383a1a",
102		"1669dce138d9b841a518c64b10914d88f5e488ea",
103		"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
104		"b8e471f58bcbca63b07bda20e428190409c2db47",
105		"b029517f6300c2da0f4b651b8642506cd6aaf45d",
106		"35e85108805c84807bc66a02d91535e1e24b38b9",
107	}
108
109	for i, commit := range commits {
110		c.Assert(commit.Hash.String(), Equals, expected[i])
111	}
112}
113
114func (s *CommitWalkerSuite) TestCommitPostIteratorWithIgnore(c *C) {
115	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
116
117	var commits []*Commit
118	NewCommitPostorderIter(commit, []plumbing.Hash{
119		plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"),
120	}).ForEach(func(c *Commit) error {
121		commits = append(commits, c)
122		return nil
123	})
124
125	c.Assert(commits, HasLen, 2)
126
127	expected := []string{
128		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
129		"918c48b83bd081e863dbe1b80f8998f058cd8294",
130	}
131	for i, commit := range commits {
132		c.Assert(commit.Hash.String(), Equals, expected[i])
133	}
134}
135
136func (s *CommitWalkerSuite) TestCommitCTimeIterator(c *C) {
137	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
138
139	var commits []*Commit
140	NewCommitIterCTime(commit, nil, nil).ForEach(func(c *Commit) error {
141		commits = append(commits, c)
142		return nil
143	})
144
145	c.Assert(commits, HasLen, 8)
146
147	expected := []string{
148		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", // 2015-04-05T23:30:47+02:00
149		"918c48b83bd081e863dbe1b80f8998f058cd8294", // 2015-03-31T13:56:18+02:00
150		"af2d6a6954d532f8ffb47615169c8fdf9d383a1a", // 2015-03-31T13:51:51+02:00
151		"1669dce138d9b841a518c64b10914d88f5e488ea", // 2015-03-31T13:48:14+02:00
152		"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", // 2015-03-31T13:47:14+02:00
153		"35e85108805c84807bc66a02d91535e1e24b38b9", // 2015-03-31T13:46:24+02:00
154		"b8e471f58bcbca63b07bda20e428190409c2db47", // 2015-03-31T13:44:52+02:00
155		"b029517f6300c2da0f4b651b8642506cd6aaf45d", // 2015-03-31T13:42:21+02:00
156	}
157	for i, commit := range commits {
158		c.Assert(commit.Hash.String(), Equals, expected[i])
159	}
160}
161
162func (s *CommitWalkerSuite) TestCommitCTimeIteratorWithIgnore(c *C) {
163	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
164
165	var commits []*Commit
166	NewCommitIterCTime(commit, nil, []plumbing.Hash{
167		plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"),
168	}).ForEach(func(c *Commit) error {
169		commits = append(commits, c)
170		return nil
171	})
172
173	c.Assert(commits, HasLen, 2)
174
175	expected := []string{
176		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
177		"918c48b83bd081e863dbe1b80f8998f058cd8294",
178	}
179	for i, commit := range commits {
180		c.Assert(commit.Hash.String(), Equals, expected[i])
181	}
182}
183
184func (s *CommitWalkerSuite) TestCommitBSFIterator(c *C) {
185	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
186
187	var commits []*Commit
188	NewCommitIterBSF(commit, nil, nil).ForEach(func(c *Commit) error {
189		commits = append(commits, c)
190		return nil
191	})
192
193	c.Assert(commits, HasLen, 8)
194
195	expected := []string{
196		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
197		"918c48b83bd081e863dbe1b80f8998f058cd8294",
198		"af2d6a6954d532f8ffb47615169c8fdf9d383a1a",
199		"1669dce138d9b841a518c64b10914d88f5e488ea",
200		"35e85108805c84807bc66a02d91535e1e24b38b9",
201		"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
202		"b029517f6300c2da0f4b651b8642506cd6aaf45d",
203		"b8e471f58bcbca63b07bda20e428190409c2db47",
204	}
205	for i, commit := range commits {
206		c.Assert(commit.Hash.String(), Equals, expected[i])
207	}
208}
209
210func (s *CommitWalkerSuite) TestCommitBSFIteratorWithIgnore(c *C) {
211	commit := s.commit(c, plumbing.NewHash(s.Fixture.Head))
212
213	var commits []*Commit
214	NewCommitIterBSF(commit, nil, []plumbing.Hash{
215		plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"),
216	}).ForEach(func(c *Commit) error {
217		commits = append(commits, c)
218		return nil
219	})
220
221	c.Assert(commits, HasLen, 2)
222
223	expected := []string{
224		"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
225		"918c48b83bd081e863dbe1b80f8998f058cd8294",
226	}
227	for i, commit := range commits {
228		c.Assert(commit.Hash.String(), Equals, expected[i])
229	}
230}
231