gunk-dev/the-valley

build.go

sourcee612ba14a52bmain's published tip history
8.5 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 published := map[string]*change.RestackPublished{}
153 switch fp.Action {
154 case hold:
155 o.Result, o.Refusal = change.RestackBlocked, strings.Join(fp.Reasons, "; ")
156 case restack:
157 prep, err := rebase.Prepare(ctx, g, f)
158 var one *rebase.Refusal
159 var many rebase.Refusals
160 switch {
161 case err == nil:
162 versions, err := prep.BuildAppends(ctx, g, principal)
163 if err != nil {
164 prep.Release(ctx, g)
165 if ctx.Err() != nil {
166 return nil, nil, ctx.Err()
167 }
168 o.Result, o.Refusal = change.RestackBlocked, err.Error()
169 break
170 }
171 out.prepared = append(out.prepared, prep)
172 o.Result, jf.Branches, jf.Versions = change.RestackComplete, prep.Branches(), versions
173 for _, v := range versions {
174 n, record := v.Version, change.HeadPrefix+v.Commit
175 published[v.ID] = &change.RestackPublished{Head: change.HeadPrefix + v.Head, Version: &n, Record: &record}
176 }
177 case errors.As(err, &one) && one.Kind == rebase.Conflict:
178 o.Result, o.Failing, o.Refusal = change.RestackConflict, one.Layer.ID, one.Error()
179 o.Conflicts = append([]string{}, one.Paths...)
180 case errors.As(err, &one) || errors.As(err, &many):
181 o.Result, o.Refusal = change.RestackBlocked, err.Error()
182 if one != nil && one.Layer != nil {
183 o.Failing = one.Layer.ID
184 }
185 default:
186 return nil, nil, err
187 }
188 }
189 jf.Observed = o
190
191 w := &Writes{Result: o.Result, Branches: []BranchWrite{}, Versions: []VersionWrite{}, Reports: []ReportWrite{}}
192 if jf.Branches != nil {
193 for _, u := range jf.Branches.Updates {
194 w.Branches = append(w.Branches, BranchWrite(u))
195 }
196 }
197 for _, v := range jf.Versions {
198 w.Versions = append(w.Versions, VersionWrite{ID: v.ID, Version: v.Version, Head: v.Head, Record: v.Commit, Lease: v.Old})
199 }
200 digest := o.Digest()
201 for _, l := range f.Layers {
202 if l.ID == "" {
203 continue
204 }
205 rw := ReportWrite{ID: l.ID, Result: o.Result, Omitted: []string{}}
206 r, omitted, err := bound(fullReport(op, s.Tip, l.ID, o, published), digest)
207 if err != nil {
208 rw.Error = err.Error()
209 w.Reports = append(w.Reports, rw)
210 continue
211 }
212 rw.Omitted = append(rw.Omitted, omitted...)
213 if current, found := s.Report(l.ID); found && sameReport(current.Report, r) {
214 rw.Unchanged = true
215 w.Reports = append(w.Reports, rw)
216 continue
217 }
218 old := s.Refs[rebase.RestackPrefix+l.ID]
219 pending, err := rebase.BuildReport(ctx, g, r, rebase.ReportMessage(l.Name, r), old)
220 if err != nil {
221 if ctx.Err() != nil {
222 return nil, nil, ctx.Err()
223 }
224 rw.Error = err.Error()
225 w.Reports = append(w.Reports, rw)
226 continue
227 }
228 rw.Commit, rw.Lease = pending.Commit, old
229 jf.Reports = append(jf.Reports, JournalReport{Pending: pending, Omitted: rw.Omitted})
230 w.Reports = append(w.Reports, rw)
231 }
232 return jf, w, nil
233}