mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 13:34:23 +01:00
using git diff COMMIT^! for comparing all commits
This commit is contained in:
1 parent
15d3d8e7de
commit
e6fd11d600
1 file changed
+14
-50
@@ -16,17 +16,12 @@ 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
|
||||||
commitA string
|
commit string
|
||||||
commitB string
|
|
||||||
branch string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func start(opts *Options, repoUrl string) {
|
func start(opts *Options, repoUrl string) {
|
||||||
@@ -70,56 +65,26 @@ 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 i < 2 || i == len(branches)-1 {
|
|
||||||
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
|
|
||||||
commits = bytes.Split(out, []byte("\n"))
|
|
||||||
for j, currCommit := range commits {
|
|
||||||
if j == len(commits)-2 {
|
if j == len(commits)-2 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
commitA = string(commits[j+1])
|
|
||||||
commitB = string(currCommit)
|
|
||||||
_, seen := cache[commitA+commitB]
|
|
||||||
if seen {
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
cache[commitA+commitB] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(commitA string, commitB string, branch string, repoName string) {
|
go func(currCommit string, repoName string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
var leakPrs bool
|
var leakPrs bool
|
||||||
var leaks []string
|
var leaks []string
|
||||||
@@ -128,8 +93,9 @@ func getLeaks(repoName string) []ReportElem {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commitCmp := fmt.Sprintf("%s^!", currCommit)
|
||||||
semaphoreChan <- struct{}{}
|
semaphoreChan <- struct{}{}
|
||||||
out, err := exec.Command("git", "diff", commitA, commitB).Output()
|
out, err := exec.Command("git", "diff", commitCmp).Output()
|
||||||
<-semaphoreChan
|
<-semaphoreChan
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -147,17 +113,15 @@ func getLeaks(repoName string) []ReportElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gitLeaks <- GitLeak{commitA + commitB, leaks, commitA, commitB, branch}
|
gitLeaks <- GitLeak{leaks, currCommit}
|
||||||
|
|
||||||
}(commitA, commitB, branch, repoName)
|
}(currCommit, 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})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in new issue
Block a user