]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: show Sum/GoModSum when listing modules
authorSam Thanawalla <samthanawalla@google.com>
Thu, 8 Feb 2024 18:32:55 +0000 (18:32 +0000)
committerSam Thanawalla <samthanawalla@google.com>
Fri, 16 Feb 2024 16:56:39 +0000 (16:56 +0000)
Fixes #52792

Tested: Ran go test cmd/go
Change-Id: Ib7006256f4dca9e9fbfce266c00253c69595d6ab
Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest
Reviewed-on: https://go-review.googlesource.com/c/go/+/562775
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/go/alldocs.go
src/cmd/go/internal/list/list.go
src/cmd/go/internal/modfetch/fetch.go
src/cmd/go/internal/modinfo/info.go
src/cmd/go/internal/modload/build.go
src/cmd/go/testdata/script/mod_list_m.txt [new file with mode: 0644]

index a6166a7fdb034e58c3ac37891b5a1b7018ddc994..5e6d54ee2e7c01ae89058afe4b7cecbda00b4347 100644 (file)
 //         Retracted  []string      // retraction information, if any (with -retracted or -u)
 //         Deprecated string        // deprecation message, if any (with -u)
 //         Error      *ModuleError  // error loading module
+//         Sum        string        // checksum for path, version (as in go.sum)
+//         GoModSum   string        // checksum for go.mod (as in go.sum)
 //         Origin     any           // provenance of module
 //         Reuse      bool          // reuse of old module info is safe
 //     }
index 66fb5aa31cb7146cc969540c6f47706c0e1cac6e..df3639cba753575368278755e73676af2b1fe2ce 100644 (file)
@@ -245,6 +245,8 @@ applied to a Go struct, but now a Module struct:
         Retracted  []string      // retraction information, if any (with -retracted or -u)
         Deprecated string        // deprecation message, if any (with -u)
         Error      *ModuleError  // error loading module
+        Sum        string        // checksum for path, version (as in go.sum)
+        GoModSum   string        // checksum for go.mod (as in go.sum)
         Origin     any           // provenance of module
         Reuse      bool          // reuse of old module info is safe
     }
index eeab6da62a2daddf5cc3de7df47b512b20157c5e..ce801d34f28cb4675316b5b72fe526ac34978f5a 100644 (file)
@@ -569,6 +569,47 @@ func HaveSum(mod module.Version) bool {
        return false
 }
 
+// RecordedSum returns the sum if the go.sum file contains an entry for mod.
+// The boolean reports true if an entry was found or
+// false if no entry found or two conflicting sums are found.
+// The entry's hash must be generated with a known hash algorithm.
+// mod.Version may have a "/go.mod" suffix to distinguish sums for
+// .mod and .zip files.
+func RecordedSum(mod module.Version) (sum string, ok bool) {
+       goSum.mu.Lock()
+       defer goSum.mu.Unlock()
+       inited, err := initGoSum()
+       foundSum := ""
+       if err != nil || !inited {
+               return "", false
+       }
+       for _, goSums := range goSum.w {
+               for _, h := range goSums[mod] {
+                       if !strings.HasPrefix(h, "h1:") {
+                               continue
+                       }
+                       if !goSum.status[modSum{mod, h}].dirty {
+                               if foundSum != "" && foundSum != h { // conflicting sums exist
+                                       return "", false
+                               }
+                               foundSum = h
+                       }
+               }
+       }
+       for _, h := range goSum.m[mod] {
+               if !strings.HasPrefix(h, "h1:") {
+                       continue
+               }
+               if !goSum.status[modSum{mod, h}].dirty {
+                       if foundSum != "" && foundSum != h { // conflicting sums exist
+                               return "", false
+                       }
+                       foundSum = h
+               }
+       }
+       return foundSum, true
+}
+
 // checkMod checks the given module's checksum and Go version.
 func checkMod(ctx context.Context, mod module.Version) {
        // Do the file I/O before acquiring the go.sum lock.
index b0adcbcfb3dc99528ca5afc7655f9da21b4c22d1..336f99245aef4655077a0749f4d43e684e86e0f4 100644 (file)
@@ -14,24 +14,25 @@ import (
 // and the fields are documented in the help text in ../list/list.go
 
 type ModulePublic struct {
-       Path       string        `json:",omitempty"` // module path
-       Version    string        `json:",omitempty"` // module version
-       Query      string        `json:",omitempty"` // version query corresponding to this version
-       Versions   []string      `json:",omitempty"` // available module versions
-       Replace    *ModulePublic `json:",omitempty"` // replaced by this module
-       Time       *time.Time    `json:",omitempty"` // time version was created
-       Update     *ModulePublic `json:",omitempty"` // available update (with -u)
-       Main       bool          `json:",omitempty"` // is this the main module?
-       Indirect   bool          `json:",omitempty"` // module is only indirectly needed by main module
-       Dir        string        `json:",omitempty"` // directory holding local copy of files, if any
-       GoMod      string        `json:",omitempty"` // path to go.mod file describing module, if any
-       GoVersion  string        `json:",omitempty"` // go version used in module
-       Retracted  []string      `json:",omitempty"` // retraction information, if any (with -retracted or -u)
-       Deprecated string        `json:",omitempty"` // deprecation message, if any (with -u)
-       Error      *ModuleError  `json:",omitempty"` // error loading module
-
-       Origin *codehost.Origin `json:",omitempty"` // provenance of module
-       Reuse  bool             `json:",omitempty"` // reuse of old module info is safe
+       Path       string           `json:",omitempty"` // module path
+       Version    string           `json:",omitempty"` // module version
+       Query      string           `json:",omitempty"` // version query corresponding to this version
+       Versions   []string         `json:",omitempty"` // available module versions
+       Replace    *ModulePublic    `json:",omitempty"` // replaced by this module
+       Time       *time.Time       `json:",omitempty"` // time version was created
+       Update     *ModulePublic    `json:",omitempty"` // available update (with -u)
+       Main       bool             `json:",omitempty"` // is this the main module?
+       Indirect   bool             `json:",omitempty"` // module is only indirectly needed by main module
+       Dir        string           `json:",omitempty"` // directory holding local copy of files, if any
+       GoMod      string           `json:",omitempty"` // path to go.mod file describing module, if any
+       GoVersion  string           `json:",omitempty"` // go version used in module
+       Retracted  []string         `json:",omitempty"` // retraction information, if any (with -retracted or -u)
+       Deprecated string           `json:",omitempty"` // deprecation message, if any (with -u)
+       Error      *ModuleError     `json:",omitempty"` // error loading module
+       Sum        string           `json:",omitempty"` // checksum for path, version (as in go.sum)
+       GoModSum   string           `json:",omitempty"` // checksum for go.mod (as in go.sum)
+       Origin     *codehost.Origin `json:",omitempty"` // provenance of module
+       Reuse      bool             `json:",omitempty"` // reuse of old module info is safe
 }
 
 type ModuleError struct {
index 5cf1487c3ede31302b89b65d7a08fe460a0b91de..6e30afd5247b36f01e5586cd267df176641566aa 100644 (file)
@@ -364,12 +364,18 @@ func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode Li
                                                m.GoMod = gomod
                                        }
                                }
+                               if gomodsum, ok := modfetch.RecordedSum(modkey(mod)); ok {
+                                       m.GoModSum = gomodsum
+                               }
                        }
                        if checksumOk("") {
                                dir, err := modfetch.DownloadDir(ctx, mod)
                                if err == nil {
                                        m.Dir = dir
                                }
+                               if sum, ok := modfetch.RecordedSum(mod); ok {
+                                       m.Sum = sum
+                               }
                        }
 
                        if mode&ListRetracted != 0 {
diff --git a/src/cmd/go/testdata/script/mod_list_m.txt b/src/cmd/go/testdata/script/mod_list_m.txt
new file mode 100644 (file)
index 0000000..d579153
--- /dev/null
@@ -0,0 +1,16 @@
+go mod tidy
+
+go list -m -json all
+stdout '"GoModSum":\s+"h1:.+"'
+stdout '"Sum":\s+"h1:.+"'
+
+-- go.mod --
+module example
+
+go 1.21
+
+require rsc.io/quote v1.5.1
+-- example.go --
+package example
+
+import _ "rsc.io/quote"
\ No newline at end of file