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