gunk-dev/the-valley

knots.go

sourceea258e0e0163main's published tip history
6.3 KiB
1package main
2
3import (
4 "fmt"
5 "strings"
6
7 "the-valley/knot"
8)
9
10// The plan graph is a project's plan as data: knots at one ref,
11// refs/the-valley/knots, with its own root history and one commit per
12// operation (the knot module). Any named principal may write it, and no
13// review stands between a write and the ref, so the hook is what holds
14// every write to the graph's rules.
15//
16// The bounds on reading it follow from the graph's limits (knot/
17// contract.go): the widest tree is one knot's comments, the walk visits
18// every knot and every comment, and the hook stops reading a push's graph
19// after knot.HookDeadline.
20var graphBudget = budget{
21 subject: "the plan graph", depth: knot.MaxDepth, entriesInTree: knot.MaxTreeEntries, entries: knot.MaxEntries,
22 treeSize: knot.MaxTreeSize, fileSize: knot.MaxKnotSize, bytes: knot.MaxReadBytes, deadline: knot.HookDeadline,
23}
24
25// graphReader reads the objects of one push's graph, within graphBudget.
26type graphReader interface {
27 // commit reads the graph commit id names.
28 commit(id string) (knot.Commit, error)
29 // tree lists every tree and file below the tree id names.
30 tree(id string) ([]knot.Entry, error)
31 // blob reads the file id names, at most max bytes.
32 blob(id string, max int) ([]byte, error)
33 // overdue is nil while the reading's deadline has not passed, and the
34 // refusal that says so once it has.
35 overdue() error
36 close()
37}
38
39type streamGraph struct{ objects *objectStream }
40
41func (gitRepository) graph() (graphReader, error) {
42 objects, err := openObjectsWithin(graphBudget)
43 if err != nil {
44 return nil, err
45 }
46 return streamGraph{objects}, nil
47}
48
49func (g streamGraph) close() { g.objects.close() }
50
51func (g streamGraph) overdue() error {
52 if !g.objects.expired() {
53 return nil
54 }
55 b := g.objects.budget
56 return fmt.Errorf("the hook could not finish checking it within the %s it spends on %s", b.deadline, b.subject)
57}
58
59func (g streamGraph) commit(id string) (knot.Commit, error) {
60 body, err := g.objects.read(id, "commit", knot.MaxCommitSize)
61 if err != nil {
62 return knot.Commit{}, fmt.Errorf("%s %v", id, err)
63 }
64 c, err := knot.ParseCommit(body, len(id)/2)
65 if err != nil {
66 return knot.Commit{}, fmt.Errorf("%s: %v", id, err)
67 }
68 return c, nil
69}
70
71// tree walks the tree within the budget, and lists what it holds for the
72// knot module to judge: the walk refuses only what would cross a bound,
73// and the module refuses every entry that is not the graph's layout.
74func (g streamGraph) tree(id string) ([]knot.Entry, error) {
75 var entries []knot.Entry
76 w := walk{objects: g.objects, hashSize: len(id) / 2, visit: func(at, mode, child string) error {
77 entries = append(entries, knot.Entry{Path: at, Mode: mode, OID: child})
78 return nil
79 }}
80 if err := w.tree(id, "", 1); err != nil {
81 return nil, err
82 }
83 return entries, nil
84}
85
86func (g streamGraph) blob(id string, max int) ([]byte, error) {
87 return g.objects.read(id, "blob", max)
88}
89
90// checkGraph decides a write to refs/the-valley/knots, and returns the
91// verb and the reason that refuse it, or "" for the reason when the write
92// is allowed.
93//
94// - Any named principal may write it. A key that names none may not.
95// - The push that creates the ref brings one commit with no parent. An
96// update brings one commit whose only parent is the tip it replaces,
97// so a push is one operation, and the ref never rewinds. No push
98// deletes the ref: its history is the record of every write, and a
99// graph that must be recovered is recovered on the host.
100// - The commit names the pushing principal as its committer, and carries
101// a Valley-Op trailer.
102// - Its tree is exactly the graph's layout, and every knot and comment
103// the commit adds or changes holds to the module's rules, against the
104// graph before it (knot.Push): no knot or comment is deleted, no
105// comment changes, a knot keeps its id, type, created and created_by,
106// a new knot's created_by and a new comment's author name the pusher,
107// every edge resolves, and no edge forms a cycle.
108func (p policy) checkGraph(principal string, u update) (verb, why string) {
109 switch {
110 case principal == "":
111 return "write", "the plan graph takes writes only from a named principal, and a key that names no principal is none"
112 case isZero(u.new):
113 return "delete", "the plan graph is never deleted: its history is the record of every write to it, and a graph that must be recovered is recovered on the host"
114 }
115 verb = "update"
116 if isZero(u.old) {
117 verb = "create"
118 }
119 g, err := p.repo.graph()
120 if err != nil {
121 return verb, "the graph could not be read: " + err.Error()
122 }
123 defer g.close()
124 c, err := g.commit(u.new)
125 if err != nil {
126 return verb, "its new tip " + err.Error()
127 }
128 switch {
129 case verb == "create" && len(c.Parents) != 0:
130 return verb, fmt.Sprintf("the commit that creates the graph has no parent, and this one has %d", len(c.Parents))
131 case verb == "update" && (len(c.Parents) != 1 || c.Parents[0] != u.old):
132 return verb, fmt.Sprintf("an update of the graph is one commit whose only parent is the tip it replaces, %s, and this one's parents are %s", u.old, parentList(c.Parents))
133 case c.Committer != principal:
134 return verb, fmt.Sprintf("its commit names %s as its committer, and %s is pushing it: every commit on the graph is committed by the principal that pushes it", c.Committer, principal)
135 }
136 entries, err := g.tree(c.Tree)
137 if err != nil {
138 return verb, "its tree: " + err.Error()
139 }
140 after, err := knot.Layout(entries)
141 if err != nil {
142 return verb, err.Error()
143 }
144 var before *knot.Index
145 if verb == "update" {
146 old, err := g.commit(u.old)
147 if err != nil {
148 return verb, "the tip it replaces, " + err.Error()
149 }
150 entries, err := g.tree(old.Tree)
151 if err != nil {
152 return verb, "the tip it replaces: its tree: " + err.Error()
153 }
154 if before, err = knot.Layout(entries); err != nil {
155 return verb, "the tip it replaces: " + err.Error()
156 }
157 }
158 if err := (knot.Push{Old: before, New: after, Read: g.blob, Pusher: principal}).Check(); err != nil {
159 return verb, err.Error()
160 }
161 // A read past the deadline is refused as it fails, but a read that
162 // began in time can end late, and the work between reads takes time
163 // too: the push is admitted only if the whole check ended in time.
164 if err := g.overdue(); err != nil {
165 return verb, err.Error()
166 }
167 return verb, ""
168}
169
170func parentList(parents []string) string {
171 if len(parents) == 0 {
172 return "none"
173 }
174 return strings.Join(parents, ", ")
175}