...

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

Documentation: cmd/go/testdata/script

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

View as plain text