aboutsummaryrefslogtreecommitdiff
path: root/internal/stdlib/stdlib.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-08-20 18:06:35 -0400
committerJonathan Amsterdam <jba@google.com>2021-08-23 19:38:19 +0000
commit9b380c5fe42de54edb8ea256f518a34d54a0c2f5 (patch)
treec1518e254db1be328b97f23d41d38f0d878f3e56 /internal/stdlib/stdlib.go
parent3c946205cc2fce26fae4f2d0848f02d1efb3f74c (diff)
downloadgo-x-pkgsite-9b380c5fe42de54edb8ea256f518a34d54a0c2f5.tar.xz
internal/stdlib: add ContentDir function
Add a function that returns the content directory of the stdlib module. Remove the part of the test that checks go.mod; it was never executing, because there is no go.mod file in any of the repos being tested. For golang/go#47834 Change-Id: Idc982620f6736ec60fe9a295f0372fd272745457 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/343965 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'internal/stdlib/stdlib.go')
-rw-r--r--internal/stdlib/stdlib.go55
1 files changed, 44 insertions, 11 deletions
diff --git a/internal/stdlib/stdlib.go b/internal/stdlib/stdlib.go
index 96ae3c59..c1f7ffed 100644
--- a/internal/stdlib/stdlib.go
+++ b/internal/stdlib/stdlib.go
@@ -13,6 +13,7 @@ import (
"bytes"
"fmt"
"io"
+ "io/fs"
"os"
"path"
"path/filepath"
@@ -331,6 +332,11 @@ func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion string, commit
// https://github.com/shurcooL/play/blob/master/256/moduleproxy/std/std.go.
defer derrors.Wrap(&err, "stdlib.Zip(%q)", requestedVersion)
+ zr, resolvedVersion, commitTime, _, err := zipInternal(requestedVersion)
+ return zr, resolvedVersion, commitTime, err
+}
+
+func zipInternal(requestedVersion string) (_ *zip.Reader, resolvedVersion string, commitTime time.Time, prefix string, err error) {
var repo *git.Repository
if UseTestData {
repo, err = getTestGoRepo(requestedVersion)
@@ -338,23 +344,23 @@ func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion string, commit
if requestedVersion == version.Latest {
requestedVersion, err = semanticVersion(requestedVersion)
if err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
}
repo, err = getGoRepo(requestedVersion)
}
if err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
var buf bytes.Buffer
z := zip.NewWriter(&buf)
head, err := repo.Head()
if err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
commit, err := repo.CommitObject(head.Hash())
if err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
resolvedVersion = requestedVersion
if SupportedBranches[requestedVersion] {
@@ -362,33 +368,60 @@ func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion string, commit
}
root, err := repo.TreeObject(commit.TreeHash)
if err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
prefixPath := ModulePath + "@" + requestedVersion
// Add top-level files.
if err := addFiles(z, repo, root, prefixPath, false); err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
// Add files from the stdlib directory.
libdir := root
for _, d := range strings.Split(Directory(resolvedVersion), "/") {
libdir, err = subTree(repo, libdir, d)
if err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
}
if err := addFiles(z, repo, libdir, prefixPath, true); err != nil {
- return nil, "", time.Time{}, err
+ return nil, "", time.Time{}, "", err
}
if err := z.Close(); err != nil {
- return nil, "", time.Time{}, err
+ 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, commit.Committer.When, 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 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(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(requestedVersion)
+ if err != nil {
+ return nil, "", time.Time{}, err
+ }
+ cdir, err := fs.Sub(zr, prefix)
+ if err != nil {
return nil, "", time.Time{}, err
}
- return zr, resolvedVersion, commit.Committer.When, nil
+ return cdir, resolvedVersion, commitTime, nil
}
func newPseudoVersion(version string, commitTime time.Time, hash plumbing.Hash) string {
@@ -452,7 +485,7 @@ func addFiles(z *zip.Writer, r *git.Repository, t *object.Tree, dirpath string,
continue
}
if e.Name == "go.mod" {
- // ignore; we'll synthesize our own
+ // Ignore; we don't need it.
continue
}
if strings.HasPrefix(e.Name, "README") && !strings.Contains(dirpath, "/") {