aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2015-11-17 09:19:51 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2015-11-17 09:19:51 +0100
commiteb57cb07025845868e40e3994ba2ec818fd3b4f7 (patch)
treeabab30e22f524efa913d0f60ffa40d3bd5fdd596
parent491de646e625801d2f809ed4a99c7ace019a3f15 (diff)
downloadgo-git-eb57cb07025845868e40e3994ba2ec818fd3b4f7.tar.gz
iters: add IsClosed field, fixes #4
-rw-r--r--commit.go13
-rw-r--r--commit_test.go16
-rw-r--r--repository_test.go11
3 files changed, 39 insertions, 1 deletions
diff --git a/commit.go b/commit.go
index 9dce880..1440403 100644
--- a/commit.go
+++ b/commit.go
@@ -113,17 +113,28 @@ func (i *CommitIter) Next() (*Commit, error) {
type iter struct {
ch chan core.Object
r *Repository
+
+ IsClosed bool
}
func newIter(r *Repository) iter {
ch := make(chan core.Object, 1)
- return iter{ch, r}
+ return iter{ch: ch, r: r}
}
func (i *iter) Add(o core.Object) {
+ if i.IsClosed {
+ return
+ }
+
i.ch <- o
}
func (i *iter) Close() {
+ if i.IsClosed {
+ return
+ }
+
+ defer func() { i.IsClosed = true }()
close(i.ch)
}
diff --git a/commit_test.go b/commit_test.go
new file mode 100644
index 0000000..14c2e74
--- /dev/null
+++ b/commit_test.go
@@ -0,0 +1,16 @@
+package git
+
+import (
+ . "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-git.v2/core"
+)
+
+type CommitCommon struct{}
+
+var _ = Suite(&CommitCommon{})
+
+func (s *CommitCommon) TestIterClose(c *C) {
+ i := &iter{ch: make(chan core.Object, 1)}
+ i.Close()
+ i.Close()
+}
diff --git a/repository_test.go b/repository_test.go
index 421160d..a8fe50a 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -69,3 +69,14 @@ func (s *SuiteRepository) TestCommits(c *C) {
c.Assert(count, Equals, 8)
}
+
+func (s *SuiteRepository) TestCommitIterClosePanic(c *C) {
+ r, err := NewRepository(RepositoryFixture, nil)
+ r.Remotes["origin"].upSrv = &MockGitUploadPackService{}
+
+ c.Assert(err, IsNil)
+ c.Assert(r.Pull("origin", "refs/heads/master"), IsNil)
+
+ commits := r.Commits()
+ commits.Close()
+}