using git diff COMMIT^! for comparing all commits

This commit is contained in:
zricethezav committed 2018-02-03 10:20:03 -06:00
1 parent 15d3d8e7de
commit e6fd11d600
1 file changed
+40 -76
+40 -76
View File
@@ -15,18 +15,13 @@ import (
) )
type ReportElem struct { type ReportElem struct {
Lines []string `json:"lines"` Lines []string `json:"lines"`
Branch string `json:"branch"` Commit string `json:"commit"`
CommitA string `json:"commitA"`
CommitB string `json:"commitB"`
} }
type GitLeak struct { type GitLeak struct {
hash string leaks []string
leaks []string commit string
commitA string
commitB string
branch string
} }
func start(opts *Options, repoUrl string) { func start(opts *Options, repoUrl string) {
@@ -70,94 +65,63 @@ func getLeaks(repoName string) []ReportElem {
var ( var (
out []byte out []byte
err error err error
branch string
commits [][]byte
wg sync.WaitGroup wg sync.WaitGroup
commitA string
commitB string
concurrent = 100 concurrent = 100
semaphoreChan = make(chan struct{}, concurrent) semaphoreChan = make(chan struct{}, concurrent)
gitLeaks = make(chan GitLeak) gitLeaks = make(chan GitLeak)
cmdLock = sync.Mutex{}
cache = make(map[string]bool)
) )
out, err = exec.Command("git", "branch", "--all").Output() out, err = exec.Command("git", "rev-list", "--all", "--remotes", "--topo-order").Output()
if err != nil { if err != nil {
log.Fatalf("error retrieving branches %v\n", err) log.Fatalf("error retrieving commits%v\n", err)
} }
// iterate through branches, git rev-list <branch> commits := bytes.Split(out, []byte("\n"))
branches := bytes.Split(out, []byte("\n")) for j, currCommitB := range commits {
currCommit := string(currCommitB)
for i, branchB := range branches { if j == len(commits)-2 {
if i < 2 || i == len(branches)-1 { break
continue
}
branch = string(bytes.Trim(branchB, " "))
cmdLock.Lock()
out, err := exec.Command("git", "rev-list", "--topo-order", branch).Output()
cmdLock.Unlock()
if err != nil {
fmt.Println("skipping branch", branch, err)
continue
} }
// iterate through commits wg.Add(1)
commits = bytes.Split(out, []byte("\n")) go func(currCommit string, repoName string) {
for j, currCommit := range commits { defer wg.Done()
if j == len(commits)-2 { var leakPrs bool
break var leaks []string
}
commitA = string(commits[j+1]) if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
commitB = string(currCommit) log.Fatal(err)
_, seen := cache[commitA+commitB]
if seen {
continue
} else {
cache[commitA+commitB] = true
} }
wg.Add(1) commitCmp := fmt.Sprintf("%s^!", currCommit)
go func(commitA string, commitB string, branch string, repoName string) { semaphoreChan <- struct{}{}
defer wg.Done() out, err := exec.Command("git", "diff", commitCmp).Output()
var leakPrs bool <-semaphoreChan
var leaks []string
if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil { if err != nil {
log.Fatal(err) return
}
lines := checkRegex(string(out))
if len(lines) == 0 {
return
}
for _, line := range lines {
leakPrs = checkEntropy(line)
if leakPrs {
leaks = append(leaks, line)
} }
}
semaphoreChan <- struct{}{} gitLeaks <- GitLeak{leaks, currCommit}
out, err := exec.Command("git", "diff", commitA, commitB).Output()
<-semaphoreChan
if err != nil { }(currCommit, repoName)
return
}
lines := checkRegex(string(out))
if len(lines) == 0 {
return
}
for _, line := range lines {
leakPrs = checkEntropy(line)
if leakPrs {
leaks = append(leaks, line)
}
}
gitLeaks <- GitLeak{commitA + commitB, leaks, commitA, commitB, branch}
}(commitA, commitB, branch, repoName)
}
} }
go func() { go func() {
for gitLeak := range gitLeaks { for gitLeak := range gitLeaks {
if len(gitLeak.leaks) != 0 { if len(gitLeak.leaks) != 0 {
fmt.Println(gitLeak.branch) fmt.Println(gitLeak.leaks)
report = append(report, ReportElem{gitLeak.leaks, gitLeak.branch, report = append(report, ReportElem{gitLeak.leaks, gitLeak.commit})
gitLeak.commitA, gitLeak.commitB})
} }
} }
}() }()