Skip to content
Open
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
20 changes: 16 additions & 4 deletions internal/git/infer_module_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package git

import (
"fmt"
"strings"

"github.com/Masterminds/semver"

"github.com/sourcegraph/scip-go/internal/command"
)
Expand All @@ -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")
Expand Down