Skip to content

Commit ebae2dc

Browse files
dmitshurrsc
authored andcommitted
go/vcs: reject update of VCS inside VCS
Manually apply same change as CL 68110 did for cmd/go/internal/get, but for golang.org/x/tools/go/vcs, to help keep them in sync. Updates golang/go#22125. Helps golang/go#11490. Change-Id: I255f7a494d9572389fc8dc8ce96891b6fcc214a0 Reviewed-on: https://go-review.googlesource.com/68352 Reviewed-by: Russ Cox <rsc@golang.org>
1 parent bbddea4 commit ebae2dc

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

go/vcs/vcs.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,28 @@ func FromDir(dir, srcRoot string) (vcs *Cmd, root string, err error) {
344344
return nil, "", fmt.Errorf("directory %q is outside source root %q", dir, srcRoot)
345345
}
346346

347+
var vcsRet *Cmd
348+
var rootRet string
349+
347350
origDir := dir
348351
for len(dir) > len(srcRoot) {
349352
for _, vcs := range vcsList {
350353
if _, err := os.Stat(filepath.Join(dir, "."+vcs.Cmd)); err == nil {
351-
return vcs, filepath.ToSlash(dir[len(srcRoot)+1:]), nil
354+
root := filepath.ToSlash(dir[len(srcRoot)+1:])
355+
// Record first VCS we find, but keep looking,
356+
// to detect mistakes like one kind of VCS inside another.
357+
if vcsRet == nil {
358+
vcsRet = vcs
359+
rootRet = root
360+
continue
361+
}
362+
// Allow .git inside .git, which can arise due to submodules.
363+
if vcsRet == vcs && vcs.Cmd == "git" {
364+
continue
365+
}
366+
// Otherwise, we have one VCS inside a different VCS.
367+
return nil, "", fmt.Errorf("directory %q uses %s, but parent %q uses %s",
368+
filepath.Join(srcRoot, rootRet), vcsRet.Cmd, filepath.Join(srcRoot, root), vcs.Cmd)
352369
}
353370
}
354371

@@ -361,6 +378,10 @@ func FromDir(dir, srcRoot string) (vcs *Cmd, root string, err error) {
361378
dir = ndir
362379
}
363380

381+
if vcsRet != nil {
382+
return vcsRet, rootRet, nil
383+
}
384+
364385
return nil, "", fmt.Errorf("directory %q is not using a known version control system", origDir)
365386
}
366387

0 commit comments

Comments
 (0)