gunk-dev/the-valley

publish.go

sourceb88442951ad7main's published tip history
7.5 KiB
1// Publishing a prepared family, in two phases:
2//
3// 1. One atomic push moves every layer's branch, each leased on the tip
4// the capture read. Either every branch moves or none does.
5// 2. Lower layers first, each change's prepared head is added to its
6// record as a new version, as `valley ready --if-active` adds one:
7// only while the change is still ready at the head the capture read.
8// Each change is appended on its own, and a failure for one does not
9// stop the others. The change may have been unreadied meanwhile, its
10// record may have moved, or the hook may refuse the write.
11//
12// Origin is read once more just before the push. Each change layer's
13// record must still be the record commit the capture read, and each plain
14// branch must still be a branch no record is on. That is the last check,
15// and a window remains between it and the push. A change unreadied or
16// withdrawn in that window, or a branch readied, has its branch moved all
17// the same. git compares only the refs a push changes, and the record refs
18// are not among them, so no client can make the push conditional on a
19// record. Phase 2 then adds each version only while the change is still
20// ready at the captured head, under a lease on the record, so a change
21// unreadied in that window stays a draft at its old head, its result says
22// so, and the integrator lands nothing for it.
23
24package rebase
25
26import (
27 "context"
28 "strings"
29
30 "the-valley/change"
31)
32
33// Result is what became of one layer of a published family.
34type Result struct {
35 Layer *Layer
36 Head string // its prepared head
37 Branch Push // what became of its branch in phase 1
38 Appended *Appended // what became of its record in phase 2; nil for a plain branch, or until appended
39}
40
41// Unfinished reports whether the layer is a change whose branch may hold
42// its prepared head with no version recording it.
43func (r *Result) Unfinished() bool {
44 moved := r.Branch == Pushed || r.Branch == Unknown
45 return r.Layer.ID != "" && moved && (r.Appended == nil || r.Appended.Version == 0)
46}
47
48// Publication is what publishing a family did, layer by layer.
49type Publication struct {
50 Family *Family
51 Results []*Result // as Family.Layers, bottom first
52 Push Push // what became of the branch push, which every branch shares
53 Refused []RefusedRef // each branch origin refused, in git's order, when it refused the push
54}
55
56// Result is the result for a layer of the family.
57func (p *Publication) Result(l *Layer) *Result {
58 for _, r := range p.Results {
59 if r.Layer == l {
60 return r
61 }
62 }
63 return nil
64}
65
66// Publish is phase 1. It reads origin again, refusing a family whose
67// records or plain branches have moved since the capture, and then pushes
68// every layer's branch at once. A refusal, or a context already done,
69// pushes nothing and returns an error. Otherwise the publication says
70// whether the branches moved, did not, or may have.
71func Publish(ctx context.Context, g Git, p *Prepared) (*Publication, error) {
72 f := p.Family
73 pub := &Publication{Family: f}
74 for _, l := range f.Layers {
75 pub.Results = append(pub.Results, &Result{Layer: l, Head: p.Heads[l]})
76 }
77 if err := ctx.Err(); err != nil {
78 return pub, err
79 }
80 if err := revalidate(ctx, g, f); err != nil {
81 return pub, err
82 }
83 args := []string{"--atomic"}
84 for _, l := range f.Layers {
85 args = append(args, "--force-with-lease=refs/heads/"+l.Branch+":"+l.Tip)
86 }
87 args = append(args, "origin")
88 for _, l := range f.Layers {
89 args = append(args, p.Heads[l]+":refs/heads/"+l.Branch)
90 }
91 pub.Push, pub.Refused = push(ctx, g, args...)
92 for _, r := range pub.Results {
93 r.Branch = pub.Push
94 }
95 if pub.Push == Unpushed {
96 return pub, ctx.Err()
97 }
98 return pub, nil
99}
100
101// revalidate reads origin once more, just before the push: each change
102// layer's record must still be the record commit the capture read, and
103// each plain branch must still be a branch no record is on.
104func revalidate(ctx context.Context, g Git, f *Family) error {
105 o, err := ListOrigin(ctx, g, InventoryPatterns...)
106 if err != nil {
107 return &Refusal{Kind: OriginUnread}
108 }
109 for _, l := range f.Layers {
110 if l.ID != "" {
111 if now := o.Refs[l.Ref]; now != l.Commit {
112 return recordMoved(ctx, g, l, now)
113 }
114 continue
115 }
116 found, err := UnlandedOn(ctx, g, o, l.Branch, f.s.verify)
117 if err != nil {
118 return &Refusal{Kind: BranchUnread, Layer: l}
119 }
120 if len(found) > 0 {
121 r := &Refusal{Kind: BecameChange, Layer: l}
122 r.Change, r.Handle = &found[0], o.Handle(found[0].ID)
123 return r
124 }
125 }
126 return nil
127}
128
129// recordMoved is the refusal for a change layer whose record is no longer
130// the record commit the capture read.
131func recordMoved(ctx context.Context, g Git, l *Layer, now string) error {
132 if now == "" {
133 return &Refusal{Kind: RecordGone, Layer: l}
134 }
135 if err := Fetch(ctx, g, now); err != nil {
136 return &Refusal{Kind: RecordUnread, Layer: l}
137 }
138 rec, err := ReadRecord(ctx, g, now)
139 if err != nil {
140 return &Refusal{Kind: RecordUnread, Layer: l}
141 }
142 r := &Refusal{Layer: l, Change: &Recorded{ID: l.ID, Commit: now, Record: rec}, Handle: l.Name}
143 switch v := rec.ActiveVersion(); {
144 case rec.State != change.Ready:
145 r.Kind = RecordNotReady
146 case HeadOf(v) != l.Head:
147 r.Kind = RecordNewHead
148 default:
149 r.Kind = RecordChanged
150 }
151 return r
152}
153
154// UnlandedOn is every change origin holds a record of whose active
155// version names branch, less each whose active version has landed. o must
156// list InventoryPatterns. Landing is read only for a change on the
157// branch, so a name no record is on costs no more than reading the
158// records.
159//
160// A landed change is left out wherever the branch has moved since. The
161// branch then holds new work, and the name is free for it. A landing read
162// wrongly could make a draft's branch read as a plain branch, but nothing
163// lands from that: the integrator refuses any request whose head is a
164// version of a change that is not ready, whatever the request is named.
165func UnlandedOn(ctx context.Context, g Git, o Origin, branch string, v Verifier) ([]Recorded, error) {
166 all, _, err := ReadAll(ctx, g, o)
167 if err != nil {
168 return nil, err
169 }
170 var on []Recorded
171 for _, n := range all {
172 if n.Record.ActiveVersion().Branch == branch {
173 on = append(on, n)
174 }
175 }
176 if len(on) == 0 {
177 return nil, nil
178 }
179 var want []string
180 for ref, oid := range o.Refs {
181 if ref == "refs/heads/"+Target || strings.HasPrefix(ref, OutcomePrefix) {
182 want = append(want, oid)
183 }
184 }
185 if err := Fetch(ctx, g, want...); err != nil {
186 return nil, err
187 }
188 s := NewSnapshot(ctx, g, o, all, v)
189 var found []Recorded
190 for _, n := range on {
191 if !s.IsLanded(n.ID, n.Record.ActiveVersion()) {
192 found = append(found, n)
193 }
194 }
195 return found, nil
196}
197
198// Append is phase 2 for one layer: a change whose branch moved gains its
199// prepared head as a new version, while it is still ready at the head the
200// capture read, with the principal as its uploader. It does nothing for a
201// plain branch, or for a layer whose branch did not move. A caller that
202// appends layer by layer appends lower layers first.
203func (p *Publication) Append(ctx context.Context, g Git, principal string, r *Result) {
204 l := r.Layer
205 if l.ID == "" || r.Branch != Pushed {
206 return
207 }
208 ap := AppendVersion(ctx, g, Append{ID: l.ID, Branch: l.Branch, Head: r.Head, IfActive: l.Head, Principal: principal})
209 r.Appended = &ap
210}
211
212// AppendAll is phase 2 for every layer, lower layers first. A context done
213// part way leaves the remaining layers unappended.
214func (p *Publication) AppendAll(ctx context.Context, g Git, principal string) {
215 for _, r := range p.Results {
216 if ctx.Err() != nil {
217 return
218 }
219 p.Append(ctx, g, principal, r)
220 }
221}