| 1 | package main |
| 2 | |
| 3 | import ( |
| 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. |
| 20 | var 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. |
| 26 | type 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 | close() |
| 34 | } |
| 35 | |
| 36 | type streamGraph struct{ objects *objectStream } |
| 37 | |
| 38 | func (gitRepository) graph() (graphReader, error) { |
| 39 | objects, err := openObjectsWithin(graphBudget) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | return streamGraph{objects}, nil |
| 44 | } |
| 45 | |
| 46 | func (g streamGraph) close() { g.objects.close() } |
| 47 | |
| 48 | func (g streamGraph) commit(id string) (knot.Commit, error) { |
| 49 | body, err := g.objects.read(id, "commit", knot.MaxCommitSize) |
| 50 | if err != nil { |
| 51 | return knot.Commit{}, fmt.Errorf("%s %v", id, err) |
| 52 | } |
| 53 | c, err := knot.ParseCommit(body, len(id)/2) |
| 54 | if err != nil { |
| 55 | return knot.Commit{}, fmt.Errorf("%s: %v", id, err) |
| 56 | } |
| 57 | return c, nil |
| 58 | } |
| 59 | |
| 60 | // tree walks the tree within the budget, and lists what it holds for the |
| 61 | // knot module to judge: the walk refuses only what would cross a bound, |
| 62 | // and the module refuses every entry that is not the graph's layout. |
| 63 | func (g streamGraph) tree(id string) ([]knot.Entry, error) { |
| 64 | var entries []knot.Entry |
| 65 | w := walk{objects: g.objects, hashSize: len(id) / 2, visit: func(at, mode, child string) error { |
| 66 | entries = append(entries, knot.Entry{Path: at, Mode: mode, OID: child}) |
| 67 | return nil |
| 68 | }} |
| 69 | if err := w.tree(id, "", 1); err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | return entries, nil |
| 73 | } |
| 74 | |
| 75 | func (g streamGraph) blob(id string, max int) ([]byte, error) { |
| 76 | return g.objects.read(id, "blob", max) |
| 77 | } |
| 78 | |
| 79 | // checkGraph decides a write to refs/the-valley/knots, and returns the |
| 80 | // verb and the reason that refuse it, or "" for the reason when the write |
| 81 | // is allowed. |
| 82 | // |
| 83 | // - Any named principal may write it. A key that names none may not. |
| 84 | // - The push that creates the ref brings one commit with no parent. An |
| 85 | // update brings one commit whose only parent is the tip it replaces, |
| 86 | // so a push is one operation, and the ref never rewinds. No push |
| 87 | // deletes the ref: its history is the record of every write, and a |
| 88 | // graph that must be recovered is recovered on the host. |
| 89 | // - The commit names the pushing principal as its committer, and carries |
| 90 | // a Valley-Op trailer. |
| 91 | // - Its tree is exactly the graph's layout, and every knot and comment |
| 92 | // the commit adds or changes holds to the module's rules, against the |
| 93 | // graph before it (knot.Push): no knot or comment is deleted, no |
| 94 | // comment changes, a knot keeps its id, type, created and created_by, |
| 95 | // a new knot's created_by and a new comment's author name the pusher, |
| 96 | // every edge resolves, and no edge forms a cycle. |
| 97 | func (p policy) checkGraph(principal string, u update) (verb, why string) { |
| 98 | switch { |
| 99 | case principal == "": |
| 100 | return "write", "the plan graph takes writes only from a named principal, and a key that names no principal is none" |
| 101 | case isZero(u.new): |
| 102 | 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" |
| 103 | } |
| 104 | verb = "update" |
| 105 | if isZero(u.old) { |
| 106 | verb = "create" |
| 107 | } |
| 108 | g, err := p.repo.graph() |
| 109 | if err != nil { |
| 110 | return verb, "the graph could not be read: " + err.Error() |
| 111 | } |
| 112 | defer g.close() |
| 113 | c, err := g.commit(u.new) |
| 114 | if err != nil { |
| 115 | return verb, "its new tip " + err.Error() |
| 116 | } |
| 117 | switch { |
| 118 | case verb == "create" && len(c.Parents) != 0: |
| 119 | return verb, fmt.Sprintf("the commit that creates the graph has no parent, and this one has %d", len(c.Parents)) |
| 120 | case verb == "update" && (len(c.Parents) != 1 || c.Parents[0] != u.old): |
| 121 | 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)) |
| 122 | case c.Committer != principal: |
| 123 | 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) |
| 124 | } |
| 125 | entries, err := g.tree(c.Tree) |
| 126 | if err != nil { |
| 127 | return verb, "its tree: " + err.Error() |
| 128 | } |
| 129 | after, err := knot.Layout(entries) |
| 130 | if err != nil { |
| 131 | return verb, err.Error() |
| 132 | } |
| 133 | var before *knot.Index |
| 134 | if verb == "update" { |
| 135 | old, err := g.commit(u.old) |
| 136 | if err != nil { |
| 137 | return verb, "the tip it replaces, " + err.Error() |
| 138 | } |
| 139 | entries, err := g.tree(old.Tree) |
| 140 | if err != nil { |
| 141 | return verb, "the tip it replaces: its tree: " + err.Error() |
| 142 | } |
| 143 | if before, err = knot.Layout(entries); err != nil { |
| 144 | return verb, "the tip it replaces: " + err.Error() |
| 145 | } |
| 146 | } |
| 147 | if err := (knot.Push{Old: before, New: after, Read: g.blob, Pusher: principal}).Check(); err != nil { |
| 148 | return verb, err.Error() |
| 149 | } |
| 150 | return verb, "" |
| 151 | } |
| 152 | |
| 153 | func parentList(parents []string) string { |
| 154 | if len(parents) == 0 { |
| 155 | return "none" |
| 156 | } |
| 157 | return strings.Join(parents, ", ") |
| 158 | } |