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: fix invalid git url #359

Merged
merged 2 commits into from
Jun 5, 2024
Merged
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
24 changes: 17 additions & 7 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,32 @@ func NewRegistryOptionsFrom(rawUrlorOciRef string, settings *settings.Settings)
// NewGitOptionsFromUrl will parse the git options from the git url.
// https, http, git and ssh are supported.
func NewGitOptionsFromUrl(parsedUrl *url.URL) *GitOptions {
if parsedUrl.Scheme == "" {
return nil
if parsedUrl.Scheme == "" || parsedUrl.Scheme == constants.GitScheme {
// go-getter do not supports git scheme, so we need to convert it to https scheme.
parsedUrl.Scheme = constants.HttpsScheme
}

commit := parsedUrl.Query().Get(constants.GitCommit)
branch := parsedUrl.Query().Get(constants.GitBranch)
tag := parsedUrl.Query().Get(constants.Tag)

// clean the query in git url
parsedUrl.RawQuery = ""
url := parsedUrl.String()

return &GitOptions{
Url: parsedUrl.Host + parsedUrl.Path,
Branch: parsedUrl.Query().Get(constants.GitBranch),
Tag: parsedUrl.Query().Get(constants.Tag),
Commit: parsedUrl.Query().Get(constants.GitCommit),
Url: url,
Branch: branch,
Commit: commit,
Tag: tag,
}
}

// NewOciOptionsFromUrl will parse the oci options from the oci url.
// https, http, oci is supported.
func NewOciOptionsFromUrl(parsedUrl *url.URL) *OciOptions {
if parsedUrl.Scheme == "" {
return nil
parsedUrl.Scheme = constants.HttpsScheme
}
return &OciOptions{
Reg: parsedUrl.Host,
Expand Down
11 changes: 8 additions & 3 deletions pkg/opt/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,22 @@ func TestNewRegistryOptionsFromRef(t *testing.T) {
opts, err = NewRegistryOptionsFrom("ssh://github.com/kcl-lang/test1?tag=0.0.1", settings)
assert.Equal(t, err, nil)
assert.Equal(t, opts.Git.Tag, "0.0.1")
assert.Equal(t, opts.Git.Url, "github.com/kcl-lang/test1")
assert.Equal(t, opts.Git.Url, "ssh://github.com/kcl-lang/test1")

opts, err = NewRegistryOptionsFrom("http://github.com/kcl-lang/test1?commit=123456", settings)
assert.Equal(t, err, nil)
assert.Equal(t, opts.Git.Commit, "123456")
assert.Equal(t, opts.Git.Url, "github.com/kcl-lang/test1")
assert.Equal(t, opts.Git.Url, "http://github.com/kcl-lang/test1")

opts, err = NewRegistryOptionsFrom("https://github.com/kcl-lang/test1?branch=main", settings)
assert.Equal(t, err, nil)
assert.Equal(t, opts.Git.Branch, "main")
assert.Equal(t, opts.Git.Url, "github.com/kcl-lang/test1")
assert.Equal(t, opts.Git.Url, "https://github.com/kcl-lang/test1")

opts, err = NewRegistryOptionsFrom("git://github.com/kcl-lang/test1?branch=main", settings)
assert.Equal(t, err, nil)
assert.Equal(t, opts.Git.Branch, "main")
assert.Equal(t, opts.Git.Url, "https://github.com/kcl-lang/test1")
}

func TestNewOciOptions(t *testing.T) {
Expand Down
Loading