2023-02-19 22:28:17 -03:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
2025-04-05 23:09:27 +01:00
|
|
|
_ "embed"
|
2023-02-19 22:28:17 -03:00
|
|
|
"runtime/debug"
|
2025-04-05 23:09:27 +01:00
|
|
|
"strings"
|
2023-02-19 22:28:17 -03:00
|
|
|
)
|
|
|
|
|
2024-05-20 22:48:05 +02:00
|
|
|
var (
|
2025-04-05 23:09:27 +01:00
|
|
|
//go:embed version.txt
|
|
|
|
version string
|
|
|
|
commit string
|
|
|
|
dirty bool
|
2024-05-20 22:48:05 +02:00
|
|
|
)
|
2023-02-19 22:28:17 -03:00
|
|
|
|
2024-05-20 22:48:05 +02:00
|
|
|
func init() {
|
2025-04-05 23:09:27 +01:00
|
|
|
version = strings.TrimSpace(version)
|
|
|
|
// Attempt to get build info from the Go runtime. We only use this if not
|
|
|
|
// built from a tagged version.
|
|
|
|
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version == "(devel)" {
|
|
|
|
commit = getCommit(info)
|
|
|
|
dirty = getDirty(info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDirty(info *debug.BuildInfo) bool {
|
|
|
|
for _, setting := range info.Settings {
|
|
|
|
if setting.Key == "vcs.modified" {
|
|
|
|
return setting.Value == "true"
|
2024-07-05 19:53:36 +02:00
|
|
|
}
|
2025-04-05 23:09:27 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCommit(info *debug.BuildInfo) string {
|
|
|
|
for _, setting := range info.Settings {
|
|
|
|
if setting.Key == "vcs.revision" {
|
|
|
|
return setting.Value[:7]
|
2024-09-24 05:45:41 +12:00
|
|
|
}
|
2023-02-19 22:28:17 -03:00
|
|
|
}
|
2025-04-05 23:09:27 +01:00
|
|
|
return ""
|
2024-05-20 22:48:05 +02:00
|
|
|
}
|
2023-02-19 22:28:17 -03:00
|
|
|
|
2025-04-05 23:09:27 +01:00
|
|
|
// GetVersion returns the version of Task. By default, this is retrieved from
|
|
|
|
// the embedded version.txt file which is kept up-to-date by our release script.
|
|
|
|
// However, it can also be overridden at build time using:
|
|
|
|
// -ldflags="-X 'github.com/go-task/task/v3/internal/version.version=vX.X.X'".
|
2024-05-20 22:48:05 +02:00
|
|
|
func GetVersion() string {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2025-04-05 23:09:27 +01:00
|
|
|
// GetVersionWithBuildInfo is the same as [GetVersion], but it also includes
|
|
|
|
// the commit hash and dirty status if available. This will only work when built
|
|
|
|
// within inside of a Git checkout.
|
|
|
|
func GetVersionWithBuildInfo() string {
|
|
|
|
var buildInfo string
|
|
|
|
if commit != "" {
|
|
|
|
buildInfo += commit
|
|
|
|
}
|
|
|
|
if dirty {
|
|
|
|
buildInfo += "-dirty"
|
|
|
|
}
|
|
|
|
if buildInfo != "" {
|
|
|
|
return version + "-" + buildInfo
|
|
|
|
}
|
|
|
|
return version
|
2023-02-19 22:28:17 -03:00
|
|
|
}
|