diff --git a/pkg/client/issues_test.go b/pkg/client/issues_test.go index 946e7085..cf672811 100644 --- a/pkg/client/issues_test.go +++ b/pkg/client/issues_test.go @@ -589,3 +589,38 @@ func TestKclIssue1788(t *testing.T) { RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_run_only_file_not_generate_mod", TestFunc: test_run_only_file_not_generate_mod}}) } + +func TestKpmIssue587(t *testing.T) { + testPath := "github.com/kcl-lang/kpm/issues/587" + + test_download_with_git_dep := func(t *testing.T, kpmcli *KpmClient) { + rootPath := getTestDir("issues") + kfilePath := filepath.Join(rootPath, testPath) + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + res, err := kpmcli.Run( + WithRunSource( + &downloader.Source{ + Local: &downloader.Local{ + Path: kfilePath, + }, + }, + ), + ) + + if err != nil { + t.Fatal(err) + } + + modFilePath := filepath.Join(testPath, "kcl.mod") + modLockFilePath := filepath.Join(testPath, "kcl.mod.lock") + + assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!") + assert.Equal(t, buf.String(), "cloning 'git://github.com/kcl-lang/flask-demo-kcl-manifests.git' with tag 'v0.1.0'\n") + assert.Equal(t, utils.DirExists(modFilePath), false) + assert.Equal(t, utils.DirExists(modLockFilePath), false) + } + + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_download_with_git_dep", TestFunc: test_download_with_git_dep}}) +} diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/kcl.mod b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/kcl.mod new file mode 100644 index 00000000..7034453e --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "issue587" +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", version = "0.0.1" } diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/kcl.mod.lock b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/kcl.mod.lock new file mode 100644 index 00000000..4ecd3536 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/kcl.mod.lock @@ -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" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/main.k b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/587/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/downloader/downloader.go b/pkg/downloader/downloader.go index b5ff073d..a24c81b7 100644 --- a/pkg/downloader/downloader.go +++ b/pkg/downloader/downloader.go @@ -148,6 +148,10 @@ func (d *GitDownloader) LatestVersion(opts *DownloadOptions) (string, error) { if opts.Offline { return "", errors.New("offline mode is enabled, the latest version is not supported") } + gitUrl, err := opts.Source.Git.GetCanonicalizedUrl() + if err != nil { + return "", err + } // TODOļ¼šsupports fetch the latest commit from the git bare repo, // after totally transfer to the new storage. // refer to cargo: https://github.com/rust-lang/cargo/blob/3dedb85a25604bdbbb8d3bf4b03162961a4facd0/crates/cargo-util-schemas/src/core/source_kind.rs#L133 @@ -168,7 +172,7 @@ func (d *GitDownloader) LatestVersion(opts *DownloadOptions) (string, error) { git.WithCommit(opts.Source.Commit), git.WithBranch(opts.Source.Branch), git.WithTag(opts.Source.Git.Tag), - git.WithRepoURL(opts.Source.Git.Url), + git.WithRepoURL(gitUrl), git.WithLocalPath(tmp), ) @@ -199,7 +203,7 @@ func (d *GitDownloader) LatestVersion(opts *DownloadOptions) (string, error) { repo, err = git.CloneWithOpts( append( cloneOpts, - git.WithRepoURL(opts.Source.Git.Url), + git.WithRepoURL(gitUrl), git.WithLocalPath(cacheFullPath), git.WithBare(true), )..., @@ -543,6 +547,11 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error { if gitSource == nil { return errors.New("git source is nil") } + // get the canonicalized git url + gitUrl, err := gitSource.GetCanonicalizedUrl() + if err != nil { + return err + } cloneOpts := []git.CloneOption{ git.WithCommit(gitSource.Commit), git.WithBranch(gitSource.Branch), @@ -603,7 +612,7 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error { _, err := git.CloneWithOpts( append( cloneOpts, - git.WithRepoURL(gitSource.Url), + git.WithRepoURL(gitUrl), git.WithLocalPath(cacheFullPath), git.WithBare(true), )..., @@ -632,10 +641,10 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error { opts.LogWriter, ) // If the cache is disabled, clone the repository from the remote git repository. - _, err := git.CloneWithOpts( + _, err = git.CloneWithOpts( append( cloneOpts, - git.WithRepoURL(gitSource.Url), + git.WithRepoURL(gitUrl), git.WithLocalPath(opts.LocalPath), )..., ) @@ -654,11 +663,11 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error { return errors.New("git source is nil") } - _, err := git.CloneWithOpts( + _, 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), ) diff --git a/pkg/downloader/source.go b/pkg/downloader/source.go index b97312c9..be5827f6 100644 --- a/pkg/downloader/source.go +++ b/pkg/downloader/source.go @@ -74,6 +74,21 @@ type Git struct { Package string `toml:"package,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 +} + // If the git source has no reference, return true. func (g *Git) NoRef() bool { return g.Version == "" && g.Tag == "" && g.Branch == "" && g.Commit == ""