-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathstdlib.go
547 lines (499 loc) · 15.6 KB
/
stdlib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package stdlib supports special handling of the Go standard library.
// Regardless of the how the standard library has been split into modules for
// development and testing, the discovery site treats it as a single module
// named "std".
package stdlib
import (
"archive/zip"
"bytes"
"context"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"golang.org/x/mod/semver"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/version"
)
// ModulePath is the name of the module for the standard library.
const ModulePath = "std"
var (
// Regexp for matching go tags. The groups are:
// 1 the major.minor version
// 2 the patch version, or empty if none
// 3 the entire prerelease, if present
// 4 the prerelease type ("beta" or "rc")
// 5 the prerelease number
tagRegexp = regexp.MustCompile(`^go(\d+\.\d+)(\.\d+|)((beta|rc)(\d+))?$`)
)
// SupportedBranches are the branches of the stdlib repo supported by pkgsite.
// This should always contain version.Master, and may sometimes have other
// branches if they are experiments that are announced to the community.
var SupportedBranches = map[string]bool{
version.Master: true,
}
// VersionForTag returns the semantic version for the Go tag, or "" if
// tag doesn't correspond to a Go release or beta tag. In special cases,
// when the tag specified is either `latest` or `master` it will return the tag.
// Examples:
//
// "go1" => "v1.0.0"
// "go1.2" => "v1.2.0"
// "go1.13beta1" => "v1.13.0-beta.1"
// "go1.9rc2" => "v1.9.0-rc.2"
// "latest" => "latest"
// "master" => "master"
func VersionForTag(tag string) string {
// Special cases for go1.
if tag == "go1" {
return "v1.0.0"
}
if tag == "go1.0" {
return ""
}
// Special case for latest and master.
if tag == version.Latest || SupportedBranches[tag] {
return tag
}
m := tagRegexp.FindStringSubmatch(tag)
if m == nil {
return ""
}
version := "v" + m[1]
if m[2] != "" {
version += m[2]
} else {
version += ".0"
}
if m[3] != "" {
version += "-" + m[4] + "." + m[5]
}
return version
}
// TagForVersion returns the Go standard library repository tag corresponding
// to semver. The Go tags differ from standard semantic versions in a few ways,
// such as beginning with "go" instead of "v".
//
// Starting with go1.21.0, the first patch release of major go versions include
// the .0 suffix. Previously, the .0 suffix was elided (golang/go#57631).
func TagForVersion(v string) (_ string, err error) {
defer derrors.Wrap(&err, "TagForVersion(%q)", v)
// Special case: a supported branch name acts as its own tag.
if SupportedBranches[v] {
return v, nil
}
if strings.HasPrefix(v, "v0.0.0") {
return version.Master, nil
}
// Special case: v1.0.0 => go1.
if v == "v1.0.0" {
return "go1", nil
}
if !semver.IsValid(v) {
return "", fmt.Errorf("%w: requested version is not a valid semantic version: %q ", derrors.InvalidArgument, v)
}
goVersion := semver.Canonical(v)
prerelease := semver.Prerelease(goVersion)
versionWithoutPrerelease := strings.TrimSuffix(goVersion, prerelease)
patch := strings.TrimPrefix(versionWithoutPrerelease, semver.MajorMinor(goVersion)+".")
if patch == "0" && (semver.Compare(v, "v1.21.0") < 0 || prerelease != "") {
// Starting with go1.21.0, the first patch version includes .0.
// Prereleases do not include .0 (we don't do prereleases for other patch releases).
versionWithoutPrerelease = strings.TrimSuffix(versionWithoutPrerelease, ".0")
}
goVersion = fmt.Sprintf("go%s", strings.TrimPrefix(versionWithoutPrerelease, "v"))
if prerelease != "" {
// Go prereleases look like "beta1" instead of "beta.1".
// "beta1" is bad for sorting (since beta10 comes before beta9), so
// require the dot form.
i := finalDigitsIndex(prerelease)
if i >= 1 {
if prerelease[i-1] != '.' {
return "", fmt.Errorf("%w: final digits in a prerelease must follow a period", derrors.InvalidArgument)
}
// Remove the dot.
prerelease = prerelease[:i-1] + prerelease[i:]
}
goVersion += strings.TrimPrefix(prerelease, "-")
}
return goVersion, nil
}
// MajorVersionForVersion returns the Go major version for version.
// E.g. "v1.13.3" => "go1".
func MajorVersionForVersion(version string) (_ string, err error) {
defer derrors.Wrap(&err, "MajorVersionForVersion(%q)", version)
tag, err := TagForVersion(version)
if err != nil {
return "", err
}
if tag == "go1" || tag == "master" {
return "go1", nil
}
i := strings.IndexRune(tag, '.')
if i < 0 {
return "", fmt.Errorf("no '.' in go tag %q", tag)
}
return tag[:i], nil
}
// finalDigitsIndex returns the index of the first digit in the sequence of digits ending s.
// If s doesn't end in digits, it returns -1.
func finalDigitsIndex(s string) int {
// Assume ASCII (since the semver package does anyway).
var i int
for i = len(s) - 1; i >= 0; i-- {
if s[i] < '0' || s[i] > '9' {
break
}
}
if i == len(s)-1 {
return -1
}
return i + 1
}
const (
GoRepoURL = "https://go.googlesource.com/go"
GoSourceRepoURL = "https://cs.opensource.google/go/go"
GitHubRepo = "github.com/golang/go"
)
// TestCommitTime is the time used for all commits when UseTestData is true.
var (
TestCommitTime = time.Date(2019, 9, 4, 1, 2, 3, 0, time.UTC)
TestMasterVersion = "v0.0.0-20190904010203-89fb59e2e920"
)
var (
goRepoMu sync.Mutex
theGoRepo goRepo = &remoteGoRepo{}
)
func getGoRepo() goRepo {
goRepoMu.Lock()
defer goRepoMu.Unlock()
return theGoRepo
}
func swapGoRepo(gr goRepo) goRepo {
goRepoMu.Lock()
defer goRepoMu.Unlock()
old := theGoRepo
theGoRepo = gr
return old
}
// WithTestData arranges for this package to use a testing version of the Go repo.
// The returned function restores the previous state. Use with defer:
//
// defer WithTestData()()
func WithTestData() func() {
return withGoRepo(&testGoRepo{})
}
func withGoRepo(gr goRepo) func() {
old := swapGoRepo(gr)
return func() {
swapGoRepo(old)
}
}
// SetGoRepoPath tells this package to obtain the Go repo from the
// local filesystem at path, instead of cloning it.
func SetGoRepoPath(path string) error {
gr := newLocalGoRepo(path)
swapGoRepo(gr)
return nil
}
func refNameForVersion(v string) (string, error) {
if SupportedBranches[v] {
return "refs/heads/" + v, nil
}
tag, err := TagForVersion(v)
if err != nil {
return "", err
}
return "refs/tags/" + tag, nil
}
// Versions returns all the semantic versions of Go that are relevant to the
// discovery site. These are all release versions (derived from tags of the
// forms "goN.N" and "goN.N.N", where N is a number) and beta or rc versions
// (derived from tags of the forms "goN.NbetaN" and "goN.N.NbetaN", and
// similarly for "rc" replacing "beta").
func Versions() (_ []string, err error) {
defer derrors.Wrap(&err, "stdlib.Versions()")
refs, err := getGoRepo().refs(context.TODO())
if err != nil {
return nil, err
}
var versions []string
for _, r := range refs {
if !strings.HasPrefix(r.name, "refs/tags/") {
continue
}
tagName := strings.TrimPrefix(r.name, "refs/tags/")
v := VersionForTag(tagName)
if v != "" {
versions = append(versions, v)
}
}
return versions, nil
}
// ResolveSupportedBranches returns the current hashes for each ref in
// SupportedBranches.
func ResolveSupportedBranches() (_ map[string]string, err error) {
defer derrors.Wrap(&err, "ResolveSupportedBranches")
refs, err := getGoRepo().refs(context.TODO())
if err != nil {
return nil, fmt.Errorf("getting refs: %v", err)
}
m := map[string]string{}
for _, r := range refs {
if !strings.HasPrefix(r.name, "refs/heads/") {
continue
}
name := strings.TrimPrefix(r.name, "refs/heads/")
if SupportedBranches[name] {
m[name] = r.hash
}
}
return m, nil
}
// Directory returns the directory of the standard library relative to the repo root.
func Directory(v string) string {
if semver.Compare(v, "v1.4.0-beta.1") >= 0 ||
SupportedBranches[v] || strings.HasPrefix(v, "v0.0.0") {
return "src"
}
// For versions older than v1.4.0-beta.1, the stdlib is in src/pkg.
return "src/pkg"
}
// EstimatedZipSize is the approximate size of
// Zip("v1.15.2").
const EstimatedZipSize = 16 * 1024 * 1024
// ZipInfo returns the proxy .info information for the module std.
func ZipInfo(requestedVersion string) (resolvedVersion string, err error) {
defer derrors.Wrap(&err, "stdlib.ZipInfo(%q)", requestedVersion)
resolvedVersion, err = semanticVersion(requestedVersion)
if err != nil {
return "", err
}
return resolvedVersion, nil
}
func commiterTime(ctx context.Context, dir, object string) (time.Time, error) {
cmd := exec.CommandContext(ctx, "git", "show", "--no-patch", "--no-notes", "--format=%aI", object)
cmd.Dir = dir
b, err := cmd.Output()
if err != nil {
return time.Time{}, fmt.Errorf("running git show: %v, %s", err, b)
}
t, err := time.Parse(time.RFC3339, string(bytes.TrimSpace(b)))
if err != nil {
return time.Time{}, fmt.Errorf("parsing time output %q from command %v: %s", b, cmd, err)
}
return t, nil
}
func zipInternal(ctx context.Context, requestedVersion string) (_ *zip.Reader, resolvedVersion string, commitTime time.Time, prefix string, err error) {
if requestedVersion == version.Latest {
requestedVersion, err = semanticVersion(requestedVersion)
if err != nil {
return nil, "", time.Time{}, "", err
}
}
dir, err := os.MkdirTemp("", "")
if err != nil {
return nil, "", time.Time{}, "", err
}
defer func() {
rmallerr := os.RemoveAll(dir)
if err == nil {
err = rmallerr
}
}()
hash, err := getGoRepo().clone(ctx, requestedVersion, dir)
if err != nil {
return nil, "", time.Time{}, "", err
}
var buf bytes.Buffer
z := zip.NewWriter(&buf)
commitTime, err = commiterTime(ctx, dir, hash)
if err != nil {
return nil, "", time.Time{}, "", err
}
resolvedVersion = requestedVersion
if SupportedBranches[requestedVersion] {
resolvedVersion = newPseudoVersion("v0.0.0", commitTime, hash)
}
prefixPath := ModulePath + "@" + requestedVersion
// Add top-level files.
if err := addFiles(z, dir, prefixPath, false); err != nil {
return nil, "", time.Time{}, "", err
}
// Add files from the stdlib directory.
libDir := filepath.Join(dir, Directory(resolvedVersion))
if err := addFiles(z, libDir, prefixPath, true); err != nil {
return nil, "", time.Time{}, "", err
}
if err := z.Close(); err != nil {
return nil, "", time.Time{}, "", err
}
br := bytes.NewReader(buf.Bytes())
zr, err := zip.NewReader(br, int64(br.Len()))
if err != nil {
return nil, "", time.Time{}, "", err
}
return zr, resolvedVersion, commitTime, prefixPath, nil
}
// ContentDir creates an fs.FS representing the entire Go standard library at the
// given version (which must have been resolved with ZipInfo) and returns a
// reader to it. It also returns the time of the commit for that version.
//
// Normally, ContentDir returns the resolved version it was passed. If the
// resolved version is a supported branch like "master", ContentDir returns a
// semantic version for the branch.
//
// ContentDir reads the standard library at the Go repository tag corresponding
// to the given semantic version.
//
// ContentDir ignores go.mod files in the standard library, treating it as if it
// were a single module named "std" at the given version.
func ContentDir(ctx context.Context, requestedVersion string) (_ fs.FS, resolvedVersion string, commitTime time.Time, err error) {
defer derrors.Wrap(&err, "stdlib.ContentDir(%q)", requestedVersion)
zr, resolvedVersion, commitTime, prefix, err := zipInternal(ctx, requestedVersion)
if err != nil {
return nil, "", time.Time{}, err
}
cdir, err := fs.Sub(zr, prefix)
if err != nil {
return nil, "", time.Time{}, err
}
return cdir, resolvedVersion, commitTime, nil
}
const pseudoHashLen = 12
func newPseudoVersion(version string, commitTime time.Time, hash string) string {
return fmt.Sprintf("%s-%s-%s", version, commitTime.Format("20060102150405"), hash[:pseudoHashLen])
}
// VersionMatchesHash reports whether v is a pseudo-version whose hash
// part matches the prefix of the given hash.
func VersionMatchesHash(v, hash string) bool {
if !version.IsPseudo(v) {
return false
}
return v[len(v)-pseudoHashLen:] == hash[:pseudoHashLen]
}
// semanticVersion returns the semantic version corresponding to the
// requestedVersion. If the requested version is version.Master, then semanticVersion
// returns it as is. The branch name is resolved to a proper pseudo-version in
// Zip.
func semanticVersion(requestedVersion string) (_ string, err error) {
defer derrors.Wrap(&err, "semanticVersion(%q)", requestedVersion)
if SupportedBranches[requestedVersion] {
return requestedVersion, nil
}
knownVersions, err := Versions()
if err != nil {
return "", err
}
switch requestedVersion {
case version.Latest:
var latestVersion string
for _, v := range knownVersions {
if !strings.HasPrefix(v, "v") {
continue
}
versionType, err := version.ParseType(v)
if err != nil {
return "", err
}
if versionType != version.TypeRelease {
// We expect there to always be at least 1 release version.
continue
}
if semver.Compare(v, latestVersion) > 0 {
latestVersion = v
}
}
return latestVersion, nil
default:
for _, v := range knownVersions {
if v == requestedVersion {
return requestedVersion, nil
}
}
}
return "", fmt.Errorf("%w: requested version unknown: %q", derrors.InvalidArgument, requestedVersion)
}
// addFiles adds the files in t to z, using dirpath as the path prefix.
// If recursive is true, it also adds the files in all subdirectories.
func addFiles(z *zip.Writer, directory string, dirpath string, recursive bool) (err error) {
defer derrors.Wrap(&err, "addFiles(zip, repository, tree, %q, %t)", dirpath, recursive)
dirents, err := os.ReadDir(directory)
if err != nil {
return err
}
for _, e := range dirents {
if strings.HasPrefix(e.Name(), ".") || strings.HasPrefix(e.Name(), "_") {
continue
}
if e.Name() == "go.mod" {
// Ignore; we don't need it.
continue
}
if strings.HasPrefix(e.Name(), "README") && !strings.Contains(dirpath, "/") {
// For versions newer than v1.4.0-beta.1, the stdlib is in src/pkg.
// This means that our construction of the zip files will return
// two READMEs at the root:
// https://golang.org/README.md and
// https://golang.org/src/README.vendor
//
// We do not want to display the README.md
// or any README.vendor.
// However, we do want to store the README in
// other directories.
continue
}
switch {
case e.Type().IsRegular():
f, err := os.Open(filepath.Join(directory, e.Name()))
if err != nil {
return err
}
if err := writeZipFile(z, path.Join(dirpath, e.Name()), f); err != nil {
_ = f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
case e.Type().IsDir():
if !recursive || e.Name() == "testdata" {
continue
}
if err != nil {
return err
}
if err := addFiles(z, filepath.Join(directory, e.Name()), path.Join(dirpath, e.Name()), recursive); err != nil {
return err
}
}
}
return nil
}
func writeZipFile(z *zip.Writer, pathname string, src io.Reader) (err error) {
defer derrors.Wrap(&err, "writeZipFile(zip, %q, src)", pathname)
dst, err := z.Create(pathname)
if err != nil {
return err
}
_, err = io.Copy(dst, src)
return err
}
// Contains reports whether the given import path could be part of the Go standard library,
// by reporting whether the first component lacks a '.'.
func Contains(path string) bool {
if i := strings.IndexByte(path, '/'); i != -1 {
path = path[:i]
}
return !strings.Contains(path, ".")
}