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

     1  # This test checks that VCS information is stamped into Go binaries by default,
     2  # controlled with -buildvcs. This test focuses on Git. Other tests focus on
     3  # other VCS tools but may not cover common functionality.
     4  
     5  [!exec:git] skip
     6  [short] skip
     7  env GOBIN=$WORK/gopath/bin
     8  env oldpath=$PATH
     9  cd repo/a
    10  
    11  # If there's no local repository, there's no VCS info.
    12  go install
    13  go version -m $GOBIN/a$GOEXE
    14  ! stdout vcs.revision
    15  rm $GOBIN/a$GOEXE
    16  
    17  # If there is a repository, but it can't be used for some reason,
    18  # there should be an error. It should hint about -buildvcs=false.
    19  # Also ensure that multiple errors are collected by "go list -e".
    20  cd ..
    21  mkdir .git
    22  env PATH=$WORK${/}fakebin${:}$oldpath
    23  chmod 0755 $WORK/fakebin/git
    24  ! exec git help
    25  cd a
    26  ! go install
    27  stderr '^error obtaining VCS status: exit status 1\n\tUse -buildvcs=false to disable VCS stamping.$'
    28  go list -e -f '{{.ImportPath}}: {{.Error}}' ./...
    29  stdout -count=1 '^example\.com/a: error obtaining VCS status'
    30  stdout -count=1 '^example\.com/a/library: <nil>'
    31  stdout -count=1 '^example\.com/a/othermain: error obtaining VCS status'
    32  cd ..
    33  env PATH=$oldpath
    34  rm .git
    35  
    36  # If there is an empty repository in a parent directory, only "uncommitted" is tagged.
    37  exec git init
    38  exec git config user.email gopher@golang.org
    39  exec git config user.name 'J.R. Gopher'
    40  cd a
    41  go install
    42  go version -m $GOBIN/a$GOEXE
    43  stdout '^\tbuild\tvcs=git$'
    44  stdout '^\tbuild\tvcs.modified=true$'
    45  ! stdout vcs.revision
    46  ! stdout vcs.time
    47  rm $GOBIN/a$GOEXE
    48  
    49  # Revision and commit time are tagged for repositories with commits.
    50  exec git add -A
    51  exec git commit -m 'initial commit'
    52  go install
    53  go version -m $GOBIN/a$GOEXE
    54  stdout '^\tbuild\tvcs.revision='
    55  stdout '^\tbuild\tvcs.time='
    56  stdout '^\tbuild\tvcs.modified=false$'
    57  rm $GOBIN/a$GOEXE
    58  
    59  # Building with -buildvcs=false suppresses the info.
    60  go install -buildvcs=false
    61  go version -m $GOBIN/a$GOEXE
    62  ! stdout vcs.revision
    63  rm $GOBIN/a$GOEXE
    64  
    65  # An untracked file is shown as uncommitted, even if it isn't part of the build.
    66  cp ../../outside/empty.txt .
    67  go install
    68  go version -m $GOBIN/a$GOEXE
    69  stdout '^\tbuild\tvcs.modified=true$'
    70  rm empty.txt
    71  rm $GOBIN/a$GOEXE
    72  
    73  # An edited file is shown as uncommitted, even if it isn't part of the build.
    74  cp ../../outside/empty.txt ../README
    75  go install
    76  go version -m $GOBIN/a$GOEXE
    77  stdout '^\tbuild\tvcs.modified=true$'
    78  exec git checkout ../README
    79  rm $GOBIN/a$GOEXE
    80  
    81  # If the build doesn't include any packages from the repository,
    82  # there should be no VCS info.
    83  go install example.com/cmd/a@v1.0.0
    84  go version -m $GOBIN/a$GOEXE
    85  ! stdout vcs.revision
    86  rm $GOBIN/a$GOEXE
    87  
    88  go mod edit -require=example.com/c@v0.0.0
    89  go mod edit -replace=example.com/c@v0.0.0=../../outside/c
    90  go install example.com/c
    91  go version -m $GOBIN/c$GOEXE
    92  ! stdout vcs.revision
    93  rm $GOBIN/c$GOEXE
    94  exec git checkout go.mod
    95  
    96  # If the build depends on a package in the repository, but it's not in the
    97  # main module, there should be no VCS info.
    98  go mod edit -require=example.com/b@v0.0.0
    99  go mod edit -replace=example.com/b@v0.0.0=../b
   100  go mod edit -require=example.com/d@v0.0.0
   101  go mod edit -replace=example.com/d@v0.0.0=../../outside/d
   102  go install example.com/d
   103  go version -m $GOBIN/d$GOEXE
   104  ! stdout vcs.revision
   105  exec git checkout go.mod
   106  rm $GOBIN/d$GOEXE
   107  
   108  # If we're loading multiple main packages,
   109  # but they share the same VCS repository,
   110  # we only need to execute VCS status commands once.
   111  go list -x ./...
   112  stdout -count=3 '^example.com'
   113  stderr -count=1 '^git status'
   114  stderr -count=1 '^git -c log.showsignature=false show'
   115  
   116  -- $WORK/fakebin/git --
   117  #!/bin/sh
   118  exit 1
   119  -- $WORK/fakebin/git.bat --
   120  exit 1
   121  -- repo/README --
   122  Far out in the uncharted backwaters of the unfashionable end of the western
   123  spiral arm of the Galaxy lies a small, unregarded yellow sun.
   124  -- repo/a/go.mod --
   125  module example.com/a
   126  
   127  go 1.18
   128  -- repo/a/a.go --
   129  package main
   130  
   131  func main() {}
   132  -- repo/a/library/f.go --
   133  package library
   134  -- repo/a/othermain/f.go --
   135  package main
   136  
   137  func main() {}
   138  -- repo/b/go.mod --
   139  module example.com/b
   140  
   141  go 1.18
   142  -- repo/b/b.go --
   143  package b
   144  -- outside/empty.txt --
   145  -- outside/c/go.mod --
   146  module example.com/c
   147  
   148  go 1.18
   149  -- outside/c/main.go --
   150  package main
   151  
   152  func main() {}
   153  -- outside/d/go.mod --
   154  module example.com/d
   155  
   156  go 1.18
   157  
   158  require example.com/b v0.0.0
   159  -- outside/d/main.go --
   160  package main
   161  
   162  import _ "example.com/b"
   163  
   164  func main() {}
   165  

View as plain text