| 1 | // valley stack: the layers of one change's stack, for valley review, |
| 2 | // which reviews a stack one layer at a time, bottom first. |
| 3 | // |
| 4 | // The layers are what valley pending lists under the change (pending.go): |
| 5 | // each change whose active version has not landed, and each branch on |
| 6 | // origin that no record is on, that main does not reach and that no landed |
| 7 | // version's head reaches, whose head the change's head strictly contains. A ready change, a draft and a branch |
| 8 | // are layers, as in pending. A withdrawn change that has not landed is a |
| 9 | // layer too, where pending names it only in an includes line: the |
| 10 | // integrator refuses to land anything above it, and review warns of that. |
| 11 | // |
| 12 | // Each layer is on its base, the nearest layer its head contains, as |
| 13 | // pending reads a base. The layers printed are the chain of bases from the |
| 14 | // change down. Linear history makes every layer under the change part of |
| 15 | // that chain. A layer off the chain, as at a fork, is not printed, and its |
| 16 | // commits are in the diff of the first layer on the chain that contains |
| 17 | // them. |
| 18 | |
| 19 | package main |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "strings" |
| 25 | "unicode/utf8" |
| 26 | |
| 27 | "the-valley/rebase" |
| 28 | ) |
| 29 | |
| 30 | // runStack prints the layers of a change's stack, bottom first, then the |
| 31 | // change itself, one line each, of fields separated by tabs: |
| 32 | // |
| 33 | // <kind> <name> <base> <head> <state> <approved> <title> |
| 34 | // |
| 35 | // The kind is change or branch. The name is what review takes: a change's |
| 36 | // handle, or a branch's name. The base is the head of the layer below, or |
| 37 | // for the bottom layer its merge base with main. The head of a change is |
| 38 | // the head its active version records. The state is ready, draft or |
| 39 | // withdrawn, or - for a branch. The approved field is approved for a ready |
| 40 | // change whose active version has an integration request at its head |
| 41 | // (approvedVersion), and - for any other layer. The title is last: a change's is its |
| 42 | // record's, and a branch's is the subject of its first own commit, as |
| 43 | // pending reads it. No field holds a tab: git refuses control characters |
| 44 | // in a ref name, and a title's are made spaces. A change with nothing |
| 45 | // under it prints one line, its own. |
| 46 | // |
| 47 | // The change is named as resolve reads a name. Its head is read again |
| 48 | // from the snapshot the layers are read from, so every line is of one |
| 49 | // read of origin. The layers are the chain valley rebase moves, as the |
| 50 | // rebase engine captures it (rebase.Capture). |
| 51 | func runStack(g git, args []string, w io.Writer) error { |
| 52 | if len(args) != 1 { |
| 53 | return failure("usage: valley stack <change>") |
| 54 | } |
| 55 | if !g.succeeds(true, "rev-parse", "--git-dir") { |
| 56 | return failure("not a git repository") |
| 57 | } |
| 58 | layers, err := stackLayers(g, newVerifier(), args[0]) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | var b strings.Builder |
| 63 | for _, l := range layers { |
| 64 | fmt.Fprintf(&b, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", l.kind, l.name, l.base, l.head, l.state, l.approved, l.title) |
| 65 | } |
| 66 | _, err = io.WriteString(w, b.String()) |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | // stackLayer is one line runStack prints. |
| 71 | type stackLayer struct { |
| 72 | kind, name, base, head, state, approved, title string |
| 73 | } |
| 74 | |
| 75 | // stackLayers is the layers runStack prints of the change named, bottom |
| 76 | // first, the change itself last, read with v. |
| 77 | func stackLayers(g git, v verifier, name string) ([]stackLayer, error) { |
| 78 | r, err := resolveRecorded(g, name) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | s, err := readSnapshot(g, v) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | if s.tip == "" { |
| 87 | return nil, failure("origin has no " + recordTarget + " to read a stack against") |
| 88 | } |
| 89 | f, err := rebase.Capture(s.land, rebase.Selection{ID: r.id, Branch: r.branch}) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | var layers []stackLayer |
| 94 | for _, l := range f.Layers { |
| 95 | if !utf8.ValidString(l.Name) { |
| 96 | return nil, failure(fmt.Sprintf("the stack under %s holds the branch %s, whose name is not UTF-8", name, shellQuote(l.Name))) |
| 97 | } |
| 98 | var base string |
| 99 | if l.Below != nil { |
| 100 | base = l.Below.Head |
| 101 | } else if base, err = s.g.output(true, "merge-base", s.tip, l.Head); err != nil { |
| 102 | return nil, failure(fmt.Sprintf("%s and %s share no history", l.Name, recordTarget)) |
| 103 | } |
| 104 | kind, state, approved, title := "branch", "-", "-", "" |
| 105 | if l.ID != "" { |
| 106 | kind, state, title = "change", l.Record.State, readable(l.Record.ActiveVersion().Title) |
| 107 | if s.approvedVersion(l.ID, l.Record) { |
| 108 | approved = "approved" |
| 109 | } |
| 110 | } else { |
| 111 | not := []string{"^" + s.tip} |
| 112 | if l.Below != nil { |
| 113 | not = []string{"^" + l.Below.Head, "^" + s.tip} |
| 114 | } |
| 115 | title = s.branchTitle(l.Head, not) |
| 116 | } |
| 117 | layers = append(layers, stackLayer{kind: kind, name: l.Name, base: base, head: l.Head, state: state, approved: approved, title: title}) |
| 118 | } |
| 119 | return layers, nil |
| 120 | } |