gunk-dev/the-valley

carry.go

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