From 439dea1b198a9edd384e3b9763a9c5672ce62b67 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Wed, 28 Dec 2022 09:38:09 -0600 Subject: 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. --- init_dir.go | 2 ++ main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/init_dir.go b/init_dir.go index 5ade692..e895f00 100644 --- a/init_dir.go +++ b/init_dir.go @@ -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) diff --git a/main.go b/main.go index d1bf0c3..dad2c73 100644 --- a/main.go +++ b/main.go @@ -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") -- cgit v1.2.3