Skip to content

Commit 04f638f

Browse files
committed
refactor: extract gitutil.IsGitURL helper
Addresses helm#11258 (comment) Signed-off-by: Dominykas Blyžė <[email protected]>
1 parent c93500d commit 04f638f

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

internal/resolver/resolver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ import (
2929

3030
"helm.sh/helm/v3/pkg/chart"
3131
"helm.sh/helm/v3/pkg/chart/loader"
32-
"helm.sh/helm/v3/pkg/gitutils"
32+
"helm.sh/helm/v3/pkg/gitutil"
3333
"helm.sh/helm/v3/pkg/helmpath"
3434
"helm.sh/helm/v3/pkg/provenance"
3535
"helm.sh/helm/v3/pkg/registry"
3636
"helm.sh/helm/v3/pkg/repo"
3737
)
3838

39-
var hasGitReference = gitutils.HasGitReference
39+
var hasGitReference = gitutil.HasGitReference
4040

4141
// Resolver resolves dependencies from semantic version ranges to a particular version.
4242
type Resolver struct {
@@ -110,7 +110,7 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string
110110
continue
111111
}
112112

113-
if strings.HasPrefix(d.Repository, "git://") {
113+
if gitutil.IsGitURL(d.Repository) {
114114

115115
found, err := hasGitReference(strings.TrimPrefix(d.Repository, "git://"), d.Version, d.Name)
116116

pkg/downloader/chart_downloader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"helm.sh/helm/v3/internal/fileutil"
3232
"helm.sh/helm/v3/internal/urlutil"
3333
"helm.sh/helm/v3/pkg/getter"
34+
"helm.sh/helm/v3/pkg/gitutil"
3435
"helm.sh/helm/v3/pkg/helmpath"
3536
"helm.sh/helm/v3/pkg/provenance"
3637
"helm.sh/helm/v3/pkg/registry"
@@ -96,7 +97,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
9697

9798
scheme := ""
9899

99-
if strings.HasPrefix(ref, "git://") {
100+
if gitutil.IsGitURL(ref) {
100101
scheme = "git"
101102
} else {
102103
scheme = u.Scheme
@@ -207,7 +208,7 @@ func (c *ChartDownloader) getOciURI(ref, version string, u *url.URL) (*url.URL,
207208
// - If version is empty, this will return the URL for the latest version
208209
// - If no version can be found, an error is returned
209210
func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, error) {
210-
if strings.HasPrefix(ref, "git://") {
211+
if gitutil.IsGitURL(ref) {
211212
gitURL := strings.TrimPrefix(ref, "git://")
212213
u, err := giturls.Parse(gitURL)
213214
if err != nil {

pkg/downloader/manager.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"helm.sh/helm/v3/pkg/chart/loader"
4141
"helm.sh/helm/v3/pkg/chartutil"
4242
"helm.sh/helm/v3/pkg/getter"
43+
"helm.sh/helm/v3/pkg/gitutil"
4344
"helm.sh/helm/v3/pkg/helmpath"
4445
"helm.sh/helm/v3/pkg/registry"
4546
"helm.sh/helm/v3/pkg/repo"
@@ -353,7 +354,7 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error {
353354
getter.WithTagName(version))
354355
}
355356

356-
if strings.HasPrefix(churl, "git://") {
357+
if gitutil.IsGitURL(churl) {
357358
version = dep.Version
358359

359360
dl.Options = append(dl.Options, getter.WithTagName(version))
@@ -489,7 +490,7 @@ Loop:
489490
}
490491

491492
// If repo is from git url, continue
492-
if strings.HasPrefix(dd.Repository, "git://") {
493+
if gitutil.IsGitURL(dd.Repository) {
493494
continue
494495
}
495496

@@ -613,7 +614,7 @@ func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string,
613614
// if dep chart is from a git url, assume it is valid for now.
614615
// if the repo does not exist then it will later error when we try to fetch branches and tags.
615616
// we could check for the repo existence here, but trying to avoid another git request.
616-
if strings.HasPrefix(dd.Repository, "git://") {
617+
if gitutil.IsGitURL(dd.Repository) {
617618
if m.Debug {
618619
fmt.Fprintf(m.Out, "Repository from git url: %s\n", strings.TrimPrefix(dd.Repository, "git:"))
619620
}
@@ -731,7 +732,7 @@ func (m *Manager) parallelRepoUpdate(repos []*repo.Entry) error {
731732
//
732733
// If it finds a URL that is "relative", it will prepend the repoURL.
733734
func (m *Manager) findChartURL(name, version, repoURL string, repos map[string]*repo.ChartRepository) (url, username, password string, insecureskiptlsverify, passcredentialsall bool, caFile, certFile, keyFile string, err error) {
734-
if strings.HasPrefix(repoURL, "git://") {
735+
if gitutil.IsGitURL(repoURL) {
735736
return repoURL, "", "", false, false, "", "", "", nil
736737
}
737738

pkg/gitutils/gitutils.go renamed to pkg/gitutil/gitutil.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,11 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
/*
18-
Package engine implements the Go text template engine as needed for Helm.
19-
20-
When Helm renders templates it does so with additional functions and different
21-
modes (e.g., strict, lint mode). This package handles the helm specific
22-
implementation.
23-
*/
24-
package gitutils
17+
package gitutil
2518

2619
import (
2720
"os"
21+
"strings"
2822

2923
"github.com/Masterminds/vcs"
3024
)
@@ -46,3 +40,8 @@ func HasGitReference(gitRepo, ref, repoName string) (bool, error) {
4640
defer os.RemoveAll(local)
4741
return repo.IsReference(ref), nil
4842
}
43+
44+
func IsGitURL(url string) bool {
45+
46+
return strings.HasPrefix(url, "git://")
47+
}

pkg/gitutil/gitutil_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright The Helm Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gitutil
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestIsGitUrl(t *testing.T) {
24+
// Test table: Given url, IsGitURL should return expect.
25+
tests := []struct {
26+
url string
27+
expect bool
28+
}{
29+
{"oci://example.com/example/chart", false},
30+
{"git://example.com/example/chart", true},
31+
{"git://https://example.com/example/chart", true},
32+
}
33+
34+
for _, test := range tests {
35+
if IsGitURL(test.url) != test.expect {
36+
t.Errorf("Expected %t for %s", test.expect, test.url)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)