Skip to content

Commit

Permalink
Fix false positive detection when inside submodules
Browse files Browse the repository at this point in the history
Signed-off-by: menehune23 <[email protected]>
  • Loading branch information
menehune23 committed Jul 14, 2023
1 parent fe28954 commit cd7c001
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
19 changes: 17 additions & 2 deletions detect.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
}
18 changes: 17 additions & 1 deletion detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 a .git file is present (in submodule)", 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{
Expand Down

0 comments on commit cd7c001

Please sign in to comment.