gunk-dev/the-valley

carry.go

source0d275c6b8a51main's published tip history
8.1 KiB
1// carry: whether review's answer about the version of a change
2// it showed carries to a later version the change gained while the
3// review was open (review.go's moved). It is information only: it files
4// no request, and the integrator judges what approve files as it judges
5// any request. valley pending reads a request at an earlier version's
6// head through the same rules (carriedApproval, pending.go).
7//
8// The later version is set against the version shown as compare sets a
9// version against its approved one (compare.go), with main's tip on
10// origin. The answer carries when the two have the same file delta, the
11// same commit messages and authors, and the change's carry mode allows
12// the landings between their bases: exact allows none, same-class none
13// that touched a path in a class the change touches, and same-paths any.
14// The mode is derived as the integrator derives it, under the policy at
15// main's tip (derive.Derive).
16//
17// The output is one line, the verdict, carries or review, and a short
18// reason, separated by a tab, and then the comparison's text, which
19// review pages when it asks the operator to look again. A reason is one
20// plain clause about the later version: for carries, why the answer
21// covers it. A review line has a third field, when review offers to
22// approve the later version in place: approve, at once, since the
23// change's commits are unchanged; view, once the operator has paged the
24// comparison, since a commit changed; or none, when the versions could
25// not be set side by side.
26
27package main
28
29import (
30 "context"
31 "fmt"
32 "io"
33 "strconv"
34 "strings"
35
36 "the-valley/change"
37 "the-valley/composed"
38 "the-valley/derive"
39)
40
41// carryUsage is what runCarry, which review calls, takes.
42const carryUsage = "carry takes <change ID> <version shown> <later version>"
43
44func runCarry(g git, args []string, w io.Writer) error {
45 var n [2]int
46 if len(args) != 3 || !change.ValidID(args[0]) {
47 return failure(carryUsage)
48 }
49 for i, s := range args[1:] {
50 v, err := strconv.Atoi(s)
51 if err != nil || v < 1 || strconv.Itoa(v) != s {
52 return failure(carryUsage)
53 }
54 n[i] = v
55 }
56 from, to := n[0], n[1]
57 if from >= to {
58 return failure(carryUsage)
59 }
60 if !g.succeeds(true, "rev-parse", "--git-dir") {
61 return failure("not a git repository")
62 }
63 h, err := readHistory(g, args[0])
64 if err != nil {
65 return err
66 }
67 c, verdict, reason, offer, err := carryBetween(g, h, from, to, func() (*composed.Model, error) { return tipModel(g, h.tip) })
68 if err != nil {
69 return err
70 }
71 if offer != "" {
72 reason += "\t" + offer
73 }
74 text, err := c.page()
75 if err != nil {
76 return err
77 }
78 _, err = fmt.Fprintf(w, "%s\t%s\n%s", verdict, reason, text)
79 return err
80}
81
82// carryBetween sets version to of the change h holds against version
83// from, at main's tip as h read it, and says whether an answer about from
84// carries to it, and why (carryVerdict). model is the policy at that tip,
85// read only when the change's carry mode decides.
86func carryBetween(g git, h *history, from, to int, model func() (*composed.Model, error)) (c comparison, verdict, reason, offer string, err error) {
87 if to > len(h.versions) {
88 return c, "", "", "", failure(fmt.Sprintf("change %s has no v%d", h.handle, to))
89 }
90 if h.tip == "" {
91 return c, "", "", "", failure("origin has no main to compare the versions against")
92 }
93 if err := fetch(g, h.tip, h.head(from), h.head(to)); err != nil {
94 return c, "", "", "", err
95 }
96 if c, err = compareWith(g, h, h.tip, reference{version: from}, to, false); err != nil {
97 return c, "", "", "", err
98 }
99 verdict, reason, offer, err = carryVerdict(g, c, from, to, model)
100 return c, verdict, reason, offer, err
101}
102
103// tipModel is the policy at main's tip, as carry reads it on its own. A
104// carry verdict reads the classes and carry mode alone, so it asks cue no
105// requirement's pin.
106func tipModel(g git, tip string) (*composed.Model, error) {
107 instance, project, done, err := policyLayers(g, tip)
108 defer done()
109 if err != nil {
110 return nil, failure(fmt.Sprintf("the policy at main did not read: %v", err))
111 }
112 schema, err := schemaFile(g, "")
113 if err != nil {
114 return nil, err
115 }
116 return policyModel(schema, instance, project, false)
117}
118
119// carryVerdict says whether an answer about version from carries to
120// version to, which c compared with it, and why, and for review, when the
121// later version is offered for approval in place.
122func carryVerdict(g git, c comparison, from, to int, model func() (*composed.Model, error)) (verdict, reason, offer string, err error) {
123 switch {
124 case c.kind == compareNone:
125 return "review", fmt.Sprintf("it could not be set against v%d", from), "none", nil
126 case c.kind == compareChanged && c.verb == "rebased":
127 return "review", rebasedReason(c, from), "approve", nil
128 case c.kind == compareChanged:
129 return "review", editedReason(c, from), "view", nil
130 case c.meta:
131 return "review", "its commit messages or authors differ", "view", nil
132 case len(c.landings) == 1:
133 return "carries", fmt.Sprintf("v%d makes the same changes as v%d on the same main", to, from), "", nil
134 }
135 mode, shared, err := carryMode(g, c, model)
136 if err != nil {
137 return "", "", "", err
138 }
139 if !carryAllows(mode, shared) {
140 return "review", fmt.Sprintf("a restack; carry mode %s holds it over what landed since", mode), "approve", nil
141 }
142 return "carries", fmt.Sprintf("v%d is v%d rebased onto the new main with no other changes", to, from), "", nil
143}
144
145// rebasedReason says that a rebase changed the change's files and not its
146// commits, and names the files main also edited, the first three. A path
147// is quoted when it holds what is not printable, so that it can neither
148// split review's tab-separated line nor drive the terminal.
149func rebasedReason(c comparison, from int) string {
150 commits := "its commit is"
151 if c.has != 1 {
152 commits = fmt.Sprintf("its %d commits are", c.has)
153 }
154 files := c.mainEdited
155 if len(files) == 0 {
156 return fmt.Sprintf("rebased onto main; %s unchanged, but its file changes differ from v%d's", commits, from)
157 }
158 quoted := make([]string, min(len(files), 3))
159 for i := range quoted {
160 quoted[i] = quotedPath(files[i])
161 }
162 named := strings.Join(quoted, ", ")
163 if len(files) > 3 {
164 named += ", …"
165 }
166 return fmt.Sprintf("rebased onto main; %s unchanged, but main also edited %d of its files (%s)", commits, len(files), named)
167}
168
169// editedReason says how many of the change's commits changed.
170func editedReason(c comparison, from int) string {
171 switch {
172 case c.had != c.has:
173 return fmt.Sprintf("its commits changed (%s, where v%d had %d)", counted(c.has, "commit"), from, c.had)
174 case c.has == 1:
175 return "its commit changed"
176 }
177 return fmt.Sprintf("its commits changed (%d of %d)", c.edited, c.has)
178}
179
180// carryMode is the change's carry mode under the policy at main's tip,
181// which model reads, and whether a landing between the two bases touched
182// a path in a class the change touches.
183func carryMode(g git, c comparison, policy func() (*composed.Model, error)) (mode string, shared bool, err error) {
184 model, err := policy()
185 if err != nil {
186 return "", false, err
187 }
188 ctx := context.Background()
189 paths, err := derive.Diff(ctx, g.dir, g.env, c.base, c.head)
190 if err != nil {
191 return "", false, failure(err.Error())
192 }
193 own, err := derive.Derive(model, paths)
194 if err != nil {
195 return "", false, failure(err.Error())
196 }
197 if own.Carry != "same-class" {
198 return own.Carry, false, nil
199 }
200 var landed []string
201 for i := 0; i+1 < len(c.landings); i++ {
202 p, err := derive.Diff(ctx, g.dir, g.env, c.landings[i+1], c.landings[i])
203 if err != nil {
204 return "", false, failure(err.Error())
205 }
206 landed = append(landed, p...)
207 }
208 theirs, err := derive.Derive(model, landed)
209 if err != nil {
210 return "", false, failure(err.Error())
211 }
212 classes := map[string]bool{}
213 for _, class := range own.Classes {
214 classes[class] = true
215 }
216 for _, class := range theirs.Classes {
217 if classes[class] {
218 return own.Carry, true, nil
219 }
220 }
221 return own.Carry, false, nil
222}
223
224// carryAllows says whether a change's carry mode lets an answer carry
225// over at least one landing, given whether a landing touched one of the
226// change's classes.
227func carryAllows(mode string, shared bool) bool {
228 switch mode {
229 case "same-paths":
230 return true
231 case "same-class":
232 return !shared
233 }
234 return false
235}