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