commits.go 1.1 KB

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