fixing everything

This commit is contained in:
zach rice committed 2018-10-24 12:28:54 -05:00
1 parent 64e7afea98
commit 98aeee9b1d
1 file changed
+29 -8
+29 -8
View File
@@ -18,6 +18,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing"
@@ -81,6 +82,7 @@ type Options struct {
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"` OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
// Process options // Process options
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
Disk bool `long:"disk" description:"Clones repo(s) to disk"` Disk bool `long:"disk" description:"Clones repo(s) to disk"`
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"` AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
SingleSearch string `long:"single-search" description:"single regular expression to search for"` SingleSearch string `long:"single-search" description:"single regular expression to search for"`
@@ -383,7 +385,7 @@ func cloneRepo() (*RepoDescriptor, error) {
}) })
} }
} else if opts.RepoPath != "" { } else if opts.RepoPath != "" {
log.Infof("opening %s", opts.Repo) log.Infof("opening %s", opts.RepoPath)
repo, err = git.PlainOpen(opts.RepoPath) repo, err = git.PlainOpen(opts.RepoPath)
} else { } else {
log.Infof("cloning %s", opts.Repo) log.Infof("cloning %s", opts.Repo)
@@ -478,19 +480,24 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
repoName string repoName string
leaks []Leak leaks []Leak
commitCount int commitCount int
commitWg sync.WaitGroup
mutex = &sync.Mutex{}
semaphore chan bool
) )
repoName = repo.name repoName = repo.name
if opts.MaxGoRoutines != 0 {
maxGo = opts.MaxGoRoutines
}
if opts.RepoPath != "" {
maxGo = 1
}
semaphore = make(chan bool, maxGo)
cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()}) cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil { if err != nil {
return nil return nil
} }
err = cIter.ForEach(func(c *object.Commit) error { err = cIter.ForEach(func(c *object.Commit) error {
defer func() {
if r := recover(); r != nil {
log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String())
}
}()
if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) { if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) {
cIter.Close() cIter.Close()
return errors.New("ErrStop") return errors.New("ErrStop")
@@ -502,15 +509,25 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
return nil return nil
} }
err = c.Parents().ForEach(func(parent *object.Commit) error {
commitWg.Add(1)
semaphore <- true
go func(c *object.Commit, parent *object.Commit) {
var ( var (
filePath string filePath string
skipFile bool skipFile bool
) )
err = c.Parents().ForEach(func(parent *object.Commit) error { defer func() {
commitWg.Done()
<-semaphore
if r := recover(); r != nil {
log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String())
}
}()
patch, err := c.Patch(parent) patch, err := c.Patch(parent)
if err != nil { if err != nil {
log.Warnf("problem generating patch for commit: %s\n", c.Hash.String()) log.Warnf("problem generating patch for commit: %s\n", c.Hash.String())
return nil return
} }
for _, f := range patch.FilePatches() { for _, f := range patch.FilePatches() {
skipFile = false skipFile = false
@@ -544,15 +561,19 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
} }
chunkLeaks := inspect(diff) chunkLeaks := inspect(diff)
for _, leak := range chunkLeaks { for _, leak := range chunkLeaks {
mutex.Lock()
leaks = append(leaks, leak) leaks = append(leaks, leak)
mutex.Unlock()
} }
} }
} }
} }
}(c, parent)
return nil return nil
}) })
return nil return nil
}) })
commitWg.Wait()
return leaks return leaks
} }