commits.go 1.2 KB

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