Skip to content

Commit

Permalink
do not fail on git rename warning (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
zricethezav committed Dec 12, 2021
1 parent b814171 commit 192b962
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions git/git.go
Expand Up @@ -13,7 +13,8 @@ import (
"github.com/rs/zerolog/log"
)

// GitLog returns a channel of gitdiff.File objects from the git log -p command for the given source.
// GitLog returns a channel of gitdiff.File objects from the
// git log -p command for the given source.
func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) {
sourceClean := filepath.Clean(source)
var cmd *exec.Cmd
Expand All @@ -22,7 +23,8 @@ func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) {
args = append(args, strings.Split(logOpts, " ")...)
cmd = exec.Command("git", args...)
} else {
cmd = exec.Command("git", "-C", sourceClean, "log", "-p", "-U0", "--full-history", "--all")
cmd = exec.Command("git", "-C", sourceClean, "log", "-p", "-U0",
"--full-history", "--all")
}

log.Debug().Msgf("executing: %s", cmd.String())
Expand All @@ -40,18 +42,21 @@ func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) {
}

go listenForStdErr(stderr)
time.Sleep(50 * time.Millisecond) // HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
// HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
time.Sleep(50 * time.Millisecond)

return gitdiff.Parse(stdout)
}

// GitDiff returns a channel of gitdiff.File objects from the git diff command for the given source.
// GitDiff returns a channel of gitdiff.File objects from
// the git diff command for the given source.
func GitDiff(source string, staged bool) (<-chan *gitdiff.File, error) {
sourceClean := filepath.Clean(source)
var cmd *exec.Cmd
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0", ".")
if staged {
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0", "--staged", ".")
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0",
"--staged", ".")
}
log.Debug().Msgf("executing: %s", cmd.String())

Expand All @@ -68,7 +73,8 @@ func GitDiff(source string, staged bool) (<-chan *gitdiff.File, error) {
}

go listenForStdErr(stderr)
time.Sleep(50 * time.Millisecond) // HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
// HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
time.Sleep(50 * time.Millisecond)

return gitdiff.Parse(stdout)
}
Expand All @@ -79,8 +85,26 @@ func listenForStdErr(stderr io.ReadCloser) {
scanner := bufio.NewScanner(stderr)
errEncountered := false
for scanner.Scan() {
log.Error().Msg(scanner.Text())
errEncountered = true
// if git throws the following error:
//
// exhaustive rename detection was skipped due to too many files.
// you may want to set your diff.renameLimit variable to at least
// (some large number) and retry the command.
//
// we skip exiting the program as git log -p/git diff will continue
// to send data to stdout and finish executing. This next bit of
// code prevents gitleaks from stopping mid scan if this error is
// encountered
if strings.Contains(scanner.Text(),
"exhaustive rename detection was skipped") ||
strings.Contains(scanner.Text(),
"you may want to set your diff.renameLimit") {

log.Warn().Msg(scanner.Text())
} else {
log.Error().Msg(scanner.Text())
errEncountered = true
}
}
if errEncountered {
os.Exit(1)
Expand Down

0 comments on commit 192b962

Please sign in to comment.