gunk-dev/the-valley

report.go

source7db83f346570main's published tip history
3.9 KiB
1package main
2
3import (
4 "fmt"
5 "strings"
6
7 "the-valley/change"
8)
9
10// A restack report is a commit at refs/the-valley/restack/<target>/<change
11// ID> whose tree holds one file, report.json (change/report.go): what the
12// host restacker observed when it tried to rebase the change. Each report
13// commit has the report it replaces as its one parent, and the first has
14// none, so the ref keeps every report the change was given.
15//
16// The bounds on reading one, which the change module defines for every
17// reader of a report.
18var reportLayout = oneFile{
19 subject: "a restack report", name: "report.json",
20 commit: change.MaxReportCommit, tree: change.MaxReportTree, blob: change.MaxReportBlob,
21}
22
23const maxReportUpdates = change.MaxReportUpdates // restack report updates in one push
24
25func (gitRepository) report(id string) (fileCommit, error) {
26 return readOneFile(id, reportLayout)
27}
28
29// hasRecord asks for-each-ref whether the change's record ref exists. A
30// pattern also matches the refs below it, so the ref is compared whole.
31func (gitRepository) hasRecord(id string) (bool, error) {
32 ref := change.Ref(recordTarget, id)
33 out, err := run(nil, "for-each-ref", "--format=%(refname)", ref)
34 if err != nil {
35 return false, err
36 }
37 for _, line := range strings.Split(string(out), "\n") {
38 if line == ref {
39 return true, nil
40 }
41 }
42 return false, nil
43}
44
45// checkReport decides a write to a restack report ref, and returns the
46// verb and the reason that refuse it, or "" for the reason when the write
47// is allowed. P is the new report commit's parents, and old the ref's
48// value before the push.
49//
50// create restack holder P = []
51// extend restack holder P = [old]
52//
53// The ref's target is main, and its change is one the repository holds a
54// record of. The report.json is a valley-restack-report/v1 report about
55// that change and that target, read within the format's bounds. No push
56// deletes a report or replaces its history.
57func (p policy) checkReport(principal string, u update) (verb, why string) {
58 if principal == "" || !p.grants.holds(principal, grantRestack) {
59 holder := principal + " holds none"
60 if principal == "" {
61 holder = "a key that names no principal holds no grant"
62 }
63 return "write", fmt.Sprintf("%s takes writes only from a principal holding the %s grant, and %s", change.RestackPrefix, grantRestack, holder)
64 }
65 target, id, ok := change.ParseRestackRef(u.ref)
66 if !ok || target != recordTarget {
67 return "write", fmt.Sprintf("a restack report's ref is %s%s/<change ID>, with the change ID as 32 letters from k to z", change.RestackPrefix, recordTarget)
68 }
69 verb = "update"
70 switch {
71 case isZero(u.new):
72 return "delete", "a restack report keeps the change's history of reports, and no push deletes one"
73 case isZero(u.old):
74 verb = "create"
75 }
76 held, err := p.repo.hasRecord(id)
77 switch {
78 case err != nil:
79 return verb, fmt.Sprintf("whether the repository holds a record of the change %s could not be read: %v", id, err)
80 case !held:
81 return verb, fmt.Sprintf("the repository holds no record of the change %s, and a restack report is about a change it records", id)
82 }
83 rc, err := p.repo.report(u.new)
84 if err != nil {
85 return verb, err.Error()
86 }
87 switch {
88 case verb == "create" && len(rc.parents) != 0:
89 return verb, fmt.Sprintf("a change's first restack report has no parent, and this one has %d", len(rc.parents))
90 case verb == "update" && (len(rc.parents) != 1 || rc.parents[0] != u.old):
91 return verb, fmt.Sprintf("a restack report has the report it replaces, %s, as its one parent, so a push only adds to the history of reports", u.old)
92 }
93 r, err := change.ParseRestackReport(rc.file)
94 switch {
95 case err != nil:
96 return verb, "its report.json does not read: " + err.Error()
97 case r.ChangeID != id:
98 return verb, fmt.Sprintf("it is about the change %s, and its ref names %s", r.ChangeID, id)
99 case r.Target != target:
100 return verb, fmt.Sprintf("it names the target %s, and its ref names %s", r.Target, target)
101 }
102 return verb, ""
103}