Skip to content

Commit fd2ef0f

Browse files
authored
feat: Make skip git the default behavior (#1584)
Also removes the `--json` flag since that has been deprecated for a while now. The json tests are not deleted, as there are the same tests with the `--format json` flag. Succeeds: #1311 Closes: #1277
1 parent f88dbb0 commit fd2ef0f

File tree

17 files changed

+35
-118
lines changed

17 files changed

+35
-118
lines changed

.github/workflows/osv-scanner-reusable-pr.yml

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ on:
2626
type: string
2727
default: |-
2828
-r
29-
--skip-git
3029
./
3130
results-file-name:
3231
description: "File name of the result SARIF file"

.github/workflows/osv-scanner-reusable.yml

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ on:
2626
type: string
2727
default: |-
2828
-r
29-
--skip-git
3029
./
3130
results-file-name:
3231
description: "File name of the result SARIF file"

.github/workflows/osv-scanner-unified-action.yml

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ jobs:
3838
with:
3939
# Just scan the root directory and docs, since everything else is fixtures
4040
scan-args: |-
41-
--skip-git
4241
./
4342
./docs/
4443
scan-pr:
@@ -52,6 +51,5 @@ jobs:
5251
with:
5352
# Just scan the root directory and docs, since everything else is fixtures
5453
scan-args: |-
55-
--skip-git
5654
./
5755
./docs/

.github/workflows/prerelease-check.yml

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ jobs:
2727
# Only scan the top level go.mod file without recursively scanning directories since
2828
# this is pipeline is about releasing the go module and binary
2929
scan-args: |-
30-
--skip-git
3130
./
3231
3332
format:

actions/scanner/action.yml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ inputs:
55
scan-args:
66
description: "Arguments to osv-scanner, separated by new line"
77
default: |-
8-
--skip-git
98
--recursive
109
./
1110
runs:

cmd/osv-scanner/__snapshots__/main_test.snap

+2-64
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ invalid verbosity level "unknown" - must be one of: error, warn, info, verbose
873873

874874
---
875875

876-
[TestRun/json_output_1 - 1]
876+
[TestRun/json_output - 1]
877877
{
878878
"results": [],
879879
"experimental_config": {
@@ -886,27 +886,7 @@ invalid verbosity level "unknown" - must be one of: error, warn, info, verbose
886886

887887
---
888888

889-
[TestRun/json_output_1 - 2]
890-
Scanning dir ./fixtures/locks-many/composer.lock
891-
Scanned <rootdir>/fixtures/locks-many/composer.lock file and found 1 package
892-
Loaded filter from: <rootdir>/fixtures/locks-many/osv-scanner.toml
893-
894-
---
895-
896-
[TestRun/json_output_2 - 1]
897-
{
898-
"results": [],
899-
"experimental_config": {
900-
"licenses": {
901-
"summary": false,
902-
"allowlist": null
903-
}
904-
}
905-
}
906-
907-
---
908-
909-
[TestRun/json_output_2 - 2]
889+
[TestRun/json_output - 2]
910890
Scanning dir ./fixtures/locks-many/composer.lock
911891
Scanned <rootdir>/fixtures/locks-many/composer.lock file and found 1 package
912892
Loaded filter from: <rootdir>/fixtures/locks-many/osv-scanner.toml
@@ -2633,48 +2613,6 @@ Loaded Packagist local db from <tempdir>/osv-scanner/Packagist/all.zip
26332613

26342614
---
26352615

2636-
[TestRun_LocalDatabases/output_with_json#01 - 1]
2637-
{
2638-
"results": [],
2639-
"experimental_config": {
2640-
"licenses": {
2641-
"summary": false,
2642-
"allowlist": null
2643-
}
2644-
}
2645-
}
2646-
2647-
---
2648-
2649-
[TestRun_LocalDatabases/output_with_json#01 - 2]
2650-
Scanning dir ./fixtures/locks-many/composer.lock
2651-
Scanned <rootdir>/fixtures/locks-many/composer.lock file and found 1 package
2652-
Loaded filter from: <rootdir>/fixtures/locks-many/osv-scanner.toml
2653-
Loaded Packagist local db from <tempdir>/osv-scanner/Packagist/all.zip
2654-
2655-
---
2656-
2657-
[TestRun_LocalDatabases/output_with_json#01 - 3]
2658-
{
2659-
"results": [],
2660-
"experimental_config": {
2661-
"licenses": {
2662-
"summary": false,
2663-
"allowlist": null
2664-
}
2665-
}
2666-
}
2667-
2668-
---
2669-
2670-
[TestRun_LocalDatabases/output_with_json#01 - 4]
2671-
Scanning dir ./fixtures/locks-many/composer.lock
2672-
Scanned <rootdir>/fixtures/locks-many/composer.lock file and found 1 package
2673-
Loaded filter from: <rootdir>/fixtures/locks-many/osv-scanner.toml
2674-
Loaded Packagist local db from <tempdir>/osv-scanner/Packagist/all.zip
2675-
2676-
---
2677-
26782616
[TestRun_LocalDatabases_AlwaysOffline/a_bunch_of_different_lockfiles_and_ecosystem - 1]
26792617
Scanning dir ./fixtures/locks-requirements
26802618
Scanned <rootdir>/fixtures/locks-requirements/my-requirements.txt file and found 1 package

cmd/osv-scanner/internal/helper/helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
// flags that require network access and values to disable them.
2323
var OfflineFlags = map[string]string{
24-
"skip-git": "true",
24+
"include-git-root": "true",
2525
"experimental-offline-vulnerabilities": "true",
2626
"experimental-no-resolve": "true",
2727
"experimental-licenses-summary": "false",

cmd/osv-scanner/main_test.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,8 @@ func TestRun(t *testing.T) {
244244
args: []string{"", "--recursive", "--no-ignore", "./fixtures/locks-gitignore"},
245245
exit: 0,
246246
},
247-
// output with json
248247
{
249-
name: "json output 1",
250-
args: []string{"", "--json", "./fixtures/locks-many/composer.lock"},
251-
exit: 0,
252-
},
253-
{
254-
name: "json output 2",
248+
name: "json output",
255249
args: []string{"", "--format", "json", "./fixtures/locks-many/composer.lock"},
256250
exit: 0,
257251
},
@@ -631,11 +625,6 @@ func TestRun_LocalDatabases(t *testing.T) {
631625
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--recursive", "--no-ignore", "./fixtures/locks-gitignore"},
632626
exit: 0,
633627
},
634-
{
635-
name: "output with json",
636-
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--json", "./fixtures/locks-many/composer.lock"},
637-
exit: 0,
638-
},
639628
{
640629
name: "output with json",
641630
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--format", "json", "./fixtures/locks-many/composer.lock"},

cmd/osv-scanner/scan/image/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func action(context *cli.Context, stdout, stderr io.Writer) (reporter.Reporter,
7676
Image: context.Args().First(),
7777
ConfigOverridePath: context.String("config"),
7878
IsImageArchive: context.Bool("archive"),
79-
SkipGit: context.Bool("skip-git"),
79+
IncludeGitRoot: context.Bool("include-git-root"),
8080
ExperimentalScannerActions: helper.GetExperimentalScannerActions(context, scanLicensesAllowlist),
8181
}
8282

cmd/osv-scanner/scan/source/main.go

+6-14
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ var projectScanFlags = []cli.Flag{
2727
Usage: "scan sbom file on this path",
2828
TakesFile: true,
2929
},
30-
&cli.BoolFlag{
31-
Name: "json",
32-
Usage: "sets output to json (deprecated, use --format json instead)",
33-
},
34-
&cli.BoolFlag{
35-
Name: "skip-git",
36-
Usage: "skip scanning git repositories",
37-
Value: false,
38-
},
3930
&cli.BoolFlag{
4031
Name: "recursive",
4132
Aliases: []string{"r"},
@@ -55,6 +46,11 @@ var projectScanFlags = []cli.Flag{
5546
Name: "no-call-analysis",
5647
Usage: "disables call graph analysis",
5748
},
49+
&cli.BoolFlag{
50+
Name: "include-git-root",
51+
Usage: "include scanning git root (non-submoduled) repositories",
52+
Value: false,
53+
},
5854
}
5955

6056
var projectScanExperimentalFlags = []cli.Flag{
@@ -106,10 +102,6 @@ func Command(stdout, stderr io.Writer, r *reporter.Reporter) *cli.Command {
106102
func Action(context *cli.Context, stdout, stderr io.Writer) (reporter.Reporter, error) {
107103
format := context.String("format")
108104

109-
if context.Bool("json") {
110-
format = "json"
111-
}
112-
113105
outputPath := context.String("output")
114106
serve := context.Bool("serve")
115107
if serve {
@@ -158,7 +150,7 @@ func Action(context *cli.Context, stdout, stderr io.Writer) (reporter.Reporter,
158150
LockfilePaths: context.StringSlice("lockfile"),
159151
SBOMPaths: context.StringSlice("sbom"),
160152
Recursive: context.Bool("recursive"),
161-
SkipGit: context.Bool("skip-git"),
153+
IncludeGitRoot: context.Bool("include-git-root"),
162154
NoIgnore: context.Bool("no-ignore"),
163155
ConfigOverridePath: context.String("config"),
164156
DirectoryPaths: context.Args().Slice(),

docs/github-action.md

-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ jobs:
138138
# Only scan the top level go.mod file without recursively scanning directories since
139139
# this is pipeline is about releasing the go module and binary
140140
scan-args: |-
141-
--skip-git
142141
./
143142
permissions:
144143
# Require writing security events to upload SARIF file to security tab
@@ -167,7 +166,6 @@ The GitHub Actions have the following optional inputs:
167166
Default:
168167
```bash
169168
--recursive # Recursively scan subdirectories
170-
--skip-git=true # Skip commit scanning to focus on dependencies
171169
./ # Start the scan from the root of the repository
172170
```
173171
- `results-file-name`: This is the name of the final SARIF file uploaded to Github.
@@ -202,7 +200,6 @@ jobs:
202200
with:
203201
scan-args: |-
204202
--recursive
205-
--skip-git=true
206203
./
207204
```
208205

docs/scan-source.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ The preceding command will find lockfiles, SBOMs, and git directories in your ta
3030

3131
The recursive flag `-r` or `--recursive` will tell the scanner to search all subdirectories in addition to the specified directory. It can find additional lockfiles, dependencies, and vulnerabilities. If your project has deeply nested subdirectories, a recursive search may take a long time.
3232

33-
Git directories are searched for the latest commit hash. Searching for git commit hash is intended to work with projects that use git submodules or a similar mechanism where dependencies are checked out as real git repositories.
34-
3533
## Ignored files
3634

3735
By default, OSV-Scanner will not scan files that are ignored by `.gitignore` files. All recursively scanned files are matched to a git repository (if it exists) and any matching `.gitignore` files within that repository are taken into account.
@@ -87,6 +85,8 @@ osv-scanner scan source --lockfile ':/path/to/my:projects/package-lock.json'
8785

8886
OSV-Scanner will automatically scan git submodules and vendored directories for C/C++ code and try to attribute them to specific dependencies and versions. See [C/C++ Scanning](<supported_languages_and_lockfiles#C/C++ scanning>) for more details.
8987

88+
By default, root git directories (i.e. git repositories that are not a submodule of a bigger git repo) are skipped. You can include those repositories by setting the `--include-git-root` flag.
89+
9090
## Scanning with call analysis
9191

9292
Call stack analysis can be performed on some languages to check if the

internal/scalibrextract/vcs/gitrepo/extractor.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414

1515
// Extractor extracts git repository hashes including submodule hashes.
1616
// This extractor will not return an error, and will just return no results if we fail to extract
17-
type Extractor struct{}
17+
type Extractor struct {
18+
IncludeRootGit bool
19+
}
1820

1921
var _ filesystem.Extractor = Extractor{}
2022

@@ -96,12 +98,14 @@ func (e Extractor) Extract(_ context.Context, input *filesystem.ScanInput) ([]*e
9698
//nolint:prealloc // Not sure how many there will be in advance.
9799
var packages []*extractor.Inventory
98100

99-
commitSHA, err := getCommitSHA(repo)
101+
if e.IncludeRootGit {
102+
commitSHA, err := getCommitSHA(repo)
100103

101-
// If error is not nil, then ignore this and continue, as it is not fatal.
102-
// The error could be because there are no commits in the repository
103-
if err == nil {
104-
packages = append(packages, createCommitQueryInventory(commitSHA, input.Path))
104+
// If error is not nil, then ignore this and continue, as it is not fatal.
105+
// The error could be because there are no commits in the repository
106+
if err == nil {
107+
packages = append(packages, createCommitQueryInventory(commitSHA, input.Path))
108+
}
105109
}
106110

107111
// If we can't get submodules, just return with what we have.

internal/scalibrextract/vcs/gitrepo/extractor_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func TestExtractor_Extract(t *testing.T) {
5050
for _, tt := range tests {
5151
t.Run(tt.Name, func(t *testing.T) {
5252
t.Parallel()
53-
extr := gitrepo.Extractor{}
53+
extr := gitrepo.Extractor{
54+
IncludeRootGit: true,
55+
}
5456
parent := filepath.Dir(tt.InputConfig.Path)
5557
err := os.Rename(path.Join(parent, "git-hidden"), path.Join(parent, ".git"))
5658
if err != nil {

pkg/osvscanner/internal/scanners/extractorbuilder.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,24 @@ func BuildSBOMExtractors() []filesystem.Extractor {
126126
// BuildWalkerExtractors returns all relevant extractors for directory scanning given the required clients
127127
// All clients can be nil, and if nil the extractors requiring those clients will not be returned.
128128
func BuildWalkerExtractors(
129-
skipGit bool,
129+
includeRootGit bool,
130130
osvdevClient *osvdev.OSVClient,
131131
dependencyClients map[osvschema.Ecosystem]client.DependencyClient,
132132
mavenAPIClient *datasource.MavenRegistryAPIClient) []filesystem.Extractor {
133133
relevantExtractors := []filesystem.Extractor{}
134134

135-
if !skipGit {
136-
relevantExtractors = append(relevantExtractors, gitrepo.Extractor{})
135+
if includeRootGit {
136+
relevantExtractors = append(relevantExtractors, gitrepo.Extractor{
137+
IncludeRootGit: includeRootGit,
138+
})
137139
}
138140
relevantExtractors = append(relevantExtractors, lockfileExtractors...)
139141
relevantExtractors = append(relevantExtractors, sbomExtractors...)
140142

141143
if osvdevClient != nil {
142144
relevantExtractors = append(relevantExtractors, vendored.Extractor{
143-
ScanGitDir: skipGit,
145+
// Only attempt to vendor check git directories if we are not skipping scanning root git directories
146+
ScanGitDir: !includeRootGit,
144147
OSVClient: osvdevClient,
145148
})
146149
}

pkg/osvscanner/osvscanner.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ScannerActions struct {
3939
DirectoryPaths []string
4040
GitCommits []string
4141
Recursive bool
42-
SkipGit bool
42+
IncludeGitRoot bool
4343
NoIgnore bool
4444
Image string
4545
IsImageArchive bool
@@ -182,8 +182,6 @@ func DoScan(actions ScannerActions, r reporter.Reporter) (models.VulnerabilityRe
182182
// --- Sanity check flags ----
183183
// TODO(v2): Move the logic of the offline flag changing other flags into here from the main.go/scan.go
184184
if actions.CompareOffline {
185-
actions.SkipGit = true
186-
187185
if len(actions.ScanLicensesAllowlist) > 0 || actions.ScanLicensesSummary {
188186
return models.VulnerabilityResults{}, errors.New("cannot retrieve licenses locally")
189187
}

pkg/osvscanner/scan.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func scan(r reporter.Reporter, accessors ExternalAccessors, actions ScannerActio
3737

3838
// --- Directories ---
3939
dirExtractors := scanners.BuildWalkerExtractors(
40-
actions.SkipGit,
40+
actions.IncludeGitRoot,
4141
accessors.OSVDevClient,
4242
accessors.DependencyClients,
4343
accessors.MavenRegistryAPIClient,

0 commit comments

Comments
 (0)