gunk-dev/the-valley

stack.go

sourceea258e0e0163main's published tip history
4.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 and main does not reach, whose head the
7// 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 "strconv"
25 "strings"
26 "unicode/utf8"
27)
28
29// runStack prints the layers of a change's stack, bottom first, then the
30// change itself, one line each, of fields separated by tabs:
31//
32// <kind> <name> <base> <head> <state> <title>
33//
34// The kind is change or branch. The name is what review takes: a change's
35// handle, or a branch's name. The base is the head of the layer below, or
36// for the bottom layer its merge base with main. The head of a change is
37// the head its active version records. The state is ready, draft or
38// withdrawn, or - for a branch. The title is last: a change's is its
39// record's, and a branch's is the subject of its first own commit, as
40// pending reads it. No field holds a tab: git refuses control characters
41// in a ref name, and a title's are made spaces. A change with nothing
42// under it prints one line, its own.
43//
44// The change is named as resolve reads a name. Its head is read again
45// from the snapshot the layers are read from, so every line is of one
46// read of origin.
47func runStack(g git, args []string, w io.Writer) error {
48 if len(args) != 1 {
49 return failure("usage: valley-go stack <change>")
50 }
51 if !g.succeeds(true, "rev-parse", "--git-dir") {
52 return failure("not a git repository")
53 }
54 r, err := resolveRecorded(g, args[0])
55 if err != nil {
56 return err
57 }
58 s, err := readSnapshot(g, verifierFromEnv())
59 if err != nil {
60 return err
61 }
62 if s.tip == "" {
63 return failure("origin has no " + recordTarget + " to read a stack against")
64 }
65 layers, err := s.layers(r)
66 if err != nil {
67 return err
68 }
69 var b strings.Builder
70 for _, e := range layers {
71 kind, base := "change", ""
72 if e.id == "" {
73 kind = "branch"
74 }
75 if !utf8.ValidString(e.ref) {
76 return failure(fmt.Sprintf("the stack under %s holds the branch %s, whose name is not UTF-8", args[0], shellQuote(e.ref)))
77 }
78 if e.base != nil {
79 base = e.base.head
80 } else if base, err = s.g.output(true, "merge-base", s.tip, e.head); err != nil {
81 return failure(fmt.Sprintf("%s and %s share no history", e.ref, recordTarget))
82 }
83 fmt.Fprintf(&b, "%s\t%s\t%s\t%s\t%s\t%s\n", kind, e.ref, base, e.head, e.state, e.title)
84 }
85 _, err = io.WriteString(w, b.String())
86 return err
87}
88
89// layers is the change r names and the layers under it, bottom first, each
90// with its base. Only the change and the entries its head contains are
91// stacked, so only they are measured.
92func (s *snapshot) layers(r resolution) ([]*entry, error) {
93 var top *entry
94 if r.id != "" {
95 for _, n := range s.all {
96 if n.id == r.id {
97 top = s.layerOf(n)
98 }
99 }
100 if top == nil {
101 return nil, failure("origin no longer holds a record of change " + r.handle)
102 }
103 } else {
104 head := s.o.refs["refs/heads/"+r.branch]
105 if head == "" {
106 return nil, failure("no such branch on origin: " + r.branch)
107 }
108 top = &entry{section: "branches", ref: r.branch, head: head, state: "-"}
109 }
110 all := []*entry{top}
111 for _, n := range s.all {
112 if n.id != top.id && unlanded(s, n) && s.strictAncestor(headOf(n.record.ActiveVersion()), top.head) {
113 all = append(all, s.layerOf(n))
114 }
115 }
116 for _, b := range s.unrecorded() {
117 if b.Branch != top.ref && s.strictAncestor(b.Head, top.head) {
118 all = append(all, &entry{section: "branches", ref: b.Branch, head: b.Head, state: "-"})
119 }
120 }
121 for _, e := range all {
122 out, _ := s.g.output(true, "log", "-1", "--format=%ct", e.head)
123 e.when, _ = strconv.ParseInt(out, 10, 64)
124 }
125 s.stack(all)
126 var chain []*entry
127 for e := top; e != nil; e = e.base {
128 chain = append([]*entry{e}, chain...)
129 }
130 return chain, nil
131}
132
133// layerOf is a change as a layer: its handle, its active version's head
134// and title, and its state. Its section is its state: what measure reads
135// a section for is whether the entry is a branch.
136func (s *snapshot) layerOf(n recorded) *entry {
137 v := n.record.ActiveVersion()
138 return &entry{
139 section: n.record.State, ref: s.o.handle(n.id), id: n.id, state: n.record.State,
140 head: headOf(v), title: readable(v.Title),
141 }
142}