Skip to content

Commit

Permalink
Merge pull request #588 from kcl-lang/fix-issue-587-for-098
Browse files Browse the repository at this point in the history
fix: fix failure when add git repo as dependency by protocol 'git://'
  • Loading branch information
He1pa authored Jan 16, 2025
2 parents 3aed38f + 0b31e8e commit 1ea8c63
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
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

0 comments on commit 1ea8c63

Please sign in to comment.