gunk-dev/the-valley

carry.go

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