gunk-dev/the-valley

publish.go

sourcefa3ec19b0cdcmain's published tip history
7.3 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 pub.Push, pub.Refused = p.Branches().Push(ctx, g)
84 for _, r := range pub.Results {
85 r.Branch = pub.Push
86 }
87 if pub.Push == Unpushed {
88 return pub, ctx.Err()
89 }
90 return pub, nil
91}
92
93// revalidate reads origin once more, just before the push: each change
94// layer's record must still be the record commit the capture read, and
95// each plain branch must still be a branch no record is on.
96func revalidate(ctx context.Context, g Git, f *Family) error {
97 o, err := ListOrigin(ctx, g, InventoryPatterns...)
98 if err != nil {
99 return &Refusal{Kind: OriginUnread}
100 }
101 for _, l := range f.Layers {
102 if l.ID != "" {
103 if now := o.Refs[l.Ref]; now != l.Commit {
104 return recordMoved(ctx, g, l, now)
105 }
106 continue
107 }
108 found, err := UnlandedOn(ctx, g, o, l.Branch, f.s.verify)
109 if err != nil {
110 return &Refusal{Kind: BranchUnread, Layer: l}
111 }
112 if len(found) > 0 {
113 r := &Refusal{Kind: BecameChange, Layer: l}
114 r.Change, r.Handle = &found[0], o.Handle(found[0].ID)
115 return r
116 }
117 }
118 return nil
119}
120
121// recordMoved is the refusal for a change layer whose record is no longer
122// the record commit the capture read.
123func recordMoved(ctx context.Context, g Git, l *Layer, now string) error {
124 if now == "" {
125 return &Refusal{Kind: RecordGone, Layer: l}
126 }
127 if err := Fetch(ctx, g, now); err != nil {
128 return &Refusal{Kind: RecordUnread, Layer: l}
129 }
130 rec, err := ReadRecord(ctx, g, now)
131 if err != nil {
132 return &Refusal{Kind: RecordUnread, Layer: l}
133 }
134 r := &Refusal{Layer: l, Change: &Recorded{ID: l.ID, Commit: now, Record: rec}, Handle: l.Name}
135 switch v := rec.ActiveVersion(); {
136 case rec.State != change.Ready:
137 r.Kind = RecordNotReady
138 case HeadOf(v) != l.Head:
139 r.Kind = RecordNewHead
140 default:
141 r.Kind = RecordChanged
142 }
143 return r
144}
145
146// UnlandedOn is every change origin holds a record of whose active
147// version names branch, less each whose active version has landed. o must
148// list InventoryPatterns. Landing is read only for a change on the
149// branch, so a name no record is on costs no more than reading the
150// records.
151//
152// A landed change is left out wherever the branch has moved since. The
153// branch then holds new work, and the name is free for it. A landing read
154// wrongly could make a draft's branch read as a plain branch, but nothing
155// lands from that: the integrator refuses any request whose head is a
156// version of a change that is not ready, whatever the request is named.
157func UnlandedOn(ctx context.Context, g Git, o Origin, branch string, v Verifier) ([]Recorded, error) {
158 all, _, err := ReadAll(ctx, g, o)
159 if err != nil {
160 return nil, err
161 }
162 var on []Recorded
163 for _, n := range all {
164 if n.Record.ActiveVersion().Branch == branch {
165 on = append(on, n)
166 }
167 }
168 if len(on) == 0 {
169 return nil, nil
170 }
171 var want []string
172 for ref, oid := range o.Refs {
173 if ref == "refs/heads/"+Target || strings.HasPrefix(ref, OutcomePrefix) {
174 want = append(want, oid)
175 }
176 }
177 if err := Fetch(ctx, g, want...); err != nil {
178 return nil, err
179 }
180 s := NewSnapshot(ctx, g, o, all, v)
181 var found []Recorded
182 for _, n := range on {
183 if !s.IsLanded(n.ID, n.Record.ActiveVersion()) {
184 found = append(found, n)
185 }
186 }
187 return found, nil
188}
189
190// Append is phase 2 for one layer: a change whose branch moved gains its
191// prepared head as a new version, while it is still ready at the head the
192// capture read, with the principal as its uploader. It does nothing for a
193// plain branch, or for a layer whose branch did not move. A caller that
194// appends layer by layer appends lower layers first.
195func (p *Publication) Append(ctx context.Context, g Git, principal string, r *Result) {
196 l := r.Layer
197 if l.ID == "" || r.Branch != Pushed {
198 return
199 }
200 ap := AppendVersion(ctx, g, Append{ID: l.ID, Branch: l.Branch, Head: r.Head, IfActive: l.Head, Principal: principal})
201 r.Appended = &ap
202}
203
204// AppendAll is phase 2 for every layer, lower layers first. A context done
205// part way leaves the remaining layers unappended.
206func (p *Publication) AppendAll(ctx context.Context, g Git, principal string) {
207 for _, r := range p.Results {
208 if ctx.Err() != nil {
209 return
210 }
211 p.Append(ctx, g, principal, r)
212 }
213}