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-e7537ba8fd6d\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-e7537ba8fd6d\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 uncommitted 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-b0226f18a7ae\s+'
    83  rm example$GOEXE
    84  
    85  # Use pseudo+dirty when uncommitted 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-b0226f18a7ae\+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-2ebc76937b49\s+'
   109  rm example$GOEXE
   110  
   111  # Create +incompatible module
   112  exec git checkout v1.0.4
   113  exec git rm go.mod
   114  exec git commit -m 'commit 6'
   115  exec git tag v2.0.0
   116  exec git checkout HEAD^ go.mod
   117  # And make the tree +dirty
   118  mv README4 README5
   119  go build
   120  go version -m example$GOEXE
   121  stdout '\s+mod\s+example\s+v2.0.0\+incompatible.dirty\s+'
   122  rm example$GOEXE
   123  
   124  # Make sure v2 works as expected.
   125  exec git checkout v1.0.4
   126  go mod edit -module example/v2
   127  exec git add .
   128  exec git commit -m 'commit 7'
   129  exec git tag v2.1.1
   130  go build
   131  go version -m example$GOEXE
   132  stdout '\s+mod\s+example/v2\s+v2.1.1\s+'
   133  rm example$GOEXE
   134  
   135  # v2+dirty
   136  mv README5 README6
   137  go build
   138  go version -m example$GOEXE
   139  stdout '\s+mod\s+example/v2\s+v2.1.1\+dirty\s+'
   140  rm example$GOEXE
   141  
   142  # v2+pseudo
   143  exec git add .
   144  exec git commit -m 'commit 8'
   145  go build
   146  go version -m example$GOEXE
   147  stdout '\s+mod\s+example/v2\s+v2.1.2-0.20220719150704-0ebeb94ecde2\s+'
   148  rm example$GOEXE
   149  
   150  # v2+pseudo+dirty
   151  mv README6 README7
   152  go build
   153  go version -m example$GOEXE
   154  stdout '\s+mod\s+example/v2\s+v2.1.2-0.20220719150704-0ebeb94ecde2\+dirty\s+'
   155  rm example$GOEXE
   156  
   157  # modules in subdirectories should be stamped with the correct tag
   158  exec git add .
   159  cd subdir
   160  exec git commit -m 'commit 9'
   161  go build
   162  go version -m subdir$GOEXE
   163  # missing tag creates a pseudo version with v2.0.0
   164  stdout '\s+mod\s+example/subdir/v2\s+v2.0.0-20220719150704-fbef6799938f\s+'
   165  rm subdir$GOEXE
   166  # tag with subdir
   167  exec git tag subdir/v2.1.0
   168  go build
   169  go version -m subdir$GOEXE
   170  stdout '\s+mod\s+example/subdir/v2\s+v2.1.0\s+'
   171  # v2+dirty
   172  mv ../README7 README8
   173  go build
   174  go version -m subdir$GOEXE
   175  stdout '\s+mod\s+example/subdir/v2\s+v2.1.0\+dirty\s+'
   176  rm subdir$GOEXE
   177  
   178  # modules in a subdirectory without a go.mod in the root should result in (devel)
   179  rm ../go.mod
   180  go build
   181  go version -m subdir$GOEXE
   182  stdout '\s+mod\s+example/subdir/v2\s+\(devel\)\s+'
   183  rm subdir$GOEXE
   184  
   185  -- $WORK/repo/go.mod --
   186  module example
   187  
   188  go 1.18
   189  -- $WORK/repo/main.go --
   190  package main
   191  
   192  func main() {
   193  }
   194  -- $WORK/copy/README --
   195  hello
   196  
   197  -- $WORK/repo/subdir/go.mod --
   198  module example/subdir/v2
   199  
   200  go 1.18
   201  
   202  -- $WORK/repo/subdir/main.go --
   203  package main
   204  
   205  func main() {
   206  }
   207  
   208  -- $WORK/home/gopher/.gitconfig --
   209  [user]
   210      name = Go Gopher
   211      email = gopher@golang.org
   212  

View as plain text