Skip to content

Commit 9ab36d3

Browse files
committed
feat: support charts from sub-folders inside git repository
Signed-off-by: Dominykas Blyžė <[email protected]>
1 parent 9c14265 commit 9ab36d3

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

internal/gitutil/gitutil.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import (
3131
var gitRepositoryURLRe = regexp.MustCompile(`^git(\+\w+)?://`)
3232

3333
type GitRepositoryURL struct {
34-
RepositoryURL string
35-
GitRemoteURL *url.URL
34+
RepositoryURL string
35+
GitRemoteURL *url.URL
36+
PathUnderGitRepository string
3637
}
3738

3839
// HasGitReference returns true if a git repository contains a specified ref (branch/tag)
@@ -71,8 +72,22 @@ func ParseGitRepositoryURL(repositoryURL string) (GitRepositoryURL, error) {
7172
return GitRepositoryURL{}, errors.Errorf("git repository URL should not contain credentials - please use git credential helpers")
7273
}
7374

75+
path := ""
76+
77+
if gitRemoteURL.Fragment != "" {
78+
query, err := url.ParseQuery(gitRemoteURL.Fragment)
79+
if err != nil {
80+
return GitRepositoryURL{}, err
81+
}
82+
83+
path = query.Get("subdirectory")
84+
}
85+
86+
gitRemoteURL.Fragment = ""
87+
7488
return GitRepositoryURL{
75-
RepositoryURL: repositoryURL,
76-
GitRemoteURL: gitRemoteURL,
89+
RepositoryURL: repositoryURL,
90+
GitRemoteURL: gitRemoteURL,
91+
PathUnderGitRepository: path,
7792
}, err
7893
}

internal/gitutil/gitutil_test.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,39 @@ func TestIsGitUrl(t *testing.T) {
4141
func TestParseGitRepositoryURL(t *testing.T) {
4242
// Test table: Given url, ParseGitRepositoryURL should return expect.
4343
tests := []struct {
44-
url string
45-
expectRepositoryURL string
46-
expectGitRepositoryURL string
44+
url string
45+
expectRepositoryURL string
46+
expectGitRemoteURL string
47+
expectedPathUnderGitRepository string
4748
}{
4849
{
49-
url: "git://example.com/example/chart",
50-
expectRepositoryURL: "git://example.com/example/chart",
51-
expectGitRepositoryURL: "git://example.com/example/chart",
50+
url: "git://example.com/example/chart",
51+
expectRepositoryURL: "git://example.com/example/chart",
52+
expectGitRemoteURL: "git://example.com/example/chart",
5253
},
5354
{
54-
url: "git+https://example.com/example/chart",
55-
expectRepositoryURL: "git+https://example.com/example/chart",
56-
expectGitRepositoryURL: "https://example.com/example/chart",
55+
url: "git+https://example.com/example/chart",
56+
expectRepositoryURL: "git+https://example.com/example/chart",
57+
expectGitRemoteURL: "https://example.com/example/chart",
58+
},
59+
{
60+
url: "git+https://example.com/example/chart#subdirectory=charts/some-chart",
61+
expectRepositoryURL: "git+https://example.com/example/chart#subdirectory=charts/some-chart",
62+
expectGitRemoteURL: "https://example.com/example/chart",
63+
expectedPathUnderGitRepository: "charts/some-chart",
5764
},
5865
}
5966

6067
for _, test := range tests {
6168
parsed, _ := ParseGitRepositoryURL(test.url)
6269
if parsed.RepositoryURL != test.expectRepositoryURL {
63-
t.Errorf("Expected RepositoryURL %s for %s, but got %s", test.expectRepositoryURL, test.url, parsed)
70+
t.Errorf("Expected RepositoryURL %s for %s, but got %s", test.expectRepositoryURL, test.url, parsed.RepositoryURL)
71+
}
72+
if parsed.GitRemoteURL.String() != test.expectGitRemoteURL {
73+
t.Errorf("Expected GitRemoteURL %s for %s, but got %s", test.expectGitRemoteURL, test.url, parsed.GitRemoteURL)
6474
}
65-
if parsed.GitRemoteURL.String() != test.expectGitRepositoryURL {
66-
t.Errorf("Expected GitRemoteURL %s for %s, but got %s", test.expectGitRepositoryURL, test.url, parsed)
75+
if parsed.PathUnderGitRepository != test.expectedPathUnderGitRepository {
76+
t.Errorf("Expected PathUnderGitRepository %s for %s, but got %s", test.expectGitRemoteURL, test.url, parsed.PathUnderGitRepository)
6777
}
6878
}
6979
}

pkg/getter/gitgetter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (g *GitGetter) get(href string) (*bytes.Buffer, error) {
7878
return nil, err
7979
}
8080

81-
ch, err := loader.LoadDir(chartTmpDir)
81+
ch, err := loader.LoadDir(filepath.Join(chartTmpDir, gitURL.PathUnderGitRepository))
8282
if err != nil {
8383
return nil, err
8484
}

0 commit comments

Comments
 (0)