package version

import "regexp"

var semverTag = regexp.MustCompile(`^refs/tags/v(\d+\.\d+\.\d+)$`)

// Resolve returns the version string to use in artifact names.
// Precedence: override > matching semver tag > short SHA.
func Resolve(override, ref, sha string) string {
	if override != "" {
		return override
	}
	if m := semverTag.FindStringSubmatch(ref); m != nil {
		return m[1]
	}
	if len(sha) > 7 {
		return sha[:7]
	}
	return sha
}
