gunk-dev/the-valley

pending.go

source6021d207164dmain's published tip history
8.0 KiB
1// Keeping a write between building it and pushing it.
2//
3// A record commit or a report commit is written before it is pushed, so a
4// caller can keep the write somewhere durable first. After a crash, the
5// caller restores it and compares origin's ref with it: at Commit, origin
6// took the push; at Old, it did not, and the write may be pushed again.
7//
8// A kept write restores to the same write, or not at all. Its form names
9// everything its push checks, and a version's mode is part of that: a
10// restack append restored as a manual one would push without reading the
11// branch again. MarshalJSON writes the form. UnmarshalJSON restores only
12// a form MarshalJSON could have written, and refuses any other: one with
13// a field missing, unknown or named twice, and one that does not agree
14// with itself, such as a version whose record names another head.
15
16package rebase
17
18import (
19 "bytes"
20 "encoding/json"
21 "errors"
22 "fmt"
23 "strings"
24
25 "the-valley/change"
26)
27
28// The schemas of the forms a write is kept in.
29const (
30 pendingVersionSchema = "valley-pending-version/v1"
31 pendingReportSchema = "valley-pending-report/v1"
32)
33
34// The modes a version is built in. The mode decides what its push checks.
35const (
36 manualMode = "manual" // BuildVersion's: the push is leased on the record ref
37 restackMode = "restack" // BuildRestack's: the push also needs origin's branch still at Head
38)
39
40// versionForm is the form a PendingVersion is kept in.
41type versionForm struct {
42 Schema string `json:"schema"`
43 Mode string `json:"mode"`
44 ID string `json:"change_id"`
45 Handle string `json:"handle"`
46 Ref string `json:"ref"`
47 Old string `json:"old"`
48 Commit string `json:"commit"`
49 Version int `json:"version"`
50 Head string `json:"head"`
51 Branch string `json:"branch"`
52 Record *change.Record `json:"record"`
53}
54
55// MarshalJSON writes p in the form it is kept in. It refuses a p that
56// UnmarshalJSON would refuse, so what it writes always restores.
57func (p PendingVersion) MarshalJSON() ([]byte, error) {
58 if err := p.check(); err != nil {
59 return nil, fmt.Errorf("the pending version cannot be kept: %w", err)
60 }
61 mode := manualMode
62 if p.restack {
63 mode = restackMode
64 }
65 return json.Marshal(versionForm{
66 Schema: pendingVersionSchema, Mode: mode, ID: p.ID, Handle: p.Handle, Ref: p.Ref, Old: p.Old,
67 Commit: p.Commit, Version: p.Version, Head: p.Head, Branch: p.Branch, Record: p.Record,
68 })
69}
70
71// UnmarshalJSON restores a PendingVersion that MarshalJSON wrote, in the
72// mode it was built in.
73func (p *PendingVersion) UnmarshalJSON(data []byte) error {
74 var f versionForm
75 if err := restore(data, &f); err != nil {
76 return fmt.Errorf("the pending version does not restore: %w", err)
77 }
78 q := PendingVersion{
79 ID: f.ID, Handle: f.Handle, Ref: f.Ref, Old: f.Old, Commit: f.Commit,
80 Record: f.Record, Version: f.Version, Head: f.Head, Branch: f.Branch, restack: f.Mode == restackMode,
81 }
82 switch {
83 case f.Schema != pendingVersionSchema:
84 return fmt.Errorf("the pending version does not restore: its schema is %q, where %q is expected", f.Schema, pendingVersionSchema)
85 case f.Mode != manualMode && f.Mode != restackMode:
86 return fmt.Errorf("the pending version does not restore: its mode is %q, where %q or %q is expected", f.Mode, manualMode, restackMode)
87 }
88 if err := q.check(); err != nil {
89 return fmt.Errorf("the pending version does not restore: %w", err)
90 }
91 *p = q
92 return nil
93}
94
95// check refuses a pending version that does not agree with itself. Its
96// record must be the change's, ready at the version it names as both its
97// active and its last, at its head and on its branch. A version is only
98// ever added to a record that exists, so Old names a commit.
99func (p *PendingVersion) check() error {
100 switch {
101 case !change.ValidID(p.ID):
102 return fmt.Errorf("%q is not a change ID", p.ID)
103 case len(p.Handle) < change.MinHandle || !strings.HasPrefix(p.ID, p.Handle):
104 return fmt.Errorf("%q is not a handle of %s", p.Handle, p.ID)
105 case p.Ref != change.Ref(Target, p.ID):
106 return fmt.Errorf("its ref %q is not the record ref of %s", p.Ref, p.ID)
107 case !IsObjectID(p.Old) || !IsObjectID(p.Commit) || !IsObjectID(p.Head):
108 return errors.New("the record it read, the record commit and the head are not each a full object ID")
109 case !change.ValidBranch(p.Branch):
110 return fmt.Errorf("%q is not a branch name", p.Branch)
111 case p.Record == nil:
112 return errors.New("it holds no record")
113 }
114 if err := p.Record.Check(); err != nil {
115 return fmt.Errorf("its record does not read: %w", err)
116 }
117 r, v := p.Record, p.Record.ActiveVersion()
118 if r.ChangeID != p.ID || r.Target != Target || r.State != change.Ready || r.Active != len(r.Versions) ||
119 v.N != p.Version || HeadOf(v) != p.Head || v.Branch != p.Branch {
120 return fmt.Errorf("its record does not hold %s ready at v%d, its last version, at %s on %s", p.ID, p.Version, short(p.Head), p.Branch)
121 }
122 return nil
123}
124
125// reportForm is the form a PendingReport is kept in.
126type reportForm struct {
127 Schema string `json:"schema"`
128 ID string `json:"change_id"`
129 Ref string `json:"ref"`
130 Old string `json:"old"`
131 Commit string `json:"commit"`
132 Report *change.RestackReport `json:"report"`
133}
134
135// MarshalJSON writes p in the form it is kept in. It refuses a p that
136// UnmarshalJSON would refuse, so what it writes always restores.
137func (p PendingReport) MarshalJSON() ([]byte, error) {
138 if err := p.check(); err != nil {
139 return nil, fmt.Errorf("the pending report cannot be kept: %w", err)
140 }
141 return json.Marshal(reportForm{Schema: pendingReportSchema, ID: p.ID, Ref: p.Ref, Old: p.Old, Commit: p.Commit, Report: p.Report})
142}
143
144// UnmarshalJSON restores a PendingReport that MarshalJSON wrote.
145func (p *PendingReport) UnmarshalJSON(data []byte) error {
146 var f reportForm
147 if err := restore(data, &f); err != nil {
148 return fmt.Errorf("the pending report does not restore: %w", err)
149 }
150 if f.Schema != pendingReportSchema {
151 return fmt.Errorf("the pending report does not restore: its schema is %q, where %q is expected", f.Schema, pendingReportSchema)
152 }
153 q := PendingReport{ID: f.ID, Ref: f.Ref, Old: f.Old, Commit: f.Commit, Report: f.Report}
154 if err := q.check(); err != nil {
155 return fmt.Errorf("the pending report does not restore: %w", err)
156 }
157 *p = q
158 return nil
159}
160
161// check refuses a pending report that does not agree with itself: its
162// report must read, and be about the change whose report ref it names.
163// Old is "" for a change that had no report.
164func (p *PendingReport) check() error {
165 switch {
166 case !change.ValidID(p.ID):
167 return fmt.Errorf("%q is not a change ID", p.ID)
168 case p.Ref != RestackPrefix+p.ID:
169 return fmt.Errorf("its ref %q is not the report ref of %s", p.Ref, p.ID)
170 case p.Old != "" && !IsObjectID(p.Old) || !IsObjectID(p.Commit):
171 return errors.New("the report it replaces and the report commit are not each a full object ID")
172 case p.Report == nil:
173 return errors.New("it holds no report")
174 }
175 if err := p.Report.Check(); err != nil {
176 return fmt.Errorf("its report does not read: %w", err)
177 }
178 if p.Report.ChangeID != p.ID {
179 return fmt.Errorf("its report is about %s, not %s", p.Report.ChangeID, p.ID)
180 }
181 return nil
182}
183
184// restore reads data into form, and takes only what json.Marshal writes
185// of form. Space between tokens is all that may differ, so a form kept
186// inside an indented file still restores. Anything else is refused: a
187// field missing, unknown or named twice, a value written another way, or
188// anything after the form.
189func restore(data []byte, form any) error {
190 dec := json.NewDecoder(bytes.NewReader(data))
191 dec.DisallowUnknownFields()
192 if err := dec.Decode(form); err != nil {
193 return err
194 }
195 var compact bytes.Buffer
196 if err := json.Compact(&compact, data); err != nil {
197 return err
198 }
199 again, err := json.Marshal(form)
200 if err != nil {
201 return err
202 }
203 if !bytes.Equal(compact.Bytes(), again) {
204 return errors.New("it is not in the form it is kept in: a field is missing or named twice, or a value is written another way")
205 }
206 return nil
207}