Skip to content

Commit b3afeaf

Browse files
authored
refactor: drop golang.org/x/tools/go/vcs dep (#5281)
we are only using one util method from golang.org/x/tools/go/vcs copy the method and adapt it to our usage
1 parent 129c8c4 commit b3afeaf

File tree

4 files changed

+57
-42
lines changed

4 files changed

+57
-42
lines changed

NOTICE.txt

-37
Original file line numberDiff line numberDiff line change
@@ -15600,43 +15600,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1560015600
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1560115601

1560215602

15603-
--------------------------------------------------------------------------------
15604-
Dependency : golang.org/x/tools/go/vcs
15605-
Version: v0.1.0-deprecated
15606-
Licence type (autodetected): BSD-3-Clause
15607-
--------------------------------------------------------------------------------
15608-
15609-
Contents of probable licence file $GOMODCACHE/golang.org/x/tools/go/vcs@v0.1.0-deprecated/LICENSE:
15610-
15611-
Copyright (c) 2009 The Go Authors. All rights reserved.
15612-
15613-
Redistribution and use in source and binary forms, with or without
15614-
modification, are permitted provided that the following conditions are
15615-
met:
15616-
15617-
* Redistributions of source code must retain the above copyright
15618-
notice, this list of conditions and the following disclaimer.
15619-
* Redistributions in binary form must reproduce the above
15620-
copyright notice, this list of conditions and the following disclaimer
15621-
in the documentation and/or other materials provided with the
15622-
distribution.
15623-
* Neither the name of Google Inc. nor the names of its
15624-
contributors may be used to endorse or promote products derived from
15625-
this software without specific prior written permission.
15626-
15627-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15628-
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15629-
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15630-
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
15631-
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
15632-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15633-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15634-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
15635-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15636-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15637-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15638-
15639-
1564015603
--------------------------------------------------------------------------------
1564115604
Dependency : google.golang.org/grpc
1564215605
Version: v1.65.0

dev-tools/mage/settings.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"gopkg.in/yaml.v3"
2424

2525
"github.com/magefile/mage/sh"
26-
"golang.org/x/tools/go/vcs" //nolint:staticcheck // this deprecation will be handled in https://github.com/elastic/elastic-agent/issues/4138
2726

2827
"github.com/elastic/elastic-agent/dev-tools/mage/gotool"
2928
v1 "github.com/elastic/elastic-agent/pkg/api/v1"
@@ -787,7 +786,7 @@ func getProjectRepoInfoUnderGopath() (*ProjectRepoInfo, error) {
787786
}
788787

789788
for _, srcDir := range srcDirs {
790-
_, root, err := vcs.FromDir(cwd, srcDir)
789+
root, err := fromDir(cwd, srcDir)
791790
if err != nil {
792791
// Try the next gopath.
793792
errs = append(errs, err.Error())
@@ -821,6 +820,62 @@ func getProjectRepoInfoUnderGopath() (*ProjectRepoInfo, error) {
821820
}, nil
822821
}
823822

823+
var vcsList = []string{
824+
"hg",
825+
"git",
826+
"svn",
827+
"bzr",
828+
}
829+
830+
func fromDir(dir, srcRoot string) (root string, err error) {
831+
// Clean and double-check that dir is in (a subdirectory of) srcRoot.
832+
dir = filepath.Clean(dir)
833+
srcRoot = filepath.Clean(srcRoot)
834+
if len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator {
835+
return "", fmt.Errorf("directory %q is outside source root %q", dir, srcRoot)
836+
}
837+
838+
var vcsRet string
839+
var rootRet string
840+
841+
origDir := dir
842+
for len(dir) > len(srcRoot) {
843+
for _, vcs := range vcsList {
844+
if _, err := os.Stat(filepath.Join(dir, "."+vcs)); err == nil {
845+
root := filepath.ToSlash(dir[len(srcRoot)+1:])
846+
// Record first VCS we find, but keep looking,
847+
// to detect mistakes like one kind of VCS inside another.
848+
if vcsRet == "" {
849+
vcsRet = vcs
850+
rootRet = root
851+
continue
852+
}
853+
// Allow .git inside .git, which can arise due to submodules.
854+
if vcsRet == vcs && vcs == "git" {
855+
continue
856+
}
857+
// Otherwise, we have one VCS inside a different VCS.
858+
return "", fmt.Errorf("directory %q uses %s, but parent %q uses %s",
859+
filepath.Join(srcRoot, rootRet), vcsRet, filepath.Join(srcRoot, root), vcs)
860+
}
861+
}
862+
863+
// Move to parent.
864+
ndir := filepath.Dir(dir)
865+
if len(ndir) >= len(dir) {
866+
// Shouldn't happen, but just in case, stop.
867+
break
868+
}
869+
dir = ndir
870+
}
871+
872+
if vcsRet != "" {
873+
return rootRet, nil
874+
}
875+
876+
return "", fmt.Errorf("directory %q is not using a known version control system", origDir)
877+
}
878+
824879
func extractCanonicalRootImportPath(rootImportPath string) string {
825880
// In order to be compatible with go modules, the root import
826881
// path of any module at major version v2 or higher must include

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ require (
7272
golang.org/x/text v0.16.0
7373
golang.org/x/time v0.5.0
7474
golang.org/x/tools v0.22.0
75-
golang.org/x/tools/go/vcs v0.1.0-deprecated
7675
google.golang.org/grpc v1.65.0
7776
google.golang.org/protobuf v1.34.2
7877
gopkg.in/ini.v1 v1.67.0

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -2277,8 +2277,6 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
22772277
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
22782278
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
22792279
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
2280-
golang.org/x/tools/go/vcs v0.1.0-deprecated h1:cOIJqWBl99H1dH5LWizPa+0ImeeJq3t3cJjaeOWUAL4=
2281-
golang.org/x/tools/go/vcs v0.1.0-deprecated/go.mod h1:zUrvATBAvEI9535oC0yWYsLsHIV4Z7g63sNPVMtuBy8=
22822280
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
22832281
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
22842282
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)