github.com/searKing/golang/go@v1.2.117/version/version_git.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package version
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  )
    11  
    12  var (
    13  	// GitTag
    14  	// NOTE: The $Format strings are replaced during 'git archive' thanks to the
    15  	// companion .gitattributes file containing 'export-subst' in this same
    16  	// directory.  See also https://git-scm.com/docs/gitattributes
    17  	GitTag    = "v0.0.0-master+$Format:%h$" // git describe --long --tags --dirty --tags --always
    18  	BuildTime = "1970-01-01T00:00:00Z"      // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
    19  	GitHash   = "$Format:%H$"               // sha1 from git, output of $(git rev-parse HEAD)
    20  
    21  	ServiceName        = filepath.Base(os.Args[0])
    22  	ServiceDisplayName = filepath.Base(os.Args[0])
    23  	ServiceDescription = ""
    24  	ServiceId          = ""
    25  )
    26  
    27  // Example
    28  // git_tag=$(shell git describe --long --tags --dirty --tags --always)
    29  // git_commit=$(shell git rev-parse HEAD)
    30  // git_build_time=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
    31  // go build -gcflags=all="-N -l" \
    32  // -ldflags "-s -X 'github.com/searKing/golang/go/version.GitTag=${git_tag}' \
    33  // -X 'github.com/searKing/golang/go/version.BuildTime=${git_build_time}' \
    34  // -X 'github.com/searKing/golang/go/version.GitHash=${git_commit}'"
    35  
    36  // Get returns GitTag as version
    37  func Get() Version {
    38  	return Version{
    39  		RawVersion: GitTag,
    40  		BuildTime:  BuildTime,
    41  		GitHash:    GitHash,
    42  	}
    43  }