...

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

Documentation: cmd/go/testdata/script

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

View as plain text