vole
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software:2222/vole.git
A free and open-source forge that lives in your Git repo.
refactor
c5902451905acdaa478f8476844ccd4a510d9ca9 · Keenan Nemetz · 17d ago · parent ec2aba4b
client/branch.go +273
| @@ -0,0 +1,273 @@ | |||
| 1 | package client | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "bytes" | ||
| 5 | "errors" | ||
| 6 | "fmt" | ||
| 7 | "io" | ||
| 8 | "math/rand/v2" | ||
| 9 | "os" | ||
| 10 | "os/exec" | ||
| 11 | "strings" | ||
| 12 | "time" | ||
| 13 | |||
| 14 | "github.com/rodent-software/vole/core/git" | ||
| 15 | "github.com/rodent-software/vole/metadata" | ||
| 16 | ) | ||
| 17 | |||
| 18 | // Branch operates on the clone in the current directory: one branch, one remote, | ||
| 19 | // one author. Issue commands use it for the metadata branch; site and release | ||
| 20 | // commands use the same plumbing for their ref, because a write over SSH is a | ||
| 21 | // write over SSH whichever ref it moves. | ||
| 22 | type Branch struct { | ||
| 23 | Repo *git.Repo | ||
| 24 | Remote string | ||
| 25 | Author string | ||
| 26 | Ref string | ||
| 27 | } | ||
| 28 | |||
| 29 | // NewBranch opens the metadata branch in the clone the process is running in. | ||
| 30 | func NewBranch(remote, ref string) (*Branch, error) { | ||
| 31 | dir, err := CurrentGitDir() | ||
| 32 | if err != nil { | ||
| 33 | return nil, err | ||
| 34 | } | ||
| 35 | b, err := OpenBranch(dir, remote, ref, "") | ||
| 36 | if err != nil { | ||
| 37 | return nil, err | ||
| 38 | } | ||
| 39 | branch := strings.TrimPrefix(b.Ref, "refs/heads/") | ||
| 40 | if cur, err := b.Repo.CurrentBranch(); err == nil && cur == branch { | ||
| 41 | return nil, fmt.Errorf("the %s branch is checked out here; git will not fetch into it. "+ | ||
| 42 | "Switch to another branch (git switch main) and run the command again", branch) | ||
| 43 | } | ||
| 44 | return b, nil | ||
| 45 | } | ||
| 46 | |||
| 47 | // OpenBranch opens a branch in an explicit git directory. That form is what a CI | ||
| 48 | // runner uses: it has a checkout it did not inherit from the process's cwd, and | ||
| 49 | // the branch is never the checked-out one. | ||
| 50 | func OpenBranch(gitDir, remote, ref, author string) (*Branch, error) { | ||
| 51 | if remote == "" { | ||
| 52 | remote = "origin" | ||
| 53 | } | ||
| 54 | if !strings.HasPrefix(ref, "refs/") { | ||
| 55 | ref = "refs/heads/" + ref | ||
| 56 | } | ||
| 57 | if author == "" { | ||
| 58 | author = DefaultAuthor(gitDir) | ||
| 59 | } | ||
| 60 | b := &Branch{Repo: git.Open(gitDir), Remote: remote, Author: author, Ref: ref} | ||
| 61 | _ = b.Fetch() // may legitimately fail on the first run: no branch yet | ||
| 62 | return b, nil | ||
| 63 | } | ||
| 64 | |||
| 65 | // Fetch resets the local branch to the remote's. It is called before a write and | ||
| 66 | // again after a lost push race, where discarding our local commit and re-applying | ||
| 67 | // the change on the new tip is cheaper than merging: every entity is its own | ||
| 68 | // file, so re-applying cannot conflict. | ||
| 69 | func (b *Branch) Fetch() error { | ||
| 70 | if !IsRemoteURL(b.Remote) && !HasRemote(b.Repo.Dir, b.Remote) { | ||
| 71 | return nil // nothing named to fetch from; the commit stays local | ||
| 72 | } | ||
| 73 | cmd := exec.Command("git", "fetch", "--quiet", b.Remote, "+"+b.Ref+":"+b.Ref) | ||
| 74 | cmd.Dir = b.Repo.Dir | ||
| 75 | var stderr bytes.Buffer | ||
| 76 | cmd.Stderr = &stderr | ||
| 77 | if err := cmd.Run(); err != nil { | ||
| 78 | return fmt.Errorf("fetch %s: %w: %s", b.Remote, err, strings.TrimSpace(stderr.String())) | ||
| 79 | } | ||
| 80 | return nil | ||
| 81 | } | ||
| 82 | |||
| 83 | // CommitAndPush pushes, and on a lost race re-applies the change on the new tip | ||
| 84 | // and tries again. Two authors editing different entities is normal and must not | ||
| 85 | // surface as an error the user has to resolve by hand; re-applying cannot conflict | ||
| 86 | // because every entity is its own file. | ||
| 87 | func (b *Branch) CommitAndPush(noPush bool, reapply func() error) error { | ||
| 88 | if noPush { | ||
| 89 | fmt.Fprintf(os.Stderr, "committed locally; push with: git push %s %s\n", b.Remote, b.Ref) | ||
| 90 | return nil | ||
| 91 | } | ||
| 92 | // A URL is a remote in its own right: a CI runner pushes to one without the clone | ||
| 93 | // having configured it as a named remote. | ||
| 94 | if !IsRemoteURL(b.Remote) && !HasRemote(b.Repo.Dir, b.Remote) { | ||
| 95 | fmt.Fprintf(os.Stderr, "no remote named %q; committed locally. Push with: git push <remote> %s\n", | ||
| 96 | b.Remote, b.Ref) | ||
| 97 | return nil | ||
| 98 | } | ||
| 99 | |||
| 100 | const attempts = 8 | ||
| 101 | announced := false | ||
| 102 | for attempt := 1; ; attempt++ { | ||
| 103 | err := b.pushOnce() | ||
| 104 | if err == nil { | ||
| 105 | return nil | ||
| 106 | } | ||
| 107 | if !isLostRace(err) { | ||
| 108 | return err | ||
| 109 | } | ||
| 110 | if attempt == attempts { | ||
| 111 | return fmt.Errorf("metadata kept moving; gave up after %d attempts: %w", attempts, err) | ||
| 112 | } | ||
| 113 | if !announced { | ||
| 114 | fmt.Fprintln(os.Stderr, "metadata moved underneath us; re-applying on the new tip") | ||
| 115 | announced = true | ||
| 116 | } | ||
| 117 | // Jitter matters: without it, concurrent writers back off in lockstep and | ||
| 118 | // collide again on every round. | ||
| 119 | time.Sleep(time.Duration(attempt*10+rand.IntN(30)) * time.Millisecond) | ||
| 120 | if ferr := b.Fetch(); ferr != nil { | ||
| 121 | return ferr | ||
| 122 | } | ||
| 123 | if rerr := reapply(); rerr != nil { | ||
| 124 | return fmt.Errorf("re-applying after a concurrent push failed: %w", rerr) | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | func (b *Branch) pushOnce() error { | ||
| 130 | cmd := exec.Command("git", "push", "--quiet", b.Remote, b.Ref+":"+b.Ref) | ||
| 131 | cmd.Dir = b.Repo.Dir | ||
| 132 | var stderr bytes.Buffer | ||
| 133 | cmd.Stdout = os.Stdout | ||
| 134 | cmd.Stderr = io.MultiWriter(os.Stderr, &stderr) | ||
| 135 | if err := cmd.Run(); err != nil { | ||
| 136 | return fmt.Errorf("%w: %s", err, strings.TrimSpace(stderr.String())) | ||
| 137 | } | ||
| 138 | return nil | ||
| 139 | } | ||
| 140 | |||
| 141 | // isLostRace distinguishes a lost race, which is retryable, from a policy | ||
| 142 | // rejection, which is not. A lost race has two shapes: our commit is no longer a | ||
| 143 | // fast-forward, or our push command arrives after someone else moved the ref and | ||
| 144 | // carries a stale old-oid ("incorrect old value provided"), or the branch is | ||
| 145 | // created by two writers at once ("reference already exists"). | ||
| 146 | func isLostRace(err error) bool { | ||
| 147 | msg := err.Error() | ||
| 148 | for _, marker := range []string{ | ||
| 149 | "non-fast-forward", | ||
| 150 | "fetch first", | ||
| 151 | "incorrect old value provided", | ||
| 152 | "reference already exists", | ||
| 153 | "stale info", | ||
| 154 | "Updates were rejected", | ||
| 155 | } { | ||
| 156 | if strings.Contains(msg, marker) { | ||
| 157 | return true | ||
| 158 | } | ||
| 159 | } | ||
| 160 | return false | ||
| 161 | } | ||
| 162 | |||
| 163 | // Issues loads the issues on this branch. | ||
| 164 | func (b *Branch) Issues() ([]*metadata.Issue, error) { | ||
| 165 | if sha, err := b.Repo.RevParse(metadata.Ref); err == nil && sha == "" { | ||
| 166 | return nil, nil | ||
| 167 | } | ||
| 168 | return metadata.LoadIssues(b.Repo, metadata.Ref) | ||
| 169 | } | ||
| 170 | |||
| 171 | // ResolveIssueID turns what a human typed into a full id. `list` prints short ids, | ||
| 172 | // so every id-taking command has to accept a prefix or the output is not usable. | ||
| 173 | func (b *Branch) ResolveIssueID(given string) (string, error) { | ||
| 174 | given = strings.ToUpper(strings.TrimSpace(given)) | ||
| 175 | if given == "" { | ||
| 176 | return "", fmt.Errorf("an issue id is required") | ||
| 177 | } | ||
| 178 | list, err := b.Issues() | ||
| 179 | if err != nil { | ||
| 180 | return "", err | ||
| 181 | } | ||
| 182 | for _, iss := range list { | ||
| 183 | if iss.ID == given { | ||
| 184 | return iss.ID, nil | ||
| 185 | } | ||
| 186 | } | ||
| 187 | var matches []string | ||
| 188 | for _, iss := range list { | ||
| 189 | if strings.HasPrefix(iss.ID, given) { | ||
| 190 | matches = append(matches, iss.ID) | ||
| 191 | } | ||
| 192 | } | ||
| 193 | switch len(matches) { | ||
| 194 | case 1: | ||
| 195 | return matches[0], nil | ||
| 196 | case 0: | ||
| 197 | return "", fmt.Errorf("no issue matching %q", given) | ||
| 198 | default: | ||
| 199 | return "", fmt.Errorf("%q matches %d issues: %s", given, len(matches), strings.Join(matches, ", ")) | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | // ApplyIssue runs mutate against one issue and pushes the result. "The branch | ||
| 204 | // already says this" is success, not an error: a rerun of a command must stay | ||
| 205 | // idempotent without growing a linear log, so Update reports ErrNoChange and the | ||
| 206 | // branch does not move. The returned issue is nil when nothing was committed. | ||
| 207 | func (b *Branch) ApplyIssue(id string, mutate func(*metadata.Issue) error, actor string, noPush bool) (*metadata.Issue, error) { | ||
| 208 | var updated *metadata.Issue | ||
| 209 | reapply := func() error { | ||
| 210 | var uerr error | ||
| 211 | updated, uerr = metadata.UpdateIssue(b.Repo, id, mutate, actor) | ||
| 212 | if errors.Is(uerr, metadata.ErrNoChange) { | ||
| 213 | // On a retry the change may already be on the branch, which is the | ||
| 214 | // outcome the caller wanted rather than a failure. | ||
| 215 | return nil | ||
| 216 | } | ||
| 217 | return uerr | ||
| 218 | } | ||
| 219 | if err := reapply(); err != nil { | ||
| 220 | return nil, err | ||
| 221 | } | ||
| 222 | if updated == nil { | ||
| 223 | return nil, nil | ||
| 224 | } | ||
| 225 | if err := b.CommitAndPush(noPush, reapply); err != nil { | ||
| 226 | return nil, err | ||
| 227 | } | ||
| 228 | return updated, nil | ||
| 229 | } | ||
| 230 | |||
| 231 | // ResolveCommentID turns a typed comment id into a full one, the way | ||
| 232 | // ResolveIssueID does for issues: a prefix has to work or printed ids are unusable. | ||
| 233 | func (b *Branch) ResolveCommentID(issueID, given string) (string, error) { | ||
| 234 | given = strings.ToUpper(strings.TrimSpace(given)) | ||
| 235 | if given == "" { | ||
| 236 | return "", fmt.Errorf("a comment id is required") | ||
| 237 | } | ||
| 238 | list, err := b.Issues() | ||
| 239 | if err != nil { | ||
| 240 | return "", err | ||
| 241 | } | ||
| 242 | for _, iss := range list { | ||
| 243 | if iss.ID != issueID { | ||
| 244 | continue | ||
| 245 | } | ||
| 246 | var matches []string | ||
| 247 | for _, cm := range iss.Comments { | ||
| 248 | if cm.ID == given { | ||
| 249 | return cm.ID, nil | ||
| 250 | } | ||
| 251 | if strings.HasPrefix(cm.ID, given) { | ||
| 252 | matches = append(matches, cm.ID) | ||
| 253 | } | ||
| 254 | } | ||
| 255 | switch len(matches) { | ||
| 256 | case 1: | ||
| 257 | return matches[0], nil | ||
| 258 | case 0: | ||
| 259 | return "", fmt.Errorf("no comment matching %q on %s", given, issueID) | ||
| 260 | default: | ||
| 261 | return "", fmt.Errorf("%q matches %d comments: %s", given, len(matches), strings.Join(matches, ", ")) | ||
| 262 | } | ||
| 263 | } | ||
| 264 | return "", fmt.Errorf("no issue matching %q", issueID) | ||
| 265 | } | ||
| 266 | |||
| 267 | // RepoName is the repository this clone's remote names, for building page URLs. | ||
| 268 | func (b *Branch) RepoName() string { | ||
| 269 | if _, _, path, err := SSHTarget(b.Repo.Dir, b.Remote); err == nil { | ||
| 270 | return RepoNameFromPath(path) | ||
| 271 | } | ||
| 272 | return "" | ||
| 273 | } | ||