Skip to content

Commit

Permalink
Fix maintainers git log scan when passing over merge commits
Browse files Browse the repository at this point in the history
Signed-off-by: John Starich <johnstarich@johnstarich.com>
JohnStarich committed Aug 21, 2020
1 parent fed78db commit 32abd29
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions internal/cmd/genolm/maintainer.go
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ import (

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
)

type Maintainer struct {
@@ -27,36 +29,40 @@ func getMaintainers(repoRoot string) ([]Maintainer, error) {

repo, err := git.PlainOpen(repoRoot)
if err != nil {
return nil, err
}
head, err := repo.Head()
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "Failed to open repo at %s", repoRoot)
}

commitIter, err := repo.Log(&git.LogOptions{
From: head.Hash(),
Order: git.LogOrderBSF,
})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "Failed to get commit log")
}

maintainersChan := make(chan Maintainer)
errsChan := make(chan error)
var wg sync.WaitGroup
var commits []*object.Commit
const maxCommits = 200
for i := 0; i < maxCommits; i++ {
commit, err := commitIter.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
fmt.Fprintln(os.Stderr, "Error processing next commit:", err)
continue
}
commits = append(commits, commit)
}
commitIter.Close()

maintainersChan := make(chan Maintainer)
errsChan := make(chan error)
var wg sync.WaitGroup
for _, commit := range commits {
wg.Add(1)
hash := commit.Hash
go func() {
defer wg.Done()
maintainer, err := NewFromCommit(repoRoot, commit.Hash)
maintainer, err := NewFromCommit(repoRoot, hash)
if err != nil {
errsChan <- err
return
@@ -84,7 +90,7 @@ func getMaintainers(repoRoot string) ([]Maintainer, error) {
uniqueEmails[m.Email].contributions += m.contributions
}
case err := <-errsChan:
return nil, err
return nil, errors.Wrap(err, "Failed to fetch contributor info from git repo")
}
}
}
@@ -111,19 +117,19 @@ func topContributors(uniqueEmails map[string]*Maintainer) []Maintainer {
func NewFromCommit(repoRoot string, hash plumbing.Hash) (Maintainer, error) {
repo, err := git.PlainOpen(repoRoot)
if err != nil {
return Maintainer{}, err
return Maintainer{}, errors.Wrapf(err, "Failed to get repo at %q", repoRoot)
}
commit, err := repo.CommitObject(hash)
if err != nil {
return Maintainer{}, err
return Maintainer{}, errors.Wrapf(err, "Failed to get commit for hash %s", hash.String())
}
maintainer := Maintainer{
Name: commit.Author.Name,
Email: commit.Author.Email,
}
stats, err := commit.Stats()
if err != nil {
return Maintainer{}, err
return Maintainer{}, errors.Wrapf(err, "Failed to get stats for hash %s", hash.String())
}
const commitWeight = 100
for _, stat := range stats {

0 comments on commit 32abd29

Please sign in to comment.