mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 12:44:22 +01:00
adding go routines does not help performance much.... posting benchmarks soon
This commit is contained in:
1 parent
87d916fc24
commit
16765c19cf
1 file changed
+45
-98
@@ -18,7 +18,6 @@ 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"
|
||||||
@@ -478,12 +477,8 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
|||||||
func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
prevCommit *object.Commit
|
|
||||||
semaphore chan bool
|
|
||||||
repoName string
|
repoName string
|
||||||
leaks []Leak
|
leaks []Leak
|
||||||
commitWg sync.WaitGroup
|
|
||||||
mutex = &sync.Mutex{}
|
|
||||||
commitCount int
|
commitCount int
|
||||||
)
|
)
|
||||||
repoName = repo.name
|
repoName = repo.name
|
||||||
@@ -491,126 +486,78 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
maxGo = opts.MaxGoRoutines
|
maxGo = opts.MaxGoRoutines
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
if c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) {
|
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) {
|
||||||
cIter.Close()
|
cIter.Close()
|
||||||
return errors.New("ErrStop")
|
return errors.New("ErrStop")
|
||||||
}
|
}
|
||||||
commitCount = commitCount + 1
|
commitCount = commitCount + 1
|
||||||
totalCommits = totalCommits + 1
|
totalCommits = totalCommits + 1
|
||||||
if whiteListCommits[c.Hash.String()] {
|
if whiteListCommits[c.Hash.String()] {
|
||||||
prevCommit = c
|
|
||||||
log.Infof("skipping commit: %s\n", c.Hash.String())
|
log.Infof("skipping commit: %s\n", c.Hash.String())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if prevCommit != nil {
|
|
||||||
if whiteListCommits[prevCommit.Hash.String()] {
|
var (
|
||||||
prevCommit = c
|
filePath string
|
||||||
log.Infof("skipping commit: %s\n", c.Hash.String())
|
skipFile bool
|
||||||
|
)
|
||||||
|
err = c.Parents().ForEach(func(parent *object.Commit) error {
|
||||||
|
patch, err := c.Patch(parent)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("problem generating patch for commit: %s\n", c.Hash.String())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
for _, f := range patch.FilePatches() {
|
||||||
|
skipFile = false
|
||||||
commitWg.Add(1)
|
from, to := f.Files()
|
||||||
semaphore <- true
|
filePath = "???"
|
||||||
go func(c *object.Commit, prevCommit *object.Commit) {
|
if from != nil {
|
||||||
var (
|
filePath = from.Path()
|
||||||
filePath string
|
} else if to != nil {
|
||||||
skipFile bool
|
filePath = to.Path()
|
||||||
)
|
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}()
|
for _, re := range whiteListFiles {
|
||||||
|
if re.FindString(filePath) != "" {
|
||||||
if prevCommit == nil {
|
skipFile = true
|
||||||
t, _ := c.Tree()
|
break
|
||||||
files := t.Files()
|
|
||||||
err := files.ForEach(func(file *object.File) error {
|
|
||||||
content, err := file.Contents()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
diff := gitDiff{
|
|
||||||
branchName: string(ref.Name()),
|
|
||||||
repoName: repoName,
|
|
||||||
filePath: file.Name,
|
|
||||||
content: content,
|
|
||||||
sha: c.Hash.String(),
|
|
||||||
author: c.Author.String(),
|
|
||||||
message: c.Message,
|
|
||||||
}
|
|
||||||
chunkLeaks := inspect(diff)
|
|
||||||
for _, leak := range chunkLeaks {
|
|
||||||
mutex.Lock()
|
|
||||||
leaks = append(leaks, leak)
|
|
||||||
mutex.Unlock()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("problem generating diff for commit: %s\n", c.Hash.String())
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else {
|
if skipFile {
|
||||||
patch, err := c.Patch(prevCommit)
|
continue
|
||||||
if err != nil {
|
|
||||||
log.Warnf("problem generating patch for commit: %s\n", c.Hash.String())
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
for _, f := range patch.FilePatches() {
|
chunks := f.Chunks()
|
||||||
skipFile = false
|
for _, chunk := range chunks {
|
||||||
from, to := f.Files()
|
if chunk.Type() == 1 || chunk.Type() == 2 {
|
||||||
filePath = "???"
|
diff := gitDiff{
|
||||||
if from != nil {
|
branchName: string(ref.Name()),
|
||||||
filePath = from.Path()
|
repoName: repoName,
|
||||||
} else if to != nil {
|
filePath: filePath,
|
||||||
filePath = to.Path()
|
content: chunk.Content(),
|
||||||
}
|
sha: c.Hash.String(),
|
||||||
for _, re := range whiteListFiles {
|
author: c.Author.String(),
|
||||||
if re.FindString(filePath) != "" {
|
message: c.Message,
|
||||||
skipFile = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
chunkLeaks := inspect(diff)
|
||||||
if skipFile {
|
for _, leak := range chunkLeaks {
|
||||||
continue
|
leaks = append(leaks, leak)
|
||||||
}
|
|
||||||
chunks := f.Chunks()
|
|
||||||
for _, chunk := range chunks {
|
|
||||||
if chunk.Type() == 1 || chunk.Type() == 2 {
|
|
||||||
diff := gitDiff{
|
|
||||||
branchName: string(ref.Name()),
|
|
||||||
repoName: repoName,
|
|
||||||
filePath: filePath,
|
|
||||||
content: chunk.Content(),
|
|
||||||
sha: prevCommit.Hash.String(),
|
|
||||||
author: prevCommit.Author.String(),
|
|
||||||
message: prevCommit.Message,
|
|
||||||
}
|
|
||||||
chunkLeaks := inspect(diff)
|
|
||||||
for _, leak := range chunkLeaks {
|
|
||||||
mutex.Lock()
|
|
||||||
leaks = append(leaks, leak)
|
|
||||||
mutex.Unlock()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(c, prevCommit)
|
return nil
|
||||||
prevCommit = c
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
commitWg.Wait()
|
|
||||||
return leaks
|
return leaks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user