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

Scan submodules too. #581

Merged
merged 9 commits into from
Oct 30, 2023
45 changes: 44 additions & 1 deletion pkg/osvscanner/osvscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -349,6 +350,30 @@ func getCommitSHA(repoDir string) (string, error) {
return head.Hash().String(), nil
}

func getSubmodules(repoDir string) (submodules []*git.SubmoduleStatus, err error) {
repo, err := git.PlainOpen(repoDir)
if err != nil {
return nil, err
}
worktree, err := repo.Worktree()
if err != nil {
return nil, err
}
ss, err := worktree.Submodules()
if err != nil {
return nil, err
}
for _, s := range ss {
status, err := s.Status()
if err != nil {
continue
}
submodules = append(submodules, status)
}

return submodules, nil
}

// Scan git repository. Expects repoDir to end with /
func scanGit(r reporter.Reporter, query *osv.BatchedQuery, repoDir string) error {
commit, err := getCommitSHA(repoDir)
Expand All @@ -357,7 +382,25 @@ func scanGit(r reporter.Reporter, query *osv.BatchedQuery, repoDir string) error
}
r.PrintText(fmt.Sprintf("Scanning %s at commit %s\n", repoDir, commit))

return scanGitCommit(query, commit, repoDir)
err = scanGitCommit(query, commit, repoDir)
if err != nil {
return err
}

submodules, err := getSubmodules(repoDir)
if err != nil {
return err
}

for _, s := range submodules {
r.PrintText(fmt.Sprintf("Scanning submodule %s at commit %s\n", s.Path, s.Expected.String()))
err = scanGitCommit(query, s.Expected.String(), path.Join(repoDir, s.Path))
if err != nil {
return err
}
}

return nil
}

func scanGitCommit(query *osv.BatchedQuery, commit string, source string) error {
Expand Down