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 download failure for git repo with protocol 'git://' #589

Merged
merged 4 commits 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
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
Loading