Skip to content

Commit

Permalink
Merge pull request #589 from kcl-lang/fix-issue-587
Browse files Browse the repository at this point in the history
fix: fix download failure for git repo with protocol 'git://'
  • Loading branch information
He1pa authored Jan 16, 2025
2 parents cf75dd2 + 37b9cc0 commit 7bfe09e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
35 changes: 35 additions & 0 deletions pkg/client/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
}
Original file line number Diff line number Diff line change
@@ -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" }
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
23 changes: 16 additions & 7 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
)

Expand Down Expand Up @@ -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),
)...,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
)...,
Expand Down Expand Up @@ -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),
)...,
)
Expand All @@ -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),
)

Expand Down
15 changes: 15 additions & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ""
Expand Down

0 comments on commit 7bfe09e

Please sign in to comment.