gunk-dev/the-valley

report.go

source5f0229dc1066main's published tip history
5.6 KiB
1package rebase
2
3import (
4 "context"
5 "fmt"
6 "strconv"
7 "strings"
8
9 "the-valley/change"
10)
11
12// A change's restack report: written as a report commit and pushed to
13// RestackPrefix<change ID> (change/report.go) in two steps, and read back
14// as the latest report the host restacker wrote about the change, and
15// whether it is current.
16//
17// A report is current only while every ref it observed still holds on
18// origin what the report says it held:
19//
20// - main, at the report's target_tip;
21// - each layer's record ref, at the record commit it read;
22// - each layer's branch, at its branch_tip, or still absent when
23// branch_tip is null. A layer that names no branch has none to compare;
24// - each blocking request, at the tip it read.
25//
26// For a layer the attempt published, the branch is compared with the head
27// the attempt moved it to, and the record with the one that recorded that
28// head, when it added a version.
29//
30// Each comparison is with what the report recorded of the ref itself,
31// never with the version's head: a branch can be missing, or somewhere
32// other than the head its version names. Absent stays absent, so a report
33// that observed a missing branch is historical once the branch exists. A
34// report that is not current is historical: it describes refs that have
35// moved since.
36
37// WriteReport writes a report commit holding r, and returns its ID. Its
38// tree holds report.json alone, the one shape the hook accepts. Its one
39// parent is the report it replaces, the commit origin's report ref names,
40// or it has none when old is "" and the change has no report yet. Nothing
41// is pushed, so a caller can keep the commit's ID and old before
42// PushReport, and compare origin's report ref with them after a crash.
43func WriteReport(ctx context.Context, g Git, r *change.RestackReport, message, old string) (string, error) {
44 data, err := change.EncodeRestackReport(r)
45 if err != nil {
46 return "", err
47 }
48 if len(data) > change.MaxReportBlob {
49 return "", fmt.Errorf("the restack report would be %d bytes, more than the %d a report may be", len(data), change.MaxReportBlob)
50 }
51 var parents []string
52 if old != "" {
53 parents = append(parents, old)
54 }
55 commit, err := writeOneFile(ctx, g, "restack report", "report.json", data, message, parents...)
56 if err != nil {
57 return "", err
58 }
59 size, err := g.output(ctx, true, "cat-file", "-s", commit)
60 if n, perr := strconv.Atoi(size); err != nil || perr != nil || n > change.MaxReportCommit {
61 return "", fmt.Errorf("the restack report's commit is %s bytes, and a report commit may be at most %d", size, change.MaxReportCommit)
62 }
63 return commit, nil
64}
65
66// ReportMessage is the message of a report commit.
67func ReportMessage(handle string, r *change.RestackReport) string {
68 return fmt.Sprintf("valley restack: %s %s\n\nChange %s, operation %s, on %s at %s.",
69 handle, r.Result, r.ChangeID, r.Operation, r.Target, strings.TrimPrefix(r.TargetTip, change.HeadPrefix))
70}
71
72// PushReport moves the change's report ref on origin from old to commit,
73// leased on old, as PushRecord moves a record ref.
74func PushReport(ctx context.Context, g Git, id, old, commit string) RecordWrite {
75 return pushLeased(ctx, g, RestackPrefix+id, old, commit)
76}
77
78// Report is a change's latest restack report.
79type Report struct {
80 Ref string // the report's ref
81 Commit string // the report commit the ref names
82 Report *change.RestackReport // what it says
83 Current bool // whether every input it observed still holds
84}
85
86// ReadReport reads the report a report commit holds. The commit must have
87// been fetched.
88func ReadReport(ctx context.Context, g Git, commit string) (*change.RestackReport, error) {
89 data, err := g.output(ctx, true, "cat-file", "blob", commit+":report.json")
90 if err != nil {
91 return nil, fmt.Errorf("the restack report %s holds no report.json", short(commit))
92 }
93 r, err := change.ParseRestackReport([]byte(data + "\n"))
94 if err != nil {
95 return nil, fmt.Errorf("the restack report %s does not read: %v", short(commit), err)
96 }
97 return r, nil
98}
99
100// Current reports whether every ref r observed still holds in o what r
101// says it held, or left it holding.
102func (o Origin) Current(r *change.RestackReport) bool {
103 if r.Target != Target || !o.holds("refs/heads/"+Target, &r.TargetTip) {
104 return false
105 }
106 for _, q := range r.Requests {
107 if !o.holds(q.Ref, &q.Tip) {
108 return false
109 }
110 }
111 for _, l := range r.Layers {
112 record, tip := l.Record, l.BranchTip
113 if p := l.Published; p != nil {
114 tip = &p.Head
115 if p.Record != nil {
116 record = *p.Record
117 }
118 }
119 if !o.holds(change.Ref(Target, l.ChangeID), &record) {
120 return false
121 }
122 if l.Branch != "" && !o.holds("refs/heads/"+l.Branch, tip) {
123 return false
124 }
125 }
126 return true
127}
128
129// holds reports whether ref is at the head want names in o, or absent from
130// o when want is nil.
131func (o Origin) holds(ref string, want *string) bool {
132 oid, present := o.Refs[ref]
133 if want == nil {
134 return !present
135 }
136 return present && oid == strings.TrimPrefix(*want, change.HeadPrefix)
137}
138
139// Report is the restack report origin holds for a change, read once per
140// snapshot. found is false when origin holds none, or holds one that does
141// not read or is about another change, which no reader shows: the hook
142// refuses such a report, so only corrupt storage holds one.
143func (s *Snapshot) Report(id string) (r Report, found bool) {
144 if r, ok := s.reports[id]; ok {
145 return r, r.Report != nil
146 }
147 r = Report{Ref: RestackPrefix + id, Commit: s.Refs[RestackPrefix+id]}
148 if r.Commit != "" {
149 if rep, err := ReadReport(s.ctx, s.g, r.Commit); err == nil && rep.ChangeID == id {
150 r.Report, r.Current = rep, s.Origin.Current(rep)
151 }
152 }
153 s.reports[id] = r
154 return r, r.Report != nil
155}