gunk-dev/the-valley

build.go

sourcecd3079668d04main's published tip history
7.2 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//
29// Building pushes nothing. What it built names its operation, the
30// attempt's local name, which every report it built carries.
31
32// Writes is what a pass would push for one family, as the plan shows it.
33type Writes struct {
34 Result string `json:"result"` // what its reports say: complete, conflict or blocked
35 Branches []BranchWrite `json:"branches"`
36 Versions []VersionWrite `json:"versions"`
37 Reports []ReportWrite `json:"reports"`
38}
39
40// BranchWrite is one branch the family's atomic push would move.
41type BranchWrite struct {
42 Branch string `json:"branch"`
43 Old string `json:"old"` // its tip as captured, which the push is leased on
44 New string `json:"new"` // its prepared head
45}
46
47// VersionWrite is one version a pass would add.
48type VersionWrite struct {
49 ID string `json:"change_id"`
50 Version int `json:"version"`
51 Head string `json:"head"`
52 Record string `json:"record"` // the record commit built
53 Lease string `json:"lease"` // the record commit it replaces, which the push is leased on
54}
55
56// ReportWrite is one change's report.
57type ReportWrite struct {
58 ID string `json:"change_id"`
59 Result string `json:"result"`
60 Commit string `json:"commit,omitempty"` // the report commit built; "" when none was
61 Lease string `json:"lease,omitempty"` // the report it replaces; "" for a change with none
62 Omitted []string `json:"omitted"` // what the report leaves out of the observation
63 Unchanged bool `json:"unchanged"` // the current report says the same, so none was built
64 Error string `json:"error,omitempty"` // why no report could be built
65}
66
67// built is what building a pass's writes made: the journal of them, and
68// the prepared families whose heads it keeps reachable until released.
69type built struct {
70 journal *Journal
71 prepared []*rebase.Prepared
72}
73
74// releasePrepared lets go of the refs that keep the prepared heads.
75func (b *built) releasePrepared(ctx context.Context, g rebase.Git) {
76 for _, p := range b.prepared {
77 p.Release(ctx, g)
78 }
79 b.prepared = nil
80}
81
82// newOperation names one attempt: when it started, to the second, and a
83// random suffix, in the shape a report's operation takes.
84func newOperation(now time.Time) string {
85 var token [4]byte
86 rand.Read(token[:])
87 return now.UTC().Format("20060102T150405Z") + "-" + hex.EncodeToString(token[:])
88}
89
90// build prepares and builds the writes of every family the plan would
91// restack or holds, and sets each such family's Writes. A conflict or a
92// refusal is a family's result; an error is a pass that cannot go on, as
93// when its context is done.
94func build(ctx context.Context, g rebase.Git, s *rebase.Snapshot, p *Plan, b binding, op string) (*built, error) {
95 out := &built{journal: &Journal{
96 Schema: journalSchema, Repository: b.Repository, Target: b.Target, Principal: b.Principal,
97 Operation: op, Tip: p.Tip, Evidence: p.Evidence, Families: []JournalFamily{},
98 }}
99 for i := range p.Families {
100 fp := &p.Families[i]
101 if fp.Action != restack && fp.Action != hold {
102 continue
103 }
104 jf, w, err := out.family(ctx, g, s, p.families[i], *fp, b.Principal, op)
105 if err != nil {
106 out.releasePrepared(ctx, g)
107 return nil, err
108 }
109 fp.Writes = w
110 if jf.Branches != nil || len(jf.Reports) > 0 {
111 out.journal.Families = append(out.journal.Families, *jf)
112 }
113 }
114 return out, nil
115}
116
117// family builds one family's writes.
118func (out *built) family(ctx context.Context, g rebase.Git, s *rebase.Snapshot, f *rebase.Family, fp FamilyPlan, principal, op string) (*JournalFamily, *Writes, error) {
119 o := observe(f, fp)
120 jf := &JournalFamily{Versions: []*rebase.PendingVersion{}, Reports: []JournalReport{}}
121 published := map[string]*change.RestackPublished{}
122 switch fp.Action {
123 case hold:
124 o.Result, o.Refusal = change.RestackBlocked, strings.Join(fp.Reasons, "; ")
125 case restack:
126 prep, err := rebase.Prepare(ctx, g, f)
127 var one *rebase.Refusal
128 var many rebase.Refusals
129 switch {
130 case err == nil:
131 versions, err := prep.BuildAppends(ctx, g, principal)
132 if err != nil {
133 prep.Release(ctx, g)
134 if ctx.Err() != nil {
135 return nil, nil, ctx.Err()
136 }
137 o.Result, o.Refusal = change.RestackBlocked, err.Error()
138 break
139 }
140 out.prepared = append(out.prepared, prep)
141 o.Result, jf.Branches, jf.Versions = change.RestackComplete, prep.Branches(), versions
142 for _, v := range versions {
143 n, record := v.Version, change.HeadPrefix+v.Commit
144 published[v.ID] = &change.RestackPublished{Head: change.HeadPrefix + v.Head, Version: &n, Record: &record}
145 }
146 case errors.As(err, &one) && one.Kind == rebase.Conflict:
147 o.Result, o.Failing, o.Refusal = change.RestackConflict, one.Layer.ID, one.Error()
148 o.Conflicts = append([]string{}, one.Paths...)
149 case errors.As(err, &one) || errors.As(err, &many):
150 o.Result, o.Refusal = change.RestackBlocked, err.Error()
151 if one != nil && one.Layer != nil {
152 o.Failing = one.Layer.ID
153 }
154 default:
155 return nil, nil, err
156 }
157 }
158 jf.Observed = o
159
160 w := &Writes{Result: o.Result, Branches: []BranchWrite{}, Versions: []VersionWrite{}, Reports: []ReportWrite{}}
161 if jf.Branches != nil {
162 for _, u := range jf.Branches.Updates {
163 w.Branches = append(w.Branches, BranchWrite(u))
164 }
165 }
166 for _, v := range jf.Versions {
167 w.Versions = append(w.Versions, VersionWrite{ID: v.ID, Version: v.Version, Head: v.Head, Record: v.Commit, Lease: v.Old})
168 }
169 digest := o.Digest()
170 for _, l := range f.Layers {
171 if l.ID == "" {
172 continue
173 }
174 rw := ReportWrite{ID: l.ID, Result: o.Result, Omitted: []string{}}
175 r, omitted, err := bound(fullReport(op, s.Tip, l.ID, o, published), digest)
176 if err != nil {
177 rw.Error = err.Error()
178 w.Reports = append(w.Reports, rw)
179 continue
180 }
181 rw.Omitted = append(rw.Omitted, omitted...)
182 if current, found := s.Report(l.ID); found && sameReport(current.Report, r) {
183 rw.Unchanged = true
184 w.Reports = append(w.Reports, rw)
185 continue
186 }
187 old := s.Refs[rebase.RestackPrefix+l.ID]
188 pending, err := rebase.BuildReport(ctx, g, r, rebase.ReportMessage(l.Name, r), old)
189 if err != nil {
190 if ctx.Err() != nil {
191 return nil, nil, ctx.Err()
192 }
193 rw.Error = err.Error()
194 w.Reports = append(w.Reports, rw)
195 continue
196 }
197 rw.Commit, rw.Lease = pending.Commit, old
198 jf.Reports = append(jf.Reports, JournalReport{Pending: pending, Omitted: rw.Omitted})
199 w.Reports = append(w.Reports, rw)
200 }
201 return jf, w, nil
202}