mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 20:54:22 +01:00
Adding commit range feature via 'commit-to' and 'commit-from' options
This commit is contained in:
1 parent
593edeae21
commit
a0f72a4e35
11 files changed
+250
-9
No files matched your search
+1
-1
@@ -40,7 +40,7 @@ func runHelper(r *Repo) error {
|
||||
}
|
||||
|
||||
// Check if we are checking uncommitted files. This is the default behavior
|
||||
// for a "$gitleaks" command with no options set
|
||||
// for a "$ gitleaks" command with no options set
|
||||
if r.Manager.Opts.CheckUncommitted() {
|
||||
if err := r.AuditUncommitted(); err != nil {
|
||||
return err
|
||||
|
||||
+55
-2
@@ -1,6 +1,7 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
"github.com/zricethezav/gitleaks/config"
|
||||
@@ -8,8 +9,9 @@ import (
|
||||
"github.com/zricethezav/gitleaks/options"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -71,6 +73,37 @@ func TestAudit(t *testing.T) {
|
||||
},
|
||||
wantPath: "../test_data/test_local_repo_two_leaks.json",
|
||||
},
|
||||
{
|
||||
description: "test local repo two leaks from commit",
|
||||
opts: options.Options{
|
||||
RepoPath: "../test_data/test_repos/test_repo_2",
|
||||
Report: "../test_data/test_local_repo_two_leaks_commit_from.json.got",
|
||||
ReportFormat: "json",
|
||||
CommitFrom: "996865bb912f3bc45898a370a13aadb315014b55",
|
||||
},
|
||||
wantPath: "../test_data/test_local_repo_two_leaks_commit_from.json",
|
||||
},
|
||||
{
|
||||
description: "test local repo two leaks to commit",
|
||||
opts: options.Options{
|
||||
RepoPath: "../test_data/test_repos/test_repo_2",
|
||||
Report: "../test_data/test_local_repo_two_leaks_commit_to.json.got",
|
||||
ReportFormat: "json",
|
||||
CommitTo: "996865bb912f3bc45898a370a13aadb315014b55",
|
||||
},
|
||||
wantPath: "../test_data/test_local_repo_two_leaks_commit_to.json",
|
||||
},
|
||||
{
|
||||
description: "test local repo two leaks range commit",
|
||||
opts: options.Options{
|
||||
RepoPath: "../test_data/test_repos/test_repo_2",
|
||||
Report: "../test_data/test_local_repo_two_leaks_commit_range.json.got",
|
||||
ReportFormat: "json",
|
||||
CommitFrom: "d8ac0b73aeeb45843319cdc5ce506516eb49bf7a",
|
||||
CommitTo: "51f6dcf6b89b93f4075ba92c400b075631a6cc93",
|
||||
},
|
||||
wantPath: "../test_data/test_local_repo_two_leaks_commit_range.json",
|
||||
},
|
||||
{
|
||||
description: "test local repo two leaks globally whitelisted",
|
||||
opts: options.Options{
|
||||
@@ -312,6 +345,8 @@ func TestAuditUncommited(t *testing.T) {
|
||||
}
|
||||
|
||||
func fileCheck(wantPath, gotPath string) error {
|
||||
var gotLeaks []manager.Leak
|
||||
var wantLeaks []manager.Leak
|
||||
want, err := ioutil.ReadFile(wantPath)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -322,11 +357,29 @@ func fileCheck(wantPath, gotPath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.Trim(string(want), "\n") != strings.Trim(string(got), "\n") {
|
||||
// TODO compare JSONs
|
||||
err = json.Unmarshal(got, &gotLeaks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(want, &wantLeaks)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort.Slice(gotLeaks, func(i, j int) bool { return (gotLeaks)[i].Commit < (gotLeaks)[j].Commit})
|
||||
sort.Slice(wantLeaks, func(i, j int) bool { return (wantLeaks)[i].Commit < (wantLeaks)[j].Commit})
|
||||
|
||||
|
||||
if !reflect.DeepEqual(gotLeaks, wantLeaks) {
|
||||
dmp := diffmatchpatch.New()
|
||||
diffs := dmp.DiffMain(string(want), string(got), false)
|
||||
return fmt.Errorf("does not equal: %s", dmp.DiffPrettyText(diffs))
|
||||
// return fmt.Errorf("does not equal: %s", cmp.Diff(gotLeaks, wantLeaks))
|
||||
}
|
||||
//if strings.Trim(string(want), "\n") != strings.Trim(string(got), "\n") {
|
||||
//}
|
||||
if err := os.Remove(gotPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -247,7 +247,7 @@ func (repo *Repo) Audit() error {
|
||||
semaphore := make(chan bool, howManyThreads(repo.Manager.Opts.Threads))
|
||||
wg := sync.WaitGroup{}
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
if c == nil {
|
||||
if c == nil || c.Hash.String() == repo.Manager.Opts.CommitTo {
|
||||
return storer.ErrStop
|
||||
}
|
||||
|
||||
|
||||
+7
-1
@@ -339,8 +339,11 @@ func fileMatched(f interface{}, re *regexp.Regexp) bool {
|
||||
// It is similar to `git log {branch}`. Default behavior is to log ALL branches so
|
||||
// gitleaks gets the full git history.
|
||||
func getLogOptions(repo *Repo) (*git.LogOptions, error) {
|
||||
var logOpts git.LogOptions
|
||||
if repo.Manager.Opts.CommitFrom != "" {
|
||||
logOpts.From = plumbing.NewHash(repo.Manager.Opts.CommitFrom)
|
||||
}
|
||||
if repo.Manager.Opts.Branch != "" {
|
||||
var logOpts git.LogOptions
|
||||
refs, err := repo.Storer.IterReferences()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -368,6 +371,9 @@ func getLogOptions(repo *Repo) (*git.LogOptions, error) {
|
||||
}
|
||||
return &logOpts, nil
|
||||
}
|
||||
if !logOpts.From.IsZero() {
|
||||
return &logOpts, nil
|
||||
}
|
||||
return &git.LogOptions{All: true}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user