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