Reduce complex logWithStopFn (#22)

main
sudo pacman -Syu 2021-08-21 02:01:19 +07:00 committed by GitHub
parent 450636ee22
commit beaaaf3149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 19 deletions

View File

@ -61,7 +61,7 @@ func (r *repo) Log(fromRev, toRev string) ([]Commit, error) {
return r.logWithStopFn(fromHash, nil, stopAtHash(toHash)) return r.logWithStopFn(fromHash, nil, stopAtHash(toHash))
} }
func (r *repo) logWithStopFn(fromHash *plumbing.Hash, beginStopFn, endStopFn stopFn) ([]Commit, error) { func (r *repo) logWithStopFn(fromHash *plumbing.Hash, beginFn, endFn stopFn) ([]Commit, error) {
cIter, err := r.r.Log(&git.LogOptions{ cIter, err := r.r.Log(&git.LogOptions{
From: *fromHash, From: *fromHash,
}) })
@ -71,24 +71,7 @@ func (r *repo) logWithStopFn(fromHash *plumbing.Hash, beginStopFn, endStopFn sto
commits := make([]Commit, 0, defaultCommitCount) commits := make([]Commit, 0, defaultCommitCount)
if err := cIter.ForEach(func(c *object.Commit) error { if err := cIter.ForEach(newIterFn(&commits, beginFn, endFn)); err != nil {
if beginStopFn != nil {
if err := beginStopFn(c); err != nil {
return err
}
}
commit := newCommit(c)
commits = append(commits, commit)
if endStopFn != nil {
if err := endStopFn(c); err != nil {
return err
}
}
return nil
}); err != nil {
return nil, fmt.Errorf("failed to iterate each git log: %w", err) return nil, fmt.Errorf("failed to iterate each git log: %w", err)
} }
@ -104,3 +87,55 @@ func stopAtHash(hash *plumbing.Hash) stopFn {
return nil return nil
} }
} }
func newIterFn(commits *[]Commit, beginFn, endFn stopFn) func(c *object.Commit) error {
if beginFn == nil && endFn == nil {
return func(c *object.Commit) error {
commit := newCommit(c)
*commits = append(*commits, commit)
return nil
}
}
if beginFn == nil {
return func(c *object.Commit) error {
commit := newCommit(c)
*commits = append(*commits, commit)
if err := endFn(c); err != nil {
return err
}
return nil
}
}
if endFn == nil {
return func(c *object.Commit) error {
if err := beginFn(c); err != nil {
return err
}
commit := newCommit(c)
*commits = append(*commits, commit)
return nil
}
}
return func(c *object.Commit) error {
if err := beginFn(c); err != nil {
return err
}
commit := newCommit(c)
*commits = append(*commits, commit)
if err := endFn(c); err != nil {
return err
}
return nil
}
}