Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease Out Of Memory chances by scanning 250 commits at a time #270

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"talisman/checksumcalculator"
"talisman/detector"
"talisman/detector/helpers"
Expand Down Expand Up @@ -46,14 +49,30 @@ func (r *Runner) RunWithoutErrors(promptContext prompt.PromptContext) int {
return r.exitStatus()
}

func getCommitCount() uint64 {
out, err := exec.Command("git", "rev-list", "--all", "--count").CombinedOutput()
if err != nil {
log.Fatal(err)
}
result := strings.Split(string(out), "\n")[0]
count, _ := strconv.ParseUint(result, 10, 64)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ignore the error ?

return count
}

//Scan scans git commit history for potential secrets and returns 0 or 1 as exit code
func (r *Runner) Scan(reportDirectory string) int {

fmt.Printf("\n\n")
utility.CreateArt("Running Scan..")
additions := scanner.GetAdditions()
ignores := &talismanrc.TalismanRC{}
detector.DefaultChain(ignores).Test(additions, ignores, r.results)
commitsToScanAtATime := uint64(250)
totalNumberOfCommits := getCommitCount()
fmt.Println("Number of commits to scan", totalNumberOfCommits)
for commitNumber := uint64(0); commitNumber < totalNumberOfCommits; commitNumber += commitsToScanAtATime {
additions := scanner.GetAdditionsInCommitRange(commitNumber, commitsToScanAtATime)
fmt.Printf("Scanning <=%d Commits after %dth commit\n", commitsToScanAtATime, commitNumber)
ignores := &talismanrc.TalismanRC{}
detector.DefaultChain(ignores).Test(additions, ignores, r.results)
}
reportsPath, err := report.GenerateReport(r.results, reportDirectory)
if err != nil {
log.Printf("error while generating report: %v", err)
Expand Down
17 changes: 10 additions & 7 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scanner
import (
"log"
"os/exec"
"strconv"
"strings"
"talisman/gitrepo"
)
Expand All @@ -12,9 +13,9 @@ type BlobsInCommits struct {
commits map[string][]string
}

// GetAdditions will get all the additions for entire git history
func GetAdditions() []gitrepo.Addition {
blobsInCommits := getBlobsInCommit()
// GetAdditionsInCommitRange will get all the additions from "afterCommitNumber"th commit to "afterCommitNumber+numberOfCommits"th commit
func GetAdditionsInCommitRange(afterCommitNumber uint64, numberOfCommits uint64) []gitrepo.Addition {
blobsInCommits := getBlobsInCommitRange(afterCommitNumber, numberOfCommits)
var additions []gitrepo.Addition
for blob := range blobsInCommits.commits {
objectDetails := strings.Split(blob, "\t")
Expand All @@ -27,8 +28,8 @@ func GetAdditions() []gitrepo.Addition {
return additions
}

func getBlobsInCommit() BlobsInCommits {
commits := getAllCommits()
func getBlobsInCommitRange(afterCommitNumber uint64, numberOfCommits uint64) BlobsInCommits {
commits := getAllCommitsInRange(afterCommitNumber, numberOfCommits)
blobsInCommits := newBlobsInCommit()
result := make(chan []string, len(commits))
for _, commit := range commits {
Expand Down Expand Up @@ -62,8 +63,10 @@ func getBlobsFromChannel(blobsInCommits BlobsInCommits, result chan []string) {
}
}

func getAllCommits() []string {
out, err := exec.Command("git", "log", "--all", "--pretty=%H").CombinedOutput()
func getAllCommitsInRange(afterCommitNumber uint64, numberOfCommits uint64) []string {
n := strconv.FormatUint(numberOfCommits, 10)
skip := strconv.FormatUint(afterCommitNumber, 10)
out, err := exec.Command("git", "log", "--all", "-"+n, "--skip="+skip, "--pretty=%H").CombinedOutput()
if err != nil {
log.Fatal(err)
}
Expand Down