github.com/decred/politeia@v1.4.0/util/version/version_buildinfo.go (about)

     1  // Copyright (c) 2021-2022 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.18
     6  // +build go1.18
     7  
     8  package version
     9  
    10  import "runtime/debug"
    11  
    12  // vcsCommitID attempts to return the version control system short commit hash
    13  // that was used to build the binary.  It currently only detects git commits.
    14  func vcsCommitID() string {
    15  	bi, ok := debug.ReadBuildInfo()
    16  	if !ok {
    17  		return ""
    18  	}
    19  	var vcs, revision string
    20  	for _, bs := range bi.Settings {
    21  		switch bs.Key {
    22  		case "vcs":
    23  			vcs = bs.Value
    24  		case "vcs.revision":
    25  			revision = bs.Value
    26  		}
    27  	}
    28  	if vcs == "" {
    29  		return ""
    30  	}
    31  	if vcs == "git" && len(revision) > 9 {
    32  		revision = revision[:9]
    33  	}
    34  	return revision
    35  }