1
2
3
4
5 package vcs
6
7 import (
8 "bytes"
9 "errors"
10 "fmt"
11 "internal/godebug"
12 "internal/lazyregexp"
13 "internal/singleflight"
14 "log"
15 urlpkg "net/url"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "strconv"
20 "strings"
21 "sync"
22 "time"
23
24 "cmd/go/internal/base"
25 "cmd/go/internal/cfg"
26 "cmd/go/internal/search"
27 "cmd/go/internal/str"
28 "cmd/go/internal/web"
29 "cmd/internal/pathcache"
30 "cmd/internal/telemetry/counter"
31
32 "golang.org/x/mod/module"
33 )
34
35
36
37 type Cmd struct {
38 Name string
39 Cmd string
40 Env []string
41 Roots []isVCSRoot
42
43 Scheme []string
44 PingCmd string
45
46 Status func(v *Cmd, rootDir string) (Status, error)
47 }
48
49
50 type Status struct {
51 Revision string
52 CommitTime time.Time
53 Uncommitted bool
54 }
55
56 var (
57
58
59
60
61
62 VCSTestRepoURL string
63
64
65 VCSTestHosts []string
66
67
68
69 VCSTestIsLocalHost func(*urlpkg.URL) bool
70 )
71
72 var defaultSecureScheme = map[string]bool{
73 "https": true,
74 "git+ssh": true,
75 "bzr+ssh": true,
76 "svn+ssh": true,
77 "ssh": true,
78 }
79
80 func (v *Cmd) IsSecure(repo string) bool {
81 u, err := urlpkg.Parse(repo)
82 if err != nil {
83
84 return false
85 }
86 if VCSTestRepoURL != "" && web.IsLocalHost(u) {
87
88
89
90 return true
91 }
92 return v.isSecureScheme(u.Scheme)
93 }
94
95 func (v *Cmd) isSecureScheme(scheme string) bool {
96 switch v.Cmd {
97 case "git":
98
99
100
101 if allow := os.Getenv("GIT_ALLOW_PROTOCOL"); allow != "" {
102 for s := range strings.SplitSeq(allow, ":") {
103 if s == scheme {
104 return true
105 }
106 }
107 return false
108 }
109 }
110 return defaultSecureScheme[scheme]
111 }
112
113
114
115 type tagCmd struct {
116 cmd string
117 pattern string
118 }
119
120
121 var vcsList = []*Cmd{
122 vcsHg,
123 vcsGit,
124 vcsSvn,
125 vcsBzr,
126 vcsFossil,
127 }
128
129
130
131 var vcsMod = &Cmd{Name: "mod"}
132
133
134
135 func vcsByCmd(cmd string) *Cmd {
136 for _, vcs := range vcsList {
137 if vcs.Cmd == cmd {
138 return vcs
139 }
140 }
141 return nil
142 }
143
144
145 var vcsHg = &Cmd{
146 Name: "Mercurial",
147 Cmd: "hg",
148
149
150
151 Env: []string{"HGPLAIN=+strictflags"},
152 Roots: []isVCSRoot{
153 vcsDirRoot(".hg"),
154 },
155
156 Scheme: []string{"https", "http", "ssh"},
157 PingCmd: "identify -- {scheme}://{repo}",
158 Status: hgStatus,
159 }
160
161 func hgStatus(vcsHg *Cmd, rootDir string) (Status, error) {
162
163 out, err := vcsHg.runOutputVerboseOnly(rootDir, `log -r. -T {node}:{date|hgdate}`)
164 if err != nil {
165 return Status{}, err
166 }
167
168 var rev string
169 var commitTime time.Time
170 if len(out) > 0 {
171
172 if i := bytes.IndexByte(out, ' '); i > 0 {
173 out = out[:i]
174 }
175 rev, commitTime, err = parseRevTime(out)
176 if err != nil {
177 return Status{}, err
178 }
179 }
180
181
182 out, err = vcsHg.runOutputVerboseOnly(rootDir, "status -S")
183 if err != nil {
184 return Status{}, err
185 }
186 uncommitted := len(out) > 0
187
188 return Status{
189 Revision: rev,
190 CommitTime: commitTime,
191 Uncommitted: uncommitted,
192 }, nil
193 }
194
195
196 func parseRevTime(out []byte) (string, time.Time, error) {
197 buf := string(bytes.TrimSpace(out))
198
199 i := strings.IndexByte(buf, ':')
200 if i < 1 {
201 return "", time.Time{}, errors.New("unrecognized VCS tool output")
202 }
203 rev := buf[:i]
204
205 secs, err := strconv.ParseInt(buf[i+1:], 10, 64)
206 if err != nil {
207 return "", time.Time{}, fmt.Errorf("unrecognized VCS tool output: %v", err)
208 }
209
210 return rev, time.Unix(secs, 0), nil
211 }
212
213
214 var vcsGit = &Cmd{
215 Name: "Git",
216 Cmd: "git",
217 Roots: []isVCSRoot{
218 vcsGitRoot{},
219 },
220
221 Scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
222
223
224
225
226
227 PingCmd: "ls-remote {scheme}://{repo}",
228
229 Status: gitStatus,
230 }
231
232 func gitStatus(vcsGit *Cmd, rootDir string) (Status, error) {
233 out, err := vcsGit.runOutputVerboseOnly(rootDir, "status --porcelain")
234 if err != nil {
235 return Status{}, err
236 }
237 uncommitted := len(out) > 0
238
239
240
241
242 var rev string
243 var commitTime time.Time
244 out, err = vcsGit.runOutputVerboseOnly(rootDir, "-c log.showsignature=false log -1 --format=%H:%ct")
245 if err != nil && !uncommitted {
246 return Status{}, err
247 } else if err == nil {
248 rev, commitTime, err = parseRevTime(out)
249 if err != nil {
250 return Status{}, err
251 }
252 }
253
254 return Status{
255 Revision: rev,
256 CommitTime: commitTime,
257 Uncommitted: uncommitted,
258 }, nil
259 }
260
261
262 var vcsBzr = &Cmd{
263 Name: "Bazaar",
264 Cmd: "bzr",
265 Roots: []isVCSRoot{
266 vcsDirRoot(".bzr"),
267 },
268
269 Scheme: []string{"https", "http", "bzr", "bzr+ssh"},
270 PingCmd: "info -- {scheme}://{repo}",
271 Status: bzrStatus,
272 }
273
274 func bzrStatus(vcsBzr *Cmd, rootDir string) (Status, error) {
275 outb, err := vcsBzr.runOutputVerboseOnly(rootDir, "version-info")
276 if err != nil {
277 return Status{}, err
278 }
279 out := string(outb)
280
281
282
283
284
285
286 var rev string
287 var commitTime time.Time
288
289 for line := range strings.SplitSeq(out, "\n") {
290 i := strings.IndexByte(line, ':')
291 if i < 0 {
292 continue
293 }
294 key := line[:i]
295 value := strings.TrimSpace(line[i+1:])
296
297 switch key {
298 case "revision-id":
299 rev = value
300 case "date":
301 var err error
302 commitTime, err = time.Parse("2006-01-02 15:04:05 -0700", value)
303 if err != nil {
304 return Status{}, errors.New("unable to parse output of bzr version-info")
305 }
306 }
307 }
308
309 outb, err = vcsBzr.runOutputVerboseOnly(rootDir, "status")
310 if err != nil {
311 return Status{}, err
312 }
313
314
315 if bytes.HasPrefix(outb, []byte("working tree is out of date")) {
316 i := bytes.IndexByte(outb, '\n')
317 if i < 0 {
318 i = len(outb)
319 }
320 outb = outb[:i]
321 }
322 uncommitted := len(outb) > 0
323
324 return Status{
325 Revision: rev,
326 CommitTime: commitTime,
327 Uncommitted: uncommitted,
328 }, nil
329 }
330
331
332 var vcsSvn = &Cmd{
333 Name: "Subversion",
334 Cmd: "svn",
335 Roots: []isVCSRoot{
336 vcsDirRoot(".svn"),
337 },
338
339
340
341
342 Scheme: []string{"https", "http", "svn", "svn+ssh"},
343 PingCmd: "info -- {scheme}://{repo}",
344 Status: svnStatus,
345 }
346
347 func svnStatus(vcsSvn *Cmd, rootDir string) (Status, error) {
348 out, err := vcsSvn.runOutputVerboseOnly(rootDir, "info --show-item last-changed-revision")
349 if err != nil {
350 return Status{}, err
351 }
352 rev := strings.TrimSpace(string(out))
353
354 out, err = vcsSvn.runOutputVerboseOnly(rootDir, "info --show-item last-changed-date")
355 if err != nil {
356 return Status{}, err
357 }
358 commitTime, err := time.Parse(time.RFC3339, strings.TrimSpace(string(out)))
359 if err != nil {
360 return Status{}, fmt.Errorf("unable to parse output of svn info: %v", err)
361 }
362
363 out, err = vcsSvn.runOutputVerboseOnly(rootDir, "status")
364 if err != nil {
365 return Status{}, err
366 }
367 uncommitted := len(out) > 0
368
369 return Status{
370 Revision: rev,
371 CommitTime: commitTime,
372 Uncommitted: uncommitted,
373 }, nil
374 }
375
376
377
378 const fossilRepoName = ".fossil"
379
380
381 var vcsFossil = &Cmd{
382 Name: "Fossil",
383 Cmd: "fossil",
384 Roots: []isVCSRoot{
385 vcsFileRoot(".fslckout"),
386 vcsFileRoot("_FOSSIL_"),
387 },
388
389 Scheme: []string{"https", "http"},
390 Status: fossilStatus,
391 }
392
393 var errFossilInfo = errors.New("unable to parse output of fossil info")
394
395 func fossilStatus(vcsFossil *Cmd, rootDir string) (Status, error) {
396 outb, err := vcsFossil.runOutputVerboseOnly(rootDir, "info")
397 if err != nil {
398 return Status{}, err
399 }
400 out := string(outb)
401
402
403
404
405
406
407
408
409 const prefix = "\ncheckout:"
410 const suffix = " UTC"
411 i := strings.Index(out, prefix)
412 if i < 0 {
413 return Status{}, errFossilInfo
414 }
415 checkout := out[i+len(prefix):]
416 i = strings.Index(checkout, suffix)
417 if i < 0 {
418 return Status{}, errFossilInfo
419 }
420 checkout = strings.TrimSpace(checkout[:i])
421
422 i = strings.IndexByte(checkout, ' ')
423 if i < 0 {
424 return Status{}, errFossilInfo
425 }
426 rev := checkout[:i]
427
428 commitTime, err := time.ParseInLocation(time.DateTime, checkout[i+1:], time.UTC)
429 if err != nil {
430 return Status{}, fmt.Errorf("%v: %v", errFossilInfo, err)
431 }
432
433
434 outb, err = vcsFossil.runOutputVerboseOnly(rootDir, "changes --differ")
435 if err != nil {
436 return Status{}, err
437 }
438 uncommitted := len(outb) > 0
439
440 return Status{
441 Revision: rev,
442 CommitTime: commitTime,
443 Uncommitted: uncommitted,
444 }, nil
445 }
446
447 func (v *Cmd) String() string {
448 return v.Name
449 }
450
451
452
453
454
455
456
457
458 func (v *Cmd) run(dir string, cmd string, keyval ...string) error {
459 _, err := v.run1(dir, cmd, keyval, true)
460 return err
461 }
462
463
464 func (v *Cmd) runVerboseOnly(dir string, cmd string, keyval ...string) error {
465 _, err := v.run1(dir, cmd, keyval, false)
466 return err
467 }
468
469
470 func (v *Cmd) runOutput(dir string, cmd string, keyval ...string) ([]byte, error) {
471 return v.run1(dir, cmd, keyval, true)
472 }
473
474
475
476 func (v *Cmd) runOutputVerboseOnly(dir string, cmd string, keyval ...string) ([]byte, error) {
477 return v.run1(dir, cmd, keyval, false)
478 }
479
480
481 func (v *Cmd) run1(dir string, cmdline string, keyval []string, verbose bool) ([]byte, error) {
482 m := make(map[string]string)
483 for i := 0; i < len(keyval); i += 2 {
484 m[keyval[i]] = keyval[i+1]
485 }
486 args := strings.Fields(cmdline)
487 for i, arg := range args {
488 args[i] = expand(m, arg)
489 }
490
491 _, err := pathcache.LookPath(v.Cmd)
492 if err != nil {
493 fmt.Fprintf(os.Stderr,
494 "go: missing %s command. See https://go.dev/s/gogetcmd\n",
495 v.Name)
496 return nil, err
497 }
498
499 cmd := exec.Command(v.Cmd, args...)
500 cmd.Dir = dir
501 if v.Env != nil {
502 cmd.Env = append(cmd.Environ(), v.Env...)
503 }
504 if cfg.BuildX {
505 fmt.Fprintf(os.Stderr, "cd %s\n", dir)
506 fmt.Fprintf(os.Stderr, "%s %s\n", v.Cmd, strings.Join(args, " "))
507 }
508 out, err := cmd.Output()
509 if err != nil {
510 if verbose || cfg.BuildV {
511 fmt.Fprintf(os.Stderr, "# cd %s; %s %s\n", dir, v.Cmd, strings.Join(args, " "))
512 if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
513 os.Stderr.Write(ee.Stderr)
514 } else {
515 fmt.Fprintln(os.Stderr, err.Error())
516 }
517 }
518 }
519 return out, err
520 }
521
522
523 func (v *Cmd) Ping(scheme, repo string) error {
524
525
526
527
528 dir := cfg.GOMODCACHE
529 if !cfg.ModulesEnabled {
530 dir = filepath.Join(cfg.BuildContext.GOPATH, "src")
531 }
532 os.MkdirAll(dir, 0o777)
533
534 release, err := base.AcquireNet()
535 if err != nil {
536 return err
537 }
538 defer release()
539
540 return v.runVerboseOnly(dir, v.PingCmd, "scheme", scheme, "repo", repo)
541 }
542
543
544
545 type vcsPath struct {
546 pathPrefix string
547 regexp *lazyregexp.Regexp
548 repo string
549 vcs string
550 check func(match map[string]string) error
551 schemelessRepo bool
552 }
553
554 var allowmultiplevcs = godebug.New("allowmultiplevcs")
555
556
557
558
559
560 func FromDir(dir, srcRoot string) (repoDir string, vcsCmd *Cmd, err error) {
561
562 dir = filepath.Clean(dir)
563 if srcRoot != "" {
564 srcRoot = filepath.Clean(srcRoot)
565 if len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator {
566 return "", nil, fmt.Errorf("directory %q is outside source root %q", dir, srcRoot)
567 }
568 }
569
570 origDir := dir
571 for len(dir) > len(srcRoot) {
572 for _, vcs := range vcsList {
573 if isVCSRootDir(dir, vcs.Roots) {
574 if vcsCmd == nil {
575
576 vcsCmd = vcs
577 repoDir = dir
578 if allowmultiplevcs.Value() == "1" {
579 allowmultiplevcs.IncNonDefault()
580 return repoDir, vcsCmd, nil
581 }
582
583
584
585
586 continue
587 }
588 if vcsCmd == vcsGit && vcs == vcsGit {
589
590
591
592 continue
593 }
594 return "", nil, fmt.Errorf("multiple VCS detected: %s in %q, and %s in %q",
595 vcsCmd.Cmd, repoDir, vcs.Cmd, dir)
596 }
597 }
598
599
600 ndir := filepath.Dir(dir)
601 if len(ndir) >= len(dir) {
602 break
603 }
604 dir = ndir
605 }
606 if vcsCmd == nil {
607 return "", nil, &vcsNotFoundError{dir: origDir}
608 }
609 return repoDir, vcsCmd, nil
610 }
611
612
613 func isVCSRootDir(dir string, roots []isVCSRoot) bool {
614 for _, root := range roots {
615 if root.isRoot(dir) {
616 return true
617 }
618 }
619 return false
620 }
621
622 type isVCSRoot interface {
623 isRoot(dir string) bool
624 }
625
626
627 type vcsFileRoot string
628
629 func (vfr vcsFileRoot) isRoot(dir string) bool {
630 fi, err := os.Stat(filepath.Join(dir, string(vfr)))
631 return err == nil && fi.Mode().IsRegular()
632 }
633
634
635 type vcsDirRoot string
636
637 func (vdr vcsDirRoot) isRoot(dir string) bool {
638 fi, err := os.Stat(filepath.Join(dir, string(vdr)))
639 return err == nil && fi.IsDir()
640 }
641
642
643
644 type vcsGitRoot struct{}
645
646 func (vcsGitRoot) isRoot(dir string) bool {
647 path := filepath.Join(dir, ".git")
648 fi, err := os.Stat(path)
649 if err != nil {
650 return false
651 }
652 if fi.IsDir() {
653 return true
654 }
655
656
657 if !fi.Mode().IsRegular() || fi.Size() == 0 || fi.Size() > 4096 {
658 return false
659 }
660 raw, err := os.ReadFile(path)
661 if err != nil {
662 return false
663 }
664 rest, ok := strings.CutPrefix(string(raw), "gitdir:")
665 if !ok {
666 return false
667 }
668 gitdir := strings.TrimSpace(rest)
669 if gitdir == "" {
670 return false
671 }
672 if !filepath.IsAbs(gitdir) {
673 gitdir = filepath.Join(dir, gitdir)
674 }
675 fi, err = os.Stat(gitdir)
676 return err == nil && fi.IsDir()
677 }
678
679 type vcsNotFoundError struct {
680 dir string
681 }
682
683 func (e *vcsNotFoundError) Error() string {
684 return fmt.Sprintf("directory %q is not using a known version control system", e.dir)
685 }
686
687 func (e *vcsNotFoundError) Is(err error) bool {
688 return err == os.ErrNotExist
689 }
690
691
692 type govcsRule struct {
693 pattern string
694 allowed []string
695 }
696
697
698 type govcsConfig []govcsRule
699
700 func parseGOVCS(s string) (govcsConfig, error) {
701 s = strings.TrimSpace(s)
702 if s == "" {
703 return nil, nil
704 }
705 var cfg govcsConfig
706 have := make(map[string]string)
707 for item := range strings.SplitSeq(s, ",") {
708 item = strings.TrimSpace(item)
709 if item == "" {
710 return nil, fmt.Errorf("empty entry in GOVCS")
711 }
712 pattern, list, found := strings.Cut(item, ":")
713 if !found {
714 return nil, fmt.Errorf("malformed entry in GOVCS (missing colon): %q", item)
715 }
716 pattern, list = strings.TrimSpace(pattern), strings.TrimSpace(list)
717 if pattern == "" {
718 return nil, fmt.Errorf("empty pattern in GOVCS: %q", item)
719 }
720 if list == "" {
721 return nil, fmt.Errorf("empty VCS list in GOVCS: %q", item)
722 }
723 if search.IsRelativePath(pattern) {
724 return nil, fmt.Errorf("relative pattern not allowed in GOVCS: %q", pattern)
725 }
726 if old := have[pattern]; old != "" {
727 return nil, fmt.Errorf("unreachable pattern in GOVCS: %q after %q", item, old)
728 }
729 have[pattern] = item
730 allowed := strings.Split(list, "|")
731 for i, a := range allowed {
732 a = strings.TrimSpace(a)
733 if a == "" {
734 return nil, fmt.Errorf("empty VCS name in GOVCS: %q", item)
735 }
736 allowed[i] = a
737 }
738 cfg = append(cfg, govcsRule{pattern, allowed})
739 }
740 return cfg, nil
741 }
742
743 func (c *govcsConfig) allow(path string, private bool, vcs string) bool {
744 for _, rule := range *c {
745 match := false
746 switch rule.pattern {
747 case "private":
748 match = private
749 case "public":
750 match = !private
751 default:
752
753
754 match = module.MatchPrefixPatterns(rule.pattern, path)
755 }
756 if !match {
757 continue
758 }
759 for _, allow := range rule.allowed {
760 if allow == vcs || allow == "all" {
761 return true
762 }
763 }
764 return false
765 }
766
767
768 return false
769 }
770
771 var (
772 govcs govcsConfig
773 govcsErr error
774 govcsOnce sync.Once
775 )
776
777
778
779
780
781
782
783
784
785
786
787
788
789 var defaultGOVCS = govcsConfig{
790 {"private", []string{"all"}},
791 {"public", []string{"git", "hg"}},
792 }
793
794
795
796
797
798 func checkGOVCS(vcs *Cmd, root string) error {
799 if vcs == vcsMod {
800
801
802
803 return nil
804 }
805
806 govcsOnce.Do(func() {
807 govcs, govcsErr = parseGOVCS(os.Getenv("GOVCS"))
808 govcs = append(govcs, defaultGOVCS...)
809 })
810 if govcsErr != nil {
811 return govcsErr
812 }
813
814 private := module.MatchPrefixPatterns(cfg.GOPRIVATE, root)
815 if !govcs.allow(root, private, vcs.Cmd) {
816 what := "public"
817 if private {
818 what = "private"
819 }
820 return fmt.Errorf("GOVCS disallows using %s for %s %s; see 'go help vcs'", vcs.Cmd, what, root)
821 }
822
823 return nil
824 }
825
826
827 type RepoRoot struct {
828 Repo string
829 Root string
830 SubDir string
831 IsCustom bool
832 VCS *Cmd
833 }
834
835 func httpPrefix(s string) string {
836 for _, prefix := range [...]string{"http:", "https:"} {
837 if strings.HasPrefix(s, prefix) {
838 return prefix
839 }
840 }
841 return ""
842 }
843
844
845 type ModuleMode int
846
847 const (
848 IgnoreMod ModuleMode = iota
849 PreferMod
850 )
851
852
853
854 func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
855 rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
856 if err == errUnknownSite {
857 rr, err = repoRootForImportDynamic(importPath, mod, security)
858 if err != nil {
859 err = importErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
860 }
861 }
862 if err != nil {
863 rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
864 if err1 == nil {
865 rr = rr1
866 err = nil
867 }
868 }
869
870
871 if err == nil && strings.Contains(importPath, "...") && strings.Contains(rr.Root, "...") {
872
873 rr = nil
874 err = importErrorf(importPath, "cannot expand ... in %q", importPath)
875 }
876
877
878 if err == nil {
879 counter.Inc("go/vcs:" + rr.VCS.Name)
880 }
881
882 return rr, err
883 }
884
885 var errUnknownSite = errors.New("dynamic lookup required to find mapping")
886
887
888
889 func repoRootFromVCSPaths(importPath string, security web.SecurityMode, vcsPaths []*vcsPath) (*RepoRoot, error) {
890 if str.HasPathPrefix(importPath, "example.net") {
891
892
893
894
895 return nil, fmt.Errorf("no modules on example.net")
896 }
897 if importPath == "rsc.io" {
898
899
900
901
902 return nil, fmt.Errorf("rsc.io is not a module")
903 }
904
905
906 if prefix := httpPrefix(importPath); prefix != "" {
907
908
909 return nil, fmt.Errorf("%q not allowed in import path", prefix+"//")
910 }
911 for _, srv := range vcsPaths {
912 if !str.HasPathPrefix(importPath, srv.pathPrefix) {
913 continue
914 }
915 m := srv.regexp.FindStringSubmatch(importPath)
916 if m == nil {
917 if srv.pathPrefix != "" {
918 return nil, importErrorf(importPath, "invalid %s import path %q", srv.pathPrefix, importPath)
919 }
920 continue
921 }
922
923
924 match := map[string]string{
925 "prefix": srv.pathPrefix + "/",
926 "import": importPath,
927 }
928 for i, name := range srv.regexp.SubexpNames() {
929 if name != "" && match[name] == "" {
930 match[name] = m[i]
931 }
932 }
933 if srv.vcs != "" {
934 match["vcs"] = expand(match, srv.vcs)
935 }
936 if srv.repo != "" {
937 match["repo"] = expand(match, srv.repo)
938 }
939 if srv.check != nil {
940 if err := srv.check(match); err != nil {
941 return nil, err
942 }
943 }
944 vcs := vcsByCmd(match["vcs"])
945 if vcs == nil {
946 return nil, fmt.Errorf("unknown version control system %q", match["vcs"])
947 }
948 if err := checkGOVCS(vcs, match["root"]); err != nil {
949 return nil, err
950 }
951 var repoURL string
952 if !srv.schemelessRepo {
953 repoURL = match["repo"]
954 } else {
955 repo := match["repo"]
956 var ok bool
957 repoURL, ok = interceptVCSTest(repo, vcs, security)
958 if !ok {
959 scheme, err := func() (string, error) {
960 for _, s := range vcs.Scheme {
961 if security == web.SecureOnly && !vcs.isSecureScheme(s) {
962 continue
963 }
964
965
966
967
968
969 if vcs.PingCmd == "" {
970 return s, nil
971 }
972 if err := vcs.Ping(s, repo); err == nil {
973 return s, nil
974 }
975 }
976 securityFrag := ""
977 if security == web.SecureOnly {
978 securityFrag = "secure "
979 }
980 return "", fmt.Errorf("no %sprotocol found for repository", securityFrag)
981 }()
982 if err != nil {
983 return nil, err
984 }
985 repoURL = scheme + "://" + repo
986 }
987 }
988 rr := &RepoRoot{
989 Repo: repoURL,
990 Root: match["root"],
991 VCS: vcs,
992 }
993 return rr, nil
994 }
995 return nil, errUnknownSite
996 }
997
998 func interceptVCSTest(repo string, vcs *Cmd, security web.SecurityMode) (repoURL string, ok bool) {
999 if VCSTestRepoURL == "" {
1000 return "", false
1001 }
1002 if vcs == vcsMod {
1003
1004
1005 return "", false
1006 }
1007
1008 if scheme, path, ok := strings.Cut(repo, "://"); ok {
1009 if security == web.SecureOnly && !vcs.isSecureScheme(scheme) {
1010 return "", false
1011 }
1012 repo = path
1013 }
1014 for _, host := range VCSTestHosts {
1015 if !str.HasPathPrefix(repo, host) {
1016 continue
1017 }
1018
1019 httpURL := VCSTestRepoURL + strings.TrimPrefix(repo, host)
1020
1021 if vcs == vcsSvn {
1022
1023
1024 u, err := urlpkg.Parse(httpURL + "?vcwebsvn=1")
1025 if err != nil {
1026 panic(fmt.Sprintf("invalid vcs-test repo URL: %v", err))
1027 }
1028 svnURL, err := web.GetBytes(u)
1029 svnURL = bytes.TrimSpace(svnURL)
1030 if err == nil && len(svnURL) > 0 {
1031 return string(svnURL) + strings.TrimPrefix(repo, host), true
1032 }
1033
1034
1035
1036 }
1037
1038 return httpURL, true
1039 }
1040 return "", false
1041 }
1042
1043
1044
1045
1046
1047 func urlForImportPath(importPath string) (*urlpkg.URL, error) {
1048 slash := strings.Index(importPath, "/")
1049 if slash < 0 {
1050 slash = len(importPath)
1051 }
1052 host, path := importPath[:slash], importPath[slash:]
1053 if !strings.Contains(host, ".") {
1054 return nil, errors.New("import path does not begin with hostname")
1055 }
1056 if len(path) == 0 {
1057 path = "/"
1058 }
1059 return &urlpkg.URL{Host: host, Path: path, RawQuery: "go-get=1"}, nil
1060 }
1061
1062
1063
1064
1065
1066 func repoRootForImportDynamic(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
1067 url, err := urlForImportPath(importPath)
1068 if err != nil {
1069 return nil, err
1070 }
1071 resp, err := web.Get(security, url)
1072 if err != nil {
1073 msg := "https fetch: %v"
1074 if security == web.Insecure {
1075 msg = "http/" + msg
1076 }
1077 return nil, fmt.Errorf(msg, err)
1078 }
1079 body := resp.Body
1080 defer body.Close()
1081 imports, err := parseMetaGoImports(body, mod)
1082 if len(imports) == 0 {
1083 if respErr := resp.Err(); respErr != nil {
1084
1085
1086 return nil, respErr
1087 }
1088 }
1089 if err != nil {
1090 return nil, fmt.Errorf("parsing %s: %v", importPath, err)
1091 }
1092
1093 mmi, err := matchGoImport(imports, importPath)
1094 if err != nil {
1095 if _, ok := err.(ImportMismatchError); !ok {
1096 return nil, fmt.Errorf("parse %s: %v", url, err)
1097 }
1098 return nil, fmt.Errorf("parse %s: no go-import meta tags (%s)", resp.URL, err)
1099 }
1100 if cfg.BuildV {
1101 log.Printf("get %q: found meta tag %#v at %s", importPath, mmi, url)
1102 }
1103
1104
1105
1106
1107
1108
1109 if mmi.Prefix != importPath {
1110 if cfg.BuildV {
1111 log.Printf("get %q: verifying non-authoritative meta tag", importPath)
1112 }
1113 var imports []metaImport
1114 url, imports, err = metaImportsForPrefix(mmi.Prefix, mod, security)
1115 if err != nil {
1116 return nil, err
1117 }
1118 metaImport2, err := matchGoImport(imports, importPath)
1119 if err != nil || mmi != metaImport2 {
1120 return nil, fmt.Errorf("%s and %s disagree about go-import for %s", resp.URL, url, mmi.Prefix)
1121 }
1122 }
1123
1124 if err := validateRepoSubDir(mmi.SubDir); err != nil {
1125 return nil, fmt.Errorf("%s: invalid subdirectory %q: %v", resp.URL, mmi.SubDir, err)
1126 }
1127
1128 if err := validateRepoRoot(mmi.RepoRoot); err != nil {
1129 return nil, fmt.Errorf("%s: invalid repo root %q: %v", resp.URL, mmi.RepoRoot, err)
1130 }
1131 var vcs *Cmd
1132 if mmi.VCS == "mod" {
1133 vcs = vcsMod
1134 } else {
1135 vcs = vcsByCmd(mmi.VCS)
1136 if vcs == nil {
1137 return nil, fmt.Errorf("%s: unknown vcs %q", resp.URL, mmi.VCS)
1138 }
1139 }
1140
1141 if err := checkGOVCS(vcs, mmi.Prefix); err != nil {
1142 return nil, err
1143 }
1144
1145 repoURL, ok := interceptVCSTest(mmi.RepoRoot, vcs, security)
1146 if !ok {
1147 repoURL = mmi.RepoRoot
1148 }
1149 rr := &RepoRoot{
1150 Repo: repoURL,
1151 Root: mmi.Prefix,
1152 SubDir: mmi.SubDir,
1153 IsCustom: true,
1154 VCS: vcs,
1155 }
1156 return rr, nil
1157 }
1158
1159
1160
1161
1162 func validateRepoSubDir(subdir string) error {
1163 if subdir == "" {
1164 return nil
1165 }
1166 if subdir[0] == '/' {
1167 return errors.New("leading slash")
1168 }
1169 if subdir[0] == '-' {
1170 return errors.New("leading hyphen")
1171 }
1172 return nil
1173 }
1174
1175
1176
1177 func validateRepoRoot(repoRoot string) error {
1178 url, err := urlpkg.Parse(repoRoot)
1179 if err != nil {
1180 return err
1181 }
1182 if url.Scheme == "" {
1183 return errors.New("no scheme")
1184 }
1185 if url.Scheme == "file" {
1186 return errors.New("file scheme disallowed")
1187 }
1188 return nil
1189 }
1190
1191 var fetchGroup singleflight.Group
1192 var (
1193 fetchCacheMu sync.Mutex
1194 fetchCache = map[string]fetchResult{}
1195 )
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205 func metaImportsForPrefix(importPrefix string, mod ModuleMode, security web.SecurityMode) (*urlpkg.URL, []metaImport, error) {
1206 setCache := func(res fetchResult) (fetchResult, error) {
1207 fetchCacheMu.Lock()
1208 defer fetchCacheMu.Unlock()
1209 fetchCache[importPrefix] = res
1210 return res, nil
1211 }
1212
1213 resi, _, _ := fetchGroup.Do(importPrefix, func() (resi any, err error) {
1214 fetchCacheMu.Lock()
1215 if res, ok := fetchCache[importPrefix]; ok {
1216 fetchCacheMu.Unlock()
1217 return res, nil
1218 }
1219 fetchCacheMu.Unlock()
1220
1221 url, err := urlForImportPath(importPrefix)
1222 if err != nil {
1223 return setCache(fetchResult{err: err})
1224 }
1225 resp, err := web.Get(security, url)
1226 if err != nil {
1227 return setCache(fetchResult{url: url, err: fmt.Errorf("fetching %s: %v", importPrefix, err)})
1228 }
1229 body := resp.Body
1230 defer body.Close()
1231 imports, err := parseMetaGoImports(body, mod)
1232 if len(imports) == 0 {
1233 if respErr := resp.Err(); respErr != nil {
1234
1235
1236 return setCache(fetchResult{url: url, err: respErr})
1237 }
1238 }
1239 if err != nil {
1240 return setCache(fetchResult{url: url, err: fmt.Errorf("parsing %s: %v", resp.URL, err)})
1241 }
1242 if len(imports) == 0 {
1243 err = fmt.Errorf("fetching %s: no go-import meta tag found in %s", importPrefix, resp.URL)
1244 }
1245 return setCache(fetchResult{url: url, imports: imports, err: err})
1246 })
1247 res := resi.(fetchResult)
1248 return res.url, res.imports, res.err
1249 }
1250
1251 type fetchResult struct {
1252 url *urlpkg.URL
1253 imports []metaImport
1254 err error
1255 }
1256
1257
1258
1259 type metaImport struct {
1260 Prefix, VCS, RepoRoot, SubDir string
1261 }
1262
1263
1264
1265 type ImportMismatchError struct {
1266 importPath string
1267 mismatches []string
1268 }
1269
1270 func (m ImportMismatchError) Error() string {
1271 formattedStrings := make([]string, len(m.mismatches))
1272 for i, pre := range m.mismatches {
1273 formattedStrings[i] = fmt.Sprintf("meta tag %s did not match import path %s", pre, m.importPath)
1274 }
1275 return strings.Join(formattedStrings, ", ")
1276 }
1277
1278
1279
1280
1281 func matchGoImport(imports []metaImport, importPath string) (metaImport, error) {
1282 match := -1
1283
1284 errImportMismatch := ImportMismatchError{importPath: importPath}
1285 for i, im := range imports {
1286 if !str.HasPathPrefix(importPath, im.Prefix) {
1287 errImportMismatch.mismatches = append(errImportMismatch.mismatches, im.Prefix)
1288 continue
1289 }
1290
1291 if match >= 0 {
1292 if imports[match].VCS == "mod" && im.VCS != "mod" {
1293
1294
1295
1296 break
1297 }
1298 return metaImport{}, fmt.Errorf("multiple meta tags match import path %q", importPath)
1299 }
1300 match = i
1301 }
1302
1303 if match == -1 {
1304 return metaImport{}, errImportMismatch
1305 }
1306 return imports[match], nil
1307 }
1308
1309
1310 func expand(match map[string]string, s string) string {
1311
1312
1313
1314 oldNew := make([]string, 0, 2*len(match))
1315 for k, v := range match {
1316 oldNew = append(oldNew, "{"+k+"}", v)
1317 }
1318 return strings.NewReplacer(oldNew...).Replace(s)
1319 }
1320
1321
1322
1323
1324
1325 var vcsPaths = []*vcsPath{
1326
1327 {
1328 pathPrefix: "github.com",
1329 regexp: lazyregexp.New(`^(?P<root>github\.com/[\w.\-]+/[\w.\-]+)(/[\w.\-]+)*$`),
1330 vcs: "git",
1331 repo: "https://{root}",
1332 check: noVCSSuffix,
1333 },
1334
1335
1336 {
1337 pathPrefix: "bitbucket.org",
1338 regexp: lazyregexp.New(`^(?P<root>bitbucket\.org/(?P<bitname>[\w.\-]+/[\w.\-]+))(/[\w.\-]+)*$`),
1339 vcs: "git",
1340 repo: "https://{root}",
1341 check: noVCSSuffix,
1342 },
1343
1344
1345 {
1346 pathPrefix: "hub.jazz.net/git",
1347 regexp: lazyregexp.New(`^(?P<root>hub\.jazz\.net/git/[a-z0-9]+/[\w.\-]+)(/[\w.\-]+)*$`),
1348 vcs: "git",
1349 repo: "https://{root}",
1350 check: noVCSSuffix,
1351 },
1352
1353
1354 {
1355 pathPrefix: "git.apache.org",
1356 regexp: lazyregexp.New(`^(?P<root>git\.apache\.org/[a-z0-9_.\-]+\.git)(/[\w.\-]+)*$`),
1357 vcs: "git",
1358 repo: "https://{root}",
1359 },
1360
1361
1362 {
1363 pathPrefix: "git.openstack.org",
1364 regexp: lazyregexp.New(`^(?P<root>git\.openstack\.org/[\w.\-]+/[\w.\-]+)(\.git)?(/[\w.\-]+)*$`),
1365 vcs: "git",
1366 repo: "https://{root}",
1367 },
1368
1369
1370 {
1371 pathPrefix: "chiselapp.com",
1372 regexp: lazyregexp.New(`^(?P<root>chiselapp\.com/user/[A-Za-z0-9]+/repository/[\w.\-]+)$`),
1373 vcs: "fossil",
1374 repo: "https://{root}",
1375 },
1376
1377
1378
1379 {
1380 regexp: lazyregexp.New(`(?P<root>(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[\w.\-]+)+?)\.(?P<vcs>bzr|fossil|git|hg|svn))(/~?[\w.\-]+)*$`),
1381 schemelessRepo: true,
1382 },
1383 }
1384
1385
1386
1387
1388
1389 var vcsPathsAfterDynamic = []*vcsPath{
1390
1391 {
1392 pathPrefix: "launchpad.net",
1393 regexp: lazyregexp.New(`^(?P<root>launchpad\.net/((?P<project>[\w.\-]+)(?P<series>/[\w.\-]+)?|~[\w.\-]+/(\+junk|[\w.\-]+)/[\w.\-]+))(/[\w.\-]+)*$`),
1394 vcs: "bzr",
1395 repo: "https://{root}",
1396 check: launchpadVCS,
1397 },
1398 }
1399
1400
1401
1402
1403 func noVCSSuffix(match map[string]string) error {
1404 repo := match["repo"]
1405 for _, vcs := range vcsList {
1406 if strings.HasSuffix(repo, "."+vcs.Cmd) {
1407 return fmt.Errorf("invalid version control suffix in %s path", match["prefix"])
1408 }
1409 }
1410 return nil
1411 }
1412
1413
1414
1415
1416
1417 func launchpadVCS(match map[string]string) error {
1418 if match["project"] == "" || match["series"] == "" {
1419 return nil
1420 }
1421 url := &urlpkg.URL{
1422 Scheme: "https",
1423 Host: "code.launchpad.net",
1424 Path: expand(match, "/{project}{series}/.bzr/branch-format"),
1425 }
1426 _, err := web.GetBytes(url)
1427 if err != nil {
1428 match["root"] = expand(match, "launchpad.net/{project}")
1429 match["repo"] = expand(match, "https://{root}")
1430 }
1431 return nil
1432 }
1433
1434
1435
1436 type importError struct {
1437 importPath string
1438 err error
1439 }
1440
1441 func importErrorf(path, format string, args ...any) error {
1442 err := &importError{importPath: path, err: fmt.Errorf(format, args...)}
1443 if errStr := err.Error(); !strings.Contains(errStr, path) {
1444 panic(fmt.Sprintf("path %q not in error %q", path, errStr))
1445 }
1446 return err
1447 }
1448
1449 func (e *importError) Error() string {
1450 return e.err.Error()
1451 }
1452
1453 func (e *importError) Unwrap() error {
1454
1455
1456 return errors.Unwrap(e.err)
1457 }
1458
1459 func (e *importError) ImportPath() string {
1460 return e.importPath
1461 }
1462
View as plain text