gunk-dev/the-valley

report.go

sourceb1cafbe3a404main's published tip history
6.9 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.
43// BuildReport does the same, and returns a write the caller can keep
44// whole.
45func WriteReport(ctx context.Context, g Git, r *change.RestackReport, message, old string) (string, error) {
46 data, err := change.EncodeRestackReport(r)
47 if err != nil {
48 return "", err
49 }
50 if len(data) > change.MaxReportBlob {
51 return "", fmt.Errorf("the restack report would be %d bytes, more than the %d a report may be", len(data), change.MaxReportBlob)
52 }
53 var parents []string
54 if old != "" {
55 parents = append(parents, old)
56 }
57 commit, err := writeOneFile(ctx, g, "restack report", "report.json", data, message, parents...)
58 if err != nil {
59 return "", err
60 }
61 size, err := g.output(ctx, true, "cat-file", "-s", commit)
62 if n, perr := strconv.Atoi(size); err != nil || perr != nil || n > change.MaxReportCommit {
63 return "", fmt.Errorf("the restack report's commit is %s bytes, and a report commit may be at most %d", size, change.MaxReportCommit)
64 }
65 return commit, nil
66}
67
68// ReportMessage is the message of a report commit.
69func ReportMessage(handle string, r *change.RestackReport) string {
70 return fmt.Sprintf("valley restack: %s %s\n\nChange %s, operation %s, on %s at %s.",
71 handle, r.Result, r.ChangeID, r.Operation, r.Target, strings.TrimPrefix(r.TargetTip, change.HeadPrefix))
72}
73
74// PushReport moves the change's report ref on origin from old to commit,
75// leased on old, as PushRecord moves a record ref.
76func PushReport(ctx context.Context, g Git, id, old, commit string) RecordWrite {
77 return pushLeased(ctx, g, RestackPrefix+id, old, commit)
78}
79
80// PendingReport is a report written into a report commit and not yet
81// pushed. A caller keeps it whole before it calls Push, in the form
82// MarshalJSON writes (pending.go), as it keeps a PendingVersion.
83type PendingReport struct {
84 ID string // the change the report is about
85 Ref string // its report ref
86 Old string // the report commit the push is leased on; "" when the change had none
87 Commit string // the report commit written
88 Report *change.RestackReport // the report Commit holds
89}
90
91// BuildReport writes the report commit holding r, replacing old, as
92// WriteReport does, and pushes nothing.
93func BuildReport(ctx context.Context, g Git, r *change.RestackReport, message, old string) (*PendingReport, error) {
94 commit, err := WriteReport(ctx, g, r, message, old)
95 if err != nil {
96 return nil, err
97 }
98 return &PendingReport{ID: r.ChangeID, Ref: RestackPrefix + r.ChangeID, Old: old, Commit: commit, Report: r}, nil
99}
100
101// Push moves the change's report ref on origin from Old to Commit, leased
102// on Old, as PushReport does.
103func (p *PendingReport) Push(ctx context.Context, g Git) RecordWrite {
104 return PushReport(ctx, g, p.ID, p.Old, p.Commit)
105}
106
107// Report is a change's latest restack report.
108type Report struct {
109 Ref string // the report's ref
110 Commit string // the report commit the ref names
111 Report *change.RestackReport // what it says
112 Current bool // whether every input it observed still holds
113}
114
115// ReadReport reads the report a report commit holds. The commit must have
116// been fetched.
117func ReadReport(ctx context.Context, g Git, commit string) (*change.RestackReport, error) {
118 data, err := g.output(ctx, true, "cat-file", "blob", commit+":report.json")
119 if err != nil {
120 return nil, fmt.Errorf("the restack report %s holds no report.json", short(commit))
121 }
122 r, err := change.ParseRestackReport([]byte(data + "\n"))
123 if err != nil {
124 return nil, fmt.Errorf("the restack report %s does not read: %v", short(commit), err)
125 }
126 return r, nil
127}
128
129// Current reports whether every ref r observed still holds in o what r
130// says it held, or left it holding.
131func (o Origin) Current(r *change.RestackReport) bool {
132 if r.Target != Target || !o.holds("refs/heads/"+Target, &r.TargetTip) {
133 return false
134 }
135 for _, q := range r.Requests {
136 if !o.holds(q.Ref, &q.Tip) {
137 return false
138 }
139 }
140 for _, l := range r.Layers {
141 record, tip := l.Record, l.BranchTip
142 if p := l.Published; p != nil {
143 tip = &p.Head
144 if p.Record != nil {
145 record = *p.Record
146 }
147 }
148 if !o.holds(change.Ref(Target, l.ChangeID), &record) {
149 return false
150 }
151 if l.Branch != "" && !o.holds("refs/heads/"+l.Branch, tip) {
152 return false
153 }
154 }
155 return true
156}
157
158// holds reports whether ref is at the head want names in o, or absent from
159// o when want is nil.
160func (o Origin) holds(ref string, want *string) bool {
161 oid, present := o.Refs[ref]
162 if want == nil {
163 return !present
164 }
165 return present && oid == strings.TrimPrefix(*want, change.HeadPrefix)
166}
167
168// Report is the restack report origin holds for a change, read once per
169// snapshot. found is false when origin holds none, or holds one that does
170// not read or is about another change, which no reader shows: the hook
171// refuses such a report, so only corrupt storage holds one.
172func (s *Snapshot) Report(id string) (r Report, found bool) {
173 if r, ok := s.reports[id]; ok {
174 return r, r.Report != nil
175 }
176 r = Report{Ref: RestackPrefix + id, Commit: s.Refs[RestackPrefix+id]}
177 if r.Commit != "" {
178 if rep, err := ReadReport(s.ctx, s.g, r.Commit); err == nil && rep.ChangeID == id {
179 r.Report, r.Current = rep, s.Origin.Current(rep)
180 }
181 }
182 s.reports[id] = r
183 return r, r.Report != nil
184}