gunk-dev/the-valley

stack.go

sourcec1620ef8763amain's published tip history
3.9 KiB
1// valley-go 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
19package main
20
21import (
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, as pending reads
41// it (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).
51func runStack(g git, args []string, w io.Writer) error {
52 if len(args) != 1 {
53 return failure("usage: valley-go stack <change>")
54 }
55 if !g.succeeds(true, "rev-parse", "--git-dir") {
56 return failure("not a git repository")
57 }
58 r, err := resolveRecorded(g, args[0])
59 if err != nil {
60 return err
61 }
62 s, err := readSnapshot(g, newVerifier())
63 if err != nil {
64 return err
65 }
66 if s.tip == "" {
67 return failure("origin has no " + recordTarget + " to read a stack against")
68 }
69 f, err := rebase.Capture(s.land, rebase.Selection{ID: r.id, Branch: r.branch})
70 if err != nil {
71 return err
72 }
73 var b strings.Builder
74 for _, l := range f.Layers {
75 if !utf8.ValidString(l.Name) {
76 return failure(fmt.Sprintf("the stack under %s holds the branch %s, whose name is not UTF-8", args[0], shellQuote(l.Name)))
77 }
78 var base string
79 if l.Below != nil {
80 base = l.Below.Head
81 } else if base, err = s.g.output(true, "merge-base", s.tip, l.Head); err != nil {
82 return failure(fmt.Sprintf("%s and %s share no history", l.Name, recordTarget))
83 }
84 kind, state, approved, title := "branch", "-", "-", ""
85 if l.ID != "" {
86 kind, state, title = "change", l.Record.State, readable(l.Record.ActiveVersion().Title)
87 if s.approvedVersion(l.ID, l.Record) {
88 approved = "approved"
89 }
90 } else {
91 not := []string{"^" + s.tip}
92 if l.Below != nil {
93 not = []string{"^" + l.Below.Head, "^" + s.tip}
94 }
95 title = s.branchTitle(l.Head, not)
96 }
97 fmt.Fprintf(&b, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", kind, l.Name, base, l.Head, state, approved, title)
98 }
99 _, err = io.WriteString(w, b.String())
100 return err
101}