Skip to content
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

Add more logging for git sources #974

Merged
merged 1 commit into from Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 21 additions & 11 deletions pkg/sources/git/git.go
Expand Up @@ -267,20 +267,29 @@ func CloneRepo(ctx context.Context, userInfo *url.Userinfo, gitUrl string, args
gitArgs = append(gitArgs, args...)
cloneCmd := exec.Command("git", gitArgs...)

safeUrl, err := stripPassword(gitUrl)
if err != nil {
ctx.Logger().V(1).Info("error stripping password from git url", "error", err)
}
logger := ctx.Logger().WithValues(
"subcommand", "git clone",
"repo", safeUrl,
"path", clonePath,
"args", args,
)

// Execute command and wait for the stdout / stderr.
output, err := cloneCmd.CombinedOutput()
if err != nil {
err = errors.WrapPrefix(err, "error running 'git clone'", 0)
}
logger.V(3).Info("git subcommand finished", "output", string(output))

if cloneCmd.ProcessState == nil {
return "", nil, errors.New("clone command exited with no output")
}
if cloneCmd.ProcessState != nil && cloneCmd.ProcessState.ExitCode() != 0 {
safeUrl, err := stripPassword(gitUrl)
if err != nil {
ctx.Logger().V(1).Info("error stripping password from git url", "error", err)
}
ctx.Logger().V(1).Info("git clone failed", "error", err, "repo", safeUrl, "output", string(output))
logger.V(1).Info("git clone failed", "error", err)
return "", nil, fmt.Errorf("could not clone repo: %s, %w", safeUrl, err)
}

Expand All @@ -289,7 +298,7 @@ func CloneRepo(ctx context.Context, userInfo *url.Userinfo, gitUrl string, args
return "", nil, fmt.Errorf("could not open cloned repo: %w", err)
}

ctx.Logger().V(1).Info("cloned repo", "repo", gitUrl, "clone-path", clonePath)
logger.V(1).Info("successfully cloned repo")
return clonePath, repo, nil
}

Expand Down Expand Up @@ -336,20 +345,21 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string

var depth int64

ctx.Logger().V(1).Info("scanning repo", "repo", urlMetadata, "base", scanOptions.BaseHash, "head", scanOptions.HeadHash)
logger := ctx.Logger().WithValues("repo", urlMetadata)
logger.V(1).Info("scanning repo", "base", scanOptions.BaseHash, "head", scanOptions.HeadHash)
for commit := range commitChan {
if len(scanOptions.BaseHash) > 0 {
if commit.Hash == scanOptions.BaseHash {
ctx.Logger().V(1).Info("reached base commit", "commit", commit.Hash)
logger.V(1).Info("reached base commit", "commit", commit.Hash)
break
}
}
if scanOptions.MaxDepth > 0 && depth >= scanOptions.MaxDepth {
ctx.Logger().V(1).Info("reached max depth", "depth", depth)
logger.V(1).Info("reached max depth", "depth", depth)
break
}
depth++
ctx.Logger().V(5).Info("scanning commit", "commit", commit.Hash, "message", commit.Message)
logger.V(5).Info("scanning commit", "commit", commit.Hash)
for _, diff := range commit.Diffs {
if !scanOptions.Filter.Pass(diff.PathB) {
continue
Expand All @@ -376,7 +386,7 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
Verify: s.verify,
}
if err := handleBinary(ctx, repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {
ctx.Logger().V(1).Info("error handling binary file", "error", err, "filename", fileName, "commit", commitHash, "file", diff.PathB)
logger.V(1).Info("error handling binary file", "error", err, "filename", fileName, "commit", commitHash, "file", diff.PathB)
}
continue
}
Expand Down