mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 05:04:22 +01:00
good ol semaphores throttling git diff
This commit is contained in:
1 parent
61c4987cd2
commit
a430e46a8e
1 file changed
+35
-33
@@ -34,6 +34,7 @@ type MemoMessage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var lock = sync.RWMutex{}
|
var lock = sync.RWMutex{}
|
||||||
|
var cmdLock = sync.Mutex{}
|
||||||
|
|
||||||
func repoStart(repoUrl string) {
|
func repoStart(repoUrl string) {
|
||||||
err := exec.Command("git", "clone", repoUrl).Run()
|
err := exec.Command("git", "clone", repoUrl).Run()
|
||||||
@@ -72,9 +73,11 @@ func (repo Repo) audit() []ReportElem {
|
|||||||
branch string
|
branch string
|
||||||
commits [][]byte
|
commits [][]byte
|
||||||
// leaks []string
|
// leaks []string
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
commitA string
|
commitA string
|
||||||
commitB string
|
commitB string
|
||||||
|
concurrent = 10
|
||||||
|
semaphoreChan = make(chan struct{}, concurrent)
|
||||||
)
|
)
|
||||||
|
|
||||||
out, err = exec.Command("git", "branch", "--all").Output()
|
out, err = exec.Command("git", "branch", "--all").Output()
|
||||||
@@ -92,44 +95,51 @@ func (repo Repo) audit() []ReportElem {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
branch = string(bytes.Trim(branchB, " "))
|
branch = string(bytes.Trim(branchB, " "))
|
||||||
|
cmdLock.Lock()
|
||||||
cmd := exec.Command("git", "rev-list", branch)
|
cmd := exec.Command("git", "rev-list", branch)
|
||||||
|
|
||||||
out, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
|
cmdLock.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("skipping branch", branch)
|
fmt.Println("skipping branch", branch, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// iterate through commits
|
// iterate through commits
|
||||||
commits = bytes.Split(out, []byte("\n"))
|
commits = bytes.Split(out, []byte("\n"))
|
||||||
wg.Add(len(commits) - 2)
|
|
||||||
for j, currCommit := range commits {
|
for j, currCommit := range commits {
|
||||||
if j == len(commits)-2 {
|
if j == len(commits)-2 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
commitA = string(commits[j+1])
|
commitA = string(commits[j+1])
|
||||||
commitB = string(currCommit)
|
commitB = string(currCommit)
|
||||||
|
_, seen := cache[commitA+commitB]
|
||||||
|
if seen {
|
||||||
|
fmt.Println("WE HAVE SEEN THIS")
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
cache[commitA+commitB] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
go func(commitA string, commitB string,
|
go func(commitA string, commitB string,
|
||||||
j int) {
|
j int, branch string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
var leakPrs bool
|
var leakPrs bool
|
||||||
var leaks []string
|
var leaks []string
|
||||||
fmt.Println(j, branch)
|
fmt.Println(j, branch)
|
||||||
|
|
||||||
lock.RLock()
|
|
||||||
_, seen := cache[commitA+commitB]
|
|
||||||
lock.RUnlock()
|
|
||||||
if seen {
|
|
||||||
fmt.Println("WE HAVE SEEN THIS")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil {
|
if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
semaphoreChan <- struct{}{}
|
||||||
|
fmt.Println("diffing", branch, j)
|
||||||
cmd := exec.Command("git", "diff", commitA, commitB)
|
cmd := exec.Command("git", "diff", commitA, commitB)
|
||||||
out, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
|
<-semaphoreChan
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("no diff: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lines := checkRegex(string(out))
|
lines := checkRegex(string(out))
|
||||||
@@ -144,29 +154,21 @@ func (repo Repo) audit() []ReportElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if len(leaks) != 0 {
|
|
||||||
// report = append(report, ReportElem{leaks, branch,
|
|
||||||
// string(commitB), string(commits[j+1])})
|
|
||||||
// }
|
|
||||||
messages <- MemoMessage{commitA + commitB, leaks, commitA, commitB, branch}
|
messages <- MemoMessage{commitA + commitB, leaks, commitA, commitB, branch}
|
||||||
|
|
||||||
}(commitA, commitB, j)
|
}(commitA, commitB, j, branch)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
|
||||||
for memoMsg := range messages {
|
|
||||||
fmt.Println(memoMsg)
|
|
||||||
lock.Lock()
|
|
||||||
cache[memoMsg.hash] = true
|
|
||||||
lock.Unlock()
|
|
||||||
if len(memoMsg.leaks) != 0 {
|
|
||||||
report = append(report, ReportElem{memoMsg.leaks, memoMsg.branch,
|
|
||||||
memoMsg.commitA, memoMsg.commitB})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
wg.Wait()
|
|
||||||
}
|
}
|
||||||
|
go func() {
|
||||||
|
for memoMsg := range messages {
|
||||||
|
fmt.Println(memoMsg)
|
||||||
|
if len(memoMsg.leaks) != 0 {
|
||||||
|
report = append(report, ReportElem{memoMsg.leaks, memoMsg.branch,
|
||||||
|
memoMsg.commitA, memoMsg.commitB})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
return report
|
return report
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user