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 failure when add git repo as dependency by protocol 'git://' #588

Merged
merged 1 commit into from
Jan 16, 2025
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
47 changes: 47 additions & 0 deletions pkg/client/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package client

import (
"bytes"
"os"
"testing"

"gotest.tools/v3/assert"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/utils"
)

func TestFixAddGitDep(t *testing.T) {
modPath := getTestDir("test_add_git_dep")
kpmcli, err := NewKpmClient()
if err != nil {
t.Fatal(err)
}

tmpKpmHome, err := os.MkdirTemp("", "kpm_home")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpKpmHome)

kpmcli.SetHomePath(tmpKpmHome)

var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)

res, err := kpmcli.Run(
WithRunSource(
&downloader.Source{
Local: &downloader.Local{
Path: modPath,
},
},
),
)

if err != nil {
t.Fatal(err)
}

assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), "name: flask_manifestsreplicas: 1labels: app: flask_manifests")
assert.Equal(t, buf.String(), "cloning 'git://github.com/kcl-lang/flask-demo-kcl-manifests.git' with tag 'v0.1.0'\n")
}
8 changes: 8 additions & 0 deletions pkg/client/test_data/test_add_git_dep/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_add_git_dep"
edition = "v0.11.0"
version = "0.0.1"

[dependencies]
flask_manifests = { git = "git://github.com/kcl-lang/flask-demo-kcl-manifests.git", tag = "v0.1.0" }

7 changes: 7 additions & 0 deletions pkg/client/test_data/test_add_git_dep/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.flask_manifests]
name = "flask_manifests"
full_name = "flask_manifests_0.0.1"
version = "0.0.1"
url = "git://github.com/kcl-lang/flask-demo-kcl-manifests.git"
git_tag = "v0.1.0"
5 changes: 5 additions & 0 deletions pkg/client/test_data/test_add_git_dep/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import flask_manifests.app

app.App{
name: "flask_manifests",
}
10 changes: 8 additions & 2 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,17 @@ func (d *GitDownloader) Download(opts DownloadOptions) error {
return errors.New("git source is nil")
}

_, err := git.CloneWithOpts(
// get the canonicalized git url
gitUrl, err := gitSource.GetCanonicalizedUrl()
if err != nil {
return err
}

_, err = git.CloneWithOpts(
git.WithCommit(gitSource.Commit),
git.WithBranch(gitSource.Branch),
git.WithTag(gitSource.Tag),
git.WithRepoURL(gitSource.Url),
git.WithRepoURL(gitUrl),
git.WithLocalPath(opts.LocalPath),
)

Expand Down
15 changes: 15 additions & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ type Git struct {
Version string `toml:"version,omitempty"`
}

// Transform the git url to the canonicalized url.
func (git *Git) GetCanonicalizedUrl() (string, error) {
url, err := url.Parse(git.Url)
if err != nil {
return "", err
}

// If the scheme is git, change it to https
if url.Scheme == constants.GitScheme {
url.Scheme = constants.HttpsScheme
}

return url.String(), nil
}

type Registry struct {
*Oci `toml:"-"`
Name string `toml:"-"`
Expand Down
Loading