gunk-dev/the-valley

id.go

source8efb16bd7315main's published tip history
4.4 KiB
1package change
2
3import (
4 "crypto/rand"
5 "strings"
6)
7
8// IDLength is the length of a change ID.
9const IDLength = 32
10
11// MinHandle is the shortest a handle may be.
12const MinHandle = 4
13
14// RefPrefix opens every change record's ref.
15const RefPrefix = "refs/the-valley/changes/"
16
17// Ref is the ref a change's record lives at.
18func Ref(target, id string) string {
19 return RefPrefix + target + "/" + id
20}
21
22// RequestPrefix opens the name of a recorded change's integration request.
23const RequestPrefix = "c-"
24
25// RequestName is the name a change's integration request is filed under,
26// and its outcome record kept under: c-<change ID>, as in
27// refs/the-valley/integration-requests/<target>/c-<change ID>.
28func RequestName(id string) string {
29 return RequestPrefix + id
30}
31
32// RequestID is the change ID a request name stands for. Every name of the
33// form c-<change ID> is reserved for the change with that ID, so ok is
34// true exactly when name is RequestName of a change ID. A request filed
35// under a branch's name never takes one of these names.
36func RequestID(name string) (id string, ok bool) {
37 id, found := strings.CutPrefix(name, RequestPrefix)
38 if !found || !ValidID(id) {
39 return "", false
40 }
41 return id, true
42}
43
44// ParseRef splits a record's ref into its target and change ID. It
45// reports false for a ref that is not refs/the-valley/changes/<target>/<ID>
46// with a branch name as the target and a change ID as the ID.
47func ParseRef(ref string) (target, id string, ok bool) {
48 rest, found := strings.CutPrefix(ref, RefPrefix)
49 if !found {
50 return "", "", false
51 }
52 slash := strings.LastIndexByte(rest, '/')
53 if slash < 0 {
54 return "", "", false
55 }
56 target, id = rest[:slash], rest[slash+1:]
57 if !ValidBranch(target) || !ValidID(id) {
58 return "", "", false
59 }
60 return target, id, true
61}
62
63// ValidID reports whether s is a change ID in jj's form: 32 letters from
64// k to z.
65func ValidID(s string) bool {
66 return len(s) == IDLength && isIDText(s)
67}
68
69func isIDText(s string) bool {
70 for _, c := range []byte(s) {
71 if c < 'k' || c > 'z' {
72 return false
73 }
74 }
75 return true
76}
77
78// NewID makes a change ID the way jj does: 16 random bytes, each half of
79// each byte written as one letter, z for 0 down to k for 15.
80func NewID() (string, error) {
81 var raw [IDLength / 2]byte
82 if _, err := rand.Read(raw[:]); err != nil {
83 return "", err
84 }
85 id := make([]byte, 0, IDLength)
86 for _, b := range raw {
87 id = append(id, 'z'-b>>4, 'z'-b&0xf)
88 }
89 return string(id), nil
90}
91
92// Handle is the shortest prefix of id, MinHandle letters at least, that
93// no other ID among shares.
94func Handle(id string, among []string) string {
95 n := MinHandle
96 for _, other := range among {
97 if other == id {
98 continue
99 }
100 common := 0
101 for common < len(id) && common < len(other) && id[common] == other[common] {
102 common++
103 }
104 if common+1 > n {
105 n = common + 1
106 }
107 }
108 if n > len(id) {
109 n = len(id)
110 }
111 return id[:n]
112}
113
114// Matching lists the IDs among that name could stand for: the ID itself
115// when name is a whole change ID, and otherwise every ID it is a prefix
116// of, when it is at least MinHandle letters from k to z. A name that
117// cannot be a handle matches nothing.
118func Matching(name string, among []string) []string {
119 if len(name) < MinHandle || len(name) > IDLength || !isIDText(name) {
120 return nil
121 }
122 var found []string
123 for _, id := range among {
124 if strings.HasPrefix(id, name) {
125 found = append(found, id)
126 }
127 }
128 return found
129}
130
131// LooksLikeHandle reports whether name has the shape of a handle or a
132// change ID, so that it could name a change rather than a branch.
133func LooksLikeHandle(name string) bool {
134 return len(name) >= MinHandle && len(name) <= IDLength && isIDText(name)
135}
136
137// ValidBranch reports whether refs/heads/<name> is a ref name git would
138// accept: what `git check-ref-format refs/heads/<name>` accepts. No part
139// between slashes is empty, opens with a dot or ends in .lock. No two dots
140// sit together, and nothing holds "@{", a control character, a space, or
141// any of ~ ^ : ? * [ \. The whole does not end in a dot.
142func ValidBranch(name string) bool {
143 if name == "" || strings.HasSuffix(name, ".") || strings.Contains(name, "..") || strings.Contains(name, "@{") {
144 return false
145 }
146 for _, part := range strings.Split(name, "/") {
147 if part == "" || part[0] == '.' || strings.HasSuffix(part, ".lock") {
148 return false
149 }
150 }
151 for _, c := range []byte(name) {
152 if c < 0x20 || c == 0x7f || strings.IndexByte(" ~^:?*[\\", c) >= 0 {
153 return false
154 }
155 }
156 return true
157}