| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 8 |
) |
| 9 |
|
| 10 |
type commitLimitIter struct { |
| 11 |
sourceIter CommitIter |
| 12 |
limitOptions LogLimitOptions |
| 13 |
} |
| 14 |
|
| 15 |
type LogLimitOptions struct { |
| 16 |
Since *time.Time |
| 17 |
Until *time.Time |
| 18 |
} |
| 19 |
|
| 20 |
func NewCommitLimitIterFromIter(commitIter CommitIter, limitOptions LogLimitOptions) CommitIter { |
| 21 |
iterator := new(commitLimitIter) |
| 22 |
iterator.sourceIter = commitIter |
| 23 |
iterator.limitOptions = limitOptions |
| 24 |
return iterator |
| 25 |
} |
| 26 |
|
| 27 |
func (c *commitLimitIter) Next() (*Commit, error) { |
| 28 |
for { |
| 29 |
commit, err := c.sourceIter.Next() |
| 30 |
if err != nil { |
| 31 |
return nil, err |
| 32 |
} |
| 33 |
|
| 34 |
if c.limitOptions.Since != nil && commit.Committer.When.Before(*c.limitOptions.Since) { |
| 35 |
continue |
| 36 |
} |
| 37 |
if c.limitOptions.Until != nil && commit.Committer.When.After(*c.limitOptions.Until) { |
| 38 |
continue |
| 39 |
} |
| 40 |
return commit, nil |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func (c *commitLimitIter) ForEach(cb func(*Commit) error) error { |
| 45 |
for { |
| 46 |
commit, nextErr := c.Next() |
| 47 |
if nextErr == io.EOF { |
| 48 |
break |
| 49 |
} |
| 50 |
if nextErr != nil { |
| 51 |
return nextErr |
| 52 |
} |
| 53 |
err := cb(commit) |
| 54 |
if err == storer.ErrStop { |
| 55 |
return nil |
| 56 |
} else if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
} |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
func (c *commitLimitIter) Close() { |
| 64 |
c.sourceIter.Close() |
| 65 |
} |