Debian Codegraph @

internal/version/version.go

raw symbols SCIP
1 // Copyright (c) 2020, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package version
5
6 import (
7 "encoding/json"
8 "fmt"
9 "os"
10 "runtime"
11 "runtime/debug"
12 "time"
13
14 "golang.org/x/mod/module"
15 )
16
17 // Note that this is not a main package, so a "var version" will not work with
18 // our go-cross script which uses -ldflags=main.version=xxx.
19
20 const ourModulePath = "mvdan.cc/gofumpt"
21
22 const fallbackVersion = "(devel)" // to match the default from runtime/debug
23
24 func findModule(info *debug.BuildInfo, modulePath string) *debug.Module {
25 if info.Main.Path == modulePath {
26 return &info.Main
27 }
28 for _, dep := range info.Deps {
29 if dep.Path == modulePath {
30 return dep
31 }
32 }
33 return nil
34 }
35
36 func gofumptVersion() string {
37 info, ok := debug.ReadBuildInfo()
38 if !ok {
39 return fallbackVersion // no build info available
40 }
41 // Note that gofumpt may be used as a library via the format package,
42 // so we cannot assume it is the main module in the build.
43 mod := findModule(info, ourModulePath)
44 if mod == nil {
45 return fallbackVersion // not found?
46 }
47 if mod.Replace != nil {
48 mod = mod.Replace
49 }
50
51 // If we found a meaningful version, we are done.
52 // If gofumpt is not the main module, stop as well,
53 // as VCS info is only for the main module.
54 if mod.Version != "(devel)" || mod != &info.Main {
55 return mod.Version
56 }
57
58 // Fall back to trying to use VCS information.
59 // Until https://github.com/golang/go/issues/50603 is implemented,
60 // manually construct something like a pseudo-version.
61 // TODO: remove when this code is dead, hopefully in Go 1.20.
62
63 // For the tests, as we don't want the VCS information to change over time.
64 if v := os.Getenv("GARBLE_TEST_BUILDSETTINGS"); v != "" {
65 var extra []debug.BuildSetting
66 if err := json.Unmarshal([]byte(v), &extra); err != nil {
67 panic(err)
68 }
69 info.Settings = append(info.Settings, extra...)
70 }
71
72 var vcsTime time.Time
73 var vcsRevision string
74 for _, setting := range info.Settings {
75 switch setting.Key {
76 case "vcs.time":
77 // If the format is invalid, we'll print a zero timestamp.
78 vcsTime, _ = time.Parse(time.RFC3339Nano, setting.Value)
79 case "vcs.revision":
80 vcsRevision = setting.Value
81 if len(vcsRevision) > 12 {
82 vcsRevision = vcsRevision[:12]
83 }
84 }
85 }
86 if vcsRevision != "" {
87 return module.PseudoVersion("", "", vcsTime, vcsRevision)
88 }
89 return fallbackVersion
90 }
91
92 func goVersion() string {
93 // For the tests, as we don't want the Go version to change over time.
94 if testVersion := os.Getenv("GO_VERSION_TEST"); testVersion != "" {
95 return testVersion
96 }
97 return runtime.Version()
98 }
99
100 func String(injected string) string {
101 if injected != "" {
102 return fmt.Sprintf("%s (%s)", injected, goVersion())
103 }
104 return fmt.Sprintf("%s (%s)", gofumptVersion(), goVersion())
105 }