From 2bd61722b81240a0c4a10fe7d407c80427e50b29 Mon Sep 17 00:00:00 2001 From: tcdsv Date: Wed, 28 Jun 2023 10:07:10 +0300 Subject: [PATCH 1/6] add depth option to git plugin --- plugins/git.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/plugins/git.go b/plugins/git.go index 01766e92..070c6fdd 100644 --- a/plugins/git.go +++ b/plugins/git.go @@ -10,9 +10,14 @@ import ( "github.com/zricethezav/gitleaks/v8/detect/git" ) +const ( + argDepth = "depth" +) + type GitPlugin struct { Plugin Channels + Depth int } func (p *GitPlugin) GetName() string { @@ -29,15 +34,24 @@ func (p *GitPlugin) DefineCommand(channels Channels) (*cobra.Command, error) { Args: cobra.MatchAll(cobra.ExactArgs(1), validGitRepoArgs), Run: func(cmd *cobra.Command, args []string) { log.Info().Msg("Git plugin started") - scanGit(args[0], channels.Items, channels.Errors) + scanGit(args[0], p.buildScanOptions(), channels.Items, channels.Errors) }, } - + flags := command.Flags() + flags.IntVar(&p.Depth, argDepth, 0, "number of commits to scan from HEAD") return command, nil } -func scanGit(path string, itemsChan chan Item, errChan chan error) { - fileChan, err := git.GitLog(path, "") +func (p *GitPlugin) buildScanOptions() string { + options := "" + if p.Depth > 0 { + options = fmt.Sprintf("--full-history --all -n %d", p.Depth) + } + return options +} + +func scanGit(path string, scanOptions string, itemsChan chan Item, errChan chan error) { + fileChan, err := git.GitLog(path, scanOptions) if err != nil { errChan <- fmt.Errorf("error while scanning git repository: %w", err) } From 7a1b24b58c979342a59041617f9d4cd96bd91c10 Mon Sep 17 00:00:00 2001 From: tcdsv Date: Wed, 28 Jun 2023 15:35:30 +0300 Subject: [PATCH 2/6] limit depth field scope --- plugins/git.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/git.go b/plugins/git.go index 070c6fdd..0f9f661a 100644 --- a/plugins/git.go +++ b/plugins/git.go @@ -17,7 +17,7 @@ const ( type GitPlugin struct { Plugin Channels - Depth int + depth int } func (p *GitPlugin) GetName() string { @@ -38,14 +38,14 @@ func (p *GitPlugin) DefineCommand(channels Channels) (*cobra.Command, error) { }, } flags := command.Flags() - flags.IntVar(&p.Depth, argDepth, 0, "number of commits to scan from HEAD") + flags.IntVar(&p.depth, argDepth, 0, "number of commits to scan from HEAD") return command, nil } func (p *GitPlugin) buildScanOptions() string { options := "" - if p.Depth > 0 { - options = fmt.Sprintf("--full-history --all -n %d", p.Depth) + if p.depth > 0 { + options = fmt.Sprintf("--full-history --all -n %d", p.depth) } return options } From 6637e56a36ee2b8ffcdd497b2f5b461a6f68d023 Mon Sep 17 00:00:00 2001 From: tcdsv Date: Wed, 28 Jun 2023 17:19:30 +0300 Subject: [PATCH 3/6] add --all option to scan all branches --- plugins/git.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/plugins/git.go b/plugins/git.go index 0f9f661a..f7c8c99c 100644 --- a/plugins/git.go +++ b/plugins/git.go @@ -3,6 +3,7 @@ package plugins import ( "fmt" "os" + "strings" "github.com/gitleaks/go-gitdiff/gitdiff" "github.com/rs/zerolog/log" @@ -11,13 +12,15 @@ import ( ) const ( - argDepth = "depth" + argDepth = "depth" + argScanAllBranches = "all" ) type GitPlugin struct { Plugin Channels - depth int + depth int + scanAllBranches bool } func (p *GitPlugin) GetName() string { @@ -38,16 +41,21 @@ func (p *GitPlugin) DefineCommand(channels Channels) (*cobra.Command, error) { }, } flags := command.Flags() + flags.BoolVar(&p.scanAllBranches, argScanAllBranches, false, "scan all branches") flags.IntVar(&p.depth, argDepth, 0, "number of commits to scan from HEAD") return command, nil } func (p *GitPlugin) buildScanOptions() string { - options := "" + var options []string + options = append(options, "--full-history") + if p.scanAllBranches { + options = append(options, "--all") + } if p.depth > 0 { - options = fmt.Sprintf("--full-history --all -n %d", p.depth) + options = append(options, fmt.Sprintf("-n %d", p.depth)) } - return options + return strings.Join(options, " ") } func scanGit(path string, scanOptions string, itemsChan chan Item, errChan chan error) { From 2ec9d7e4cb59174ce215890690a3d108451060bf Mon Sep 17 00:00:00 2001 From: tcdsv <33223663+tcdsv@users.noreply.github.com> Date: Wed, 28 Jun 2023 17:45:40 +0300 Subject: [PATCH 4/6] cleanup Co-authored-by: Baruch Odem (Rothkoff) --- plugins/git.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/git.go b/plugins/git.go index f7c8c99c..2d8b34c2 100644 --- a/plugins/git.go +++ b/plugins/git.go @@ -47,8 +47,7 @@ func (p *GitPlugin) DefineCommand(channels Channels) (*cobra.Command, error) { } func (p *GitPlugin) buildScanOptions() string { - var options []string - options = append(options, "--full-history") + options := []string{"--full-history"} if p.scanAllBranches { options = append(options, "--all") } From 73a892b477fcbbf15eac4a0609b98d0b0eb3f62b Mon Sep 17 00:00:00 2001 From: tcdsv Date: Wed, 28 Jun 2023 17:48:25 +0300 Subject: [PATCH 5/6] improve naming --- plugins/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git.go b/plugins/git.go index 2d8b34c2..81900b8b 100644 --- a/plugins/git.go +++ b/plugins/git.go @@ -13,7 +13,7 @@ import ( const ( argDepth = "depth" - argScanAllBranches = "all" + argScanAllBranches = "all-branches" ) type GitPlugin struct { From 1f21a733f22dea1b4665834c491ea19236eda623 Mon Sep 17 00:00:00 2001 From: tcdsv Date: Wed, 28 Jun 2023 17:54:21 +0300 Subject: [PATCH 6/6] improve command description --- plugins/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git.go b/plugins/git.go index 81900b8b..f2d2bdb9 100644 --- a/plugins/git.go +++ b/plugins/git.go @@ -41,7 +41,7 @@ func (p *GitPlugin) DefineCommand(channels Channels) (*cobra.Command, error) { }, } flags := command.Flags() - flags.BoolVar(&p.scanAllBranches, argScanAllBranches, false, "scan all branches") + flags.BoolVar(&p.scanAllBranches, argScanAllBranches, false, "scan all branches [default: false]") flags.IntVar(&p.depth, argDepth, 0, "number of commits to scan from HEAD") return command, nil }