| 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 | // The rebase engine (../rebase) does the work. It captures the chain from |
| 9 | // one snapshot of origin, refuses a stack it cannot move whole and changes |
| 10 | // nothing, rebases every layer in one throwaway worktree, bottom first, |
| 11 | // and publishes in two phases: one atomic push of every layer's branch, |
| 12 | // leased on the tips it read, and then, bottom first, each change's |
| 13 | // rebased head added to its record as a new version, as |
| 14 | // ready --if-active adds one. This verb words what the engine reports. It |
| 15 | // refuses a stack that holds a change when git config valley.principal |
| 16 | // names no one, since phase 2 writes each record as that principal. |
| 17 | // |
| 18 | // A stack prints a manifest at the end: each layer's old and new head, |
| 19 | // whether its branch moved, whether a version was added, and the exact |
| 20 | // valley ready command that finishes each layer left unfinished. A change |
| 21 | // alone prints what rebase has always printed. |
| 22 | // |
| 23 | // An interrupt or a SIGTERM while the stack is rebased cancels the |
| 24 | // rebase: the engine removes its worktree, nothing is pushed, and the verb |
| 25 | // exits 130. |
| 26 | |
| 27 | package main |
| 28 | |
| 29 | import ( |
| 30 | "context" |
| 31 | "errors" |
| 32 | "fmt" |
| 33 | "io" |
| 34 | "os" |
| 35 | "os/signal" |
| 36 | "strings" |
| 37 | "sync/atomic" |
| 38 | "syscall" |
| 39 | |
| 40 | "the-valley/change" |
| 41 | "the-valley/rebase" |
| 42 | ) |
| 43 | |
| 44 | const rebaseUsage = "usage: valley-go rebase [--head <sha>] <change>" |
| 45 | |
| 46 | // What became of a layer's branch in the push. |
| 47 | const ( |
| 48 | pushNot = "not pushed" |
| 49 | pushDone = "pushed" |
| 50 | pushUnknown = "may have moved" |
| 51 | ) |
| 52 | |
| 53 | // errReported is a failure whose words are printed already. |
| 54 | var errReported = errors.New("reported") |
| 55 | |
| 56 | // errInterrupted is a rebase a signal cancelled. It prints nothing, and |
| 57 | // exits 130. |
| 58 | var errInterrupted = errors.New("interrupted") |
| 59 | |
| 60 | // layer is one line of the manifest: a layer of the stack, and what became |
| 61 | // of it. |
| 62 | type layer struct { |
| 63 | name string // a change's handle, or a branch's name |
| 64 | id string // a change's ID; "" for a plain branch |
| 65 | branch string // the branch the rebase pushes |
| 66 | head string // the head rebased |
| 67 | rebased string // its rebased head |
| 68 | pushed string // what became of its branch: pushNot, pushDone or pushUnknown |
| 69 | added int // the version phase 2 added, or 0 |
| 70 | } |
| 71 | |
| 72 | // rebaseRun is one rebase of a stack. |
| 73 | type rebaseRun struct { |
| 74 | g git |
| 75 | s *snapshot |
| 76 | f *rebase.Family |
| 77 | given string // the name the change was given by |
| 78 | stdout, stderr io.Writer |
| 79 | } |
| 80 | |
| 81 | // top is the change the stack is under, the last of its layers. |
| 82 | func (r *rebaseRun) top() *rebase.Layer { return r.f.Start } |
| 83 | func (r *rebaseRun) single() bool { return len(r.f.Layers) == 1 } |
| 84 | |
| 85 | // index is a layer's place in the stack, from 0 at the bottom. |
| 86 | func (r *rebaseRun) index(l *rebase.Layer) int { |
| 87 | for k, m := range r.f.Layers { |
| 88 | if m == l { |
| 89 | return k |
| 90 | } |
| 91 | } |
| 92 | return -1 |
| 93 | } |
| 94 | |
| 95 | // place is where a layer sits, for a refusal that names it: nothing for a |
| 96 | // change alone, and otherwise ", layer <k> of <n> under <top>,". |
| 97 | func (r *rebaseRun) place(l *rebase.Layer) string { |
| 98 | if r.single() { |
| 99 | return "" |
| 100 | } |
| 101 | return fmt.Sprintf(", layer %d of %d under %s,", r.index(l)+1, len(r.f.Layers), r.top().Name) |
| 102 | } |
| 103 | |
| 104 | func parseRebaseArgs(args []string) (name, want string, err error) { |
| 105 | var rest []string |
| 106 | for i := 0; i < len(args); i++ { |
| 107 | switch a := args[i]; { |
| 108 | case a == "--head" && i+1 < len(args): |
| 109 | want = args[i+1] |
| 110 | i++ |
| 111 | case strings.HasPrefix(a, "--head="): |
| 112 | want = strings.TrimPrefix(a, "--head=") |
| 113 | case strings.HasPrefix(a, "-"): |
| 114 | return "", "", failure(rebaseUsage) |
| 115 | default: |
| 116 | rest = append(rest, a) |
| 117 | } |
| 118 | } |
| 119 | if len(rest) != 1 || (want != "" && !isObjectID(want)) { |
| 120 | return "", "", failure(rebaseUsage) |
| 121 | } |
| 122 | return rest[0], want, nil |
| 123 | } |
| 124 | |
| 125 | // runRebase rebases the stack under the change named. --head is the head |
| 126 | // the caller showed as the change's: the rebase refuses when the change |
| 127 | // is at another head by now. |
| 128 | func runRebase(g git, args []string, stdout, stderr io.Writer) int { |
| 129 | err := rebaseStack(g, args, stdout, stderr) |
| 130 | switch { |
| 131 | case err == nil: |
| 132 | return 0 |
| 133 | case errors.Is(err, errInterrupted): |
| 134 | return 130 |
| 135 | case !errors.Is(err, errReported): |
| 136 | fmt.Fprintf(stderr, "valley: %s\n", err) |
| 137 | } |
| 138 | return 1 |
| 139 | } |
| 140 | |
| 141 | func rebaseStack(g git, args []string, stdout, stderr io.Writer) error { |
| 142 | name, want, err := parseRebaseArgs(args) |
| 143 | if err != nil { |
| 144 | return err |
| 145 | } |
| 146 | if !g.succeeds(true, "rev-parse", "--git-dir") { |
| 147 | return failure("not a git repository") |
| 148 | } |
| 149 | res, err := resolveRecorded(g, name) |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | if res.id != "" && res.branch == "" { |
| 154 | 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", |
| 155 | res.handle, res.version, res.head, res.handle)) |
| 156 | } |
| 157 | s, err := readSnapshot(g, verifierFromEnv()) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | if s.tip == "" { |
| 162 | return failure("origin has no " + recordTarget + " to rebase onto") |
| 163 | } |
| 164 | f, err := rebase.Capture(s.land, rebase.Selection{ID: res.id, Branch: res.branch, Head: want}) |
| 165 | if err != nil { |
| 166 | return err |
| 167 | } |
| 168 | r := &rebaseRun{g: g, s: s, f: f, given: name, stdout: stdout, stderr: stderr} |
| 169 | if err := f.Check(); err != nil { |
| 170 | return r.refused(err) |
| 171 | } |
| 172 | // review offers [b]ase only to a stack that is stale. One that |
| 173 | // fast-forwards already is left alone, rather than rewritten for |
| 174 | // nothing. |
| 175 | if top := r.top(); f.FastForwards() { |
| 176 | fmt.Fprintf(stdout, "%s already fast-forwards from origin/%s; nothing rebased\n head %s\n", top.Branch, recordTarget, top.Head) |
| 177 | return nil |
| 178 | } |
| 179 | principal, err := r.needPrincipal() |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | eg := g.engine() |
| 184 | eg.Stdout = stdout |
| 185 | p, err := r.prepare(eg) |
| 186 | if err != nil { |
| 187 | return err |
| 188 | } |
| 189 | defer p.Release(context.Background(), eg) |
| 190 | pub, err := rebase.Publish(context.Background(), eg, p) |
| 191 | if err != nil { |
| 192 | return r.refused(err) |
| 193 | } |
| 194 | return r.publish(eg, pub, principal) |
| 195 | } |
| 196 | |
| 197 | // needPrincipal is the principal git config valley.principal names, and |
| 198 | // refuses a stack that holds a change when it names no one: phase 2 writes |
| 199 | // each change's record as that principal, and finding out there would |
| 200 | // leave every branch moved and no version added. |
| 201 | func (r *rebaseRun) needPrincipal() (string, error) { |
| 202 | changes := false |
| 203 | for _, l := range r.f.Layers { |
| 204 | changes = changes || l.ID != "" |
| 205 | } |
| 206 | p, err := r.g.output(true, "config", "valley.principal") |
| 207 | if !changes || err == nil && change.ValidPrincipal(p) { |
| 208 | return p, nil |
| 209 | } |
| 210 | what := fmt.Sprintf("rebasing change %s adds the rebased head to its record", r.top().Name) |
| 211 | if !r.single() { |
| 212 | what = fmt.Sprintf("rebasing the stack under %s adds each change's rebased head to its record", r.top().Name) |
| 213 | } |
| 214 | 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>") |
| 215 | } |
| 216 | |
| 217 | // prepare has the engine rebase every layer, the user's checkout |
| 218 | // untouched. An interrupt or a SIGTERM meanwhile cancels the rebase, and |
| 219 | // once the engine has removed its worktree the run ends with |
| 220 | // errInterrupted. |
| 221 | func (r *rebaseRun) prepare(eg rebase.Git) (*rebase.Prepared, error) { |
| 222 | ctx, cancel := context.WithCancel(context.Background()) |
| 223 | defer cancel() |
| 224 | interrupted := make(chan os.Signal, 1) |
| 225 | signal.Notify(interrupted, os.Interrupt, syscall.SIGTERM) |
| 226 | defer signal.Stop(interrupted) |
| 227 | var signalled atomic.Bool |
| 228 | done := make(chan struct{}) |
| 229 | defer close(done) |
| 230 | go func() { |
| 231 | select { |
| 232 | case <-interrupted: |
| 233 | signalled.Store(true) |
| 234 | cancel() |
| 235 | case <-done: |
| 236 | } |
| 237 | }() |
| 238 | p, err := rebase.Prepare(ctx, eg, r.f) |
| 239 | if signalled.Load() || errors.Is(err, context.Canceled) || errors.Is(err, rebase.ErrInterrupted) { |
| 240 | if p != nil { |
| 241 | p.Release(context.Background(), eg) |
| 242 | } |
| 243 | return nil, errInterrupted |
| 244 | } |
| 245 | if err != nil { |
| 246 | return nil, r.refused(err) |
| 247 | } |
| 248 | return p, nil |
| 249 | } |
| 250 | |
| 251 | // refused words a refusal the engine gives as valley rebase has always |
| 252 | // worded it: with the layer's place in the stack, and the command that |
| 253 | // acts on it. Several are one line each. |
| 254 | func (r *rebaseRun) refused(err error) error { |
| 255 | var many rebase.Refusals |
| 256 | if errors.As(err, &many) { |
| 257 | var words []string |
| 258 | for _, one := range many { |
| 259 | words = append(words, r.words(one)) |
| 260 | } |
| 261 | return failure(strings.Join(words, "\nvalley: ")) |
| 262 | } |
| 263 | var one *rebase.Refusal |
| 264 | if errors.As(err, &one) { |
| 265 | return failure(r.words(one)) |
| 266 | } |
| 267 | return err |
| 268 | } |
| 269 | |
| 270 | func (r *rebaseRun) words(x *rebase.Refusal) string { |
| 271 | l := x.Layer |
| 272 | switch x.Kind { |
| 273 | case rebase.NoBranch: |
| 274 | return fmt.Sprintf("change %s v%d%s records no branch, so the rebase has no branch to push it to; nothing rebased", |
| 275 | l.Name, l.Version, r.place(l)) |
| 276 | case rebase.BranchMissing: |
| 277 | if r.single() { |
| 278 | return "no such branch on origin: " + l.Branch |
| 279 | } |
| 280 | return fmt.Sprintf("change %s v%d%s is on the branch %s, and origin has no such branch; nothing rebased", |
| 281 | l.Name, l.Version, r.place(l), l.Branch) |
| 282 | case rebase.NotReady: |
| 283 | return fmt.Sprintf("change %s v%d%s is %s, not ready; nothing rebased. Ready it first: %s", |
| 284 | l.Name, l.Version, r.place(l), l.Record.State, readyHint(l.ID, l.Branch)) |
| 285 | case rebase.HeadMoved: |
| 286 | if l.ID == "" { |
| 287 | return fmt.Sprintf("origin/%s is at %s, and is no longer at %s, the head shown; nothing rebased", l.Branch, l.Head, x.Commit) |
| 288 | } |
| 289 | return fmt.Sprintf("change %s is at v%d, %s, and this run began at %s; nothing rebased. Review it again at its new head", |
| 290 | l.Name, l.Version, l.Head, versionAt(l, x.Commit)) |
| 291 | case rebase.TipMoved: |
| 292 | return 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", |
| 293 | l.Name, l.Version, r.place(l), l.Head, l.Branch, l.Tip, l.Name, readyCommand(l.Tip, l.ID, l.Branch)) |
| 294 | case rebase.BranchOfChange: |
| 295 | return 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", |
| 296 | x.Handle, x.Version.N, headOf(x.Version), l.Branch, r.place(l), l.Tip, x.Handle, readyCommand(l.Tip, x.Change.ID, l.Branch)) |
| 297 | case rebase.SharedBranch: |
| 298 | return fmt.Sprintf("%s and %s, layers %d and %d of %d under %s, are both on the branch %s; nothing rebased", |
| 299 | x.Other.Name, l.Name, r.index(x.Other)+1, r.index(l)+1, len(r.f.Layers), r.top().Name, l.Branch) |
| 300 | case rebase.Conflict: |
| 301 | if r.single() { |
| 302 | return fmt.Sprintf("%s conflicts with main — resolve by hand; nothing changed", l.Branch) |
| 303 | } |
| 304 | return fmt.Sprintf("%s, layer %d of %d under %s, conflicts with %s — resolve by hand; nothing changed", |
| 305 | l.Name, r.index(l)+1, len(r.f.Layers), r.top().Name, x.Below) |
| 306 | case rebase.RecordNotReady: |
| 307 | v := x.Change.Record.ActiveVersion() |
| 308 | return fmt.Sprintf("change %s v%d%s is %s, not ready; nothing pushed. Ready it first: %s", |
| 309 | l.Name, v.N, r.place(l), x.Change.Record.State, readyHint(l.ID, v.Branch)) |
| 310 | case rebase.RecordNewHead: |
| 311 | v := x.Change.Record.ActiveVersion() |
| 312 | return 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", |
| 313 | l.Name, r.place(l), v.N, headOf(v), l.Version, l.Head) |
| 314 | case rebase.RecordChanged: |
| 315 | return fmt.Sprintf("the record of change %s%s changed while it was rebased; nothing pushed. Rebase it again: valley rebase %s", |
| 316 | l.Name, r.place(l), r.top().Name) |
| 317 | case rebase.BranchUnread: |
| 318 | return fmt.Sprintf("could not read %s again against origin's records; nothing pushed", strings.TrimSuffix(r.plainName(l), ",")) |
| 319 | case rebase.BecameChange: |
| 320 | n, h, name := x.Change, x.Handle, r.plainName(l) |
| 321 | v := n.Record.ActiveVersion() |
| 322 | switch { |
| 323 | case n.Record.State != change.Ready: |
| 324 | return fmt.Sprintf("%s is now change %s v%d, which is %s, not ready; nothing pushed. Ready it first: %s", |
| 325 | name, h, v.N, n.Record.State, readyHint(n.ID, v.Branch)) |
| 326 | case headOf(v) != l.Head: |
| 327 | return 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", |
| 328 | name, h, v.N, headOf(v), l.Head) |
| 329 | } |
| 330 | return 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) |
| 331 | } |
| 332 | return x.Error() |
| 333 | } |
| 334 | |
| 335 | // plainName is how a refusal names a plain branch of the stack: as it was |
| 336 | // given, for a branch alone, and otherwise as origin's, with its place. |
| 337 | func (r *rebaseRun) plainName(l *rebase.Layer) string { |
| 338 | if r.single() { |
| 339 | return r.given |
| 340 | } |
| 341 | return "origin/" + l.Branch + r.place(l) |
| 342 | } |
| 343 | |
| 344 | // versionAt names a head of a change by its version too, when its record |
| 345 | // has one there. |
| 346 | func versionAt(l *rebase.Layer, head string) string { |
| 347 | for _, v := range l.Record.Versions { |
| 348 | if headOf(v) == head { |
| 349 | return fmt.Sprintf("v%d, %s", v.N, head) |
| 350 | } |
| 351 | } |
| 352 | return head |
| 353 | } |
| 354 | |
| 355 | // publish says what became of the branch push, adds each change's version |
| 356 | // when the branches moved, and says what became of each layer. |
| 357 | func (r *rebaseRun) publish(eg rebase.Git, pub *rebase.Publication, principal string) error { |
| 358 | top := r.top() |
| 359 | switch pub.Push { |
| 360 | case rebase.Refused: |
| 361 | if r.single() { |
| 362 | return failure(fmt.Sprintf("lease on origin/%s failed (it moved since the fetch); nothing pushed", top.Branch)) |
| 363 | } |
| 364 | r.writeManifest(pub) |
| 365 | var why []string |
| 366 | for _, ref := range pub.Refused { |
| 367 | why = append(why, "origin/"+strings.TrimPrefix(ref.Ref, "refs/heads/")+": "+ref.Why) |
| 368 | } |
| 369 | 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", |
| 370 | strings.Join(why, "; "), top.Name)) |
| 371 | case rebase.Unknown: |
| 372 | r.writeManifest(pub) |
| 373 | var refs []string |
| 374 | for _, l := range r.f.Layers { |
| 375 | refs = append(refs, shellQuote("refs/heads/"+l.Branch)) |
| 376 | } |
| 377 | return failure("the push of the branches failed, and whether origin took it is not known. Check with git ls-remote origin " + |
| 378 | strings.Join(refs, " ") + ": a branch at its new head is pushed, and the command above finishes its change") |
| 379 | case rebase.Unpushed: |
| 380 | return failure("could not run the push of the branches; nothing pushed") |
| 381 | } |
| 382 | if r.single() { |
| 383 | fmt.Fprintf(r.stdout, "rebased %s onto origin/%s, pushed with --force-with-lease\n", top.Branch, recordTarget) |
| 384 | fmt.Fprintf(r.stdout, " head %s\n", pub.Result(top).Head) |
| 385 | } |
| 386 | r.addVersions(eg, pub, principal) |
| 387 | if !r.single() { |
| 388 | r.writeManifest(pub) |
| 389 | } |
| 390 | r.otherStacks() |
| 391 | n := len(unfinished(r.rows(pub))) |
| 392 | switch { |
| 393 | case n > 0 && r.single(): |
| 394 | return errReported |
| 395 | case n == 1: |
| 396 | return failure(fmt.Sprintf("1 change of the stack under %s is unfinished; finish it with the command above", top.Name)) |
| 397 | case n > 0: |
| 398 | return failure(fmt.Sprintf("%d changes of the stack under %s are unfinished; finish each with the command above", n, top.Name)) |
| 399 | } |
| 400 | return nil |
| 401 | } |
| 402 | |
| 403 | // addVersions is phase 2: bottom first, each change's rebased head is |
| 404 | // added to its record as a new version, while the change is still ready |
| 405 | // at the head the rebase began with. A change alone says what ready says, |
| 406 | // as it always has. |
| 407 | func (r *rebaseRun) addVersions(eg rebase.Git, pub *rebase.Publication, principal string) { |
| 408 | for _, res := range pub.Results { |
| 409 | l := res.Layer |
| 410 | if l.ID == "" { |
| 411 | continue |
| 412 | } |
| 413 | pub.Append(context.Background(), eg, principal, res) |
| 414 | if ap := res.Appended; ap.Err != nil { |
| 415 | fmt.Fprintf(r.stderr, "valley: %s\n", ap.Err) |
| 416 | if r.single() { |
| 417 | 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", |
| 418 | l.Branch, res.Head, l.Name, readyCommand(res.Head, l.ID, l.Branch)) |
| 419 | } |
| 420 | } else if r.single() { |
| 421 | fmt.Fprintf(r.stdout, "%s %s v%d ready\n", project(r.g), ap.Handle, ap.Version) |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // rows is the manifest's line for each layer, bottom first. |
| 427 | func (r *rebaseRun) rows(pub *rebase.Publication) []*layer { |
| 428 | var rows []*layer |
| 429 | for _, res := range pub.Results { |
| 430 | l := res.Layer |
| 431 | row := &layer{name: l.Name, id: l.ID, branch: l.Branch, head: l.Head, rebased: res.Head, pushed: pushNot} |
| 432 | switch res.Branch { |
| 433 | case rebase.Pushed: |
| 434 | row.pushed = pushDone |
| 435 | case rebase.Unknown: |
| 436 | row.pushed = pushUnknown |
| 437 | } |
| 438 | if res.Appended != nil { |
| 439 | row.added = res.Appended.Version |
| 440 | } |
| 441 | rows = append(rows, row) |
| 442 | } |
| 443 | return rows |
| 444 | } |
| 445 | |
| 446 | // unfinished is each change layer whose branch may hold its rebased head |
| 447 | // with no version recording it. |
| 448 | func unfinished(layers []*layer) []*layer { |
| 449 | var left []*layer |
| 450 | for _, l := range layers { |
| 451 | if l.id != "" && l.pushed != pushNot && l.added == 0 { |
| 452 | left = append(left, l) |
| 453 | } |
| 454 | } |
| 455 | return left |
| 456 | } |
| 457 | |
| 458 | func (r *rebaseRun) writeManifest(pub *rebase.Publication) { |
| 459 | io.WriteString(r.stdout, manifest(r.top().Name, r.rows(pub))) |
| 460 | } |
| 461 | |
| 462 | // manifest is what the rebase did to each layer, bottom first, one line |
| 463 | // each: its old and new head, what became of its branch, and for a change |
| 464 | // whether a version was added. Then, for each change left unfinished, the |
| 465 | // valley ready command that finishes it. |
| 466 | func manifest(top string, layers []*layer) string { |
| 467 | var b strings.Builder |
| 468 | fmt.Fprintf(&b, "the stack under %s, bottom first:\n", top) |
| 469 | width := 0 |
| 470 | for _, l := range layers { |
| 471 | width = max(width, columns(l.name)) |
| 472 | } |
| 473 | for _, l := range layers { |
| 474 | rebased := "(not rebased)" |
| 475 | if l.rebased != "" { |
| 476 | rebased = short(l.rebased) |
| 477 | } |
| 478 | status := "origin/" + l.branch + " " + l.pushed |
| 479 | switch { |
| 480 | case l.id != "" && l.added > 0: |
| 481 | status += fmt.Sprintf(", v%d added", l.added) |
| 482 | case l.id != "": |
| 483 | status += ", no version added" |
| 484 | } |
| 485 | fmt.Fprintf(&b, " %s%s %s -> %s %s\n", l.name, strings.Repeat(" ", width-columns(l.name)), short(l.head), rebased, status) |
| 486 | } |
| 487 | for _, l := range unfinished(layers) { |
| 488 | fmt.Fprintf(&b, "to finish %s: %s\n", l.name, readyCommand(l.rebased, l.id, l.branch)) |
| 489 | } |
| 490 | return b.String() |
| 491 | } |
| 492 | |
| 493 | // otherStacks names each ready change and plain branch outside the stack |
| 494 | // whose head contains a layer's old head, such as a second stack on a |
| 495 | // lower layer. The rebase left it on that old head, and does not move it. |
| 496 | func (r *rebaseRun) otherStacks() { |
| 497 | ids, branches := map[string]bool{}, map[string]bool{} |
| 498 | for _, l := range r.f.Layers { |
| 499 | ids[l.ID], branches[l.Branch] = true, true |
| 500 | } |
| 501 | on := func(head string) *rebase.Layer { |
| 502 | for k := len(r.f.Layers) - 1; k >= 0; k-- { |
| 503 | if l := r.f.Layers[k]; head == l.Head || r.s.reach(head)[l.Head] { |
| 504 | return l |
| 505 | } |
| 506 | } |
| 507 | return nil |
| 508 | } |
| 509 | for _, n := range r.s.all { |
| 510 | if ids[n.id] || n.record.State != change.Ready || !unlanded(r.s, n) { |
| 511 | continue |
| 512 | } |
| 513 | if l := on(headOf(n.record.ActiveVersion())); l != nil { |
| 514 | h := r.s.o.handle(n.id) |
| 515 | fmt.Fprintf(r.stdout, "%s is now on an old head of %s; rebase it with valley rebase %s\n", h, l.Name, h) |
| 516 | } |
| 517 | } |
| 518 | for _, b := range r.s.unrecorded() { |
| 519 | if l := on(b.Head); l != nil && !branches[b.Branch] { |
| 520 | 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)) |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // readyHint is the valley ready line that readies a change again, naming |
| 526 | // the branch unless its version records none, as bin/valley's ready_hint |
| 527 | // writes it. |
| 528 | func readyHint(id, branch string) string { |
| 529 | if branch == "" { |
| 530 | return "valley ready --change " + id |
| 531 | } |
| 532 | return "valley ready --change " + id + " " + shellQuote(branch) |
| 533 | } |
| 534 | |
| 535 | // readyCommand is the valley ready line that adds head to a change's |
| 536 | // record as a new version on branch. |
| 537 | func readyCommand(head, id, branch string) string { |
| 538 | return "valley ready --head " + head + " --change " + id + " " + shellQuote(branch) |
| 539 | } |
| 540 | |
| 541 | // runBrings prints each change that has not landed, other than the one |
| 542 | // --change names, with a version head among the commits a head would |
| 543 | // bring to main: those it reaches and main does not. One handle a line, |
| 544 | // in the order of the changes' IDs. approve refuses a head that does not |
| 545 | // fast-forward from main and would bring one, since it is a layer of a |
| 546 | // stack that has to be rebased first. |
| 547 | func runBrings(g git, args []string, w io.Writer) error { |
| 548 | const usage = "usage: valley-go brings [--change <id>] <head>" |
| 549 | var except, head string |
| 550 | for i := 0; i < len(args); i++ { |
| 551 | switch { |
| 552 | case args[i] == "--change" && i+1 < len(args): |
| 553 | except = args[i+1] |
| 554 | i++ |
| 555 | case head == "" && isObjectID(args[i]): |
| 556 | head = args[i] |
| 557 | default: |
| 558 | return failure(usage) |
| 559 | } |
| 560 | } |
| 561 | if head == "" { |
| 562 | return failure(usage) |
| 563 | } |
| 564 | if !g.succeeds(true, "cat-file", "-e", head+"^{commit}") { |
| 565 | return failure("no commit " + head + " here") |
| 566 | } |
| 567 | s, err := readSnapshot(g, verifierFromEnv()) |
| 568 | if err != nil { |
| 569 | return err |
| 570 | } |
| 571 | if s.tip == "" { |
| 572 | return failure("origin has no " + recordTarget + " to read against") |
| 573 | } |
| 574 | reach := s.reach(head) |
| 575 | var b strings.Builder |
| 576 | for _, n := range s.all { |
| 577 | if n.id == except || !unlanded(s, n) { |
| 578 | continue |
| 579 | } |
| 580 | for _, v := range n.record.Versions { |
| 581 | if reach[headOf(v)] { |
| 582 | fmt.Fprintln(&b, s.o.handle(n.id)) |
| 583 | break |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | _, err = io.WriteString(w, b.String()) |
| 588 | return err |
| 589 | } |