| 1 | // valley-go rebase: a change's whole stack, rebased onto main. |
| 2 | // |
| 3 | // `valley rebase <change>` and review's [b]ase run it. The stack is what |
| 4 | // valley-go stack lists (stack.go): the change, and the chain of layers |
| 5 | // under it, each on the one below. A change with nothing under it is a |
| 6 | // stack of one, and is rebased the same way. |
| 7 | // |
| 8 | // Each layer's own commits are rebased onto the rebased layer below it, |
| 9 | // and the bottom's onto main, in one throwaway worktree, bottom first. So |
| 10 | // every layer keeps its own delta, and the stack stays a stack. Commits |
| 11 | // under the bottom that a lower change landed through an integrator |
| 12 | // merge drop out: their content is on main already. They are the commits |
| 13 | // a landed version's head reaches (inventory.go reads what landed). |
| 14 | // |
| 15 | // The rebase refuses, and changes nothing, when the stack cannot move |
| 16 | // whole: |
| 17 | // |
| 18 | // - a change layer is not ready, names no branch, or its branch is |
| 19 | // missing on origin or not at its recorded head; |
| 20 | // - a plain branch in the stack is the branch of a change that is no |
| 21 | // layer, as a change is once a rebase has moved its branch and could |
| 22 | // not add the rebased head to its record; |
| 23 | // - two layers are on one branch; |
| 24 | // - the commits to be rebased hold a merge, or a version head of a |
| 25 | // change that is not ready or is no layer of the stack. A rebase gives |
| 26 | // every commit a new ID, so such a commit would ride into the stack |
| 27 | // under an ID no version records. That is the rule the integrator |
| 28 | // lands by (integrator/records.go), applied before the rebase; |
| 29 | // - a layer is a change and git config valley.principal names no one. |
| 30 | // |
| 31 | // It then publishes in two phases: |
| 32 | // |
| 33 | // 1. One atomic push moves every layer's branch, each leased on the tip |
| 34 | // the rebase read. Either every branch moves or none does. |
| 35 | // 2. Bottom first, each change's rebased head is added to its record as |
| 36 | // a new version, as ready --if-active adds one: only while the change |
| 37 | // is still ready at the head the rebase began with. A failure for one |
| 38 | // change does not stop the others. The change may have been unreadied |
| 39 | // meanwhile, its record may have moved, or the hook may refuse the |
| 40 | // write. |
| 41 | // |
| 42 | // A stack prints a manifest at the end: each layer's old and new head, |
| 43 | // whether its branch moved, whether a version was added, and the exact |
| 44 | // valley ready command that finishes each layer left unfinished. A change |
| 45 | // alone prints what rebase has always printed. |
| 46 | |
| 47 | package main |
| 48 | |
| 49 | import ( |
| 50 | "errors" |
| 51 | "fmt" |
| 52 | "io" |
| 53 | "os" |
| 54 | "os/exec" |
| 55 | "os/signal" |
| 56 | "strings" |
| 57 | "syscall" |
| 58 | |
| 59 | "the-valley/change" |
| 60 | ) |
| 61 | |
| 62 | const rebaseUsage = "usage: valley-go rebase [--head <sha>] <change>" |
| 63 | |
| 64 | // What became of a layer's branch in the push. |
| 65 | const ( |
| 66 | pushNot = "not pushed" |
| 67 | pushDone = "pushed" |
| 68 | pushUnknown = "may have moved" |
| 69 | ) |
| 70 | |
| 71 | // errReported is a failure whose words are printed already. |
| 72 | var errReported = errors.New("reported") |
| 73 | |
| 74 | // layer is one layer of a stack as the rebase captured it, and what |
| 75 | // became of it. |
| 76 | type layer struct { |
| 77 | k int // its place, from 0 at the bottom |
| 78 | name string // a change's handle, or a branch's name |
| 79 | id string // a change's ID; "" for a plain branch |
| 80 | branch string // the branch the rebase pushes |
| 81 | tip string // that branch's tip on origin as read; "" when it is missing |
| 82 | |
| 83 | // A change's record as the rebase read it: its ref and commit, its |
| 84 | // state, and its active version. |
| 85 | ref, commit, state string |
| 86 | record *change.Record |
| 87 | version int |
| 88 | |
| 89 | head string // the head rebased: the active version's, or the branch's tip |
| 90 | base string // the commit its own commits start above |
| 91 | |
| 92 | rebased string // its rebased head |
| 93 | pushed string // what became of its branch: pushNot, pushDone or pushUnknown |
| 94 | added int // the version phase 2 added, or 0 |
| 95 | } |
| 96 | |
| 97 | // rebaseRun is one rebase of a stack. |
| 98 | type rebaseRun struct { |
| 99 | g git |
| 100 | s *snapshot |
| 101 | given string // the name the change was given by |
| 102 | layers []*layer // bottom first, the change itself last |
| 103 | tip string // main on origin as read: what the stack is rebased onto |
| 104 | cut bool // whether landed commits under the bottom drop out |
| 105 | stdout, stderr io.Writer |
| 106 | } |
| 107 | |
| 108 | func (r *rebaseRun) top() *layer { return r.layers[len(r.layers)-1] } |
| 109 | func (r *rebaseRun) single() bool { return len(r.layers) == 1 } |
| 110 | |
| 111 | // place is where a layer sits, for a refusal that names it: nothing for a |
| 112 | // change alone, and otherwise ", layer <k> of <n> under <top>,". |
| 113 | func (r *rebaseRun) place(l *layer) string { |
| 114 | if r.single() { |
| 115 | return "" |
| 116 | } |
| 117 | return fmt.Sprintf(", layer %d of %d under %s,", l.k+1, len(r.layers), r.top().name) |
| 118 | } |
| 119 | |
| 120 | func parseRebaseArgs(args []string) (name, want string, err error) { |
| 121 | var rest []string |
| 122 | for i := 0; i < len(args); i++ { |
| 123 | switch a := args[i]; { |
| 124 | case a == "--head" && i+1 < len(args): |
| 125 | want = args[i+1] |
| 126 | i++ |
| 127 | case strings.HasPrefix(a, "--head="): |
| 128 | want = strings.TrimPrefix(a, "--head=") |
| 129 | case strings.HasPrefix(a, "-"): |
| 130 | return "", "", failure(rebaseUsage) |
| 131 | default: |
| 132 | rest = append(rest, a) |
| 133 | } |
| 134 | } |
| 135 | if len(rest) != 1 || (want != "" && !isObjectID(want)) { |
| 136 | return "", "", failure(rebaseUsage) |
| 137 | } |
| 138 | return rest[0], want, nil |
| 139 | } |
| 140 | |
| 141 | // runRebase rebases the stack under the change named. --head is the head |
| 142 | // the caller showed as the change's: the rebase refuses when the change |
| 143 | // is at another head by now. |
| 144 | func runRebase(g git, args []string, stdout, stderr io.Writer) int { |
| 145 | err := rebaseStack(g, args, stdout, stderr) |
| 146 | switch { |
| 147 | case err == nil: |
| 148 | return 0 |
| 149 | case !errors.Is(err, errReported): |
| 150 | fmt.Fprintf(stderr, "valley: %s\n", err) |
| 151 | } |
| 152 | return 1 |
| 153 | } |
| 154 | |
| 155 | func rebaseStack(g git, args []string, stdout, stderr io.Writer) error { |
| 156 | name, want, err := parseRebaseArgs(args) |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | if !g.succeeds(true, "rev-parse", "--git-dir") { |
| 161 | return failure("not a git repository") |
| 162 | } |
| 163 | res, err := resolveRecorded(g, name) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | if res.id != "" && res.branch == "" { |
| 168 | return failure(fmt.Sprintf("change %s v%d records no branch, and rebase acts on a change's branch. Approve its head alone: valley approve --head %s %s", |
| 169 | res.handle, res.version, res.head, res.handle)) |
| 170 | } |
| 171 | s, err := readSnapshot(g, verifierFromEnv()) |
| 172 | if err != nil { |
| 173 | return err |
| 174 | } |
| 175 | if s.tip == "" { |
| 176 | return failure("origin has no " + recordTarget + " to rebase onto") |
| 177 | } |
| 178 | chain, err := s.layers(res) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | r := &rebaseRun{g: g, s: s, given: name, tip: s.tip, stdout: stdout, stderr: stderr} |
| 183 | r.capture(chain) |
| 184 | if err := r.preflight(want); err != nil { |
| 185 | return err |
| 186 | } |
| 187 | // review offers [b]ase only to a stack that is stale. One that |
| 188 | // fast-forwards already is left alone, rather than rewritten for |
| 189 | // nothing. |
| 190 | if top := r.top(); g.succeeds(true, "merge-base", "--is-ancestor", r.tip, r.layers[0].head) { |
| 191 | fmt.Fprintf(stdout, "%s already fast-forwards from origin/%s; nothing rebased\n head %s\n", top.branch, recordTarget, top.head) |
| 192 | return nil |
| 193 | } |
| 194 | if err := r.needPrincipal(); err != nil { |
| 195 | return err |
| 196 | } |
| 197 | if err := r.rebase(); err != nil { |
| 198 | return err |
| 199 | } |
| 200 | if err := r.revalidate(); err != nil { |
| 201 | return err |
| 202 | } |
| 203 | return r.publish() |
| 204 | } |
| 205 | |
| 206 | // capture reads each layer's identity from the snapshot the stack was |
| 207 | // read from: a change's record ref and commit, its active version, head |
| 208 | // and branch, and every branch's tip. |
| 209 | func (r *rebaseRun) capture(chain []*entry) { |
| 210 | for k, e := range chain { |
| 211 | l := &layer{k: k, name: e.ref, id: e.id, head: e.head, state: e.state} |
| 212 | if e.id == "" { |
| 213 | l.branch = e.ref |
| 214 | } |
| 215 | for _, n := range r.s.all { |
| 216 | if e.id != "" && n.id == e.id { |
| 217 | v := n.record.ActiveVersion() |
| 218 | l.ref, l.commit, l.record = change.Ref(recordTarget, n.id), n.commit, n.record |
| 219 | l.version, l.branch = v.N, v.Branch |
| 220 | } |
| 221 | } |
| 222 | if l.branch != "" { |
| 223 | l.tip = r.s.o.refs["refs/heads/"+l.branch] |
| 224 | } |
| 225 | r.layers = append(r.layers, l) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // preflight refuses a stack the rebase cannot move whole, before anything |
| 230 | // is rebased, and finds where each layer's own commits start. |
| 231 | func (r *rebaseRun) preflight(want string) error { |
| 232 | var tips []string // the branches not at their changes' recorded heads |
| 233 | inStack := map[string]bool{} |
| 234 | for _, l := range r.layers { |
| 235 | inStack[l.id] = true |
| 236 | } |
| 237 | for _, l := range r.layers { |
| 238 | switch { |
| 239 | case l.id != "" && l.branch == "": |
| 240 | return failure(fmt.Sprintf("change %s v%d%s records no branch, so the rebase has no branch to push it to; nothing rebased", |
| 241 | l.name, l.version, r.place(l))) |
| 242 | case l.tip == "" && r.single(): |
| 243 | return failure("no such branch on origin: " + l.branch) |
| 244 | case l.tip == "": |
| 245 | return failure(fmt.Sprintf("change %s v%d%s is on the branch %s, and origin has no such branch; nothing rebased", |
| 246 | l.name, l.version, r.place(l), l.branch)) |
| 247 | case l.id != "" && l.state != change.Ready: |
| 248 | return failure(fmt.Sprintf("change %s v%d%s is %s, not ready; nothing rebased. Ready it first: %s", |
| 249 | l.name, l.version, r.place(l), l.state, readyHint(l.id, l.branch))) |
| 250 | case l == r.top() && want != "" && l.head != want && l.id != "": |
| 251 | return failure(fmt.Sprintf("change %s is at v%d, %s, and this run began at %s; nothing rebased. Review it again at its new head", |
| 252 | l.name, l.version, l.head, r.versionAt(l, want))) |
| 253 | case l == r.top() && want != "" && l.head != want: |
| 254 | return failure(fmt.Sprintf("origin/%s is at %s, and is no longer at %s, the head shown; nothing rebased", l.branch, l.head, want)) |
| 255 | case l.id != "" && l.tip != l.head: |
| 256 | tips = append(tips, fmt.Sprintf("change %s v%d%s records the head %s, and origin/%s is at %s; nothing rebased. Ready the branch's head as a new version of %s to act on it: %s", |
| 257 | l.name, l.version, r.place(l), l.head, l.branch, l.tip, l.name, readyCommand(l.tip, l.id, l.branch))) |
| 258 | } |
| 259 | if l.id != "" { |
| 260 | continue |
| 261 | } |
| 262 | // A plain branch that is a change's branch holds a head no version |
| 263 | // of that change records, as after a rebase that moved the branch |
| 264 | // and could not add the version. |
| 265 | for _, n := range r.s.all { |
| 266 | v := n.record.ActiveVersion() |
| 267 | if inStack[n.id] || v.Branch != l.branch || !unlanded(r.s, n) { |
| 268 | continue |
| 269 | } |
| 270 | h := r.s.o.handle(n.id) |
| 271 | tips = append(tips, fmt.Sprintf("change %s v%d records the head %s, and origin/%s%s is at %s; nothing rebased. Ready the branch's head as a new version of %s to act on it: %s", |
| 272 | h, v.N, headOf(v), l.branch, r.place(l), l.tip, h, readyCommand(l.tip, n.id, l.branch))) |
| 273 | } |
| 274 | } |
| 275 | // Two layers on one branch leave a change's branch off its recorded |
| 276 | // head as well, so they are refused first, for what they are. |
| 277 | on := map[string]*layer{} |
| 278 | for _, l := range r.layers { |
| 279 | if o := on[l.branch]; o != nil { |
| 280 | return failure(fmt.Sprintf("%s and %s, layers %d and %d of %d under %s, are both on the branch %s; nothing rebased", |
| 281 | o.name, l.name, o.k+1, l.k+1, len(r.layers), r.top().name, l.branch)) |
| 282 | } |
| 283 | on[l.branch] = l |
| 284 | } |
| 285 | if len(tips) > 0 { |
| 286 | return failure(strings.Join(tips, "\nvalley: ")) |
| 287 | } |
| 288 | return r.commits(inStack) |
| 289 | } |
| 290 | |
| 291 | // commits reads the commits the rebase moves: those the top's head |
| 292 | // reaches, less those main reaches and those a landed version's head |
| 293 | // reaches under the bottom. It refuses a merge among them, and a version |
| 294 | // head of a change that is not ready or is no layer. It sets where each |
| 295 | // layer's own commits start: for the bottom, the parent of its oldest |
| 296 | // commit; for every other layer, the head of the layer below. |
| 297 | func (r *rebaseRun) commits(inStack map[string]bool) error { |
| 298 | bottom, top := r.layers[0], r.top() |
| 299 | not := []string{"^" + r.tip} |
| 300 | reach := r.s.reach(bottom.head) |
| 301 | for _, n := range r.s.all { |
| 302 | for _, v := range n.record.Versions { |
| 303 | if h := headOf(v); reach[h] && r.s.isLanded(n.id, v) { |
| 304 | not, r.cut = append(not, "^"+h), true |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | out, err := r.g.output(true, append([]string{"rev-list", "--parents", top.head}, not...)...) |
| 309 | if err != nil { |
| 310 | return failure(fmt.Sprintf("could not read the commits under %s; nothing rebased", top.name)) |
| 311 | } |
| 312 | moved := map[string]bool{} |
| 313 | for _, line := range strings.Split(out, "\n") { |
| 314 | f := strings.Fields(line) |
| 315 | if len(f) > 2 { |
| 316 | return failure(fmt.Sprintf("the commits under %s include the merge %s, and a rebase moves a line of commits; nothing rebased", top.name, short(f[0]))) |
| 317 | } |
| 318 | if len(f) > 0 { |
| 319 | moved[f[0]] = true |
| 320 | } |
| 321 | } |
| 322 | for _, n := range r.s.all { |
| 323 | for _, v := range n.record.Versions { |
| 324 | c := headOf(v) |
| 325 | if !moved[c] || (inStack[n.id] && n.record.State == change.Ready) { |
| 326 | continue |
| 327 | } |
| 328 | h, why := r.s.o.handle(n.id), "which is no layer of this stack" |
| 329 | if n.record.State != change.Ready { |
| 330 | why = fmt.Sprintf("which is %s, not ready", n.record.State) |
| 331 | } |
| 332 | return failure(fmt.Sprintf("the commit %s under %s is v%d of change %s, %s; nothing rebased. A rebase gives each commit a new ID, which no version of %s would record", |
| 333 | short(c), top.name, v.N, h, why, h)) |
| 334 | } |
| 335 | } |
| 336 | out, err = r.g.output(true, append([]string{"rev-list", "--parents", "--reverse", bottom.head}, not...)...) |
| 337 | if err != nil { |
| 338 | return failure(fmt.Sprintf("could not read the commits of %s; nothing rebased", bottom.name)) |
| 339 | } |
| 340 | bottom.base = bottom.head |
| 341 | if oldest, _, _ := strings.Cut(out, "\n"); oldest != "" { |
| 342 | f := strings.Fields(oldest) |
| 343 | if len(f) < 2 { |
| 344 | return failure(fmt.Sprintf("%s and %s share no history; nothing rebased", bottom.name, recordTarget)) |
| 345 | } |
| 346 | bottom.base = f[1] |
| 347 | } |
| 348 | for k := 1; k < len(r.layers); k++ { |
| 349 | r.layers[k].base = r.layers[k-1].head |
| 350 | } |
| 351 | return nil |
| 352 | } |
| 353 | |
| 354 | // versionAt names a head of a change by its version too, when its record |
| 355 | // has one there. |
| 356 | func (r *rebaseRun) versionAt(l *layer, head string) string { |
| 357 | for _, v := range l.record.Versions { |
| 358 | if headOf(v) == head { |
| 359 | return fmt.Sprintf("v%d, %s", v.N, head) |
| 360 | } |
| 361 | } |
| 362 | return head |
| 363 | } |
| 364 | |
| 365 | // needPrincipal refuses a stack that holds a change when git config |
| 366 | // valley.principal names no one: phase 2 writes each change's record as |
| 367 | // that principal, and finding out there would leave every branch moved |
| 368 | // and no version added. |
| 369 | func (r *rebaseRun) needPrincipal() error { |
| 370 | changes := false |
| 371 | for _, l := range r.layers { |
| 372 | changes = changes || l.id != "" |
| 373 | } |
| 374 | if p, err := r.g.output(true, "config", "valley.principal"); !changes || err == nil && change.ValidPrincipal(p) { |
| 375 | return nil |
| 376 | } |
| 377 | what := fmt.Sprintf("rebasing change %s adds the rebased head to its record", r.top().name) |
| 378 | if !r.single() { |
| 379 | what = fmt.Sprintf("rebasing the stack under %s adds each change's rebased head to its record", r.top().name) |
| 380 | } |
| 381 | return failure(what + " as the principal your key pushes as, and git config valley.principal names none, so nothing is rebased; set it with git config valley.principal <name>") |
| 382 | } |
| 383 | |
| 384 | // rebase rebases every layer in one throwaway worktree, bottom first, the |
| 385 | // user's checkout untouched: the bottom onto main, and each layer above |
| 386 | // onto the rebased layer below it. A conflict anywhere ends it with |
| 387 | // nothing pushed. The worktree is removed however the rebase ends, an |
| 388 | // interrupt included. |
| 389 | func (r *rebaseRun) rebase() error { |
| 390 | dir, err := os.MkdirTemp("", "valley-rebase.") |
| 391 | if err != nil { |
| 392 | return failure("could not make a directory for the rebase; nothing changed") |
| 393 | } |
| 394 | wt := r.g |
| 395 | wt.dir = dir |
| 396 | added := false |
| 397 | // git runs in dir only once it is a worktree: a directory that is not |
| 398 | // one would leave git to find the repository above it. |
| 399 | cleanup := func() { |
| 400 | if added { |
| 401 | wt.succeeds(true, "rebase", "--abort") |
| 402 | r.g.succeeds(true, "worktree", "remove", "--force", dir) |
| 403 | } |
| 404 | os.RemoveAll(dir) |
| 405 | } |
| 406 | interrupted := make(chan os.Signal, 1) |
| 407 | done := make(chan struct{}) |
| 408 | signal.Notify(interrupted, os.Interrupt, syscall.SIGTERM) |
| 409 | go func() { |
| 410 | select { |
| 411 | case <-interrupted: |
| 412 | cleanup() |
| 413 | os.Exit(130) |
| 414 | case <-done: |
| 415 | } |
| 416 | }() |
| 417 | defer func() { |
| 418 | signal.Stop(interrupted) |
| 419 | close(done) |
| 420 | cleanup() |
| 421 | }() |
| 422 | |
| 423 | bottom := r.layers[0] |
| 424 | if _, err := r.g.output(false, "worktree", "add", "--quiet", "--detach", dir, bottom.head); err != nil { |
| 425 | return failure(fmt.Sprintf("could not check out origin/%s for the rebase; nothing changed", bottom.branch)) |
| 426 | } |
| 427 | added = true |
| 428 | onto := r.tip |
| 429 | for _, l := range r.layers { |
| 430 | // rebase.updateRefs would move the person's own branches that |
| 431 | // point into the commits rebased. |
| 432 | args := []string{"-c", "rebase.updateRefs=false", "rebase", "--quiet"} |
| 433 | if l.k == 0 && !r.cut { |
| 434 | args = append(args, onto, l.head) |
| 435 | } else { |
| 436 | args = append(args, "--onto", onto, l.base, l.head) |
| 437 | } |
| 438 | if err := wt.passing(r.stdout, args...); err != nil { |
| 439 | return failure(r.conflict(l)) |
| 440 | } |
| 441 | head, err := wt.output(true, "rev-parse", "HEAD") |
| 442 | if err != nil { |
| 443 | return failure(fmt.Sprintf("could not read the rebased head of %s; nothing pushed", l.branch)) |
| 444 | } |
| 445 | if head == onto { |
| 446 | return failure(fmt.Sprintf("%s adds nothing to %s once rebased, as when its commits have landed already; nothing pushed", l.name, r.below(l))) |
| 447 | } |
| 448 | l.rebased, onto = head, head |
| 449 | } |
| 450 | return nil |
| 451 | } |
| 452 | |
| 453 | // below is what a layer is rebased onto, as a refusal names it. |
| 454 | func (r *rebaseRun) below(l *layer) string { |
| 455 | if l.k == 0 { |
| 456 | return recordTarget |
| 457 | } |
| 458 | return r.layers[l.k-1].name + " as rebased" |
| 459 | } |
| 460 | |
| 461 | func (r *rebaseRun) conflict(l *layer) string { |
| 462 | if r.single() { |
| 463 | return fmt.Sprintf("%s conflicts with main — resolve by hand; nothing changed", l.branch) |
| 464 | } |
| 465 | return fmt.Sprintf("%s, layer %d of %d under %s, conflicts with %s — resolve by hand; nothing changed", |
| 466 | l.name, l.k+1, len(r.layers), r.top().name, r.below(l)) |
| 467 | } |
| 468 | |
| 469 | // revalidate reads origin once more, just before the push: each change |
| 470 | // layer's record must still be the record commit the rebase read, and each |
| 471 | // plain branch must still be a branch no record is on. |
| 472 | // |
| 473 | // This is the last check, and a window remains between it and the push. |
| 474 | // A change unreadied or withdrawn in that window, or a branch readied, is |
| 475 | // rebased all the same. git compares only the refs a push changes, and the |
| 476 | // record refs are not among them, so no client can make the push |
| 477 | // conditional on a record. Phase 2 then adds each version only while the |
| 478 | // change is still ready at the head the rebase began with, under a lease |
| 479 | // on the record, so a change unreadied in that window stays a draft at |
| 480 | // its old head, the manifest names it, and the integrator lands nothing |
| 481 | // for it. |
| 482 | func (r *rebaseRun) revalidate() error { |
| 483 | o, err := listOrigin(r.g, inventoryPatterns...) |
| 484 | if err != nil { |
| 485 | return failure("could not read origin again before the push; nothing pushed") |
| 486 | } |
| 487 | for _, l := range r.layers { |
| 488 | if l.id != "" { |
| 489 | if now := o.refs[l.ref]; now != l.commit { |
| 490 | return r.recordMoved(l, now) |
| 491 | } |
| 492 | continue |
| 493 | } |
| 494 | name := r.given |
| 495 | if !r.single() { |
| 496 | name = "origin/" + l.branch + r.place(l) |
| 497 | } |
| 498 | found, err := unlandedOn(r.g, o, l.branch) |
| 499 | if err != nil { |
| 500 | return failure(fmt.Sprintf("could not read %s again against origin's records; nothing pushed", strings.TrimSuffix(name, ","))) |
| 501 | } |
| 502 | if len(found) == 0 { |
| 503 | continue |
| 504 | } |
| 505 | n := found[0] |
| 506 | v, h := n.record.ActiveVersion(), o.handle(n.id) |
| 507 | switch { |
| 508 | case n.record.State != change.Ready: |
| 509 | return failure(fmt.Sprintf("%s is now change %s v%d, which is %s, not ready; nothing pushed. Ready it first: %s", |
| 510 | name, h, v.N, n.record.State, readyHint(n.id, v.Branch))) |
| 511 | case headOf(v) != l.head: |
| 512 | return failure(fmt.Sprintf("%s is now change %s v%d, which records the head %s, and this run began at %s; nothing pushed. Review it again by its handle", |
| 513 | name, h, v.N, headOf(v), l.head)) |
| 514 | } |
| 515 | return failure(fmt.Sprintf("%s is now change %s v%d; nothing pushed. Rebase it again by its handle: valley rebase %s", name, h, v.N, h)) |
| 516 | } |
| 517 | return nil |
| 518 | } |
| 519 | |
| 520 | // recordMoved is the refusal for a change layer whose record is no longer |
| 521 | // the record commit the rebase read. |
| 522 | func (r *rebaseRun) recordMoved(l *layer, now string) error { |
| 523 | if now == "" { |
| 524 | return failure(fmt.Sprintf("origin no longer holds a record of change %s; nothing pushed", l.name)) |
| 525 | } |
| 526 | rec, err := func() (*change.Record, error) { |
| 527 | if err := fetch(r.g, now); err != nil { |
| 528 | return nil, err |
| 529 | } |
| 530 | return readRecord(r.g, now) |
| 531 | }() |
| 532 | if err != nil { |
| 533 | return failure(fmt.Sprintf("could not read the record of change %s again; nothing pushed", l.name)) |
| 534 | } |
| 535 | v := rec.ActiveVersion() |
| 536 | switch { |
| 537 | case rec.State != change.Ready: |
| 538 | return failure(fmt.Sprintf("change %s v%d%s is %s, not ready; nothing pushed. Ready it first: %s", |
| 539 | l.name, v.N, r.place(l), rec.State, readyHint(l.id, v.Branch))) |
| 540 | case headOf(v) != l.head: |
| 541 | return failure(fmt.Sprintf("change %s%s is at v%d, %s, and this run began at v%d, %s; nothing pushed. Review it again at its new head", |
| 542 | l.name, r.place(l), v.N, headOf(v), l.version, l.head)) |
| 543 | } |
| 544 | return failure(fmt.Sprintf("the record of change %s%s changed while it was rebased; nothing pushed. Rebase it again: valley rebase %s", |
| 545 | l.name, r.place(l), r.top().name)) |
| 546 | } |
| 547 | |
| 548 | // publish pushes every branch, then adds each change's version, and says |
| 549 | // what became of each layer. |
| 550 | func (r *rebaseRun) publish() error { |
| 551 | top := r.top() |
| 552 | switch outcome, why := r.pushBranches(); outcome { |
| 553 | case pushNot: |
| 554 | if r.single() { |
| 555 | return failure(fmt.Sprintf("lease on origin/%s failed (it moved since the fetch); nothing pushed", top.branch)) |
| 556 | } |
| 557 | r.writeManifest() |
| 558 | return failure(fmt.Sprintf("origin refused the push of the stack's branches, so none moved (%s); nothing pushed. Rebase the stack again: valley rebase %s", |
| 559 | strings.Join(why, "; "), top.name)) |
| 560 | case pushUnknown: |
| 561 | r.writeManifest() |
| 562 | var refs []string |
| 563 | for _, l := range r.layers { |
| 564 | refs = append(refs, shellQuote("refs/heads/"+l.branch)) |
| 565 | } |
| 566 | return failure("the push of the branches failed, and whether origin took it is not known. Check with git ls-remote origin " + |
| 567 | strings.Join(refs, " ") + ": a branch at its new head is pushed, and the command above finishes its change") |
| 568 | } |
| 569 | if r.single() { |
| 570 | fmt.Fprintf(r.stdout, "rebased %s onto origin/%s, pushed with --force-with-lease\n", top.branch, recordTarget) |
| 571 | fmt.Fprintf(r.stdout, " head %s\n", top.rebased) |
| 572 | } |
| 573 | r.addVersions() |
| 574 | if !r.single() { |
| 575 | r.writeManifest() |
| 576 | } |
| 577 | r.otherStacks() |
| 578 | n := len(unfinished(r.layers)) |
| 579 | switch { |
| 580 | case n > 0 && r.single(): |
| 581 | return errReported |
| 582 | case n == 1: |
| 583 | return failure(fmt.Sprintf("1 change of the stack under %s is unfinished; finish it with the command above", top.name)) |
| 584 | case n > 0: |
| 585 | return failure(fmt.Sprintf("%d changes of the stack under %s are unfinished; finish each with the command above", n, top.name)) |
| 586 | } |
| 587 | return nil |
| 588 | } |
| 589 | |
| 590 | // pushBranches is phase 1: one atomic push of every layer's branch, each |
| 591 | // leased on the tip the rebase read. It says whether the branches moved, |
| 592 | // did not, or may have; and, when origin refused them, why, as git |
| 593 | // reports each ref. |
| 594 | func (r *rebaseRun) pushBranches() (string, []string) { |
| 595 | args := []string{"push", "--atomic", "--porcelain"} |
| 596 | for _, l := range r.layers { |
| 597 | args = append(args, "--force-with-lease=refs/heads/"+l.branch+":"+l.tip) |
| 598 | } |
| 599 | args = append(args, "origin") |
| 600 | for _, l := range r.layers { |
| 601 | args = append(args, l.rebased+":refs/heads/"+l.branch) |
| 602 | } |
| 603 | out, err := r.g.output(false, args...) |
| 604 | outcome := pushDone |
| 605 | var why []string |
| 606 | if err != nil { |
| 607 | outcome = pushUnknown |
| 608 | // A ref git reports as refused is one origin did not move, and |
| 609 | // an atomic push moves all of its refs or none. With no ref |
| 610 | // reported, the push may have reached origin before it failed. |
| 611 | for _, line := range strings.Split(out, "\n") { |
| 612 | f := strings.SplitN(line, "\t", 3) |
| 613 | if len(f) == 3 && f[0] == "!" { |
| 614 | _, to, _ := strings.Cut(f[1], ":") |
| 615 | why = append(why, "origin/"+strings.TrimPrefix(to, "refs/heads/")+": "+f[2]) |
| 616 | outcome = pushNot |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | for _, l := range r.layers { |
| 621 | l.pushed = outcome |
| 622 | } |
| 623 | return outcome, why |
| 624 | } |
| 625 | |
| 626 | // addVersions is phase 2: bottom first, each change's rebased head is |
| 627 | // added to its record as a new version, while the change is still ready |
| 628 | // at the head the rebase began with. A change alone says what ready says, |
| 629 | // as it always has. |
| 630 | func (r *rebaseRun) addVersions() { |
| 631 | for _, l := range r.layers { |
| 632 | if l.id == "" { |
| 633 | continue |
| 634 | } |
| 635 | var w io.Writer = io.Discard |
| 636 | if r.single() { |
| 637 | w = r.stdout |
| 638 | } |
| 639 | n, err := ready(r.g, readyArgs{head: l.rebased, id: l.id, ifActive: l.head, branch: l.branch}, w) |
| 640 | if err != nil { |
| 641 | fmt.Fprintf(r.stderr, "valley: %s\n", err) |
| 642 | if r.single() { |
| 643 | fmt.Fprintf(r.stderr, "valley: origin/%s is rebased to %s, and no version of %s records it. Ready it once the change is ready: %s\n", |
| 644 | l.branch, l.rebased, l.name, readyCommand(l.rebased, l.id, l.branch)) |
| 645 | } |
| 646 | continue |
| 647 | } |
| 648 | l.added = n |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // unfinished is each change layer whose branch may hold its rebased head |
| 653 | // with no version recording it. |
| 654 | func unfinished(layers []*layer) []*layer { |
| 655 | var left []*layer |
| 656 | for _, l := range layers { |
| 657 | if l.id != "" && l.pushed != pushNot && l.added == 0 { |
| 658 | left = append(left, l) |
| 659 | } |
| 660 | } |
| 661 | return left |
| 662 | } |
| 663 | |
| 664 | func (r *rebaseRun) writeManifest() { |
| 665 | io.WriteString(r.stdout, manifest(r.top().name, r.layers)) |
| 666 | } |
| 667 | |
| 668 | // manifest is what the rebase did to each layer, bottom first, one line |
| 669 | // each: its old and new head, what became of its branch, and for a change |
| 670 | // whether a version was added. Then, for each change left unfinished, the |
| 671 | // valley ready command that finishes it. |
| 672 | func manifest(top string, layers []*layer) string { |
| 673 | var b strings.Builder |
| 674 | fmt.Fprintf(&b, "the stack under %s, bottom first:\n", top) |
| 675 | width := 0 |
| 676 | for _, l := range layers { |
| 677 | width = max(width, columns(l.name)) |
| 678 | } |
| 679 | for _, l := range layers { |
| 680 | rebased := "(not rebased)" |
| 681 | if l.rebased != "" { |
| 682 | rebased = short(l.rebased) |
| 683 | } |
| 684 | status := "origin/" + l.branch + " " + l.pushed |
| 685 | switch { |
| 686 | case l.id != "" && l.added > 0: |
| 687 | status += fmt.Sprintf(", v%d added", l.added) |
| 688 | case l.id != "": |
| 689 | status += ", no version added" |
| 690 | } |
| 691 | fmt.Fprintf(&b, " %s%s %s -> %s %s\n", l.name, strings.Repeat(" ", width-columns(l.name)), short(l.head), rebased, status) |
| 692 | } |
| 693 | for _, l := range unfinished(layers) { |
| 694 | fmt.Fprintf(&b, "to finish %s: %s\n", l.name, readyCommand(l.rebased, l.id, l.branch)) |
| 695 | } |
| 696 | return b.String() |
| 697 | } |
| 698 | |
| 699 | // otherStacks names each ready change and plain branch outside the stack |
| 700 | // whose head contains a layer's old head, such as a second stack on a |
| 701 | // lower layer. The rebase left it on that old head, and does not move it. |
| 702 | func (r *rebaseRun) otherStacks() { |
| 703 | ids, branches := map[string]bool{}, map[string]bool{} |
| 704 | for _, l := range r.layers { |
| 705 | ids[l.id], branches[l.branch] = true, true |
| 706 | } |
| 707 | on := func(head string) *layer { |
| 708 | for k := len(r.layers) - 1; k >= 0; k-- { |
| 709 | if l := r.layers[k]; head == l.head || r.s.reach(head)[l.head] { |
| 710 | return l |
| 711 | } |
| 712 | } |
| 713 | return nil |
| 714 | } |
| 715 | for _, n := range r.s.all { |
| 716 | if ids[n.id] || n.record.State != change.Ready || !unlanded(r.s, n) { |
| 717 | continue |
| 718 | } |
| 719 | if l := on(headOf(n.record.ActiveVersion())); l != nil { |
| 720 | h := r.s.o.handle(n.id) |
| 721 | fmt.Fprintf(r.stdout, "%s is now on an old head of %s; rebase it with valley rebase %s\n", h, l.name, h) |
| 722 | } |
| 723 | } |
| 724 | for _, b := range r.s.unrecorded() { |
| 725 | if l := on(b.Head); l != nil && !branches[b.Branch] { |
| 726 | fmt.Fprintf(r.stdout, "%s is now on an old head of %s; rebase it with valley rebase %s\n", branchName(b.Branch), l.name, shellQuote(b.Branch)) |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // readyHint is the valley ready line that readies a change again, naming |
| 732 | // the branch unless its version records none, as bin/valley's ready_hint |
| 733 | // writes it. |
| 734 | func readyHint(id, branch string) string { |
| 735 | if branch == "" { |
| 736 | return "valley ready --change " + id |
| 737 | } |
| 738 | return "valley ready --change " + id + " " + shellQuote(branch) |
| 739 | } |
| 740 | |
| 741 | // readyCommand is the valley ready line that adds head to a change's |
| 742 | // record as a new version on branch. |
| 743 | func readyCommand(head, id, branch string) string { |
| 744 | return "valley ready --head " + head + " --change " + id + " " + shellQuote(branch) |
| 745 | } |
| 746 | |
| 747 | // passing runs git with its output on w, and its diagnostics on g's |
| 748 | // stderr. |
| 749 | func (g git) passing(w io.Writer, args ...string) error { |
| 750 | cmd := exec.Command("git", args...) |
| 751 | cmd.Env, cmd.Dir, cmd.Stdout, cmd.Stderr = g.env, g.dir, w, g.stderr |
| 752 | return cmd.Run() |
| 753 | } |
| 754 | |
| 755 | // runBrings prints each change that has not landed, other than the one |
| 756 | // --change names, with a version head among the commits a head would |
| 757 | // bring to main: those it reaches and main does not. One handle a line, |
| 758 | // in the order of the changes' IDs. approve refuses a head that does not |
| 759 | // fast-forward from main and would bring one, since it is a layer of a |
| 760 | // stack that has to be rebased first. |
| 761 | func runBrings(g git, args []string, w io.Writer) error { |
| 762 | const usage = "usage: valley-go brings [--change <id>] <head>" |
| 763 | var except, head string |
| 764 | for i := 0; i < len(args); i++ { |
| 765 | switch { |
| 766 | case args[i] == "--change" && i+1 < len(args): |
| 767 | except = args[i+1] |
| 768 | i++ |
| 769 | case head == "" && isObjectID(args[i]): |
| 770 | head = args[i] |
| 771 | default: |
| 772 | return failure(usage) |
| 773 | } |
| 774 | } |
| 775 | if head == "" { |
| 776 | return failure(usage) |
| 777 | } |
| 778 | if !g.succeeds(true, "cat-file", "-e", head+"^{commit}") { |
| 779 | return failure("no commit " + head + " here") |
| 780 | } |
| 781 | s, err := readSnapshot(g, verifierFromEnv()) |
| 782 | if err != nil { |
| 783 | return err |
| 784 | } |
| 785 | if s.tip == "" { |
| 786 | return failure("origin has no " + recordTarget + " to read against") |
| 787 | } |
| 788 | reach := s.reach(head) |
| 789 | var b strings.Builder |
| 790 | for _, n := range s.all { |
| 791 | if n.id == except || !unlanded(s, n) { |
| 792 | continue |
| 793 | } |
| 794 | for _, v := range n.record.Versions { |
| 795 | if reach[headOf(v)] { |
| 796 | fmt.Fprintln(&b, s.o.handle(n.id)) |
| 797 | break |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | _, err = io.WriteString(w, b.String()) |
| 802 | return err |
| 803 | } |