gunk-dev/the-valley

stack.go

source85e8f9abb273main's published tip history
3.6 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 "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> <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 title is last: a change's is its
40// record's, and a branch's is the subject of its first own commit, as
41// pending reads it. No field holds a tab: git refuses control characters
42// in a ref name, and a title's are made spaces. A change with nothing
43// under it prints one line, its own.
44//
45// The change is named as resolve reads a name. Its head is read again
46// from the snapshot the layers are read from, so every line is of one
47// read of origin. The layers are the chain valley rebase moves, as the
48// rebase engine captures it (rebase.Capture).
49func runStack(g git, args []string, w io.Writer) error {
50 if len(args) != 1 {
51 return failure("usage: valley-go stack <change>")
52 }
53 if !g.succeeds(true, "rev-parse", "--git-dir") {
54 return failure("not a git repository")
55 }
56 r, err := resolveRecorded(g, args[0])
57 if err != nil {
58 return err
59 }
60 s, err := readSnapshot(g, newVerifier())
61 if err != nil {
62 return err
63 }
64 if s.tip == "" {
65 return failure("origin has no " + recordTarget + " to read a stack against")
66 }
67 f, err := rebase.Capture(s.land, rebase.Selection{ID: r.id, Branch: r.branch})
68 if err != nil {
69 return err
70 }
71 var b strings.Builder
72 for _, l := range f.Layers {
73 if !utf8.ValidString(l.Name) {
74 return failure(fmt.Sprintf("the stack under %s holds the branch %s, whose name is not UTF-8", args[0], shellQuote(l.Name)))
75 }
76 var base string
77 if l.Below != nil {
78 base = l.Below.Head
79 } else if base, err = s.g.output(true, "merge-base", s.tip, l.Head); err != nil {
80 return failure(fmt.Sprintf("%s and %s share no history", l.Name, recordTarget))
81 }
82 kind, state, title := "branch", "-", ""
83 if l.ID != "" {
84 kind, state, title = "change", l.Record.State, readable(l.Record.ActiveVersion().Title)
85 } else {
86 not := []string{"^" + s.tip}
87 if l.Below != nil {
88 not = []string{"^" + l.Below.Head, "^" + s.tip}
89 }
90 title = s.branchTitle(l.Head, not)
91 }
92 fmt.Fprintf(&b, "%s\t%s\t%s\t%s\t%s\t%s\n", kind, l.Name, base, l.Head, state, title)
93 }
94 _, err = io.WriteString(w, b.String())
95 return err
96}