chore(git): better error return

main
hau 2020-12-23 18:05:36 +07:00
parent e4236463e0
commit d3648c41e3
1 changed files with 13 additions and 11 deletions

View File

@ -1,6 +1,8 @@
package git package git
import ( import (
"fmt"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
@ -46,7 +48,7 @@ func (r *repo) Log(fromRev string) ([]Commit, error) {
fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev)) fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev))
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to resolve %s: %w", fromRev, err)
} }
return r.log(fromHash) return r.log(fromHash)
@ -60,7 +62,7 @@ func (r *repo) LogExcludeTo(fromRev, toRev string) ([]Commit, error) {
fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev)) fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev))
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to resolve %s: %w", fromRev, err)
} }
if toRev == "" { if toRev == "" {
@ -69,7 +71,7 @@ func (r *repo) LogExcludeTo(fromRev, toRev string) ([]Commit, error) {
toHash, err := r.r.ResolveRevision(plumbing.Revision(toRev)) toHash, err := r.r.ResolveRevision(plumbing.Revision(toRev))
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to resolve %s: %w", toRev, err)
} }
return r.logWithStopFnFirst(fromHash, stopAtHash(toHash)) return r.logWithStopFnFirst(fromHash, stopAtHash(toHash))
@ -83,7 +85,7 @@ func (r *repo) LogIncludeTo(fromRev, toRev string) ([]Commit, error) {
fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev)) fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev))
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to resolve %s: %w", fromRev, err)
} }
if toRev == "" { if toRev == "" {
@ -92,7 +94,7 @@ func (r *repo) LogIncludeTo(fromRev, toRev string) ([]Commit, error) {
toHash, err := r.r.ResolveRevision(plumbing.Revision(toRev)) toHash, err := r.r.ResolveRevision(plumbing.Revision(toRev))
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to resolve %s: %w", toRev, err)
} }
return r.logWithStopFnLast(fromHash, stopAtHash(toHash)) return r.logWithStopFnLast(fromHash, stopAtHash(toHash))
@ -103,7 +105,7 @@ func (r *repo) log(fromHash *plumbing.Hash) ([]Commit, error) {
From: *fromHash, From: *fromHash,
}) })
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to git log: %w", err)
} }
commits := make([]Commit, 0, defaultCommitCount) commits := make([]Commit, 0, defaultCommitCount)
@ -114,7 +116,7 @@ func (r *repo) log(fromHash *plumbing.Hash) ([]Commit, error) {
return nil return nil
}); err != nil { }); err != nil {
return nil, err return nil, fmt.Errorf("failed to iterate each git log: %w", err)
} }
return commits, nil return commits, nil
@ -125,7 +127,7 @@ func (r *repo) logWithStopFnFirst(fromHash *plumbing.Hash, fn stopFn) ([]Commit,
From: *fromHash, From: *fromHash,
}) })
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to git log: %w", err)
} }
commits := make([]Commit, 0, defaultCommitCount) commits := make([]Commit, 0, defaultCommitCount)
@ -142,7 +144,7 @@ func (r *repo) logWithStopFnFirst(fromHash *plumbing.Hash, fn stopFn) ([]Commit,
return nil return nil
}); err != nil { }); err != nil {
return nil, err return nil, fmt.Errorf("failed to iterate each git log: %w", err)
} }
return commits, nil return commits, nil
@ -153,7 +155,7 @@ func (r *repo) logWithStopFnLast(fromHash *plumbing.Hash, fn stopFn) ([]Commit,
From: *fromHash, From: *fromHash,
}) })
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to git log: %w", err)
} }
commits := make([]Commit, 0, defaultCommitCount) commits := make([]Commit, 0, defaultCommitCount)
@ -170,7 +172,7 @@ func (r *repo) logWithStopFnLast(fromHash *plumbing.Hash, fn stopFn) ([]Commit,
return nil return nil
}); err != nil { }); err != nil {
return nil, err return nil, fmt.Errorf("failed to iterate each git log: %w", err)
} }
return commits, nil return commits, nil