Skip to content

Commit 2900f1b

Browse files
committed
refactor: consolidate git:// url normalization
Addresses helm#11258 (comment) Signed-off-by: Dominykas Blyžė <[email protected]>
1 parent 04f638f commit 2900f1b

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

internal/resolver/resolver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string
110110
continue
111111
}
112112

113-
if gitutil.IsGitURL(d.Repository) {
113+
if gitutil.IsGitRepository(d.Repository) {
114114

115-
found, err := hasGitReference(strings.TrimPrefix(d.Repository, "git://"), d.Version, d.Name)
115+
found, err := hasGitReference(gitutil.RepositoryURLToGitURL(d.Repository), d.Version, d.Name)
116116

117117
if err != nil {
118118
return nil, err

pkg/downloader/chart_downloader.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
9797

9898
scheme := ""
9999

100-
if gitutil.IsGitURL(ref) {
100+
if gitutil.IsGitRepository(ref) {
101101
scheme = "git"
102102
} else {
103103
scheme = u.Scheme
@@ -114,7 +114,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
114114
}
115115

116116
name := filepath.Base(u.Path)
117-
if u.Scheme == registry.OCIScheme || u.Scheme == "git" {
117+
if u.Scheme == registry.OCIScheme {
118118
idx := strings.LastIndexByte(name, ':')
119119
name = fmt.Sprintf("%s-%s.tgz", name[:idx], name[idx+1:])
120120
}
@@ -208,8 +208,8 @@ func (c *ChartDownloader) getOciURI(ref, version string, u *url.URL) (*url.URL,
208208
// - If version is empty, this will return the URL for the latest version
209209
// - If no version can be found, an error is returned
210210
func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, error) {
211-
if gitutil.IsGitURL(ref) {
212-
gitURL := strings.TrimPrefix(ref, "git://")
211+
if gitutil.IsGitRepository(ref) {
212+
gitURL := gitutil.RepositoryURLToGitURL(ref)
213213
u, err := giturls.Parse(gitURL)
214214
if err != nil {
215215
return nil, errors.Errorf("invalid git URL format: %s", gitURL)

pkg/downloader/manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error {
354354
getter.WithTagName(version))
355355
}
356356

357-
if gitutil.IsGitURL(churl) {
357+
if gitutil.IsGitRepository(churl) {
358358
version = dep.Version
359359

360360
dl.Options = append(dl.Options, getter.WithTagName(version))
@@ -490,7 +490,7 @@ Loop:
490490
}
491491

492492
// If repo is from git url, continue
493-
if gitutil.IsGitURL(dd.Repository) {
493+
if gitutil.IsGitRepository(dd.Repository) {
494494
continue
495495
}
496496

@@ -614,7 +614,7 @@ func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string,
614614
// if dep chart is from a git url, assume it is valid for now.
615615
// if the repo does not exist then it will later error when we try to fetch branches and tags.
616616
// we could check for the repo existence here, but trying to avoid another git request.
617-
if gitutil.IsGitURL(dd.Repository) {
617+
if gitutil.IsGitRepository(dd.Repository) {
618618
if m.Debug {
619619
fmt.Fprintf(m.Out, "Repository from git url: %s\n", strings.TrimPrefix(dd.Repository, "git:"))
620620
}
@@ -732,7 +732,7 @@ func (m *Manager) parallelRepoUpdate(repos []*repo.Entry) error {
732732
//
733733
// If it finds a URL that is "relative", it will prepend the repoURL.
734734
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) {
735-
if gitutil.IsGitURL(repoURL) {
735+
if gitutil.IsGitRepository(repoURL) {
736736
return repoURL, "", "", false, false, "", "", "", nil
737737
}
738738

pkg/getter/gitgetter.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ package getter
1717

1818
import (
1919
"bytes"
20-
"strings"
21-
2220
"fmt"
21+
"helm.sh/helm/v3/pkg/gitutil"
2322
"os"
2423
"path/filepath"
2524

@@ -61,7 +60,7 @@ func (g *GitGetter) Get(href string, options ...Option) (*bytes.Buffer, error) {
6160
}
6261

6362
func (g *GitGetter) get(href string) (*bytes.Buffer, error) {
64-
gitURL := strings.TrimPrefix(href, "git://")
63+
gitURL := gitutil.RepositoryURLToGitURL(href)
6564
version := g.opts.version
6665
chartName := g.opts.chartName
6766
if version == "" {

pkg/gitutil/gitutil.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/Masterminds/vcs"
2424
)
2525

26+
// HasGitReference returns true if a git repository contains a specified ref (branch/tag)
2627
func HasGitReference(gitRepo, ref, repoName string) (bool, error) {
2728
local, err := os.MkdirTemp("", repoName)
2829
if err != nil {
@@ -41,7 +42,12 @@ func HasGitReference(gitRepo, ref, repoName string) (bool, error) {
4142
return repo.IsReference(ref), nil
4243
}
4344

44-
func IsGitURL(url string) bool {
45-
45+
// IsGitRepository determines whether a URL is to be treated as a git repository URL
46+
func IsGitRepository(url string) bool {
4647
return strings.HasPrefix(url, "git://")
4748
}
49+
50+
// RepositoryURLToGitURL converts a repository URL into a URL that `git clone` could consume
51+
func RepositoryURLToGitURL(url string) string {
52+
return strings.TrimPrefix(url, "git://")
53+
}

pkg/gitutil/gitutil_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestIsGitUrl(t *testing.T) {
24-
// Test table: Given url, IsGitURL should return expect.
24+
// Test table: Given url, IsGitRepository should return expect.
2525
tests := []struct {
2626
url string
2727
expect bool
@@ -32,7 +32,7 @@ func TestIsGitUrl(t *testing.T) {
3232
}
3333

3434
for _, test := range tests {
35-
if IsGitURL(test.url) != test.expect {
35+
if IsGitRepository(test.url) != test.expect {
3636
t.Errorf("Expected %t for %s", test.expect, test.url)
3737
}
3838
}

0 commit comments

Comments
 (0)