6
6
"fmt"
7
7
"os"
8
8
"os/exec"
9
+ "path"
9
10
"path/filepath"
10
11
"strings"
11
12
@@ -349,6 +350,30 @@ func getCommitSHA(repoDir string) (string, error) {
349
350
return head .Hash ().String (), nil
350
351
}
351
352
353
+ func getSubmodules (repoDir string ) (submodules []* git.SubmoduleStatus , err error ) {
354
+ repo , err := git .PlainOpen (repoDir )
355
+ if err != nil {
356
+ return nil , err
357
+ }
358
+ worktree , err := repo .Worktree ()
359
+ if err != nil {
360
+ return nil , err
361
+ }
362
+ ss , err := worktree .Submodules ()
363
+ if err != nil {
364
+ return nil , err
365
+ }
366
+ for _ , s := range ss {
367
+ status , err := s .Status ()
368
+ if err != nil {
369
+ continue
370
+ }
371
+ submodules = append (submodules , status )
372
+ }
373
+
374
+ return submodules , nil
375
+ }
376
+
352
377
// Scan git repository. Expects repoDir to end with /
353
378
func scanGit (r reporter.Reporter , query * osv.BatchedQuery , repoDir string ) error {
354
379
commit , err := getCommitSHA (repoDir )
@@ -357,7 +382,25 @@ func scanGit(r reporter.Reporter, query *osv.BatchedQuery, repoDir string) error
357
382
}
358
383
r .PrintText (fmt .Sprintf ("Scanning %s at commit %s\n " , repoDir , commit ))
359
384
360
- return scanGitCommit (query , commit , repoDir )
385
+ err = scanGitCommit (query , commit , repoDir )
386
+ if err != nil {
387
+ return err
388
+ }
389
+
390
+ submodules , err := getSubmodules (repoDir )
391
+ if err != nil {
392
+ return err
393
+ }
394
+
395
+ for _ , s := range submodules {
396
+ r .PrintText (fmt .Sprintf ("Scanning submodule %s at commit %s\n " , s .Path , s .Expected .String ()))
397
+ err = scanGitCommit (query , s .Expected .String (), path .Join (repoDir , s .Path ))
398
+ if err != nil {
399
+ return err
400
+ }
401
+ }
402
+
403
+ return nil
361
404
}
362
405
363
406
func scanGitCommit (query * osv.BatchedQuery , commit string , source string ) error {
0 commit comments