diff options
author | Russ Cox <rsc@golang.org> | 2018-08-18 14:16:26 -0400 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2018-08-22 15:50:54 +0000 |
commit | e1ad7cdf4f976e2985d1ea8cf36c97850c6cfd67 (patch) | |
tree | bb016c6f65de79963077672b92a37e0e71b15ae6 | |
parent | 811b0c1bf5b4034d32881ea090e0548444a9c14c (diff) | |
download | go-e1ad7cdf4f976e2985d1ea8cf36c97850c6cfd67.tar.gz go-e1ad7cdf4f976e2985d1ea8cf36c97850c6cfd67.zip |
[release-branch.go1.11] cmd/go: add go.sum entries to go mod download -json output
Clients of 'go mod download', particularly proxies, may need
the hashes of the content they downloaded, for checking against
go.sum entries or recording elsewhere.
Change-Id: Ic36c882cefc540678e1bc5a3dae1e865d181aa69
Reviewed-on: https://go-review.googlesource.com/129802
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
(cherry picked from commit 46033d7639cb1399029b99bb0cdc53d2b8f4bd08)
Reviewed-on: https://go-review.googlesource.com/130615
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r-- | src/cmd/go/internal/modcmd/download.go | 38 | ||||
-rw-r--r-- | src/cmd/go/internal/modfetch/cache.go | 17 | ||||
-rw-r--r-- | src/cmd/go/internal/modfetch/fetch.go | 11 | ||||
-rw-r--r-- | src/cmd/go/testdata/script/mod_download.txt | 2 |
4 files changed, 51 insertions, 17 deletions
diff --git a/src/cmd/go/internal/modcmd/download.go b/src/cmd/go/internal/modcmd/download.go index 2f072d73cf..cf42eff58a 100644 --- a/src/cmd/go/internal/modcmd/download.go +++ b/src/cmd/go/internal/modcmd/download.go @@ -32,13 +32,15 @@ to standard output, describing each downloaded module (or failure), corresponding to this Go struct: type Module struct { - Path string // module path - Version string // module version - Error string // error loading module - Info string // absolute path to cached .info file - GoMod string // absolute path to cached .mod file - Zip string // absolute path to cached .zip file - Dir string // absolute path to cached source root directory + Path string // module path + Version string // module version + Error string // error loading module + Info string // absolute path to cached .info file + GoMod string // absolute path to cached .mod file + Zip string // absolute path to cached .zip file + Dir string // absolute path to cached source root directory + Sum string // checksum for path, version (as in go.sum) + GoModSum string // checksum for go.mod (as in go.sum) } See 'go help modules' for more about module queries. @@ -52,13 +54,15 @@ func init() { } type moduleJSON struct { - Path string `json:",omitempty"` - Version string `json:",omitempty"` - Error string `json:",omitempty"` - Info string `json:",omitempty"` - GoMod string `json:",omitempty"` - Zip string `json:",omitempty"` - Dir string `json:",omitempty"` + Path string `json:",omitempty"` + Version string `json:",omitempty"` + Error string `json:",omitempty"` + Info string `json:",omitempty"` + GoMod string `json:",omitempty"` + Zip string `json:",omitempty"` + Dir string `json:",omitempty"` + Sum string `json:",omitempty"` + GoModSum string `json:",omitempty"` } func runDownload(cmd *base.Command, args []string) { @@ -98,12 +102,18 @@ func runDownload(cmd *base.Command, args []string) { m.Error = err.Error() return } + m.GoModSum, err = modfetch.GoModSum(m.Path, m.Version) + if err != nil { + m.Error = err.Error() + return + } mod := module.Version{Path: m.Path, Version: m.Version} m.Zip, err = modfetch.DownloadZip(mod) if err != nil { m.Error = err.Error() return } + m.Sum = modfetch.Sum(mod) m.Dir, err = modfetch.Download(mod) if err != nil { m.Error = err.Error() diff --git a/src/cmd/go/internal/modfetch/cache.go b/src/cmd/go/internal/modfetch/cache.go index efcd4854e8..1f9cc96c3e 100644 --- a/src/cmd/go/internal/modfetch/cache.go +++ b/src/cmd/go/internal/modfetch/cache.go @@ -290,6 +290,23 @@ func GoModFile(path, version string) (string, error) { return file, nil } +// GoModSum returns the go.sum entry for the module version's go.mod file. +// (That is, it returns the entry listed in go.sum as "path version/go.mod".) +func GoModSum(path, version string) (string, error) { + if !semver.IsValid(version) { + return "", fmt.Errorf("invalid version %q", version) + } + data, err := GoMod(path, version) + if err != nil { + return "", err + } + sum, err := goModSum(data) + if err != nil { + return "", err + } + return sum, nil +} + var errNotCached = fmt.Errorf("not in cache") // readDiskStat reads a cached stat result from disk, diff --git a/src/cmd/go/internal/modfetch/fetch.go b/src/cmd/go/internal/modfetch/fetch.go index 480579156f..2e26bac434 100644 --- a/src/cmd/go/internal/modfetch/fetch.go +++ b/src/cmd/go/internal/modfetch/fetch.go @@ -253,12 +253,17 @@ func checkSum(mod module.Version) { checkOneSum(mod, h) } +// goModSum returns the checksum for the go.mod contents. +func goModSum(data []byte) (string, error) { + return dirhash.Hash1([]string{"go.mod"}, func(string) (io.ReadCloser, error) { + return ioutil.NopCloser(bytes.NewReader(data)), nil + }) +} + // checkGoMod checks the given module's go.mod checksum; // data is the go.mod content. func checkGoMod(path, version string, data []byte) { - h, err := dirhash.Hash1([]string{"go.mod"}, func(string) (io.ReadCloser, error) { - return ioutil.NopCloser(bytes.NewReader(data)), nil - }) + h, err := goModSum(data) if err != nil { base.Fatalf("go: verifying %s %s go.mod: %v", path, version, err) } diff --git a/src/cmd/go/testdata/script/mod_download.txt b/src/cmd/go/testdata/script/mod_download.txt index ef931cfd30..6be6acb360 100644 --- a/src/cmd/go/testdata/script/mod_download.txt +++ b/src/cmd/go/testdata/script/mod_download.txt @@ -15,6 +15,8 @@ stdout '^\t"Version": "v1.5.0"' stdout '^\t"Info": ".*(\\\\|/)pkg(\\\\|/)mod(\\\\|/)cache(\\\\|/)download(\\\\|/)rsc.io(\\\\|/)quote(\\\\|/)@v(\\\\|/)v1.5.0.info"' stdout '^\t"GoMod": ".*(\\\\|/)pkg(\\\\|/)mod(\\\\|/)cache(\\\\|/)download(\\\\|/)rsc.io(\\\\|/)quote(\\\\|/)@v(\\\\|/)v1.5.0.mod"' stdout '^\t"Zip": ".*(\\\\|/)pkg(\\\\|/)mod(\\\\|/)cache(\\\\|/)download(\\\\|/)rsc.io(\\\\|/)quote(\\\\|/)@v(\\\\|/)v1.5.0.zip"' +stdout '^\t"Sum": "h1:6fJa6E\+wGadANKkUMlZ0DhXFpoKlslOQDCo259XtdIE="' # hash of testdata/mod version, not real version! +stdout '^\t"GoModSum": "h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe\+TKr0="' ! stdout '"Error"' # download queries above should not have added to go.mod. |