-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor git source to allow ScanOptions and use source in engine #1518
Conversation
6b6b06e
to
2140f23
Compare
options := &gogit.PlainOpenOptions{ | ||
DetectDotGit: true, | ||
EnableDotGitCommonDir: true, | ||
} | ||
|
||
repo, err := gogit.PlainOpenWithOptions(c.RepoPath, options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the same options that the git source uses to open a git directory:
trufflehog/pkg/sources/git/git.go
Lines 236 to 242 in 8ec5e49
func RepoFromPath(path string) (*git.Repository, error) { | |
options := &git.PlainOpenOptions{ | |
DetectDotGit: true, | |
EnableDotGitCommonDir: true, | |
} | |
return git.PlainOpenWithOptions(path, options) | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the source will now ignore any error returned from PlainOpenWithOptions
though, but we are preparing the repo already in main
which should catch any issues:
Lines 338 to 341 in 8ec5e49
repoPath, remote, err = git.PrepareRepoSinceCommit(ctx, *gitScanURI, *gitScanSinceCommit) | |
if err != nil || repoPath == "" { | |
logFatal(err, "error preparing git repo for scanning") | |
} |
return s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) | ||
return s.git.ScanRepo(ctx, repo, path, s.scanOptions, chunksChan) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not initializing s.scanOptions
, so the default value of nil
will be used if it's not set. This is okay because the ScanRepo
function already catches this:
trufflehog/pkg/sources/git/git.go
Lines 562 to 565 in 8ec5e49
func (s *Git) ScanRepo(ctx context.Context, repo *git.Repository, repoPath string, scanOptions *ScanOptions, chunksChan chan *sources.Chunk) error { | |
if scanOptions == nil { | |
scanOptions = NewScanOptions() | |
} |
82db573
to
f4ce4b4
Compare
Refactor the Chunks method of the git Source to call out to two helper methods: scanRepos and scanDirs which scans s.conn.Repositories and s.conn.Directories respectively. The only notable change in behavior is that a credential is no longer necessary if there are no s.conn.Repositories to scan.
f4ce4b4
to
8873812
Compare
If I understand this correctly, you refactored the Git source to allow it to be used in a new way. What are the old ways that it's (presumably) still being used? |
The main difference is that we weren't doing a git scan using a The problem I encountered when changing it was that the |
Signed-off-by: Savely Krasovsky <[email protected]>
* feat: initial support for bare repositories * feat: use concatenation instead of formatting and os.Getenv instead of os.Environ Signed-off-by: Savely Krasovsky <[email protected]> * fix: go-git update with pre-receive hooks fix Signed-off-by: Savely Krasovsky <[email protected]> * fix: remove info about pre-receive hook from README.md for now Signed-off-by: Savely Krasovsky <[email protected]> * fix: don't scan staged while using --bare option, fixes to make it work with the latest master Signed-off-by: Savely Krasovsky <[email protected]> * fix: small refactor according to #1518 Signed-off-by: Savely Krasovsky <[email protected]> --------- Signed-off-by: Savely Krasovsky <[email protected]>
Refactor the Chunks method of the git Source to call out to two helper methods: scanRepos and scanDirs which scans s.conn.Repositories and s.conn.Directories respectively. The only notable change in behavior is that a credential is no longer necessary if there are no s.conn.Repositories to scan.