Skip to content

Commit cd7c001

Browse files
committed
Fix false positive detection when inside submodules
Signed-off-by: menehune23 <meyera@vmware.com>
1 parent fe28954 commit cd7c001

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎detect.go‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package git
22

33
import (
4+
"errors"
5+
"io/fs"
6+
"os"
47
"path/filepath"
58

69
"github.com/paketo-buildpacks/packit/v2"
7-
"github.com/paketo-buildpacks/packit/v2/fs"
810
)
911

1012
func Detect(bindingResolver BindingResolver) packit.DetectFunc {
1113
return func(context packit.DetectContext) (packit.DetectResult, error) {
12-
exist, err := fs.Exists(filepath.Join(context.WorkingDir, ".git"))
14+
exist, err := gitDirExists(context.WorkingDir)
1315
if err != nil {
1416
return packit.DetectResult{}, err
1517
}
@@ -26,3 +28,16 @@ func Detect(bindingResolver BindingResolver) packit.DetectFunc {
2628
return packit.DetectResult{}, nil
2729
}
2830
}
31+
32+
func gitDirExists(workingDir string) (bool, error) {
33+
info, err := os.Stat(filepath.Join(workingDir, ".git"))
34+
if errors.Is(err, fs.ErrNotExist) {
35+
return false, nil
36+
}
37+
38+
if err != nil {
39+
return false, err
40+
}
41+
42+
return info.IsDir(), nil
43+
}

‎detect_test.go‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88

99
"github.com/paketo-buildpacks/packit/v2"
1010
"github.com/paketo-buildpacks/packit/v2/servicebindings"
11+
1112
"github.com/paketo-buildpacks/git"
1213
"github.com/paketo-buildpacks/git/fakes"
13-
"github.com/sclevine/spec"
1414

1515
. "github.com/onsi/gomega"
16+
"github.com/sclevine/spec"
1617
)
1718

1819
func testDetect(t *testing.T, context spec.G, it spec.S) {
@@ -59,6 +60,21 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
5960
})
6061

6162
context("when a .git directory is not present", func() {
63+
context("when a .git file is present (in submodule)", func() {
64+
it.Before(func() {
65+
err := os.WriteFile(filepath.Join(workingDir, ".git"), nil, os.ModePerm)
66+
Expect(err).NotTo(HaveOccurred())
67+
})
68+
69+
it("fails detections", func() {
70+
_, err := detect(packit.DetectContext{
71+
WorkingDir: workingDir,
72+
Platform: packit.Platform{Path: "some-platform"},
73+
})
74+
Expect(err).To(MatchError(packit.Fail.WithMessage("failed to find .git directory and no git credential service bindings present")))
75+
})
76+
})
77+
6278
context("when there are no git-credentials service bindings", func() {
6379
it("fails detections", func() {
6480
_, err := detect(packit.DetectContext{

0 commit comments

Comments
 (0)