Skip to content

Commit 81922ad

Browse files
committed
cmd/go/internal/vcs: avoid treating git submodule directory as repository root
Before the change, FromDir treats a directory contains a .git entry as a git repository root, no matter whether the entry is a directory or a normal file. When git submodule is used, the directory of the submodule contains a .git file (it's a normal file, not a directory). In this case, the directory of this submodule should not be treated as the repository root.
1 parent 3ba3b48 commit 81922ad

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/cmd/go/internal/vcs/vcs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,12 @@ func FromDir(dir, srcRoot string, allowNesting bool) (repoDir string, vcsCmd *Cm
814814
origDir := dir
815815
for len(dir) > len(srcRoot) {
816816
for _, vcs := range vcsList {
817-
if _, err := statAny(dir, vcs.RootNames); err == nil {
817+
if fi, err := statAny(dir, vcs.RootNames); err == nil {
818+
// git submodule contains .git file, but its directory is not a
819+
// git repository root.
820+
if !fi.IsDir() && fi.Name() == ".git" {
821+
continue
822+
}
818823
// Record first VCS we find.
819824
// If allowNesting is false (as it is in GOPATH), keep looking for
820825
// repositories in parent directories and report an error if one is

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ go version -m $GOBIN/a$GOEXE
1414
! stdout vcs.revision
1515
rm $GOBIN/a$GOEXE
1616

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.
19+
cd ../gitsubmodule
20+
go install
21+
go version -m $GOBIN/gitsubmodule$GOEXE
22+
! stdout vcs.revision
23+
rm $GOBIN/gitsubmodule$GOEXE
24+
1725
# If there is a repository, but it can't be used for some reason,
1826
# there should be an error. It should hint about -buildvcs=false.
1927
# Also ensure that multiple errors are collected by "go list -e".
@@ -141,6 +149,16 @@ module example.com/b
141149
go 1.18
142150
-- repo/b/b.go --
143151
package b
152+
-- repo/gitsubmodule/.git --
153+
gitdir: ../.git/modules/gitsubmodule
154+
-- repo/gitsubmodule/go.mod --
155+
module example.com/gitsubmodule
156+
157+
go 1.18
158+
-- repo/gitsubmodule/main.go --
159+
package main
160+
161+
func main() {}
144162
-- outside/empty.txt --
145163
-- outside/c/go.mod --
146164
module example.com/c

0 commit comments

Comments
 (0)