Repository: go
Issue: cmd/go: binary does not contain VCS information when compiled in git worktree dir
Labels: NeedsInvestigation,GoCommand

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.20rc3 linux/amd64
</pre>

### Does this issue reproduce with the latest release?

Yes.

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/rogpeppe/.cache/go-build"
GOENV="/home/rogpeppe/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/rogpeppe/src/go/pkg/mod"
GONOPROXY="github.com/influxdata/idpe"
GONOSUMDB="github.com/influxdata/idpe"
GOOS="linux"
GOPATH="/home/rogpeppe/src/go"
GOPRIVATE="github.com/influxdata/idpe"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/rogpeppe/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/rogpeppe/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20rc3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/rogpeppe/src/cuelabs/cue2/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3009011258=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

### What did you do?

Run `testscript` on the following file:

```
# set up git repository and build first binary
cd d1
exec git init
exec git add .
exec git commit -m 'initial commit'
exec go build -o $TMPDIR/prog1 .

# set up worktree and build second binary
exec git worktree add -b main1 ../d2 HEAD
cd ../d2
exec go build -o $TMPDIR/prog2 .

# ensure first binary has VCS info
exec go version -m $TMPDIR/prog1
stdout 'vcs.revision='

# ensure second binary has VCS info
exec go version -m $TMPDIR/prog2
stdout 'vcs.revision='

-- d1/go.mod --
module example

-- d1/main.go --
package main

func main() {}
```


### What did you expect to see?

A successful test.

### What did you see instead?
The binary built in a git worktree directory does not contain VCS information.
I suspect the logic doesn't understand that `.git` can be a file was well as a directory.

```
# set up git repository and build first binary (0.096s)
# set up worktree and build second binary (0.088s)
# ensure first binary has VCS info (0.002s)
# ensure second binary has VCS info (0.002s)
> exec go version -m $TMPDIR/prog2
[stdout]
$WORK/.tmp/prog2: go1.20rc3
	path	example
	mod	example	(devel)	
	build	-buildmode=exe
	build	-compiler=gc
	build	CGO_ENABLED=1
	build	CGO_CFLAGS=
	build	CGO_CPPFLAGS=
	build	CGO_CXXFLAGS=
	build	CGO_LDFLAGS=
	build	GOARCH=amd64
	build	GOOS=linux
	build	GOAMD64=v1
> stdout 'vcs.revision='
FAIL: /tmp/testscript498691614/x.txtar/script.txtar:19: no match for `vcs.revision=` found in stdout
error running /tmp/x.txtar in /tmp/testscript498691614/x.txtar
```