Skip to content

Commit

Permalink
fix: add LatestVersion for gitdownloader
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Nov 4, 2024
1 parent 3f395ec commit c0a41a1
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 5 deletions.
9 changes: 7 additions & 2 deletions pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"kcl-lang.io/kpm/pkg/utils"
)

func testAddWithModSpec(t *testing.T) {
func TestAddWithModSpec(t *testing.T) {
tests := []struct {
name string
pkgSubPath string
Expand Down Expand Up @@ -44,9 +44,14 @@ func testAddWithModSpec(t *testing.T) {
},
{
name: "TestAddOciWithNoTag",
pkgSubPath: "no_tag",
pkgSubPath: "no_oci_ref",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld",
},
{
name: "TestAddGitWithNoTag",
pkgSubPath: "no_git_ref",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git",
},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestDownloadGitWithPackage", testDownloadGitWithPackage)
test.RunTestWithGlobalLock(t, "TestModandLockFilesWithGitPackageDownload", testModandLockFilesWithGitPackageDownload)
test.RunTestWithGlobalLock(t, "TestDependencyGraph", testDependencyGraph)
test.RunTestWithGlobalLock(t, "testAddWithModSpec", testAddWithModSpec)
test.RunTestWithGlobalLock(t, "testAddWithModSpec", TestAddWithModSpec)
}

// TestDownloadOci test download from oci registry.
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mod_spec/no_git_ref/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
flask_manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", 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 = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "ade147b"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mod_spec/no_git_ref/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Empty file.
36 changes: 35 additions & 1 deletion pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,41 @@ type GitDownloader struct{}
func (d *GitDownloader) LatestVersion(opts *DownloadOptions) (string, error) {
// TODO:supports fetch the latest commit from the git bare repo,
// after totally transfer to the new storage.
return "main", nil
// refer to cargo: https://github.com/rust-lang/cargo/blob/3dedb85a25604bdbbb8d3bf4b03162961a4facd0/crates/cargo-util-schemas/src/core/source_kind.rs#L133
var err error
tmp, err := os.MkdirTemp("", "")
if err != nil {
return "", err
}
tmp = filepath.Join(tmp, constants.GitScheme)

defer func() {
err = os.RemoveAll(tmp)
}()

repo, err := git.CloneWithOpts(
git.WithCommit(opts.Source.Commit),
git.WithBranch(opts.Source.Branch),
git.WithTag(opts.Source.Git.Tag),
git.WithRepoURL(opts.Source.Git.Url),
git.WithLocalPath(tmp),
)

if err != nil {
return "", err
}

ref, err := repo.Head()
if err != nil {
return "", err
}

commit, err := repo.CommitObject(ref.Hash())
if err != nil {
return "", err
}

return commit.Hash.String()[:7], nil
}

// OciDownloader is the downloader for the OCI source.
Expand Down
2 changes: 1 addition & 1 deletion pkg/visitor/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error {
s.Oci.Tag = latest
}
if s.Git != nil {
s.Git.Branch = latest
s.Git.Commit = latest
}
}
var modPath string
Expand Down

0 comments on commit c0a41a1

Please sign in to comment.