| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/base64" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | "unicode" |
| 11 | "unicode/utf8" |
| 12 | |
| 13 | "the-valley/change" |
| 14 | "the-valley/rebase" |
| 15 | ) |
| 16 | |
| 17 | // A family's reports are cut from one observation of it: everything the |
| 18 | // attempt read and what came of it, in full. The journal keeps the |
| 19 | // observation whole. Each recorded change in the family gets a report of |
| 20 | // its own, in the format change/report.go defines, about every recorded |
| 21 | // change in the family, bottom first. A plain branch is no report layer. |
| 22 | // |
| 23 | // A report holds what the format allows and no more: 64 layers, 100 |
| 24 | // conflict paths of at most 1024 bytes of UTF-8, 64 blocking requests |
| 25 | // whose refs are at most 256 bytes, a refusal of at most 2048 bytes, no |
| 26 | // control character in any of them, and 64 KiB of JSON in all. When the |
| 27 | // observation holds more, the report is a fallback. It keeps what fits, in |
| 28 | // the observation's order: |
| 29 | // |
| 30 | // - when the family has more than 64 recorded changes, only the change |
| 31 | // the report is about and the layer that failed; |
| 32 | // - each conflict path and each blocking request that fits on its own, |
| 33 | // up to the format's count; |
| 34 | // - its refusal, with each run of control characters made one space, |
| 35 | // cut to fit. |
| 36 | // |
| 37 | // Its refusal then ends by saying what it leaves out, and naming the |
| 38 | // digest of the whole observation the journal keeps. Two fallbacks that |
| 39 | // show the same are told apart by that digest. If the report is still more |
| 40 | // than 64 KiB, it sheds its conflict paths, then its blocking requests, |
| 41 | // then every layer but the change's own and the failing one. |
| 42 | |
| 43 | // Observation is a family as one attempt saw it, in full. |
| 44 | type Observation struct { |
| 45 | Action string `json:"action"` // the planner's: restack or hold |
| 46 | Result string `json:"result"` // what the reports say came of it: complete, conflict or blocked |
| 47 | Layers []ObservedLayer `json:"layers"` // every layer, plain branches included, bottom first |
| 48 | Requests []RequestPlan `json:"requests"` // every related request the planner considered |
| 49 | Reasons []string `json:"reasons"` // why the planner held it |
| 50 | Notes []string `json:"notes"` // what the planner noted |
| 51 | Failing string `json:"failing_layer"` // the change ID of the layer that failed; "" for none |
| 52 | Conflicts rawPaths `json:"conflict_paths_base64"` // every path that conflicted, as git gave it |
| 53 | Refusal string `json:"refusal"` // why nothing was published, in full; "" for nothing |
| 54 | } |
| 55 | |
| 56 | // rawPaths are file names as git gives them: bytes that need not be |
| 57 | // UTF-8. JSON holds only UTF-8, and json.Marshal would write each byte |
| 58 | // that is not as U+FFFD, so a path such as a Latin-1 file name would not |
| 59 | // read back as git gave it. The journal therefore keeps each path in |
| 60 | // standard base64, which reads back byte for byte. A report takes only |
| 61 | // the paths that are UTF-8 as they stand (bound). |
| 62 | type rawPaths []string |
| 63 | |
| 64 | func (ps rawPaths) MarshalJSON() ([]byte, error) { |
| 65 | var encoded []string |
| 66 | if ps != nil { |
| 67 | encoded = make([]string, len(ps)) |
| 68 | } |
| 69 | for i, p := range ps { |
| 70 | encoded[i] = base64.StdEncoding.EncodeToString([]byte(p)) |
| 71 | } |
| 72 | return json.Marshal(encoded) |
| 73 | } |
| 74 | |
| 75 | func (ps *rawPaths) UnmarshalJSON(data []byte) error { |
| 76 | var encoded []string |
| 77 | if err := json.Unmarshal(data, &encoded); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | var out rawPaths |
| 81 | if encoded != nil { |
| 82 | out = make(rawPaths, len(encoded)) |
| 83 | } |
| 84 | for i, e := range encoded { |
| 85 | p, err := base64.StdEncoding.Strict().DecodeString(e) |
| 86 | if err != nil { |
| 87 | return fmt.Errorf("conflict path %d is not in base64: %w", i, err) |
| 88 | } |
| 89 | out[i] = string(p) |
| 90 | } |
| 91 | *ps = out |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | // ObservedLayer is one layer as the attempt read it. |
| 96 | type ObservedLayer struct { |
| 97 | Name string `json:"name"` // a change's handle, or a plain branch's name |
| 98 | ID string `json:"change_id"` // "" for a plain branch |
| 99 | Branch string `json:"branch"` // the branch a rebase pushes |
| 100 | BranchTip string `json:"branch_tip"` // that branch's tip on origin; "" when origin had none |
| 101 | Record string `json:"record"` // the record commit read; "" for a plain branch |
| 102 | Version int `json:"version"` // the active version; 0 for a plain branch |
| 103 | Head string `json:"head"` // the head rebased |
| 104 | State string `json:"state"` |
| 105 | Below string `json:"below"` // the layer it sits on, by name; "" for the bottom |
| 106 | } |
| 107 | |
| 108 | // observe is the observation of a family the planner judged, before |
| 109 | // anything is tried. |
| 110 | func observe(f *rebase.Family, fp FamilyPlan) Observation { |
| 111 | o := Observation{Action: fp.Action, Requests: fp.Requests, Reasons: fp.Reasons, Notes: fp.Notes, Conflicts: []string{}} |
| 112 | for _, l := range f.Layers { |
| 113 | ol := ObservedLayer{Name: l.Name, ID: l.ID, Branch: l.Branch, BranchTip: l.Tip, Head: l.Head, State: string(l.State())} |
| 114 | if l.ID != "" { |
| 115 | ol.Record, ol.Version = l.Commit, l.Version |
| 116 | } |
| 117 | if l.Below != nil { |
| 118 | ol.Below = l.Below.Name |
| 119 | } |
| 120 | o.Layers = append(o.Layers, ol) |
| 121 | } |
| 122 | return o |
| 123 | } |
| 124 | |
| 125 | // Digest is the first 16 hex digits of the SHA-256 of the observation as |
| 126 | // the journal keeps it. |
| 127 | func (o Observation) Digest() string { |
| 128 | data, _ := json.Marshal(o) |
| 129 | sum := sha256.Sum256(data) |
| 130 | return hex.EncodeToString(sum[:])[:16] |
| 131 | } |
| 132 | |
| 133 | // fullReport is the report about the change id that the observation gives, |
| 134 | // before it is cut to the format's bounds. published is what the attempt |
| 135 | // published for each change, by ID. |
| 136 | func fullReport(op, tip, id string, o Observation, published map[string]*change.RestackPublished) *change.RestackReport { |
| 137 | r := &change.RestackReport{ |
| 138 | Schema: change.RestackSchema, ChangeID: id, Target: rebase.Target, TargetTip: change.HeadPrefix + tip, |
| 139 | Operation: op, Result: o.Result, Layers: []change.RestackLayer{}, Conflicts: o.Conflicts, Requests: []change.RestackRequest{}, |
| 140 | } |
| 141 | for _, l := range o.Layers { |
| 142 | if l.ID == "" { |
| 143 | continue |
| 144 | } |
| 145 | rl := change.RestackLayer{ |
| 146 | ChangeID: l.ID, Record: change.HeadPrefix + l.Record, Version: l.Version, |
| 147 | Head: change.HeadPrefix + l.Head, Branch: l.Branch, Published: published[l.ID], |
| 148 | } |
| 149 | if l.BranchTip != "" { |
| 150 | tip := change.HeadPrefix + l.BranchTip |
| 151 | rl.BranchTip = &tip |
| 152 | } |
| 153 | r.Layers = append(r.Layers, rl) |
| 154 | } |
| 155 | for _, q := range o.Requests { |
| 156 | if !q.Releases { |
| 157 | r.Requests = append(r.Requests, change.RestackRequest{Ref: rebase.RequestPrefix + q.Name, Tip: change.HeadPrefix + q.Head}) |
| 158 | } |
| 159 | } |
| 160 | if o.Failing != "" { |
| 161 | failing := o.Failing |
| 162 | r.Failing = &failing |
| 163 | } |
| 164 | if o.Refusal != "" { |
| 165 | refusal := o.Refusal |
| 166 | r.Refusal = &refusal |
| 167 | } |
| 168 | return r |
| 169 | } |
| 170 | |
| 171 | // bound cuts a report to the format's bounds, as the comment at the top of |
| 172 | // this file describes, and says what it left out: nothing for a report |
| 173 | // that fits whole. digest names the whole observation. It errs only when |
| 174 | // not even the change's own layer fits. |
| 175 | func bound(full *change.RestackReport, digest string) (*change.RestackReport, []string, error) { |
| 176 | r := *full |
| 177 | var omitted []string |
| 178 | if n := len(r.Layers); n > change.MaxRestackLayers { |
| 179 | r.Layers = essential(&r) |
| 180 | omitted = append(omitted, fmt.Sprintf("%d of the family's %d recorded changes", n-len(r.Layers), n)) |
| 181 | } |
| 182 | paths, cut := []string{}, 0 |
| 183 | for _, p := range r.Conflicts { |
| 184 | if len(paths) < change.MaxConflictPaths && fits(p, change.MaxConflictPath) { |
| 185 | paths = append(paths, p) |
| 186 | } else { |
| 187 | cut++ |
| 188 | } |
| 189 | } |
| 190 | if cut > 0 { |
| 191 | omitted = append(omitted, fmt.Sprintf("%d of its %d conflict paths", cut, len(r.Conflicts))) |
| 192 | } |
| 193 | r.Conflicts = paths |
| 194 | requests, seen, cut := []change.RestackRequest{}, map[string]bool{}, 0 |
| 195 | for _, q := range r.Requests { |
| 196 | name, ok := strings.CutPrefix(q.Ref, rebase.RequestPrefix) |
| 197 | if ok && len(requests) < change.MaxBlockingRequest && change.ValidBranch(name) && utf8.ValidString(name) && |
| 198 | len(q.Ref) <= change.MaxRequestRef && !seen[q.Ref] && change.ValidHead(q.Tip) { |
| 199 | seen[q.Ref] = true |
| 200 | requests = append(requests, q) |
| 201 | } else { |
| 202 | cut++ |
| 203 | } |
| 204 | } |
| 205 | if cut > 0 { |
| 206 | omitted = append(omitted, fmt.Sprintf("%d of its %d blocking requests", cut, len(r.Requests))) |
| 207 | } |
| 208 | r.Requests = requests |
| 209 | refusal := "" |
| 210 | if full.Refusal != nil { |
| 211 | refusal = plain(*full.Refusal) |
| 212 | } |
| 213 | if len(refusal) > change.MaxRefusal-noteRoom { |
| 214 | refusal = shorten(refusal, change.MaxRefusal-noteRoom) |
| 215 | omitted = append(omitted, "the end of its refusal") |
| 216 | } |
| 217 | |
| 218 | for { |
| 219 | out := r |
| 220 | out.Refusal = refusalWith(refusal, omitted, digest) |
| 221 | data, err := change.EncodeRestackReport(&out) |
| 222 | if err != nil { |
| 223 | return nil, nil, err |
| 224 | } |
| 225 | if len(data) <= change.MaxReportBlob { |
| 226 | return &out, omitted, nil |
| 227 | } |
| 228 | switch { |
| 229 | case len(r.Conflicts) > 0: |
| 230 | omitted = append(omitted, fmt.Sprintf("the %d conflict paths left, to fit in %d bytes", len(r.Conflicts), change.MaxReportBlob)) |
| 231 | r.Conflicts = []string{} |
| 232 | case len(r.Requests) > 0: |
| 233 | omitted = append(omitted, fmt.Sprintf("the %d blocking requests left, to fit in %d bytes", len(r.Requests), change.MaxReportBlob)) |
| 234 | r.Requests = []change.RestackRequest{} |
| 235 | case len(essential(&r)) < len(r.Layers): |
| 236 | kept := essential(&r) |
| 237 | omitted = append(omitted, fmt.Sprintf("%d more of the family's recorded changes, to fit in %d bytes", len(r.Layers)-len(kept), change.MaxReportBlob)) |
| 238 | r.Layers = kept |
| 239 | default: |
| 240 | return nil, nil, fmt.Errorf("no report of %s fits in %d bytes, even with its own layer alone", r.ChangeID, change.MaxReportBlob) |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // essential is the layers a fallback report keeps: the change's own, and |
| 246 | // the failing one, in the family's order. |
| 247 | func essential(r *change.RestackReport) []change.RestackLayer { |
| 248 | var keep []change.RestackLayer |
| 249 | for _, l := range r.Layers { |
| 250 | if l.ChangeID == r.ChangeID || r.Failing != nil && l.ChangeID == *r.Failing { |
| 251 | keep = append(keep, l) |
| 252 | } |
| 253 | } |
| 254 | return keep |
| 255 | } |
| 256 | |
| 257 | // fits reports whether s is 1 to max bytes of UTF-8 holding no control |
| 258 | // character, as a report's conflict path must be. |
| 259 | func fits(s string, max int) bool { |
| 260 | return len(s) >= 1 && len(s) <= max && utf8.ValidString(s) && !strings.ContainsFunc(s, unicode.IsControl) |
| 261 | } |
| 262 | |
| 263 | // plain is s as UTF-8 with each run of control characters one space. |
| 264 | func plain(s string) string { |
| 265 | var b strings.Builder |
| 266 | space := false |
| 267 | for _, r := range strings.ToValidUTF8(s, "�") { |
| 268 | if unicode.IsControl(r) { |
| 269 | if !space { |
| 270 | b.WriteByte(' ') |
| 271 | } |
| 272 | space = true |
| 273 | continue |
| 274 | } |
| 275 | space = false |
| 276 | b.WriteRune(r) |
| 277 | } |
| 278 | return strings.TrimSpace(b.String()) |
| 279 | } |
| 280 | |
| 281 | // refusalWith is a report's refusal: the observation's, and, when the |
| 282 | // report leaves anything out, a note of what and of the digest of the |
| 283 | // whole, cut to fit the format. nil is no refusal. |
| 284 | func refusalWith(refusal string, omitted []string, digest string) *string { |
| 285 | if len(omitted) == 0 && refusal == "" { |
| 286 | return nil |
| 287 | } |
| 288 | note := "" |
| 289 | if len(omitted) > 0 { |
| 290 | note = fmt.Sprintf("This report leaves out %s; the restacker's journal keeps the whole observation, digest %s.", |
| 291 | strings.Join(omitted, ", "), digest) |
| 292 | } |
| 293 | if room := change.MaxRefusal - len(note) - len(" "); len(refusal) > room { |
| 294 | refusal = shorten(refusal, room) |
| 295 | } |
| 296 | out := strings.TrimSpace(refusal + " " + note) |
| 297 | return &out |
| 298 | } |
| 299 | |
| 300 | // noteRoom is what a refusal leaves for the note of what a fallback leaves |
| 301 | // out, which is never longer. |
| 302 | const noteRoom = 600 |
| 303 | |
| 304 | // shorten is s cut at a character boundary to at most n bytes, an ellipsis |
| 305 | // marking the cut. |
| 306 | func shorten(s string, n int) string { |
| 307 | if len(s) <= n { |
| 308 | return s |
| 309 | } |
| 310 | end := n - len("…") |
| 311 | for end > 0 && !utf8.RuneStart(s[end]) { |
| 312 | end-- |
| 313 | } |
| 314 | return strings.TrimSpace(s[:max(end, 0)]) + "…" |
| 315 | } |
| 316 | |
| 317 | // sameReport reports whether two reports say the same: the same |
| 318 | // observations and the same result. The operation that wrote each is not |
| 319 | // compared, since an unchanged observation written again by a later |
| 320 | // attempt says nothing new. |
| 321 | func sameReport(a, b *change.RestackReport) bool { |
| 322 | if a == nil || b == nil { |
| 323 | return a == b |
| 324 | } |
| 325 | return bytesOf(a) == bytesOf(b) |
| 326 | } |
| 327 | |
| 328 | func bytesOf(r *change.RestackReport) string { |
| 329 | c := *r |
| 330 | c.Operation = "" |
| 331 | if c.Conflicts == nil { |
| 332 | c.Conflicts = []string{} |
| 333 | } |
| 334 | if c.Requests == nil { |
| 335 | c.Requests = []change.RestackRequest{} |
| 336 | } |
| 337 | if c.Layers == nil { |
| 338 | c.Layers = []change.RestackLayer{} |
| 339 | } |
| 340 | data, _ := json.Marshal(c) |
| 341 | return string(data) |
| 342 | } |