gunk-dev/the-valley

landing.go

source57b716740065main's published tip history
2.9 KiB
1package change
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7)
8
9// A landing record says that one head landed: the commit it landed as, and
10// the request name it landed under. The integrator writes one for every
11// head it lands, at refs/the-valley/integration-landings/<target>/<head>,
12// in the same ref transaction that moves the target (integrator/refs.go).
13// Nobody else writes one: the push hook refuses every push to the
14// namespace.
15//
16// The ref points at a commit with no parent. Its tree holds one file,
17// landing, of three lines:
18//
19// landed <the commit the head landed as>
20// request <the request name it landed under>
21// head <the head>
22//
23// An outcome record is kept per request name, so the next version of a
24// change to land under the same name replaces it. A landing record is kept
25// per head, so a later landing leaves it in place.
26
27// LandingPrefix opens every landing record's ref.
28const LandingPrefix = "refs/the-valley/integration-landings/"
29
30// LandingFile is the one file a landing record's tree holds.
31const LandingFile = "landing"
32
33// LandingRef is the ref of the landing record for head on target, a branch
34// name such as main.
35func LandingRef(target, head string) string {
36 return LandingPrefix + target + "/" + head
37}
38
39// Landing is what a landing record says.
40type Landing struct {
41 Landed string // the commit the head landed as
42 Request string // the request name it landed under
43 Head string // the head that landed
44}
45
46// Format is the landing file that records l.
47func (l Landing) Format() []byte {
48 return []byte(fmt.Sprintf("landed %s\nrequest %s\nhead %s\n", l.Landed, l.Request, l.Head))
49}
50
51// ParseLanding reads a landing file. It reads the three lines Format
52// writes, in that order and nothing else: each commit a full lowercase
53// object ID, and the request name with no space or control character in
54// it.
55func ParseLanding(data []byte) (Landing, error) {
56 lines := strings.Split(string(data), "\n")
57 if len(lines) != 4 || lines[3] != "" {
58 return Landing{}, errors.New("a landing record is three lines, each ending in a newline")
59 }
60 var values [3]string
61 for i, key := range []string{"landed", "request", "head"} {
62 value, ok := strings.CutPrefix(lines[i], key+" ")
63 if !ok || value == "" || strings.ContainsFunc(value, func(r rune) bool { return r <= ' ' || r == 0x7f }) {
64 return Landing{}, fmt.Errorf("line %d of a landing record is not %s and a value with no space in it", i+1, key)
65 }
66 values[i] = value
67 }
68 l := Landing{Landed: values[0], Request: values[1], Head: values[2]}
69 if !isObjectID(l.Landed) || !isObjectID(l.Head) {
70 return Landing{}, errors.New("a landing record names its commits as full lowercase object IDs")
71 }
72 return l, nil
73}
74
75// isObjectID reports whether s is a full SHA-1 or SHA-256 object ID in
76// lowercase hex.
77func isObjectID(s string) bool {
78 if len(s) != 40 && len(s) != 64 {
79 return false
80 }
81 for _, c := range []byte(s) {
82 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
83 return false
84 }
85 }
86 return true
87}