github.com/unicornultrafoundation/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/internal/build/env.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package build
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"regexp"
    24  	"strconv"
    25  	"strings"
    26  	"time"
    27  )
    28  
    29  var (
    30  	// These flags override values in build env.
    31  	GitCommitFlag   = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
    32  	GitBranchFlag   = flag.String("git-branch", "", `Overrides git branch being built`)
    33  	GitTagFlag      = flag.String("git-tag", "", `Overrides git tag being built`)
    34  	BuildnumFlag    = flag.String("buildnum", "", `Overrides CI build number`)
    35  	PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
    36  	CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
    37  )
    38  
    39  // Environment contains metadata provided by the build environment.
    40  type Environment struct {
    41  	Name                      string // name of the environment
    42  	Repo                      string // name of GitHub repo
    43  	Commit, Date, Branch, Tag string // Git info
    44  	Buildnum                  string
    45  	IsPullRequest             bool
    46  	IsCronJob                 bool
    47  }
    48  
    49  func (env Environment) String() string {
    50  	return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
    51  		env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
    52  }
    53  
    54  // Env returns metadata about the current CI environment, falling back to LocalEnv
    55  // if not running on CI.
    56  func Env() Environment {
    57  	switch {
    58  	case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
    59  		commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
    60  		if commit == "" {
    61  			commit = os.Getenv("TRAVIS_COMMIT")
    62  		}
    63  		return Environment{
    64  			Name:          "travis",
    65  			Repo:          os.Getenv("TRAVIS_REPO_SLUG"),
    66  			Commit:        commit,
    67  			Date:          getDate(commit),
    68  			Branch:        os.Getenv("TRAVIS_BRANCH"),
    69  			Tag:           os.Getenv("TRAVIS_TAG"),
    70  			Buildnum:      os.Getenv("TRAVIS_BUILD_NUMBER"),
    71  			IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
    72  			IsCronJob:     os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
    73  		}
    74  	case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
    75  		commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT")
    76  		if commit == "" {
    77  			commit = os.Getenv("APPVEYOR_REPO_COMMIT")
    78  		}
    79  		return Environment{
    80  			Name:          "appveyor",
    81  			Repo:          os.Getenv("APPVEYOR_REPO_NAME"),
    82  			Commit:        commit,
    83  			Date:          getDate(commit),
    84  			Branch:        os.Getenv("APPVEYOR_REPO_BRANCH"),
    85  			Tag:           os.Getenv("APPVEYOR_REPO_TAG_NAME"),
    86  			Buildnum:      os.Getenv("APPVEYOR_BUILD_NUMBER"),
    87  			IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
    88  			IsCronJob:     os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
    89  		}
    90  	default:
    91  		return LocalEnv()
    92  	}
    93  }
    94  
    95  // LocalEnv returns build environment metadata gathered from git.
    96  func LocalEnv() Environment {
    97  	env := applyEnvFlags(Environment{Name: "local", Repo: "Fantom-foundation/go-ethereum"})
    98  
    99  	head := readGitFile("HEAD")
   100  	if fields := strings.Fields(head); len(fields) == 2 {
   101  		head = fields[1]
   102  	} else {
   103  		// In this case we are in "detached head" state
   104  		// see: https://git-scm.com/docs/git-checkout#_detached_head
   105  		// Additional check required to verify, that file contains commit hash
   106  		commitRe, _ := regexp.Compile("^([0-9a-f]{40})$")
   107  		if commit := commitRe.FindString(head); commit != "" && env.Commit == "" {
   108  			env.Commit = commit
   109  		}
   110  		return env
   111  	}
   112  	if env.Commit == "" {
   113  		env.Commit = readGitFile(head)
   114  	}
   115  	env.Date = getDate(env.Commit)
   116  	if env.Branch == "" {
   117  		if head != "HEAD" {
   118  			env.Branch = strings.TrimPrefix(head, "refs/heads/")
   119  		}
   120  	}
   121  	if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
   122  		env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
   123  	}
   124  	return env
   125  }
   126  
   127  func firstLine(s string) string {
   128  	return strings.Split(s, "\n")[0]
   129  }
   130  
   131  func getDate(commit string) string {
   132  	if commit == "" {
   133  		return ""
   134  	}
   135  	out := RunGit("show", "-s", "--format=%ct", commit)
   136  	if out == "" {
   137  		return ""
   138  	}
   139  	date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
   140  	if err != nil {
   141  		panic(fmt.Sprintf("failed to parse git commit date: %v", err))
   142  	}
   143  	return time.Unix(date, 0).Format("20060102")
   144  }
   145  
   146  func applyEnvFlags(env Environment) Environment {
   147  	if !flag.Parsed() {
   148  		panic("you need to call flag.Parse before Env or LocalEnv")
   149  	}
   150  	if *GitCommitFlag != "" {
   151  		env.Commit = *GitCommitFlag
   152  	}
   153  	if *GitBranchFlag != "" {
   154  		env.Branch = *GitBranchFlag
   155  	}
   156  	if *GitTagFlag != "" {
   157  		env.Tag = *GitTagFlag
   158  	}
   159  	if *BuildnumFlag != "" {
   160  		env.Buildnum = *BuildnumFlag
   161  	}
   162  	if *PullRequestFlag {
   163  		env.IsPullRequest = true
   164  	}
   165  	if *CronJobFlag {
   166  		env.IsCronJob = true
   167  	}
   168  	return env
   169  }