gunk-dev/the-valley

origin.go

sourcebda0fd39abcamain's published tip history
6.0 KiB
1package rebase
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "sort"
9 "strings"
10
11 "the-valley/change"
12)
13
14// Target is the one target a change may name for now. The hook refuses a
15// record for any other.
16const Target = "main"
17
18// The namespaces of origin's integration requests, the integrator's
19// outcome records, the attestations the evidence is kept under, and the
20// host restacker's reports.
21const (
22 RequestPrefix = "refs/the-valley/integration-requests/" + Target + "/"
23 OutcomePrefix = "refs/the-valley/integration-outcomes/" + Target + "/"
24 AttestationRefs = change.AttestationRefs
25 RestackPrefix = change.RestackPrefix + Target + "/"
26)
27
28// RecordPatterns are the ls-remote patterns that list every record.
29var RecordPatterns = []string{change.RefPrefix + Target + "/*"}
30
31// InventoryPatterns are the ls-remote patterns of every ref a snapshot
32// reads: every branch, every record, every integration request and
33// outcome, every attestation ref, and every restack report.
34var InventoryPatterns = []string{
35 "refs/heads/*",
36 change.RefPrefix + Target + "/*",
37 RequestPrefix + "*",
38 OutcomePrefix + "*",
39 AttestationRefs + "*",
40 RestackPrefix + "*",
41}
42
43// Origin is one ls-remote snapshot of the refs a reader reads.
44type Origin struct {
45 Refs map[string]string // ref → object ID
46}
47
48// ListOrigin takes one snapshot of origin's refs matching the patterns.
49// ls-remote matches a pattern against the tail of a ref name, so every ref
50// is compared whole afterwards.
51func ListOrigin(ctx context.Context, g Git, patterns ...string) (Origin, error) {
52 out, err := g.output(ctx, false, append([]string{"ls-remote", "--refs", "origin"}, patterns...)...)
53 if err != nil {
54 return Origin{}, errors.New("could not read origin")
55 }
56 o := Origin{Refs: map[string]string{}}
57 for _, line := range strings.Split(out, "\n") {
58 oid, ref, ok := strings.Cut(line, "\t")
59 if ok && ref != "" {
60 o.Refs[ref] = oid
61 }
62 }
63 return o, nil
64}
65
66// Changes is the ID of every change origin holds a record for, in order.
67func (o Origin) Changes() []string {
68 var ids []string
69 for ref := range o.Refs {
70 if target, id, ok := change.ParseRef(ref); ok && target == Target {
71 ids = append(ids, id)
72 }
73 }
74 sort.Strings(ids)
75 return ids
76}
77
78// Handle is the change's handle among the changes origin holds, and this
79// one, which may not be among them yet.
80func (o Origin) Handle(id string) string {
81 return change.Handle(id, append(o.Changes(), id))
82}
83
84// Fetch brings the objects named into this checkout's object store,
85// without writing FETCH_HEAD, moving a ref or fetching tags. An object
86// already here is not asked for: a fetch brings an object with everything
87// it reaches, so one that is here has what it reaches here too.
88func Fetch(ctx context.Context, g Git, oids ...string) error {
89 var query bytes.Buffer
90 for _, oid := range oids {
91 if oid != "" {
92 fmt.Fprintln(&query, oid)
93 }
94 }
95 if query.Len() == 0 {
96 return nil
97 }
98 present, _ := g.input(ctx, true, &query, "cat-file", "--batch-check=%(objectname)")
99 here := map[string]bool{}
100 for _, line := range strings.Split(present, "\n") {
101 here[line] = true
102 }
103 var stdin bytes.Buffer
104 for _, oid := range oids {
105 if oid != "" && !here[oid] {
106 fmt.Fprintln(&stdin, oid)
107 }
108 }
109 if stdin.Len() == 0 {
110 return nil
111 }
112 if _, err := g.input(ctx, false, &stdin, "fetch", "--quiet", "--no-tags", "--no-write-fetch-head", "--stdin", "origin"); err != nil {
113 return errors.New("could not fetch from origin")
114 }
115 return nil
116}
117
118// Recorded is a change origin holds a record of: its ID, the record commit
119// origin's ref points at, and the record that commit holds.
120type Recorded struct {
121 ID string
122 Commit string
123 Record *change.Record
124}
125
126// ReadRecord reads the record a record commit holds. The commit must have
127// been fetched.
128func ReadRecord(ctx context.Context, g Git, commit string) (*change.Record, error) {
129 data, err := g.output(ctx, true, "cat-file", "blob", commit+":record.json")
130 if err != nil {
131 return nil, fmt.Errorf("the change record %s holds no record.json", short(commit))
132 }
133 r, err := change.Parse([]byte(data + "\n"))
134 if err != nil {
135 return nil, fmt.Errorf("the change record %s does not read: %v", short(commit), err)
136 }
137 return r, nil
138}
139
140// UnreadRecord is a record on origin that does not read.
141type UnreadRecord struct {
142 Ref, Commit, Why string
143}
144
145// ReadAll reads every record origin holds, in the order of their IDs. A
146// record that does not read is kept apart, in unread, and is no change for
147// any reader: the integrator treats it the same way. The hook refuses such
148// a record, so only corrupt storage holds one. err is for origin that
149// could not be read.
150func ReadAll(ctx context.Context, g Git, o Origin) (all []Recorded, unread []UnreadRecord, err error) {
151 ids := o.Changes()
152 var commits []string
153 for _, id := range ids {
154 commits = append(commits, o.Refs[change.Ref(Target, id)])
155 }
156 if err := Fetch(ctx, g, commits...); err != nil {
157 return nil, nil, err
158 }
159 for i, id := range ids {
160 r, err := ReadRecord(ctx, g, commits[i])
161 if err != nil {
162 unread = append(unread, UnreadRecord{Ref: change.Ref(Target, id), Commit: commits[i], Why: err.Error()})
163 continue
164 }
165 all = append(all, Recorded{ID: id, Commit: commits[i], Record: r})
166 }
167 return all, unread, nil
168}
169
170// HeadOf is a version's head as an object ID.
171func HeadOf(v change.Version) string {
172 return strings.TrimPrefix(v.Head, change.HeadPrefix)
173}
174
175// ChangeName is the change segment of a request ref. The integrator reads
176// a request as <prefix>/<target>/<change> and refuses a change segment
177// holding a slash (integrator/refs.go), so a branch name with one is
178// written with dashes instead. bin/valley's change_name is the same
179// mapping, and the request approve files is named by it.
180func ChangeName(branch string) string {
181 return strings.ReplaceAll(branch, "/", "-")
182}
183
184// RequestNames are the names a request for a version is filed under: its
185// branch, the way approve names one, and c-<change ID>.
186func RequestNames(id string, v change.Version) []string {
187 var names []string
188 if v.Branch != "" {
189 names = append(names, ChangeName(v.Branch))
190 }
191 return append(names, change.RequestName(id))
192}