Text file src/cmd/go/testdata/script/build_version_stamping_git.txt

     1  # Test that the version of a binary is stamped using git tag information.
     2  # See https://go.dev/issue/50603
     3  
     4  [short] skip 'constructs a local git repo'
     5  [!git] skip
     6  
     7  # Redirect git to a test-specific .gitconfig.
     8  # GIT_CONFIG_GLOBAL suffices for git 2.32.0 and newer.
     9  # For older git versions we also set $HOME.
    10  env GIT_CONFIG_GLOBAL=$WORK${/}home${/}gopher${/}.gitconfig
    11  env HOME=$WORK${/}home${/}gopher
    12  exec git config --global --show-origin user.name
    13  stdout 'Go Gopher'
    14  
    15  cd $WORK/repo
    16  # Use devel when git information is missing.
    17  go build
    18  go version -m example$GOEXE
    19  stdout '\s+mod\s+example\s+\(devel\)'
    20  rm example$GOEXE
    21  
    22  env GIT_AUTHOR_NAME='Go Gopher'
    23  env GIT_AUTHOR_EMAIL='gopher@golang.org'
    24  env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
    25  env GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL
    26  
    27  exec git init
    28  env GIT_COMMITTER_DATE=2022-07-19T11:07:00-04:00
    29  env GIT_AUTHOR_DATE=2022-07-19T11:07:00-04:00
    30  exec git add .
    31  exec git commit -m 'initial commit'
    32  exec git branch -m main
    33  
    34  # Use a 0.0.0 pseudo-version when no tags are present.
    35  go build
    36  go version -m example$GOEXE
    37  stdout '\s+mod\s+example\s+v0.0.0-20220719150700-b52f952448d2\s+'
    38  rm example$GOEXE
    39  
    40  # Use a 0.0.0 pseudo-version if the current tag is not a valid semantic version.
    41  exec git tag 1.0.1
    42  go build
    43  go version -m example$GOEXE
    44  stdout '\s+mod\s+example\s+v0.0.0-20220719150700-b52f952448d2\s+'
    45  rm example$GOEXE
    46  
    47  # Use the current tag which has a valid semantic version to stamp the version.
    48  exec git tag v1.0.1
    49  go build
    50  go version -m example$GOEXE
    51  stdout '\s+mod\s+example\s+v1.0.1\s+'
    52  rm example$GOEXE
    53  
    54  # Use tag+dirty when there are uncomitted changes present.
    55  cp $WORK/copy/README $WORK/repo/README
    56  go build
    57  go version -m example$GOEXE
    58  stdout '\s+mod\s+example\s+v1.0.1\+dirty\s+'
    59  rm example$GOEXE
    60  
    61  env GIT_COMMITTER_DATE=2022-07-19T11:07:01-04:00
    62  env GIT_AUTHOR_DATE=2022-07-19T11:07:01-04:00
    63  exec git add .
    64  exec git commit -m 'commit 2'
    65  
    66  # Use the updated tag to stamp the version.
    67  exec git tag v1.0.2
    68  go build
    69  go version -m example$GOEXE
    70  stdout '\s+mod\s+example\s+v1.0.2\s+'
    71  rm example$GOEXE
    72  
    73  env GIT_COMMITTER_DATE=2022-07-19T11:07:02-04:00
    74  env GIT_AUTHOR_DATE=2022-07-19T11:07:02-04:00
    75  mv README README2
    76  exec git add .
    77  exec git commit -m 'commit 3'
    78  
    79  # Use a pseudo-version when current commit doesn't match a tagged version.
    80  go build
    81  go version -m example$GOEXE
    82  stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-deaeab06f7fe\s+'
    83  rm example$GOEXE
    84  
    85  # Use pseudo+dirty when uncomitted changes are present.
    86  mv README2 README3
    87  go build
    88  go version -m example$GOEXE
    89  stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-deaeab06f7fe\+dirty\s+'
    90  rm example$GOEXE
    91  
    92  # Make sure we always use the previously tagged version to generate the pseudo-version at a untagged revision.
    93  env GIT_COMMITTER_DATE=2022-07-19T11:07:03-04:00
    94  env GIT_AUTHOR_DATE=2022-07-19T11:07:03-04:00
    95  exec git add .
    96  exec git commit -m 'commit 4'
    97  
    98  mv README3 README4
    99  env GIT_COMMITTER_DATE=2022-07-19T11:07:04-04:00
   100  env GIT_AUTHOR_DATE=2022-07-19T11:07:04-04:00
   101  exec git add .
   102  exec git commit -m 'commit 5'
   103  exec git tag v1.0.4
   104  # Jump back to commit 4 which is untagged.
   105  exec git checkout ':/commit 4'
   106  go build
   107  go version -m example$GOEXE
   108  stdout '\s+mod\s+example\s+v1.0.3-0.20220719150703-2e239bf29c13\s+'
   109  rm example$GOEXE
   110  
   111  -- $WORK/repo/go.mod --
   112  module example
   113  
   114  go 1.18
   115  -- $WORK/repo/main.go --
   116  package main
   117  
   118  func main() {
   119  }
   120  -- $WORK/copy/README --
   121  hello
   122  
   123  -- $WORK/home/gopher/.gitconfig --
   124  [user]
   125      name = Go Gopher
   126      email = gopher@golang.org
   127  

View as plain text