commits.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package scan
  2. import (
  3. "github.com/go-git/go-git/v5"
  4. log "github.com/sirupsen/logrus"
  5. "github.com/zricethezav/gitleaks/v7/config"
  6. "github.com/zricethezav/gitleaks/v7/options"
  7. )
  8. // CommitsScanner is a commit scanner
  9. type CommitsScanner struct {
  10. opts options.Options
  11. cfg config.Config
  12. repo *git.Repository
  13. repoName string
  14. commits []string
  15. }
  16. // NewCommitsScanner creates and returns a commits scanner, notice the 's' in commits
  17. func NewCommitsScanner(opts options.Options, cfg config.Config, repo *git.Repository, commits []string) *CommitsScanner {
  18. return &CommitsScanner{
  19. opts: opts,
  20. cfg: cfg,
  21. repo: repo,
  22. commits: commits,
  23. repoName: getRepoName(opts),
  24. }
  25. }
  26. // Scan kicks off a CommitsScanner Scan
  27. func (css *CommitsScanner) Scan() (Report, error) {
  28. var scannerReport Report
  29. for _, commitHash := range css.commits {
  30. c, err := obtainCommit(css.repo, commitHash)
  31. if err != nil {
  32. log.Errorf("skipping %s, err: %v", commitHash, err)
  33. continue
  34. }
  35. cs := NewCommitScanner(css.opts, css.cfg, css.repo, c)
  36. commitReport, err := cs.Scan()
  37. if err != nil {
  38. return scannerReport, err
  39. }
  40. scannerReport.Leaks = append(scannerReport.Leaks, commitReport.Leaks...)
  41. scannerReport.Commits++
  42. }
  43. return scannerReport, nil
  44. }