Use existing build information for version if not embedded with build script

Go1.12 introduced BuildInfo which embeds build information. It does
not embed build date to facilitate reproducable builds by default.
If build information is embedded from build script, use the information
provided by the Go build system.
This commit is contained in:
Daniel Theophanes 2022-12-28 09:38:09 -06:00 committed by Jakob Unterwurzacher
parent ff32e99791
commit 439dea1b19
2 changed files with 50 additions and 0 deletions

View File

@ -55,6 +55,8 @@ func isDir(dir string) error {
// In reverse mode, we create .gocryptfs.reverse.conf and the directory does
// not need to be empty.
func initDir(args *argContainer) {
initGIT()
var err error
if args.reverse {
_, err = os.Stat(args.config)

48
main.go
View File

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"strings"
@ -36,6 +37,51 @@ var BuildDate = "0000-00-00"
// raceDetector is set to true by race.go if we are compiled with "go build -race"
var raceDetector bool
func initGIT() {
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}
if strings.HasPrefix(GitVersion, `[`) {
GitVersion = bi.Main.Version
var gitDate string
var gitDirty bool
for _, item := range bi.Settings {
switch item.Key {
case "vcs.revision":
GitVersion = item.Value
case "vcs.time":
gitDate = item.Value
gitDate = strings.Map(func(r rune) rune {
switch {
case r >= '0' && r <= '9':
return r
default:
return -1
}
}, gitDate)
case "vcs.modified":
gitDirty, _ = strconv.ParseBool(item.Value)
}
}
if len(gitDate) > 0 {
GitVersion = gitDate + "-" + GitVersion
}
if gitDirty {
GitVersion = GitVersion + " (dirty)"
}
}
const fuseModule = `github.com/hanwen/go-fuse/v2`
if strings.HasPrefix(GitVersionFuse, `[`) {
for _, item := range bi.Deps {
if item.Path == fuseModule {
GitVersionFuse = item.Version
}
}
}
}
// loadConfig loads the config file `args.config` and decrypts the masterkey,
// or gets via the `-masterkey` or `-zerokey` command line options, if specified.
func loadConfig(args *argContainer) (masterkey []byte, cf *configfile.ConfFile, err error) {
@ -140,6 +186,8 @@ func changePassword(args *argContainer) {
// printVersion prints a version string like this:
// gocryptfs v1.7-32-gcf99cfd; go-fuse v1.0.0-174-g22a9cb9; 2019-05-12 go1.12 linux/amd64
func printVersion() {
initGIT()
var tagsSlice []string
if stupidgcm.BuiltWithoutOpenssl {
tagsSlice = append(tagsSlice, "without_openssl")