_scripts: add option to disable invoking git during build (#2798)

This is useful to be able to build using Delve's build scripts when
working from within a directory that has been extracted from a source tarball,
or wherever we wouldn't have git history.

This can be invoked as:

```
  go run _scripts/make.go build --no-git
```
This commit is contained in:
Herby Gillot
2021-12-13 13:24:41 -05:00
committed by GitHub
parent c0fa4e32d7
commit d514b4c6e9

View File

@ -24,6 +24,7 @@ var TestSet, TestRegex, TestBackend, TestBuildMode string
var Tags *[]string var Tags *[]string
var Architecture string var Architecture string
var OS string var OS string
var DisableGit bool
func NewMakeCommands() *cobra.Command { func NewMakeCommands() *cobra.Command {
RootCommand := &cobra.Command{ RootCommand := &cobra.Command{
@ -68,6 +69,7 @@ func NewMakeCommands() *cobra.Command {
}, },
} }
Tags = buildCmd.PersistentFlags().StringArray("tags", []string{}, "Build tags") Tags = buildCmd.PersistentFlags().StringArray("tags", []string{}, "Build tags")
buildCmd.PersistentFlags().BoolVarP(&DisableGit, "no-git", "G", false, "Do not use git")
buildCmd.PersistentFlags().StringVar(&Architecture, "GOARCH", "", "Architecture to build for") buildCmd.PersistentFlags().StringVar(&Architecture, "GOARCH", "", "Architecture to build for")
buildCmd.PersistentFlags().StringVar(&OS, "GOOS", "", "OS to build for") buildCmd.PersistentFlags().StringVar(&OS, "GOOS", "", "OS to build for")
RootCommand.AddCommand(buildCmd) RootCommand.AddCommand(buildCmd)
@ -291,11 +293,11 @@ func prepareMacnative() string {
} }
func buildFlags() []string { func buildFlags() []string {
buildSHA, err := exec.Command("git", "rev-parse", "HEAD").CombinedOutput() buildSHA, err := getBuildSHA()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(fmt.Errorf("error getting build SHA via git: %w", err))
} }
ldFlags := "-X main.Build=" + strings.TrimSpace(string(buildSHA)) ldFlags := "-X main.Build=" + buildSHA
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
ldFlags = "-s " + ldFlags ldFlags = "-s " + ldFlags
} }
@ -496,6 +498,22 @@ func allPackages() []string {
return r return r
} }
// getBuildSHA will invoke git to return the current SHA of the commit at HEAD.
// If invoking git has been disabled, it will return an empty string instead.
func getBuildSHA() (string, error) {
if DisableGit {
return "", nil
}
buildSHA, err := exec.Command("git", "rev-parse", "HEAD").CombinedOutput()
if err != nil {
return "", err
}
shaStr := strings.TrimSpace(string(buildSHA))
return shaStr, nil
}
func main() { func main() {
allPackages() // checks that vendor directory is synced as a side effect allPackages() // checks that vendor directory is synced as a side effect
NewMakeCommands().Execute() NewMakeCommands().Execute()