Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive detection when inside submodules #276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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{
Expand Down