diff --git a/pkg/client/client.go b/pkg/client/client.go index 20c285c1..aef48855 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -827,12 +827,13 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* return nil, err } dep.FullName = dep.GenDepFullName() - // If the dependency is from git commit, the version is the commit id. - // If the dependency is from git tag, the version is the tag. - dep.Version, err = dep.Source.Git.GetValidGitReference() + + modFile, err := c.LoadModFile(localPath) if err != nil { return nil, err } + dep.Version = modFile.Pkg.Version + dep.Source.Git.Version = modFile.Pkg.Version } if dep.Source.Oci != nil { diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 83102f12..afd189ce 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -16,6 +16,7 @@ import ( "github.com/dominikbraun/graph" "github.com/otiai10/copy" "github.com/stretchr/testify/assert" + "golang.org/x/mod/module" "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kpm/pkg/env" "kcl-lang.io/kpm/pkg/git" @@ -181,28 +182,32 @@ func TestDependencyGraph(t *testing.T) { adjMap, err := depGraph.AdjacencyMap() assert.Equal(t, err, nil) + m := func(Path, Version string) module.Version { + return module.Version{Path, Version} + } + edgeProp := graph.EdgeProperties{ Attributes: map[string]string{}, Weight: 0, Data: nil, } assert.Equal(t, adjMap, - map[string]map[string]graph.Edge[string]{ - "dependency_graph@0.0.1": { - "teleport@0.1.0": {Source: "dependency_graph@0.0.1", Target: "teleport@0.1.0", Properties: edgeProp}, - "rabbitmq@0.0.1": {Source: "dependency_graph@0.0.1", Target: "rabbitmq@0.0.1", Properties: edgeProp}, - "agent@0.1.0": {Source: "dependency_graph@0.0.1", Target: "agent@0.1.0", Properties: edgeProp}, + map[module.Version]map[module.Version]graph.Edge[module.Version]{ + m("dependency_graph", "0.0.1"): { + m("teleport", "0.1.0"): {Source: m("dependency_graph", "0.0.1"), Target: m("teleport", "0.1.0"), Properties: edgeProp}, + m("rabbitmq", "0.0.1"): {Source: m("dependency_graph", "0.0.1"), Target: m("rabbitmq", "0.0.1"), Properties: edgeProp}, + m("agent", "0.1.0"): {Source: m("dependency_graph", "0.0.1"), Target: m("agent", "0.1.0"), Properties: edgeProp}, }, - "teleport@0.1.0": { - "k8s@1.28": {Source: "teleport@0.1.0", Target: "k8s@1.28", Properties: edgeProp}, + m("teleport", "0.1.0"): { + m("k8s", "1.28"): {Source: m("teleport", "0.1.0"), Target: m("k8s", "1.28"), Properties: edgeProp}, }, - "rabbitmq@0.0.1": { - "k8s@1.28": {Source: "rabbitmq@0.0.1", Target: "k8s@1.28", Properties: edgeProp}, + m("rabbitmq", "0.0.1"): { + m("k8s", "1.28"): {Source: m("rabbitmq", "0.0.1"), Target: m("k8s", "1.28"), Properties: edgeProp}, }, - "agent@0.1.0": { - "k8s@1.28": {Source: "agent@0.1.0", Target: "k8s@1.28", Properties: edgeProp}, + m("agent", "0.1.0"): { + m("k8s", "1.28"): {Source: m("agent", "0.1.0"), Target: m("k8s", "1.28"), Properties: edgeProp}, }, - "k8s@1.28": {}, + m("k8s", "1.28"): {}, }, ) } diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect index 475d36f8..112fdd08 100644 --- a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect +++ b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect @@ -4,4 +4,4 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" } +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db", version = "0.1.0" } diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect index ccc62bba..f8a58f5b 100644 --- a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect +++ b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect @@ -2,7 +2,8 @@ [dependencies.catalog] name = "catalog" full_name = "catalog_a29e3db" - version = "a29e3db" + version = "0.1.0" sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks=" url = "https://github.com/KusionStack/catalog.git" commit = "a29e3db" + version = "0.1.0" diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index c92a4275..baab5471 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -298,10 +298,11 @@ func (oci *Oci) FromString(ociUrl string) (*Oci, error) { // Git is the package source from git registry. type Git struct { - Url string `toml:"url,omitempty"` - Branch string `toml:"branch,omitempty"` - Commit string `toml:"commit,omitempty"` - Tag string `toml:"git_tag,omitempty"` + Url string `toml:"url,omitempty"` + Branch string `toml:"branch,omitempty"` + Commit string `toml:"commit,omitempty"` + Tag string `toml:"git_tag,omitempty"` + Version string `toml:"version,omitempty"` } // GetValidGitReference will get the valid git reference from git source. diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 6bc8a34e..8a9a3204 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -118,6 +118,7 @@ func (source *Source) MarshalTOML() string { const GIT_URL_PATTERN = "git = \"%s\"" const TAG_PATTERN = "tag = \"%s\"" const GIT_COMMIT_PATTERN = "commit = \"%s\"" +const VERSION_PATTERN = "version = \"%s\"" const SEPARATOR = ", " func (git *Git) MarshalTOML() string { @@ -133,6 +134,10 @@ func (git *Git) MarshalTOML() string { sb.WriteString(SEPARATOR) sb.WriteString(fmt.Sprintf(GIT_COMMIT_PATTERN, git.Commit)) } + if len(git.Version) != 0 { + sb.WriteString(SEPARATOR) + sb.WriteString(fmt.Sprintf(VERSION_PATTERN, git.Version)) + } return sb.String() }