gunk-dev/the-valley

publish.go

sourceea038b6f45c6main's published tip history
9.3 KiB
1package main
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io/fs"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "the-valley/change"
13 "the-valley/rebase"
14)
15
16// Publishing. A write pass makes the writes its journal names the way
17// recovery would make them after a crash: it reads origin, decides every
18// write (recover.go), and pushes the ones still to finish, one phase of
19// each family at a time. A family's branch push comes first. Origin is
20// read again, so its versions are decided against the branches as origin
21// holds them and against the requests filed meanwhile. Its versions come
22// next, lower layers first, and its reports last, each after another
23// read. So a pass that ends anywhere leaves a journal the next pass
24// finishes in the same way, and a push whose answer was lost is decided
25// by what origin shows on the next read.
26//
27// A pass lets go of the journal once no family in it is open. A family is
28// open while its branch push is still to finish, or while origin took its
29// branch push and a version of it is neither added nor stopped for good,
30// as a version paused by a request that might still land is. Until then
31// the pass starts no new operation, since the journal holds one operation
32// at a time. A family whose branch push stopped, and so moved nothing, is
33// left for the next plan.
34//
35// A family whose branches moved gets reports that say what did happen.
36// complete is every version added. partial is a version not added: one
37// that stopped, or one that is paused, which the report says while the
38// family is open and replaces once it is not. Such a report is built from
39// the journal's observation and origin as read, and is not journaled: a
40// pass that ends before pushing it builds it again from the same journal.
41// A report that says what origin's current report says is not pushed.
42
43// maxSteps bounds how many times one pass reads origin and pushes what is
44// still to finish. A family takes three: its branches, its versions and its
45// reports. A write still to finish after the last is left to the next pass.
46const maxSteps = 6
47
48// Action is one write a pass pushed, and what became of it.
49type Action struct {
50 Operation string `json:"operation"`
51 Family int `json:"family"` // the family's place in the journal
52 Write string `json:"write"` // branches, version or report
53 ID string `json:"change_id,omitempty"` // the change a version or a report is about
54 Commit string `json:"commit,omitempty"` // the record or report commit pushed
55 Result string `json:"result"` // pushed, refused, unknown or unpushed
56 Why string `json:"why,omitempty"` // why origin did not take it, when it did not
57}
58
59// settled is what settling a journal did.
60type settled struct {
61 actions []Action
62 last *Recovery // what recovery decided of the journal at the last read
63 closed bool // the journal was let go
64}
65
66// settle makes the journal's writes as recovery decides them, and lets go
67// of the journal once no family in it is open.
68func settle(ctx context.Context, g rebase.Git, c config, j *Journal) (*settled, error) {
69 out := &settled{actions: []Action{}}
70 for step := 0; step < maxSteps; step++ {
71 rd, err := readOrigin(ctx, g, c)
72 if err != nil {
73 return out, err
74 }
75 out.last = decide(j, rd)
76 if pushed(ctx, g, j, out.last, &out.actions) {
77 continue
78 }
79 if err := ctx.Err(); err != nil {
80 return out, err
81 }
82 if err := reportWhatHappened(ctx, g, j, out.last, rd.s, &out.actions); err != nil {
83 return out, err
84 }
85 if open(j, out.last) {
86 return out, nil
87 }
88 if err := release(ctx, g, j.Operation); err != nil {
89 return out, err
90 }
91 if err := forget(c.state); err != nil {
92 return out, err
93 }
94 out.closed = true
95 return out, nil
96 }
97 return out, nil
98}
99
100// readOrigin reads one snapshot of origin, and what recovery reads it
101// through.
102func readOrigin(ctx context.Context, g rebase.Git, c config) (reading, error) {
103 s, err := rebase.ReadSnapshot(ctx, g, c.verify)
104 if err != nil {
105 return reading{}, err
106 }
107 requests, evidence := requestsOf(ctx, g, s)
108 return reading{
109 s: s, requests: requests, evidence: evidence, in: incoming(ctx, g, s.Tip), is: ancestry(ctx, g),
110 have: func(oid string) bool { return run(ctx, g, "cat-file", "-e", oid) == nil },
111 landed: s.IsLanded,
112 }, nil
113}
114
115// decisionsOf is each family's decisions, by write and change ID.
116func decisionsOf(r *Recovery) map[int]map[string]Decision {
117 by := map[int]map[string]Decision{}
118 for _, d := range r.Decisions {
119 if by[d.Family] == nil {
120 by[d.Family] = map[string]Decision{}
121 }
122 by[d.Family][d.Write+" "+d.ID] = d
123 }
124 return by
125}
126
127// pushed pushes, for each family, the first phase that has a write still
128// to finish: its branch push, then its versions, then its reports. It
129// reports whether it tried any push.
130func pushed(ctx context.Context, g rebase.Git, j *Journal, r *Recovery, actions *[]Action) bool {
131 tried := false
132 by := decisionsOf(r)
133 record := func(a Action) {
134 a.Operation = j.Operation
135 *actions = append(*actions, a)
136 tried = true
137 }
138 for i, f := range j.Families {
139 ds := by[i]
140 if ctx.Err() != nil {
141 return tried
142 }
143 if f.Branches != nil && ds["branches "].Action == writeFinish {
144 push, refused := f.Branches.Push(ctx, g)
145 a := Action{Family: i, Write: "branches", Result: push.String()}
146 var why []string
147 for _, ref := range refused {
148 why = append(why, ref.Ref+": "+ref.Why)
149 }
150 a.Why = strings.Join(why, "; ")
151 record(a)
152 continue
153 }
154 versions := false
155 for _, v := range f.Versions {
156 if ds["version "+v.ID].Action != writeFinish {
157 continue
158 }
159 ap := v.Push(ctx, g)
160 a := Action{Family: i, Write: "version", ID: v.ID, Commit: v.Commit, Result: ap.Write.Push.String(), Why: ap.Write.Why}
161 if ap.Err != nil && a.Why == "" {
162 a.Why = ap.Err.Error()
163 }
164 record(a)
165 versions = true
166 }
167 if versions {
168 continue
169 }
170 for _, rep := range f.Reports {
171 p := rep.Pending
172 if ds["report "+p.ID].Action != writeFinish {
173 continue
174 }
175 w := p.Push(ctx, g)
176 record(Action{Family: i, Write: "report", ID: p.ID, Commit: p.Commit, Result: w.Push.String(), Why: w.Why})
177 }
178 }
179 return tried
180}
181
182// moved reports whether origin took a family's branch push.
183func moved(f JournalFamily, ds map[string]Decision) bool {
184 return f.Branches != nil && ds["branches "].Action == writeDone
185}
186
187// open reports whether any family of the journal is open: its branch push
188// still to finish, or its branches moved and a version neither added nor
189// stopped for good.
190func open(j *Journal, r *Recovery) bool {
191 by := decisionsOf(r)
192 for i, f := range j.Families {
193 ds := by[i]
194 if f.Branches != nil && ds["branches "].Action == writeFinish {
195 return true
196 }
197 if !moved(f, ds) {
198 continue
199 }
200 for _, v := range f.Versions {
201 if !ds["version "+v.ID].settled() {
202 return true
203 }
204 }
205 }
206 return false
207}
208
209// reportWhatHappened pushes, for each family whose branches moved and
210// whose journaled reports are not all on origin, a report for each of its
211// changes that says what did happen. It builds each from the family's
212// observation: published at its prepared head, with the version when
213// origin holds it. A change origin no longer records gets none, since the
214// hook takes no report about it.
215func reportWhatHappened(ctx context.Context, g rebase.Git, j *Journal, r *Recovery, s *rebase.Snapshot, actions *[]Action) error {
216 by := decisionsOf(r)
217 for i, f := range j.Families {
218 ds := by[i]
219 if !moved(f, ds) {
220 continue
221 }
222 done := true
223 for _, rep := range f.Reports {
224 done = done && ds["report "+rep.Pending.ID].Action == writeDone
225 }
226 if done {
227 continue
228 }
229 heads := map[string]string{}
230 for _, u := range f.Branches.Updates {
231 heads[u.Branch] = u.New
232 }
233 o := f.Observed
234 o.Result, o.Refusal = change.RestackComplete, ""
235 published := map[string]*change.RestackPublished{}
236 var why []string
237 for _, l := range o.Layers {
238 if l.ID != "" {
239 published[l.ID] = &change.RestackPublished{Head: change.HeadPrefix + heads[l.Branch]}
240 }
241 }
242 for _, v := range f.Versions {
243 d := ds["version "+v.ID]
244 if d.Action == writeDone {
245 n, record := v.Version, change.HeadPrefix+v.Commit
246 published[v.ID].Version, published[v.ID].Record = &n, &record
247 continue
248 }
249 o.Result = change.RestackPartial
250 why = append(why, fmt.Sprintf("%s v%d was not added: %s", v.Handle, v.Version, d.Why))
251 }
252 o.Refusal = strings.Join(why, "; ")
253 digest := o.Digest()
254 for _, l := range o.Layers {
255 if l.ID == "" || s.Refs[change.Ref(rebase.Target, l.ID)] == "" {
256 continue
257 }
258 rep, _, err := bound(fullReport(j.Operation, j.Tip, l.ID, o, published), digest)
259 if err != nil {
260 return err
261 }
262 if current, found := s.Report(l.ID); found && sameReport(current.Report, rep) {
263 continue
264 }
265 old := s.Refs[rebase.RestackPrefix+l.ID]
266 p, err := rebase.BuildReport(ctx, g, rep, rebase.ReportMessage(l.Name, rep), old)
267 if err != nil {
268 return err
269 }
270 w := p.Push(ctx, g)
271 *actions = append(*actions, Action{Operation: j.Operation, Family: i, Write: "report", ID: l.ID, Commit: p.Commit, Result: w.Push.String(), Why: w.Why})
272 }
273 }
274 return ctx.Err()
275}
276
277// forget removes the journal from the state directory, durably: the
278// directory is synced after.
279func forget(dir string) error {
280 if err := os.Remove(filepath.Join(dir, journalFile)); err != nil && !errors.Is(err, fs.ErrNotExist) {
281 return err
282 }
283 d, err := os.Open(dir)
284 if err != nil {
285 return err
286 }
287 defer d.Close()
288 return d.Sync()
289}