From 8a1b11b75ab3de50d1230517708d6178de65e424 Mon Sep 17 00:00:00 2001 From: menehune23 Date: Fri, 14 Jul 2023 12:23:58 -0700 Subject: [PATCH] Fix false positive detection when inside submodules Signed-off-by: menehune23 --- detect.go | 19 +++++++++++++++++-- detect_test.go | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/detect.go b/detect.go index 22fb875..df84880 100644 --- a/detect.go +++ b/detect.go @@ -1,15 +1,17 @@ package git import ( + "errors" + "io/fs" + "os" "path/filepath" "github.com/paketo-buildpacks/packit/v2" - "github.com/paketo-buildpacks/packit/v2/fs" ) func Detect(bindingResolver BindingResolver) packit.DetectFunc { return func(context packit.DetectContext) (packit.DetectResult, error) { - exist, err := fs.Exists(filepath.Join(context.WorkingDir, ".git")) + exist, err := gitDirExists(context.WorkingDir) if err != nil { return packit.DetectResult{}, err } @@ -26,3 +28,16 @@ func Detect(bindingResolver BindingResolver) packit.DetectFunc { return packit.DetectResult{}, nil } } + +func gitDirExists(workingDir string) (bool, error) { + info, err := os.Stat(filepath.Join(workingDir, ".git")) + if errors.Is(err, fs.ErrNotExist) { + return false, nil + } + + if err != nil { + return false, err + } + + return info.IsDir(), nil +} diff --git a/detect_test.go b/detect_test.go index cb850e0..7c300a2 100644 --- a/detect_test.go +++ b/detect_test.go @@ -8,11 +8,12 @@ import ( "github.com/paketo-buildpacks/packit/v2" "github.com/paketo-buildpacks/packit/v2/servicebindings" + "github.com/paketo-buildpacks/git" "github.com/paketo-buildpacks/git/fakes" - "github.com/sclevine/spec" . "github.com/onsi/gomega" + "github.com/sclevine/spec" ) 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) { }) context("when a .git directory is not present", func() { + context("when in a submodule (.git is a file, not dir)", func() { + it.Before(func() { + err := os.WriteFile(filepath.Join(workingDir, ".git"), nil, os.ModePerm) + Expect(err).NotTo(HaveOccurred()) + }) + + it("fails detections", func() { + _, err := detect(packit.DetectContext{ + WorkingDir: workingDir, + Platform: packit.Platform{Path: "some-platform"}, + }) + Expect(err).To(MatchError(packit.Fail.WithMessage("failed to find .git directory and no git credential service bindings present"))) + }) + }) + context("when there are no git-credentials service bindings", func() { it("fails detections", func() { _, err := detect(packit.DetectContext{