2022-01-22 00:28:41 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-git/go-git/v5"
|
2022-02-10 18:54:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
2022-01-22 00:28:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ScanOptions struct {
|
2023-03-28 15:46:03 +00:00
|
|
|
Filter *common.Filter
|
|
|
|
BaseHash string // When scanning a git.Log, this is the oldest/first commit.
|
|
|
|
HeadHash string
|
|
|
|
MaxDepth int64
|
2023-08-03 16:23:41 +00:00
|
|
|
Bare bool
|
2023-03-28 15:46:03 +00:00
|
|
|
ExcludeGlobs []string
|
|
|
|
LogOptions *git.LogOptions
|
2022-01-22 00:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ScanOption func(*ScanOptions)
|
|
|
|
|
|
|
|
func ScanOptionFilter(filter *common.Filter) ScanOption {
|
|
|
|
return func(scanOptions *ScanOptions) {
|
|
|
|
scanOptions.Filter = filter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:12:58 +00:00
|
|
|
func ScanOptionBaseHash(hash string) ScanOption {
|
2022-01-22 00:28:41 +00:00
|
|
|
return func(scanOptions *ScanOptions) {
|
2022-03-15 00:12:58 +00:00
|
|
|
scanOptions.BaseHash = hash
|
2022-02-17 01:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:12:58 +00:00
|
|
|
func ScanOptionHeadCommit(hash string) ScanOption {
|
2022-02-17 01:10:42 +00:00
|
|
|
return func(scanOptions *ScanOptions) {
|
2022-03-15 00:12:58 +00:00
|
|
|
scanOptions.HeadHash = hash
|
2022-01-22 00:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ScanOptionMaxDepth(maxDepth int64) ScanOption {
|
|
|
|
return func(scanOptions *ScanOptions) {
|
|
|
|
scanOptions.MaxDepth = maxDepth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 15:46:03 +00:00
|
|
|
func ScanOptionExcludeGlobs(globs []string) ScanOption {
|
|
|
|
return func(scanOptions *ScanOptions) {
|
|
|
|
scanOptions.ExcludeGlobs = globs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 00:28:41 +00:00
|
|
|
func ScanOptionLogOptions(logOptions *git.LogOptions) ScanOption {
|
|
|
|
return func(scanOptions *ScanOptions) {
|
|
|
|
scanOptions.LogOptions = logOptions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 16:23:41 +00:00
|
|
|
func ScanOptionBare(bare bool) ScanOption {
|
|
|
|
return func(scanOptions *ScanOptions) {
|
|
|
|
scanOptions.Bare = bare
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 00:28:41 +00:00
|
|
|
func NewScanOptions(options ...ScanOption) *ScanOptions {
|
|
|
|
scanOptions := &ScanOptions{
|
2022-03-15 00:12:58 +00:00
|
|
|
Filter: common.FilterEmpty(),
|
|
|
|
BaseHash: "",
|
|
|
|
HeadHash: "",
|
|
|
|
MaxDepth: -1,
|
2022-01-22 00:28:41 +00:00
|
|
|
LogOptions: &git.LogOptions{
|
2022-01-24 19:59:04 +00:00
|
|
|
All: true,
|
2022-01-22 00:28:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, option := range options {
|
|
|
|
option(scanOptions)
|
|
|
|
}
|
|
|
|
return scanOptions
|
|
|
|
}
|