gunk-dev/the-valley

carry.go

source1ca8baacb603main'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 _, err = fmt.Fprintf(w, "%s\t%s\n%s", verdict, reason, c.text)
75 return err
76}
77
78// carryBetween sets version to of the change h holds against version
79// from, at main's tip as h read it, and says whether an answer about from
80// carries to it, and why (carryVerdict). model is the policy at that tip,
81// read only when the change's carry mode decides.
82func carryBetween(g git, h *history, from, to int, model func() (*composed.Model, error)) (c comparison, verdict, reason, offer string, err error) {
83 if to > len(h.versions) {
84 return c, "", "", "", failure(fmt.Sprintf("change %s has no v%d", h.handle, to))
85 }
86 if h.tip == "" {
87 return c, "", "", "", failure("origin has no main to compare the versions against")
88 }
89 if err := fetch(g, h.tip, h.head(from), h.head(to)); err != nil {
90 return c, "", "", "", err
91 }
92 if c, err = compareWith(g, h, h.tip, reference{version: from}, to, false); err != nil {
93 return c, "", "", "", err
94 }
95 verdict, reason, offer, err = carryVerdict(g, c, from, to, model)
96 return c, verdict, reason, offer, err
97}
98
99// tipModel is the policy at main's tip, as carry reads it on its own. A
100// carry verdict reads the classes and carry mode alone, so it asks cue no
101// requirement's pin.
102func tipModel(g git, tip string) (*composed.Model, error) {
103 instance, project, done, err := policyLayers(g, tip)
104 defer done()
105 if err != nil {
106 return nil, failure(fmt.Sprintf("the policy at main did not read: %v", err))
107 }
108 schema, err := schemaFile(g, "")
109 if err != nil {
110 return nil, err
111 }
112 return policyModel(schema, instance, project, false)
113}
114
115// carryVerdict says whether an answer about version from carries to
116// version to, which c compared with it, and why, and for review, when the
117// later version is offered for approval in place.
118func carryVerdict(g git, c comparison, from, to int, model func() (*composed.Model, error)) (verdict, reason, offer string, err error) {
119 switch {
120 case c.kind == compareNone:
121 return "review", fmt.Sprintf("it could not be set against v%d", from), "none", nil
122 case c.kind == compareChanged && c.verb == "rebased":
123 return "review", rebasedReason(c, from), "approve", nil
124 case c.kind == compareChanged:
125 return "review", editedReason(c, from), "view", nil
126 case c.meta:
127 return "review", "its commit messages or authors differ", "view", nil
128 case len(c.landings) == 1:
129 return "carries", fmt.Sprintf("v%d makes the same changes as v%d on the same main", to, from), "", nil
130 }
131 mode, shared, err := carryMode(g, c, model)
132 if err != nil {
133 return "", "", "", err
134 }
135 if !carryAllows(mode, shared) {
136 return "review", fmt.Sprintf("a restack; carry mode %s holds it over what landed since", mode), "approve", nil
137 }
138 return "carries", fmt.Sprintf("v%d is v%d rebased onto the new main with no other changes", to, from), "", nil
139}
140
141// rebasedReason says that a rebase changed the change's files and not its
142// commits, and names the files main also edited, the first three. A path
143// is quoted when it holds what is not printable, so that it can neither
144// split review's tab-separated line nor drive the terminal.
145func rebasedReason(c comparison, from int) string {
146 commits := "its commit is"
147 if c.has != 1 {
148 commits = fmt.Sprintf("its %d commits are", c.has)
149 }
150 files := c.mainEdited
151 if len(files) == 0 {
152 return fmt.Sprintf("rebased onto main; %s unchanged, but its file changes differ from v%d's", commits, from)
153 }
154 quoted := make([]string, min(len(files), 3))
155 for i := range quoted {
156 quoted[i] = quotedPath(files[i])
157 }
158 named := strings.Join(quoted, ", ")
159 if len(files) > 3 {
160 named += ", …"
161 }
162 return fmt.Sprintf("rebased onto main; %s unchanged, but main also edited %d of its files (%s)", commits, len(files), named)
163}
164
165// editedReason says how many of the change's commits changed.
166func editedReason(c comparison, from int) string {
167 switch {
168 case c.had != c.has:
169 return fmt.Sprintf("its commits changed (%s, where v%d had %d)", counted(c.has, "commit"), from, c.had)
170 case c.has == 1:
171 return "its commit changed"
172 }
173 return fmt.Sprintf("its commits changed (%d of %d)", c.edited, c.has)
174}
175
176// carryMode is the change's carry mode under the policy at main's tip,
177// which model reads, and whether a landing between the two bases touched
178// a path in a class the change touches.
179func carryMode(g git, c comparison, policy func() (*composed.Model, error)) (mode string, shared bool, err error) {
180 model, err := policy()
181 if err != nil {
182 return "", false, err
183 }
184 ctx := context.Background()
185 paths, err := derive.Diff(ctx, g.dir, g.env, c.base, c.head)
186 if err != nil {
187 return "", false, failure(err.Error())
188 }
189 own, err := derive.Derive(model, paths)
190 if err != nil {
191 return "", false, failure(err.Error())
192 }
193 if own.Carry != "same-class" {
194 return own.Carry, false, nil
195 }
196 var landed []string
197 for i := 0; i+1 < len(c.landings); i++ {
198 p, err := derive.Diff(ctx, g.dir, g.env, c.landings[i+1], c.landings[i])
199 if err != nil {
200 return "", false, failure(err.Error())
201 }
202 landed = append(landed, p...)
203 }
204 theirs, err := derive.Derive(model, landed)
205 if err != nil {
206 return "", false, failure(err.Error())
207 }
208 classes := map[string]bool{}
209 for _, class := range own.Classes {
210 classes[class] = true
211 }
212 for _, class := range theirs.Classes {
213 if classes[class] {
214 return own.Carry, true, nil
215 }
216 }
217 return own.Carry, false, nil
218}
219
220// carryAllows says whether a change's carry mode lets an answer carry
221// over at least one landing, given whether a landing touched one of the
222// change's classes.
223func carryAllows(mode string, shared bool) bool {
224 switch mode {
225 case "same-paths":
226 return true
227 case "same-class":
228 return !shared
229 }
230 return false
231}