Skip to content

Commit

Permalink
Clean subpath to ensure dots are handled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
xtreme-shane-lattanzio committed Nov 24, 2023
1 parent e119c97 commit d467372
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/git/remote_git_resolver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"path"
"regexp"
"strings"

Expand All @@ -22,6 +23,10 @@ func (*remoteGitResolver) Resolve(auth transport.AuthMethod, sourceConfig corev1
var resolvedConfig corev1alpha1.ResolvedSourceConfig
var err error

if sourceConfig.SubPath == "." {
sourceConfig.SubPath = ""
}

if sourceConfig.SubPath != "" {
resolvedConfig, err = resolveSourceWithSubpath(auth, sourceConfig)
} else {
Expand Down Expand Up @@ -97,7 +102,7 @@ func resolveSourceWithSubpath(auth transport.AuthMethod, sourceConfig corev1alph
effectiveCommit := ""

if refSourceType == corev1alpha1.Branch {
effectiveCommit, err = latestCommitForSubpath(r, sourceConfig.SubPath)
effectiveCommit, err = latestCommitForSubpath(r, path.Clean(sourceConfig.SubPath))
} else {
effectiveCommit = ref.Hash().String()
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/git/remote_git_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ func testRemoteGitResolver(t *testing.T, when spec.G, it spec.S) {
})
})

when("source is a branch with latest commit and a . subpath", func() {
it("returns branch with resolved commit", func() {
gitResolver := remoteGitResolver{}

resolvedGitSource, err := gitResolver.Resolve(anonymousAuth, corev1alpha1.SourceConfig{
Git: &corev1alpha1.Git{
URL: url,
Revision: "master",
},
SubPath: ".",
})
require.NoError(t, err)

assert.Equal(t, corev1alpha1.ResolvedSourceConfig{
Git: &corev1alpha1.ResolvedGitSource{
URL: url,
Revision: fixtureHEADMasterCommit,
Type: corev1alpha1.Branch,
},
}, resolvedGitSource)
})
})

when("source is a branch with older subpath", func() {
it("returns branch with resolved commit", func() {
gitResolver := remoteGitResolver{}
Expand Down

0 comments on commit d467372

Please sign in to comment.