github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/modfetch/codehost/codehost.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package codehost defines the interface implemented by a code hosting source, 6 // along with support code for use by implementations. 7 package codehost 8 9 import ( 10 "github.com/shogo82148/std/context" 11 "github.com/shogo82148/std/io" 12 "github.com/shogo82148/std/time" 13 ) 14 15 // Downloaded size limits. 16 const ( 17 MaxGoMod = 16 << 20 18 MaxLICENSE = 16 << 20 19 MaxZipFile = 500 << 20 20 ) 21 22 // A Repo represents a code hosting source. 23 // Typical implementations include local version control repositories, 24 // remote version control servers, and code hosting sites. 25 // 26 // A Repo must be safe for simultaneous use by multiple goroutines, 27 // and callers must not modify returned values, which may be cached and shared. 28 type Repo interface { 29 CheckReuse(ctx context.Context, old *Origin, subdir string) error 30 31 Tags(ctx context.Context, prefix string) (*Tags, error) 32 33 Stat(ctx context.Context, rev string) (*RevInfo, error) 34 35 Latest(ctx context.Context) (*RevInfo, error) 36 37 ReadFile(ctx context.Context, rev, file string, maxSize int64) (data []byte, err error) 38 39 ReadZip(ctx context.Context, rev, subdir string, maxSize int64) (zip io.ReadCloser, err error) 40 41 RecentTag(ctx context.Context, rev, prefix string, allowed func(tag string) bool) (tag string, err error) 42 43 DescendsFrom(ctx context.Context, rev, tag string) (bool, error) 44 } 45 46 // An Origin describes the provenance of a given repo method result. 47 // It can be passed to CheckReuse (usually in a different go command invocation) 48 // to see whether the result remains up-to-date. 49 type Origin struct { 50 VCS string `json:",omitempty"` 51 URL string `json:",omitempty"` 52 Subdir string `json:",omitempty"` 53 54 Hash string `json:",omitempty"` 55 56 // If TagSum is non-empty, then the resolution of this module version 57 // depends on the set of tags present in the repo, specifically the tags 58 // of the form TagPrefix + a valid semver version. 59 // If the matching repo tags and their commit hashes still hash to TagSum, 60 // the Origin is still valid (at least as far as the tags are concerned). 61 // The exact checksum is up to the Repo implementation; see (*gitRepo).Tags. 62 TagPrefix string `json:",omitempty"` 63 TagSum string `json:",omitempty"` 64 65 // If Ref is non-empty, then the resolution of this module version 66 // depends on Ref resolving to the revision identified by Hash. 67 // If Ref still resolves to Hash, the Origin is still valid (at least as far as Ref is concerned). 68 // For Git, the Ref is a full ref like "refs/heads/main" or "refs/tags/v1.2.3", 69 // and the Hash is the Git object hash the ref maps to. 70 // Other VCS might choose differently, but the idea is that Ref is the name 71 // with a mutable meaning while Hash is a name with an immutable meaning. 72 Ref string `json:",omitempty"` 73 74 // If RepoSum is non-empty, then the resolution of this module version 75 // failed due to the repo being available but the version not being present. 76 // This depends on the entire state of the repo, which RepoSum summarizes. 77 // For Git, this is a hash of all the refs and their hashes. 78 RepoSum string `json:",omitempty"` 79 } 80 81 // A Tags describes the available tags in a code repository. 82 type Tags struct { 83 Origin *Origin 84 List []Tag 85 } 86 87 // A Tag describes a single tag in a code repository. 88 type Tag struct { 89 Name string 90 Hash string 91 } 92 93 // A RevInfo describes a single revision in a source code repository. 94 type RevInfo struct { 95 Origin *Origin 96 Name string 97 Short string 98 Version string 99 Time time.Time 100 Tags []string 101 } 102 103 // UnknownRevisionError is an error equivalent to fs.ErrNotExist, but for a 104 // revision rather than a file. 105 type UnknownRevisionError struct { 106 Rev string 107 } 108 109 func (e *UnknownRevisionError) Error() string 110 111 func (UnknownRevisionError) Is(err error) bool 112 113 // ErrNoCommits is an error equivalent to fs.ErrNotExist indicating that a given 114 // repository or module contains no commits. 115 var ErrNoCommits error = noCommitsError{} 116 117 // AllHex reports whether the revision rev is entirely lower-case hexadecimal digits. 118 func AllHex(rev string) bool 119 120 // ShortenSHA1 shortens a SHA1 hash (40 hex digits) to the canonical length 121 // used in pseudo-versions (12 hex digits). 122 func ShortenSHA1(rev string) string 123 124 // WorkDir returns the name of the cached work directory to use for the 125 // given repository type and name. 126 func WorkDir(ctx context.Context, typ, name string) (dir, lockfile string, err error) 127 128 type RunError struct { 129 Cmd string 130 Err error 131 Stderr []byte 132 HelpText string 133 } 134 135 func (e *RunError) Error() string 136 137 // Run runs the command line in the given directory 138 // (an empty dir means the current directory). 139 // It returns the standard output and, for a non-zero exit, 140 // a *RunError indicating the command, exit status, and standard error. 141 // Standard error is unavailable for commands that exit successfully. 142 func Run(ctx context.Context, dir string, cmdline ...any) ([]byte, error) 143 144 func RunWithStdin(ctx context.Context, dir string, stdin io.Reader, cmdline ...any) ([]byte, error)