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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
type kind = Blob | Commit | Tag | Tree
type 'hash t = {
obj : 'hash;
kind : kind;
tag : string;
tagger : User.t option;
message : string option;
}
module type S = sig
type hash
type nonrec t = hash t
val make : hash -> kind -> ?tagger:User.t -> tag:string -> string option -> t
val format : t Encore.t
include S.DIGEST with type t := t and type hash := hash
include S.BASE with type t := t
val length : t -> int64
val obj : t -> hash
val tag : t -> string
val message : t -> string option
val kind : t -> kind
val tagger : t -> User.t option
end
module Make (Hash : S.HASH) = struct
type hash = Hash.t
type nonrec t = hash t
let make target kind ?tagger ~tag message =
{ obj = target; kind; tag; tagger; message }
let pp_kind ppf = function
| Blob -> Fmt.string ppf "Blob"
| Commit -> Fmt.string ppf "Commit"
| Tag -> Fmt.string ppf "Tag"
| Tree -> Fmt.string ppf "Tree"
let pp ppf { obj; kind; tag; tagger; message } =
Fmt.pf ppf
"{ @[<hov>obj = %a;@ kind = %a;@ tag = %s;@ tagger = %a;@ message = %a@] \
}"
Hash.pp obj pp_kind kind tag
(Fmt.hvbox (Fmt.option User.pp))
tagger
Fmt.(option (hvbox text))
message
let string_of_kind = function
| Commit -> "commit"
| Tag -> "tag"
| Tree -> "tree"
| Blob -> "blob"
module Syntax = struct
let safe_exn f x = try f x with _ -> raise Encore.Bij.Bijection
let hex =
Encore.Bij.v ~fwd:(safe_exn Hash.of_hex) ~bwd:(safe_exn Hash.to_hex)
let user =
Encore.Bij.v
~fwd:(fun str ->
match
Angstrom.parse_string ~consume:Angstrom.Consume.All
(Encore.to_angstrom User.format)
str
with
| Ok v -> v
| Error _ -> raise Encore.Bij.Bijection)
~bwd:(fun v ->
Encore.Lavoisier.emit_string v (Encore.to_lavoisier User.format))
let kind =
Encore.Bij.v
~fwd:(function
| "tree" -> Tree
| "blob" -> Blob
| "commit" -> Commit
| "tag" -> Tag
| _ -> raise Encore.Bij.Bijection)
~bwd:(function
| Blob -> "blob" | Tree -> "tree" | Commit -> "commit" | Tag -> "tag")
let tag =
Encore.Bij.v
~fwd:(fun ((_, obj), (_, kind), (_, tag), tagger, message) ->
{ obj; kind; tag; tagger = Stdlib.Option.map snd tagger; message })
~bwd:(fun { obj; kind; tag; tagger; message } ->
let tagger = Stdlib.Option.map (fun x -> "tagger", x) tagger in
("object", obj), ("type", kind), ("tag", tag), tagger, message)
let is_not_sp chr = chr <> ' '
let is_not_lf chr = chr <> '\x0a'
let always x _ = x
let rest =
let open Encore.Syntax in
let open Encore.Either in
fix @@ fun m ->
let cons = Encore.Bij.cons <$> (while0 (always true) <* commit <*> m) in
let nil = pure ~compare:(fun () () -> true) () in
Encore.Bij.v
~fwd:(function L cons -> cons | R () -> [])
~bwd:(function _ :: _ as lst -> L lst | [] -> R ())
<$> peek cons nil
let rest : string Encore.t =
let open Encore.Syntax in
Encore.Bij.v ~fwd:(String.concat "") ~bwd:(fun x -> [ x ]) <$> rest
let rest =
let open Encore.Syntax in
let open Encore.Either in
let fwd = function L str -> Some str | R _ -> None in
let bwd = function Some str -> L str | None -> R "" in
map (Encore.Bij.v ~fwd ~bwd)
(peek ((Encore.Bij.char '\x0a' <$> any) *> rest) (const ""))
let binding ?key value =
let open Encore.Syntax in
let value =
value <$> (while1 is_not_lf <* (Encore.Bij.char '\x0a' <$> any))
in
match key with
| Some key -> const key <* (Encore.Bij.char ' ' <$> any) <*> value
| None -> while1 is_not_sp <* (Encore.Bij.char ' ' <$> any) <*> value
let t =
let open Encore.Syntax in
binding ~key:"object" hex
<*> binding ~key:"type" kind
<*> binding ~key:"tag" Encore.Bij.identity
<*> option (binding ~key:"tagger" user)
<*> rest
let format = Encore.Syntax.map Encore.Bij.(compose obj5 tag) t
end
let format = Syntax.format
let length t =
let string x = Int64.of_int (String.length x) in
let ( + ) = Int64.add in
let user_length =
match t.tagger with
| Some user -> string "tagger" + 1L + User.length user + 1L
| None -> 0L
in
string "object"
+ 1L
+ Int64.of_int (Hash.digest_size * 2)
+ 1L
+ string "type"
+ 1L
+ string (string_of_kind t.kind)
+ 1L
+ string "tag"
+ 1L
+ string t.tag
+ 1L
+ user_length
+ match t.message with None -> 0L | Some str -> 1L + string str
let digest value =
Stream.digest
{
Stream.empty = Hash.empty;
Stream.feed_string = (fun str ctx -> Hash.feed_string ctx str);
Stream.feed_bigstring = (fun bstr ctx -> Hash.feed_bigstring ctx bstr);
Stream.get = Hash.get;
}
`Tag length
(Encore.to_lavoisier format)
value
let obj { obj; _ } = obj
let tag { tag; _ } = tag
let message { message; _ } = message
let kind { kind; _ } = kind
let tagger { tagger; _ } = tagger
let equal = ( = )
let compare = Stdlib.compare
let hash = Hashtbl.hash
module Set = Set.Make (struct
type nonrec t = t
let compare = compare
end)
module Map = Map.Make (struct
type nonrec t = t
let compare = compare
end)
end