From c8eac0e9a0229987f6b74194e30613443e45442a Mon Sep 17 00:00:00 2001 From: Max Ignatenko Date: Sun, 5 Oct 2025 22:31:35 +0100 Subject: [PATCH] fix: return only one tag when `git tag` returns multiple This makes version inference work correctly on github.com/kubernetes/api, returning just "v0.34.1" instead of "kubernetes-1.34.1\nv0.34.1". --- internal/git/infer_module_version.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/git/infer_module_version.go b/internal/git/infer_module_version.go index 4776ff2..a21a585 100644 --- a/internal/git/infer_module_version.go +++ b/internal/git/infer_module_version.go @@ -2,6 +2,9 @@ package git import ( "fmt" + "strings" + + "github.com/Masterminds/semver" "github.com/sourcegraph/scip-go/internal/command" ) @@ -10,12 +13,21 @@ import ( // directory. This will be either the work tree commit's tag, or it will be the // short revhash of the HEAD commit. func InferModuleVersion(dir string) (string, error) { - version, err := command.Run(dir, "git", "tag", "-l", "--points-at", "HEAD") + tags, err := command.Run(dir, "git", "tag", "-l", "--points-at", "HEAD") if err != nil { - return "", fmt.Errorf("failed to tags for current commit: %v\n%s", err, version) + return "", fmt.Errorf("failed to tags for current commit: %v\n%s", err, tags) + } + lines := strings.Split(tags, "\n") + for _, tag := range lines { + _, err := semver.NewVersion(tag) + if err == nil { + // Correctly parsed as a version, so just return it. + return tag, nil + } } - if version != "" { - return version, nil + if len(lines) > 0 { + // None of the tags look like a version, but return one of them anyway. + return lines[0], nil } commit, err := command.Run(dir, "git", "rev-parse", "HEAD")