| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | |
| 10 | "the-valley/change" |
| 11 | ) |
| 12 | |
| 13 | // A change record is a commit at refs/the-valley/changes/<target>/<change |
| 14 | // ID> whose tree holds one file, record.json (the change module). The |
| 15 | // record commit's parents tie it to what it records: a new change's record |
| 16 | // has the head it marks ready as its one parent, and each later record has |
| 17 | // the record it replaces as its first parent, then the head of the version |
| 18 | // it adds, if it adds one. So a record ref holds on to every head the |
| 19 | // change was ever ready at, as a topic branch holds on to its commits. |
| 20 | // |
| 21 | // The bounds on reading one, which the change module defines for every |
| 22 | // reader of a record. They are far above what the valley command writes, |
| 23 | // and they are checked before any object is read past them. |
| 24 | const ( |
| 25 | maxRecordCommit = change.MaxRecordCommit // bytes in a record commit |
| 26 | maxRecordTree = change.MaxRecordTree // bytes in its tree |
| 27 | maxRecordBlob = change.MaxRecordBlob // bytes in its record.json |
| 28 | maxRecordUpdates = change.MaxRecordUpdates // change record updates in one push |
| 29 | maxRecords = change.MaxRecords // change records a repository holds; a soft bound, below |
| 30 | |
| 31 | // recordTarget is the one target a change may name for now. |
| 32 | recordTarget = "main" |
| 33 | ) |
| 34 | |
| 35 | // recordCommit is what a record commit says: its parents, in order, and |
| 36 | // its record.json, unread. |
| 37 | type recordCommit struct { |
| 38 | parents []string |
| 39 | record []byte |
| 40 | } |
| 41 | |
| 42 | func (gitRepository) record(id string) (recordCommit, error) { |
| 43 | objects, err := openObjects() |
| 44 | if err != nil { |
| 45 | return recordCommit{}, err |
| 46 | } |
| 47 | defer objects.close() |
| 48 | objects.subject = "a change record" |
| 49 | return readRecord(objects, id) |
| 50 | } |
| 51 | |
| 52 | func readRecord(objects *objectStream, id string) (recordCommit, error) { |
| 53 | hashSize := len(id) / 2 |
| 54 | body, err := objects.read(id, "commit", maxRecordCommit) |
| 55 | if err != nil { |
| 56 | return recordCommit{}, fmt.Errorf("it %v", err) |
| 57 | } |
| 58 | tree, parents, err := commitLinks(body, hashSize) |
| 59 | if err != nil { |
| 60 | return recordCommit{}, err |
| 61 | } |
| 62 | entries, err := objects.read(tree, "tree", maxRecordTree) |
| 63 | if err != nil { |
| 64 | return recordCommit{}, fmt.Errorf("its tree %v", err) |
| 65 | } |
| 66 | if len(entries) == 0 { |
| 67 | return recordCommit{}, errors.New("its tree is empty, and a change record's tree holds record.json alone") |
| 68 | } |
| 69 | mode, name, blob, rest, err := treeEntry(entries, hashSize) |
| 70 | switch { |
| 71 | case err != nil: |
| 72 | return recordCommit{}, fmt.Errorf("its tree: %v", err) |
| 73 | case len(rest) > 0: |
| 74 | return recordCommit{}, errors.New("its tree holds more than one entry, and a change record's tree holds record.json alone") |
| 75 | case name != "record.json" || mode != "100644": |
| 76 | return recordCommit{}, fmt.Errorf("its tree holds %q with mode %s, and a change record's tree holds record.json alone, as a plain file", name, mode) |
| 77 | } |
| 78 | data, err := objects.read(blob, "blob", maxRecordBlob) |
| 79 | if err != nil { |
| 80 | return recordCommit{}, fmt.Errorf("its record.json %v", err) |
| 81 | } |
| 82 | return recordCommit{parents: parents, record: data}, nil |
| 83 | } |
| 84 | |
| 85 | // commitLinks reads the tree and the parents out of a commit object, as |
| 86 | // git reads them: a tree line first, and then every parent line that |
| 87 | // follows it directly. A parent line anywhere later is one git ignores, |
| 88 | // and a record that holds one is refused rather than read two ways. |
| 89 | func commitLinks(body []byte, hashSize int) (tree string, parents []string, err error) { |
| 90 | header, _, _ := bytes.Cut(body, []byte("\n\n")) |
| 91 | lines := strings.Split(string(header), "\n") |
| 92 | tree, ok := strings.CutPrefix(lines[0], "tree ") |
| 93 | if !ok || !isHexID(tree, hashSize) { |
| 94 | return "", nil, errors.New("it is a commit git could not have written: it does not open with its tree") |
| 95 | } |
| 96 | rest := lines[1:] |
| 97 | for len(rest) > 0 && strings.HasPrefix(rest[0], "parent ") { |
| 98 | parent := strings.TrimPrefix(rest[0], "parent ") |
| 99 | if !isHexID(parent, hashSize) { |
| 100 | return "", nil, errors.New("it is a commit git could not have written: a parent is not an object ID") |
| 101 | } |
| 102 | parents = append(parents, parent) |
| 103 | rest = rest[1:] |
| 104 | } |
| 105 | for _, line := range rest { |
| 106 | if strings.HasPrefix(line, "parent ") { |
| 107 | return "", nil, errors.New("it names a parent after its other headers, which git does not read as a parent") |
| 108 | } |
| 109 | } |
| 110 | return tree, parents, nil |
| 111 | } |
| 112 | |
| 113 | func (gitRepository) kind(id string) (string, error) { |
| 114 | out, err := run(nil, "cat-file", "-t", id) |
| 115 | if err != nil { |
| 116 | return "", errors.New("names an object this repository does not hold") |
| 117 | } |
| 118 | return strings.TrimSpace(string(out)), nil |
| 119 | } |
| 120 | |
| 121 | // records counts the refs under refs/the-valley/changes/, past the bound |
| 122 | // by one at most: for-each-ref stops printing there. |
| 123 | // |
| 124 | // The bound it is checked against is soft. A pre-receive hook runs before |
| 125 | // git installs any ref, and nothing serializes the hooks of two pushes, so |
| 126 | // pushes running at once can each count the same records and each create |
| 127 | // more. The repository can then hold more than maxRecords, by at most the |
| 128 | // record updates those pushes had in flight: maxRecordUpdates each. Once |
| 129 | // it is over the bound, every later create is refused. |
| 130 | func (gitRepository) records() (int, error) { |
| 131 | out, err := run(nil, "for-each-ref", fmt.Sprintf("--count=%d", maxRecords+1), "--format=x", change.RefPrefix) |
| 132 | if err != nil { |
| 133 | return 0, err |
| 134 | } |
| 135 | return bytes.Count(out, []byte("\n")), nil |
| 136 | } |
| 137 | |
| 138 | // checkRecord decides a write to a change record ref, by the table of |
| 139 | // transitions below, and returns the verb and the reason that refuse it, |
| 140 | // or "" for the reason when the write is allowed. P is the new record |
| 141 | // commit's parents, old the ref's value before the push, and h the head a |
| 142 | // version names. |
| 143 | // |
| 144 | // create any named principal P = [h] |
| 145 | // new version owner or request holder P = [old, h] |
| 146 | // unready owner or request holder P = [old] ready → draft |
| 147 | // re-ready owner or request holder P = [old] draft or withdrawn → ready |
| 148 | // withdraw owner or request holder P = [old] ready or draft → withdrawn |
| 149 | // delete request holder — |
| 150 | // |
| 151 | // A new version always leaves the change ready, from whatever state it |
| 152 | // was in. A record whose one parent is old changes the state and the seq |
| 153 | // alone. A withdrawn change comes back ready, at the same version or a new |
| 154 | // one, and never as a draft. Every other combination is refused. |
| 155 | func (p policy) checkRecord(principal string, u update) (verb, why string) { |
| 156 | target, id, ok := change.ParseRef(u.ref) |
| 157 | if !ok || target != recordTarget { |
| 158 | return "write", fmt.Sprintf("a change record's ref is %s%s/<change ID>, with the change ID as 32 letters from k to z", change.RefPrefix, recordTarget) |
| 159 | } |
| 160 | if principal == "" { |
| 161 | return "write", "a change record takes writes only from a named principal, and a key that names no principal is none" |
| 162 | } |
| 163 | switch { |
| 164 | case isZero(u.new): |
| 165 | if !p.grants.holds(principal, grantRequest) { |
| 166 | return "delete", fmt.Sprintf("only a principal holding the %s grant may delete a change record, and %s holds none", grantRequest, principal) |
| 167 | } |
| 168 | return "delete", "" |
| 169 | case isZero(u.old): |
| 170 | if p.existingRecords+p.recordCreates > maxRecords { |
| 171 | return "create", fmt.Sprintf("the repository holds %d change records, and this push would create %d more, past the %d it may hold", p.existingRecords, p.recordCreates, maxRecords) |
| 172 | } |
| 173 | return "create", p.checkCreate(principal, target, id, u.new) |
| 174 | default: |
| 175 | return "update", p.checkUpdate(principal, target, id, u.old, u.new) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // checkCreate holds a new change's first record to the create row. |
| 180 | func (p policy) checkCreate(principal, target, id, commit string) string { |
| 181 | rc, err := p.repo.record(commit) |
| 182 | if err != nil { |
| 183 | return err.Error() |
| 184 | } |
| 185 | if len(rc.parents) != 1 { |
| 186 | return fmt.Sprintf("the record that creates a change has the head it marks ready as its one parent, and this one has %d parents", len(rc.parents)) |
| 187 | } |
| 188 | h := rc.parents[0] |
| 189 | if why := p.isCommit(h); why != "" { |
| 190 | return why |
| 191 | } |
| 192 | r, err := change.Parse(rc.record) |
| 193 | if err != nil { |
| 194 | return "its record.json does not read: " + err.Error() |
| 195 | } |
| 196 | if why := sameChange(r, target, id); why != "" { |
| 197 | return why |
| 198 | } |
| 199 | v := r.Versions[0] |
| 200 | switch { |
| 201 | case len(r.Versions) != 1: |
| 202 | return fmt.Sprintf("a new change's record holds one version, and this one holds %d", len(r.Versions)) |
| 203 | case v.Head != change.HeadPrefix+h: |
| 204 | return fmt.Sprintf("its version 1 names the head %s, and the record commit's parent is %s", v.Head, h) |
| 205 | case v.Uploader != principal: |
| 206 | return fmt.Sprintf("its version 1 names %s as its uploader, and %s is pushing it", v.Uploader, principal) |
| 207 | case r.Owner != principal: |
| 208 | return fmt.Sprintf("it names %s as the owner, and a new change is owned by the principal that pushes it, %s", r.Owner, principal) |
| 209 | case r.Seq != 1: |
| 210 | return fmt.Sprintf("its seq is %d, and a new change's is 1", r.Seq) |
| 211 | case r.Active != 1: |
| 212 | return fmt.Sprintf("its active is %d, and a new change's is 1", r.Active) |
| 213 | case r.State != change.Ready: |
| 214 | return fmt.Sprintf("its state is %s, and a new change is %s", r.State, change.Ready) |
| 215 | } |
| 216 | return "" |
| 217 | } |
| 218 | |
| 219 | // checkUpdate holds an update of a change's record to the rows after |
| 220 | // create. Which row applies is read from the record commit's parents, and |
| 221 | // the record has to say what that row says. |
| 222 | func (p policy) checkUpdate(principal, target, id, old, commit string) string { |
| 223 | prev, err := p.repo.record(old) |
| 224 | if err != nil { |
| 225 | return "the record it replaces " + err.Error() |
| 226 | } |
| 227 | before, err := change.Parse(prev.record) |
| 228 | if err != nil { |
| 229 | return "the record it replaces does not read: " + err.Error() |
| 230 | } |
| 231 | if principal != before.Owner && !p.grants.holds(principal, grantRequest) { |
| 232 | return fmt.Sprintf("only the change's owner, %s, or a principal holding the %s grant may update its record", before.Owner, grantRequest) |
| 233 | } |
| 234 | rc, err := p.repo.record(commit) |
| 235 | if err != nil { |
| 236 | return err.Error() |
| 237 | } |
| 238 | after, err := change.Parse(rc.record) |
| 239 | if err != nil { |
| 240 | return "its record.json does not read: " + err.Error() |
| 241 | } |
| 242 | if why := sameChange(after, target, id); why != "" { |
| 243 | return why |
| 244 | } |
| 245 | switch { |
| 246 | case len(rc.parents) == 1 && rc.parents[0] == old: |
| 247 | return checkStateChange(before, after) |
| 248 | case len(rc.parents) == 2 && rc.parents[0] == old: |
| 249 | h := rc.parents[1] |
| 250 | if why := p.isCommit(h); why != "" { |
| 251 | return why |
| 252 | } |
| 253 | return checkNewVersion(principal, before, after, h) |
| 254 | default: |
| 255 | return "a record that updates a change has the record it replaces as its first parent, and no other parent but the head of the version it adds" |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // checkStateChange holds a record whose one parent is the record it |
| 260 | // replaces to the unready, re-ready and withdraw rows: the state changes, |
| 261 | // by one of those rows, and the seq goes up by one. Nothing else changes, |
| 262 | // and so the active version stays. |
| 263 | func checkStateChange(before, after *change.Record) string { |
| 264 | switch { |
| 265 | case after.State == before.State: |
| 266 | return fmt.Sprintf("the change is %s already, and a record whose one parent is the record it replaces changes the state", before.State) |
| 267 | case before.State == change.Withdrawn && after.State == change.Draft: |
| 268 | return "the change is withdrawn, and a withdrawn change comes back ready, never as a draft" |
| 269 | case after.Seq != before.Seq+1: |
| 270 | return fmt.Sprintf("its seq is %d, and an update's is the seq before it plus one, %d", after.Seq, before.Seq+1) |
| 271 | case after.Owner != before.Owner: |
| 272 | return fmt.Sprintf("it changes the owner from %s to %s, and no update does", before.Owner, after.Owner) |
| 273 | case after.Active != before.Active: |
| 274 | return fmt.Sprintf("it changes active from %d to %d, and a change of state keeps the active version", before.Active, after.Active) |
| 275 | case !reflect.DeepEqual(after.Versions, before.Versions): |
| 276 | return "it changes the versions, and a change of state changes only the state and the seq" |
| 277 | } |
| 278 | return "" |
| 279 | } |
| 280 | |
| 281 | // checkNewVersion holds a new version to adding exactly one version, at |
| 282 | // the head h, and to the fields that follow from it. |
| 283 | func checkNewVersion(principal string, before, after *change.Record, h string) string { |
| 284 | if len(after.Versions) != len(before.Versions)+1 { |
| 285 | return fmt.Sprintf("a new version adds one version to the %d before it, and this record holds %d", len(before.Versions), len(after.Versions)) |
| 286 | } |
| 287 | for i, v := range before.Versions { |
| 288 | if after.Versions[i] != v { |
| 289 | return fmt.Sprintf("it changes version %d, and the versions before a new one stay exactly as they were", v.N) |
| 290 | } |
| 291 | } |
| 292 | v := after.Last() |
| 293 | switch { |
| 294 | case v.Head != change.HeadPrefix+h: |
| 295 | return fmt.Sprintf("its new version names the head %s, and the record commit's second parent is %s", v.Head, h) |
| 296 | case v.Head == before.ActiveVersion().Head: |
| 297 | return fmt.Sprintf("its new version's head is the active version's, %s, and a new version needs a new head", v.Head) |
| 298 | case v.Uploader != principal: |
| 299 | return fmt.Sprintf("its new version names %s as its uploader, and %s is pushing it", v.Uploader, principal) |
| 300 | case after.Active != v.N: |
| 301 | return fmt.Sprintf("its active is %d, and a new version becomes the active one, %d", after.Active, v.N) |
| 302 | case after.State != change.Ready: |
| 303 | return fmt.Sprintf("its state is %s, and a new version makes the change %s", after.State, change.Ready) |
| 304 | case after.Seq != before.Seq+1: |
| 305 | return fmt.Sprintf("its seq is %d, and an update's is the seq before it plus one, %d", after.Seq, before.Seq+1) |
| 306 | case after.Owner != before.Owner: |
| 307 | return fmt.Sprintf("it changes the owner from %s to %s, and no update does", before.Owner, after.Owner) |
| 308 | } |
| 309 | return "" |
| 310 | } |
| 311 | |
| 312 | // sameChange holds a record to naming the change and the target its ref |
| 313 | // names. Neither ever changes, so an update keeps them too. |
| 314 | func sameChange(r *change.Record, target, id string) string { |
| 315 | switch { |
| 316 | case r.ChangeID != id: |
| 317 | return fmt.Sprintf("it names the change %s, and its ref names %s", r.ChangeID, id) |
| 318 | case r.Target != target: |
| 319 | return fmt.Sprintf("it names the target %s, and its ref names %s", r.Target, target) |
| 320 | } |
| 321 | return "" |
| 322 | } |
| 323 | |
| 324 | // isCommit refuses a head that is not a commit. |
| 325 | func (p policy) isCommit(h string) string { |
| 326 | kind, err := p.repo.kind(h) |
| 327 | switch { |
| 328 | case err != nil: |
| 329 | return fmt.Sprintf("its head %s %v", h, err) |
| 330 | case kind != "commit": |
| 331 | return fmt.Sprintf("its head %s is a %s, and a head is a commit", h, kind) |
| 332 | } |
| 333 | return "" |
| 334 | } |
| 335 | |
| 336 | func isHexID(s string, size int) bool { |
| 337 | if len(s) != 2*size { |
| 338 | return false |
| 339 | } |
| 340 | for _, c := range []byte(s) { |
| 341 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 342 | return false |
| 343 | } |
| 344 | } |
| 345 | return true |
| 346 | } |