gunk-dev/the-valley

id.go

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