-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_commit.go
148 lines (132 loc) · 3.72 KB
/
type_commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ghid
import (
"fmt"
"strconv"
"github.com/vmihailenco/msgpack/v5"
"github.com/vmihailenco/msgpack/v5/msgpcode"
)
func init() {
RegisterDecodeV1(TypeCommit, func(key []byte) (KeyV1, error) {
repo, rest, err := decodeV1IDAndRest(TypeCommit, key)
if err != nil {
return nil, err
}
return CommitKey{RepoID: RepoID(repo), SHA: string(rest)}, nil
})
RegisterDecodeV2(TypeCommit, func(key msgpack.RawMessage) (KeyV2, error) {
var arr []any
err := msgpack.Unmarshal(key, &arr)
if err != nil {
return nil, err
}
if len(arr) != 3 {
return nil, fmt.Errorf("unsupported IDv2 commit key: %#v", arr)
}
if v, ok := asUint64(arr[0]); !ok || v != 0 {
return nil, fmt.Errorf("unsupported IDv2 commit key: %#v", arr)
}
repo, ok := asUint64(arr[1])
if !ok {
return nil, fmt.Errorf("unsupported IDv2 commit key: %#v", arr)
}
sha, ok := arr[2].(string)
if !ok {
return nil, fmt.Errorf("unsupported IDv2 commit key: %#v", arr)
}
return CommitKey{RepoID: RepoID(repo), SHA: sha}, nil
})
RegisterDecodeV1(TypeTag, func(key []byte) (KeyV1, error) {
repo, rest, err := decodeV1IDAndRest(TypeTag, key)
if err != nil {
return nil, err
}
return TagKey{RepoID: RepoID(repo), SHA: string(rest)}, nil
})
RegisterDecodeV2(TypeTag, func(key msgpack.RawMessage) (KeyV2, error) {
var arr []any
err := msgpack.Unmarshal(key, &arr)
if err != nil {
return nil, err
}
if len(arr) != 3 {
return nil, fmt.Errorf("unsupported IDv2 tag key: %#v", arr)
}
if v, ok := asUint64(arr[0]); !ok || v != 0 {
return nil, fmt.Errorf("unsupported IDv2 tag key: %#v", arr)
}
repo, ok := asUint64(arr[1])
if !ok {
return nil, fmt.Errorf("unsupported IDv2 tag key: %#v", arr)
}
sha, ok := arr[2].(string)
if !ok {
return nil, fmt.Errorf("unsupported IDv2 tag key: %#v", arr)
}
return TagKey{RepoID: RepoID(repo), SHA: sha}, nil
})
}
func msgpackEncodeStr16(s string) msgpack.RawMessage {
n := len(s)
buf := make([]byte, 1+2+n)
buf[0] = msgpcode.Str16
buf[1] = byte(n >> 8)
buf[2] = byte(n)
copy(buf[3:], s)
return buf
}
var (
_ KeyV1 = CommitKey{}
_ KeyV2 = CommitKey{}
)
// CommitKey is a unique key for Commit nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#commit.
type CommitKey struct {
RepoID RepoID // corresponds to repository.databaseId
SHA string // corresponds to oid
}
// Type implements Key.
func (r CommitKey) Type() string {
return TypeCommit
}
// GetRepoID implements KeyWithRepo.
func (r CommitKey) GetRepoID() RepoID {
return r.RepoID
}
// KeyV1 implements KeyV1.
func (r CommitKey) KeyV1() string {
return strconv.FormatUint(uint64(r.RepoID), 10) + ":" + r.SHA
}
// KeyV2 implements KeyV2.
func (r CommitKey) KeyV2() msgpack.RawMessage {
// GitHub encodes commit SHA as string16, not string8, even though SHA length is only 40 (<256). Optimization?
return mustEncodeV2([]any{uint(0), uint(r.RepoID), msgpackEncodeStr16(r.SHA)})
}
var (
_ KeyV1 = TagKey{}
_ KeyV2 = TagKey{}
)
// TagKey is a unique key for Tag nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#tag.
type TagKey struct {
RepoID RepoID // corresponds to repository.databaseId
SHA string // corresponds to oid
}
// Type implements Key.
func (r TagKey) Type() string {
return TypeTag
}
// GetRepoID implements KeyWithRepo.
func (r TagKey) GetRepoID() RepoID {
return r.RepoID
}
// KeyV1 implements KeyV1.
func (r TagKey) KeyV1() string {
return strconv.FormatUint(uint64(r.RepoID), 10) + ":" + r.SHA
}
// KeyV2 implements KeyV2.
func (r TagKey) KeyV2() msgpack.RawMessage {
// GitHub encodes commit SHA as string16, not string8, even though SHA length is only 40 (<256). Optimization?
return mustEncodeV2([]any{uint(0), uint(r.RepoID), msgpackEncodeStr16(r.SHA)})
}