gunk-dev/the-valley

carry.go

source87ce0621d5ddmain's published tip history
5.0 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 (bin/valley's moved). It is information only: it files
4// no request, and the integrator judges what approve files as it judges
5// any request.
6//
7// The later version is set against the version shown as compare sets a
8// version against its approved one (compare.go), with main's tip on
9// origin. The answer carries when the two have the same file delta, the
10// same commit messages and authors, and the change's carry mode allows
11// the landings between their bases: exact allows none, same-class none
12// that touched a path in a class the change touches, and same-paths any.
13// The mode is derived as the integrator derives it, under the policy at
14// main's tip (derive.Derive).
15//
16// The output is one line, the verdict, carries or review, and a short
17// reason, separated by a tab, and then the comparison's text, which
18// review pages when it asks the operator to look again.
19
20package main
21
22import (
23 "context"
24 "fmt"
25 "io"
26 "strconv"
27
28 "the-valley/change"
29 "the-valley/derive"
30)
31
32const carryUsage = "usage: valley-go carry <change ID> <version shown> <later version>"
33
34func runCarry(g git, args []string, w io.Writer) error {
35 var n [2]int
36 if len(args) != 3 || !change.ValidID(args[0]) {
37 return failure(carryUsage)
38 }
39 for i, s := range args[1:] {
40 v, err := strconv.Atoi(s)
41 if err != nil || v < 1 || strconv.Itoa(v) != s {
42 return failure(carryUsage)
43 }
44 n[i] = v
45 }
46 from, to := n[0], n[1]
47 if from >= to {
48 return failure(carryUsage)
49 }
50 if !g.succeeds(true, "rev-parse", "--git-dir") {
51 return failure("not a git repository")
52 }
53 h, err := readHistory(g, args[0])
54 if err != nil {
55 return err
56 }
57 if to > len(h.versions) {
58 return failure(fmt.Sprintf("change %s has no v%d", h.handle, to))
59 }
60 if h.tip == "" {
61 return failure("origin has no main to compare the versions against")
62 }
63 if err := fetch(g, h.tip, h.head(from), h.head(to)); err != nil {
64 return err
65 }
66 c, err := compareWith(g, h, h.tip, reference{version: from}, to)
67 if err != nil {
68 return err
69 }
70 verdict, reason, err := carryVerdict(g, h.tip, c, from)
71 if err != nil {
72 return err
73 }
74 _, err = fmt.Fprintf(w, "%s\t%s\n%s", verdict, reason, c.text)
75 return err
76}
77
78// carryVerdict says whether an answer about version from carries to the
79// version c compared with it, and why.
80func carryVerdict(g git, tip string, c comparison, from int) (verdict, reason string, err error) {
81 switch {
82 case c.kind == compareNone:
83 return "review", fmt.Sprintf("it could not be set against v%d", from), nil
84 case c.kind == compareChanged:
85 return "review", fmt.Sprintf("its changes differ from v%d's", from), nil
86 case c.meta:
87 return "review", fmt.Sprintf("its commit messages or authors differ from v%d's", from), nil
88 case len(c.landings) == 1:
89 return "carries", fmt.Sprintf("same changes as v%d", from), nil
90 }
91 mode, shared, err := carryMode(g, tip, c)
92 if err != nil {
93 return "", "", err
94 }
95 if !carryAllows(mode, shared) {
96 return "review", fmt.Sprintf("a restack of v%d, and carry mode %s holds it over what landed since", from, mode), nil
97 }
98 return "carries", fmt.Sprintf("a restack of v%d; same changes", from), nil
99}
100
101// carryMode is the change's carry mode under the policy at main's tip,
102// and whether a landing between the two bases touched a path in a class
103// the change touches.
104func carryMode(g git, tip string, c comparison) (mode string, shared bool, err error) {
105 instance, project, done, err := policyLayers(g, tip)
106 defer done()
107 if err != nil {
108 return "", false, failure(fmt.Sprintf("the policy at main did not read: %v", err))
109 }
110 schema, err := schemaFile(g, "")
111 if err != nil {
112 return "", false, err
113 }
114 model, err := policyModel(schema, instance, project, true)
115 if err != nil {
116 return "", false, err
117 }
118 ctx := context.Background()
119 paths, err := derive.Diff(ctx, g.dir, g.env, c.base, c.head)
120 if err != nil {
121 return "", false, failure(err.Error())
122 }
123 own, err := derive.Derive(model, paths)
124 if err != nil {
125 return "", false, failure(err.Error())
126 }
127 if own.Carry != "same-class" {
128 return own.Carry, false, nil
129 }
130 var landed []string
131 for i := 0; i+1 < len(c.landings); i++ {
132 p, err := derive.Diff(ctx, g.dir, g.env, c.landings[i+1], c.landings[i])
133 if err != nil {
134 return "", false, failure(err.Error())
135 }
136 landed = append(landed, p...)
137 }
138 theirs, err := derive.Derive(model, landed)
139 if err != nil {
140 return "", false, failure(err.Error())
141 }
142 classes := map[string]bool{}
143 for _, class := range own.Classes {
144 classes[class] = true
145 }
146 for _, class := range theirs.Classes {
147 if classes[class] {
148 return own.Carry, true, nil
149 }
150 }
151 return own.Carry, false, nil
152}
153
154// carryAllows says whether a change's carry mode lets an answer carry
155// over at least one landing, given whether a landing touched one of the
156// change's classes.
157func carryAllows(mode string, shared bool) bool {
158 switch mode {
159 case "same-paths":
160 return true
161 case "same-class":
162 return !shared
163 }
164 return false
165}