Only scanned staged git changes. (#1143)

This commit is contained in:
ahrav 2023-03-01 08:58:36 -08:00 committed by GitHub
parent 0cf9139df6
commit aa47e5e248
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions

View file

@ -139,7 +139,8 @@ func (c *Parser) RepoPath(ctx context.Context, source string, head string, abbre
// Unstaged parses the output of the `git diff` command for the `source` path. // Unstaged parses the output of the `git diff` command for the `source` path.
func (c *Parser) Unstaged(ctx context.Context, source string) (chan Commit, error) { func (c *Parser) Unstaged(ctx context.Context, source string) (chan Commit, error) {
args := []string{"-C", source, "diff", "-p", "-U5", "--full-history", "--diff-filter=AM", "--date=format:%a %b %d %H:%M:%S %Y %z", "HEAD"} // Provide the --cached flag to diff to get the diff of the staged changes.
args := []string{"-C", source, "diff", "-p", "-U5", "--cached", "--full-history", "--diff-filter=AM", "--date=format:%a %b %d %H:%M:%S %Y %z", "HEAD"}
cmd := exec.Command("git", args...) cmd := exec.Command("git", args...)

View file

@ -472,9 +472,9 @@ func (s *Git) gitChunk(ctx context.Context, diff gitparse.Diff, fileName, email,
} }
} }
// ScanUnstaged chunks unstaged changes. // ScanStaged chunks staged changes.
func (s *Git) ScanUnstaged(ctx context.Context, repo *git.Repository, path string, scanOptions *ScanOptions, chunksChan chan *sources.Chunk) error { func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string, scanOptions *ScanOptions, chunksChan chan *sources.Chunk) error {
// get the URL metadata for reporting (may be empty) // Get the URL metadata for reporting (may be empty).
urlMetadata := getSafeRemoteURL(repo, "origin") urlMetadata := getSafeRemoteURL(repo, "origin")
commitChan, err := gitparse.NewParser().Unstaged(ctx, path) commitChan, err := gitparse.NewParser().Unstaged(ctx, path)
@ -488,11 +488,11 @@ func (s *Git) ScanUnstaged(ctx context.Context, repo *git.Repository, path strin
var depth int64 var depth int64
var reachedBase = false var reachedBase = false
ctx.Logger().V(1).Info("scanning unstaged changes", "path", path) ctx.Logger().V(1).Info("scanning staged changes", "path", path)
for commit := range commitChan { for commit := range commitChan {
for _, diff := range commit.Diffs { for _, diff := range commit.Diffs {
logger := ctx.Logger().WithValues("filename", diff.PathB, "commit", commit.Hash, "file", diff.PathB) logger := ctx.Logger().WithValues("filename", diff.PathB, "commit", commit.Hash, "file", diff.PathB)
logger.V(2).Info("scanning unstaged changes from git") logger.V(2).Info("scanning staged changes from git")
if scanOptions.MaxDepth > 0 && depth >= scanOptions.MaxDepth { if scanOptions.MaxDepth > 0 && depth >= scanOptions.MaxDepth {
logger.V(1).Info("reached max depth") logger.V(1).Info("reached max depth")
@ -525,7 +525,7 @@ func (s *Git) ScanUnstaged(ctx context.Context, repo *git.Repository, path strin
// Handle binary files by reading the entire file rather than using the diff. // Handle binary files by reading the entire file rather than using the diff.
if diff.IsBinary { if diff.IsBinary {
commitHash := plumbing.NewHash(hash) commitHash := plumbing.NewHash(hash)
metadata := s.sourceMetadataFunc(fileName, email, "Unstaged", when, urlMetadata, 0) metadata := s.sourceMetadataFunc(fileName, email, "Staged", when, urlMetadata, 0)
chunkSkel := &sources.Chunk{ chunkSkel := &sources.Chunk{
SourceName: s.sourceName, SourceName: s.sourceName,
SourceID: s.sourceID, SourceID: s.sourceID,
@ -539,7 +539,7 @@ func (s *Git) ScanUnstaged(ctx context.Context, repo *git.Repository, path strin
continue continue
} }
metadata := s.sourceMetadataFunc(fileName, email, "Unstaged", when, urlMetadata, int64(diff.LineStart)) metadata := s.sourceMetadataFunc(fileName, email, "Staged", when, urlMetadata, int64(diff.LineStart))
chunksChan <- &sources.Chunk{ chunksChan <- &sources.Chunk{
SourceName: s.sourceName, SourceName: s.sourceName,
SourceID: s.sourceID, SourceID: s.sourceID,
@ -564,7 +564,7 @@ func (s *Git) ScanRepo(ctx context.Context, repo *git.Repository, repoPath strin
if err := s.ScanCommits(ctx, repo, repoPath, scanOptions, chunksChan); err != nil { if err := s.ScanCommits(ctx, repo, repoPath, scanOptions, chunksChan); err != nil {
return err return err
} }
if err := s.ScanUnstaged(ctx, repo, repoPath, scanOptions, chunksChan); err != nil { if err := s.ScanStaged(ctx, repo, repoPath, scanOptions, chunksChan); err != nil {
ctx.Logger().V(1).Info("error scanning unstaged changes", "error", err) ctx.Logger().V(1).Info("error scanning unstaged changes", "error", err)
} }