@@ -23,7 +23,6 @@ import (
23
23
"gopkg.in/yaml.v3"
24
24
25
25
"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
27
26
28
27
"github.com/elastic/elastic-agent/dev-tools/mage/gotool"
29
28
v1 "github.com/elastic/elastic-agent/pkg/api/v1"
@@ -787,7 +786,7 @@ func getProjectRepoInfoUnderGopath() (*ProjectRepoInfo, error) {
787
786
}
788
787
789
788
for _ , srcDir := range srcDirs {
790
- _ , root , err := vcs . FromDir (cwd , srcDir )
789
+ root , err := fromDir (cwd , srcDir )
791
790
if err != nil {
792
791
// Try the next gopath.
793
792
errs = append (errs , err .Error ())
@@ -821,6 +820,62 @@ func getProjectRepoInfoUnderGopath() (*ProjectRepoInfo, error) {
821
820
}, nil
822
821
}
823
822
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
+
824
879
func extractCanonicalRootImportPath (rootImportPath string ) string {
825
880
// In order to be compatible with go modules, the root import
826
881
// path of any module at major version v2 or higher must include
0 commit comments