gunk-dev/the-valley

build.go

source8e09e4639197main's published tip history
8.6 KiB
1package main
2
3import (
4 "context"
5 "crypto/rand"
6 "encoding/hex"
7 "errors"
8 "strings"
9 "time"
10
11 "the-valley/change"
12 "the-valley/rebase"
13)
14
15// Building a pass's writes. For each family the planner would restack,
16// the family is prepared in a throwaway worktree, and its writes are built
17// in the scratch repository: the one atomic branch push, each change's
18// next version, lower layers first, and each recorded change's report. A
19// family the planner holds gets only its reports, which say blocked. A
20// family whose rebase conflicts, or that the engine refuses to prepare,
21// gets reports that say conflict or blocked, and moves nothing. Each
22// version is built from the record the capture read, so every write is
23// built before any push, and all of them can be kept in the journal first
24// (keep).
25//
26// A report that says the same as the change's current report on origin,
27// apart from the operation that wrote it, is not built again (sameReport).
28// A family whose every change has a current report that says conflict is
29// not rebased again: nothing that report observed has moved, so the rebase
30// would conflict as it did (reportedConflict).
31//
32// Building pushes nothing. What it built names its operation, the
33// attempt's local name, which every report it built carries.
34
35// Writes is what a pass would push for one family, as the plan shows it.
36type Writes struct {
37 Result string `json:"result"` // what its reports say: complete, conflict or blocked
38 Branches []BranchWrite `json:"branches"`
39 Versions []VersionWrite `json:"versions"`
40 Reports []ReportWrite `json:"reports"`
41}
42
43// BranchWrite is one branch the family's atomic push would move.
44type BranchWrite struct {
45 Branch string `json:"branch"`
46 Old string `json:"old"` // its tip as captured, which the push is leased on
47 New string `json:"new"` // its prepared head
48}
49
50// VersionWrite is one version a pass would add.
51type VersionWrite struct {
52 ID string `json:"change_id"`
53 Version int `json:"version"`
54 Head string `json:"head"`
55 Record string `json:"record"` // the record commit built
56 Lease string `json:"lease"` // the record commit it replaces, which the push is leased on
57}
58
59// ReportWrite is one change's report.
60type ReportWrite struct {
61 ID string `json:"change_id"`
62 Result string `json:"result"`
63 Commit string `json:"commit,omitempty"` // the report commit built; "" when none was
64 Lease string `json:"lease,omitempty"` // the report it replaces; "" for a change with none
65 Omitted []string `json:"omitted"` // what the report leaves out of the observation
66 Unchanged bool `json:"unchanged"` // the current report says the same, so none was built
67 Error string `json:"error,omitempty"` // why no report could be built
68}
69
70// built is what building a pass's writes made: the journal of them, and
71// the prepared families whose heads it keeps reachable until released.
72type built struct {
73 journal *Journal
74 prepared []*rebase.Prepared
75}
76
77// releasePrepared lets go of the refs that keep the prepared heads.
78func (b *built) releasePrepared(ctx context.Context, g rebase.Git) {
79 for _, p := range b.prepared {
80 p.Release(ctx, g)
81 }
82 b.prepared = nil
83}
84
85// newOperation names one attempt: when it started, to the second, and a
86// random suffix, in the shape a report's operation takes.
87func newOperation(now time.Time) string {
88 var token [4]byte
89 rand.Read(token[:])
90 return now.UTC().Format("20060102T150405Z") + "-" + hex.EncodeToString(token[:])
91}
92
93// build prepares and builds the writes of every family the plan would
94// restack or holds, and sets each such family's Writes. A conflict or a
95// refusal is a family's result; an error is a pass that cannot go on, as
96// when its context is done.
97func build(ctx context.Context, g rebase.Git, s *rebase.Snapshot, p *Plan, b binding, op string) (*built, error) {
98 out := &built{journal: &Journal{
99 Schema: journalSchema, Repository: b.Repository, Target: b.Target, Principal: b.Principal, Families: []JournalFamily{},
100 }}
101 for i := range p.Families {
102 fp := &p.Families[i]
103 if fp.Action != restack && fp.Action != hold {
104 continue
105 }
106 if fp.Action == restack && reportedConflict(s, p.families[i]) {
107 fp.Notes = append(fp.Notes, "its current reports say conflict, and nothing they observed has moved, so it is not rebased again")
108 fp.Writes = &Writes{Result: change.RestackConflict, Branches: []BranchWrite{}, Versions: []VersionWrite{}, Reports: []ReportWrite{}}
109 for _, l := range p.families[i].Layers {
110 if l.ID != "" {
111 fp.Writes.Reports = append(fp.Writes.Reports, ReportWrite{ID: l.ID, Result: change.RestackConflict, Omitted: []string{}, Unchanged: true})
112 }
113 }
114 continue
115 }
116 jf, w, err := out.family(ctx, g, s, p.families[i], *fp, b.Principal, op)
117 if err != nil {
118 out.releasePrepared(ctx, g)
119 return nil, err
120 }
121 fp.Writes = w
122 if jf.Branches != nil || len(jf.Reports) > 0 {
123 jf.Operation, jf.Tip, jf.Evidence = op, p.Tip, p.Evidence
124 out.journal.Families = append(out.journal.Families, *jf)
125 }
126 }
127 return out, nil
128}
129
130// reportedConflict reports whether every recorded change in the family has
131// a report on origin that says conflict and is current: main, each record,
132// each branch and each blocking request are where that report saw them.
133func reportedConflict(s *rebase.Snapshot, f *rebase.Family) bool {
134 recorded := false
135 for _, l := range f.Layers {
136 if l.ID == "" {
137 continue
138 }
139 r, found := s.Report(l.ID)
140 if !found || !r.Current || r.Report.Result != change.RestackConflict {
141 return false
142 }
143 recorded = true
144 }
145 return recorded
146}
147
148// family builds one family's writes.
149func (out *built) family(ctx context.Context, g rebase.Git, s *rebase.Snapshot, f *rebase.Family, fp FamilyPlan, principal, op string) (*JournalFamily, *Writes, error) {
150 o := observe(f, fp)
151 jf := &JournalFamily{Versions: []*rebase.PendingVersion{}, Reports: []JournalReport{}, Replacements: []JournalReport{},
152 Accepted: Accepted{Versions: []string{}}}
153 published := map[string]*change.RestackPublished{}
154 switch fp.Action {
155 case hold:
156 o.Result, o.Refusal = change.RestackBlocked, strings.Join(fp.Reasons, "; ")
157 case restack:
158 prep, err := rebase.Prepare(ctx, g, f)
159 var one *rebase.Refusal
160 var many rebase.Refusals
161 switch {
162 case err == nil:
163 versions, err := prep.BuildAppends(ctx, g, principal)
164 if err != nil {
165 prep.Release(ctx, g)
166 if ctx.Err() != nil {
167 return nil, nil, ctx.Err()
168 }
169 o.Result, o.Refusal = change.RestackBlocked, err.Error()
170 break
171 }
172 out.prepared = append(out.prepared, prep)
173 o.Result, jf.Branches, jf.Versions = change.RestackComplete, prep.Branches(), versions
174 for _, v := range versions {
175 n, record := v.Version, change.HeadPrefix+v.Commit
176 published[v.ID] = &change.RestackPublished{Head: change.HeadPrefix + v.Head, Version: &n, Record: &record}
177 }
178 case errors.As(err, &one) && one.Kind == rebase.Conflict:
179 o.Result, o.Failing, o.Refusal = change.RestackConflict, one.Layer.ID, one.Error()
180 o.Conflicts = append([]string{}, one.Paths...)
181 case errors.As(err, &one) || errors.As(err, &many):
182 o.Result, o.Refusal = change.RestackBlocked, err.Error()
183 if one != nil && one.Layer != nil {
184 o.Failing = one.Layer.ID
185 }
186 default:
187 return nil, nil, err
188 }
189 }
190 jf.Observed = o
191
192 w := &Writes{Result: o.Result, Branches: []BranchWrite{}, Versions: []VersionWrite{}, Reports: []ReportWrite{}}
193 if jf.Branches != nil {
194 for _, u := range jf.Branches.Updates {
195 w.Branches = append(w.Branches, BranchWrite(u))
196 }
197 }
198 for _, v := range jf.Versions {
199 w.Versions = append(w.Versions, VersionWrite{ID: v.ID, Version: v.Version, Head: v.Head, Record: v.Commit, Lease: v.Old})
200 }
201 digest := o.Digest()
202 for _, l := range f.Layers {
203 if l.ID == "" {
204 continue
205 }
206 rw := ReportWrite{ID: l.ID, Result: o.Result, Omitted: []string{}}
207 r, omitted, err := bound(fullReport(op, s.Tip, l.ID, o, published), digest)
208 if err != nil {
209 rw.Error = err.Error()
210 w.Reports = append(w.Reports, rw)
211 continue
212 }
213 rw.Omitted = append(rw.Omitted, omitted...)
214 if current, found := s.Report(l.ID); found && sameReport(current.Report, r) {
215 rw.Unchanged = true
216 w.Reports = append(w.Reports, rw)
217 continue
218 }
219 old := s.Refs[rebase.RestackPrefix+l.ID]
220 pending, err := rebase.BuildReport(ctx, g, r, rebase.ReportMessage(l.Name, r), old)
221 if err != nil {
222 if ctx.Err() != nil {
223 return nil, nil, ctx.Err()
224 }
225 rw.Error = err.Error()
226 w.Reports = append(w.Reports, rw)
227 continue
228 }
229 rw.Commit, rw.Lease = pending.Commit, old
230 jf.Reports = append(jf.Reports, JournalReport{Pending: pending, Omitted: rw.Omitted})
231 w.Reports = append(w.Reports, rw)
232 }
233 return jf, w, nil
234}