Source file mem.ml
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
let src = Logs.Src.create "git.mem" ~doc:"logs git's memory back-end"
module Log = (val Logs.src_log src : Logs.LOG)
let err_not_found n k =
let str = Printf.sprintf "Git.Mem.%s: %s not found" n k in
Log.err (fun l -> l "Raising an invalid argument from %s" n) ;
Lwt.fail (Invalid_argument str)
module Make (H : Digestif.S) (Inflate : S.INFLATE) (Deflate : S.DEFLATE) =
struct
module Inflate = Inflate
module Deflate = Deflate
module Hash = Hash.Make (H)
module Buffer = Cstruct_buffer
module Value = Value.Raw (Hash) (Inflate) (Deflate)
module Reference = Reference.Make (Hash)
module PDec = Unpack.Stream (Hash) (Inflate)
module PEnc = Pack.Stream (Hash) (Deflate)
module Revidx = Map.Make (Int64)
type kind = [`Commit | `Tree | `Blob | `Tag]
type buffer = {ztmp: Cstruct.t; window: Inflate.window}
let default_buffer () =
let ztmp = Cstruct.create 0x800 in
let window = Inflate.window () in
{ztmp; window}
let buffer ?ztmp ?etmp:_ ?dtmp:_ ?raw:_ ?window () =
let ztmp = match ztmp with None -> Cstruct.create 0x800 | Some x -> x in
let window = match window with None -> Inflate.window () | Some x -> x in
{ztmp; window}
type t =
{ root: Fpath.t
; dotgit: Fpath.t
; buffer: (buffer -> unit Lwt.t) -> unit Lwt.t
; compression: int
; values: (Hash.t, Value.t Lazy.t) Hashtbl.t
; inflated: (Hash.t, kind * Cstruct.t) Hashtbl.t
; refs: (Reference.t, [`H of Hash.t | `R of Reference.t]) Hashtbl.t
; mutable head: Reference.head_contents option }
type error =
[ `Unresolved_object
| `Decoder_flow of string
| `Delta of PEnc.Delta.error
| `Pack of PDec.error
| Error.not_found ]
let with_buffer t f =
let open Lwt.Infix in
let c = ref None in
t.buffer (fun buf -> f buf >|= fun x -> c := Some x)
>|= fun () -> match !c with Some x -> x | None -> assert false
let pp_error ppf = function
| #Error.not_found as err -> Error.pp_not_found ppf err
| `Unresolved_object ->
Fmt.pf ppf "We still have unresolved objects from PACK stream"
| `Decoder_flow s -> Fmt.pf ppf "%s" s
| `Delta e -> Fmt.pf ppf "%a" PEnc.Delta.pp_error e
| `Pack e -> Fmt.pf ppf "%a" PDec.pp_error e
let root t = t.root
let dotgit t = t.dotgit
let compression t = t.compression
let v ?dotgit ?(compression = 6) ?buffer root =
let dotgit =
match dotgit with Some d -> d | None -> Fpath.(root / ".git")
in
if compression < 0 || compression > 9 then
failwith "level should be between 0 and 9" ;
let buffer =
match buffer with
| Some f -> f
| None ->
let p =
Lwt_pool.create 4 (fun () -> Lwt.return (default_buffer ()))
in
Lwt_pool.use p
in
let t =
{ root
; compression
; dotgit
; buffer
; values= Hashtbl.create 1024
; inflated= Hashtbl.create 1024
; refs= Hashtbl.create 8
; head= None }
in
Lwt.return (Ok t : (t, error) result)
let reset t =
Log.info (fun l -> l "Reset memory store") ;
Hashtbl.clear t.values ;
Hashtbl.clear t.inflated ;
Hashtbl.clear t.refs ;
Lwt.return (Ok ())
let clear_caches _ = Lwt.return ()
let write t value =
let hash = Value.digest value in
let kind =
match value with
| Value.Commit _ -> `Commit
| Value.Blob _ -> `Blob
| Value.Tree _ -> `Tree
| Value.Tag _ -> `Tag
in
let raw = Cstruct.create 0x100 in
let etmp = Cstruct.create 0x100 in
if Hashtbl.mem t.values hash then Lwt.return (Ok (hash, 0))
else
match Value.to_raw ~raw ~etmp value with
| Error `Never -> assert false
| Ok inflated ->
Hashtbl.add t.values hash (lazy value) ;
Hashtbl.add t.inflated hash (kind, Cstruct.of_string inflated) ;
Lwt.return
(Ok (hash, String.length inflated) : (Hash.t * int, error) result)
let digest kind raw =
let len = Cstruct.len raw in
let ctx = Hash.init () in
let hdr =
Fmt.strf "%s %d\000%!"
( match kind with
| `Commit -> "commit"
| `Blob -> "blob"
| `Tree -> "tree"
| `Tag -> "tag" )
len
in
let ctx = Hash.feed_string ctx hdr in
let ctx = Hash.feed_bigstring ctx (Cstruct.to_bigarray raw) in
Hash.get ctx
let write_inflated t ~kind inflated =
let hash = digest kind inflated in
if Hashtbl.mem t.values hash then Lwt.return hash
else
let value =
lazy
( match Value.of_raw ~kind inflated with
| Error err ->
let str = Fmt.strf "%a" Error.Decoder.pp_error err in
raise (Failure str)
| Ok value -> value )
in
Hashtbl.add t.inflated hash (kind, inflated) ;
Hashtbl.add t.values hash value ;
Lwt.return hash
let read_inflated t h =
try
let value = Lazy.force (Hashtbl.find t.values h) in
let kind =
match value with
| Value.Commit _ -> `Commit
| Value.Blob _ -> `Blob
| Value.Tree _ -> `Tree
| Value.Tag _ -> `Tag
in
let raw = Cstruct.create 0x100 in
let etmp = Cstruct.create 0x100 in
match Value.to_raw_without_header ~raw ~etmp value with
| Ok raw -> Lwt.return (Some (kind, Cstruct.of_string raw))
| Error `Never -> assert false
with Not_found -> Lwt.return None
let read_aux t h =
try Ok (Lazy.force (Hashtbl.find t.values h)) with Not_found ->
Error `Not_found
let read t h = Lwt.return (read_aux t h)
let keys t = Hashtbl.fold (fun k _ l -> k :: l) t []
let list t = Lwt.return (keys t.values)
let mem t h = Lwt.return (Hashtbl.mem t.values h)
let size t h =
let v =
match read_aux t h with
| Ok (Value.Blob v) -> Ok (Value.Blob.length v)
| Ok (Value.Commit _ | Value.Tag _ | Value.Tree _) | Error _ ->
Error `Not_found
in
Lwt.return v
let read_exn t h =
match read_aux t h with
| Error _ -> err_not_found "read_exn" (Hash.to_hex h)
| Ok v -> Lwt.return v
let contents t =
let hashes = keys t.values in
let values =
List.fold_left
(fun acc h ->
match read_aux t h with Ok v -> (h, v) :: acc | Error _ -> acc )
[] hashes
in
Lwt.return (Ok values)
module T = Traverse_bfs.Make (struct
module Hash = Hash
module Inflate = Inflate
module Deflate = Deflate
module Value = Value
type nonrec t = t
type nonrec error = error
let pp_error = pp_error
let read = read
end)
let fold = T.fold
let iter = T.iter
module Pack = struct
open Lwt.Infix
type stream = unit -> Cstruct.t option Lwt.t
module GC = Collector.Make (struct
module Hash = Hash
module Inflate = Inflate
module Value = Value
module Deflate = Deflate
module PEnc = PEnc
type nonrec t = t
type nonrec error = error
type nonrec kind = kind
let pp_error = pp_error
let read_inflated = read_inflated
let contents _ = assert false
end)
let make = GC.make_stream
let option_map f v = match v with Some v -> Some (f v) | None -> None
let option_map_default f d v = match v with Some v -> f v | None -> d
let option_default v = function Some v -> v | None -> v
type optimized_hunk = Insert of (int * int) | Copy of (int * int)
let pp_optimized_hunk ppf = function
| Insert (off, len) ->
Fmt.pf ppf "(Insert { @[<hov>length = %d;@ offset = %d;@] })" len off
| Copy (off, len) ->
Fmt.pf ppf "(Copy { @[<hov>length = %d;@ offset = %d;@] })" len off
let from git stream =
let cstruct_copy cs =
let ln = Cstruct.len cs in
let rs = Cstruct.create ln in
Cstruct.blit cs 0 rs 0 ln ; rs
in
let apply hunks_descr hunks buffer_hunks source target =
if Cstruct.len target < hunks_descr.PDec.Hunk.target_length then
raise (Invalid_argument "apply") ;
let target_length =
List.fold_left
(fun acc -> function
| Insert (off, len) ->
Cstruct.blit buffer_hunks off target acc len ;
acc + len
| Copy (off, len) ->
Cstruct.blit source off target acc len ;
acc + len )
0 hunks
in
if target_length = hunks_descr.PDec.Hunk.target_length then
Ok (Cstruct.sub target 0 target_length)
else
Fmt.kstrf
(fun x -> Error x)
"Bad undelta-ification (result: %d, expect: %d)" target_length
hunks_descr.PDec.Hunk.target_length
in
let k2k = function
| PDec.Commit -> `Commit
| PDec.Tag -> `Tag
| PDec.Tree -> `Tree
| PDec.Blob -> `Blob
| PDec.Hunk _ -> invalid_arg "k2k"
in
let empty = Cstruct.create 0 in
let buffer = Buffer.create 0x800 in
let buffer_hunks = Buffer.create 0x800 in
let queue = Queue.create () in
let rec go ~revidx ?(src = empty) ?hunks state =
match PDec.eval src state with
| `Await state -> (
stream ()
>>= function
| Some raw ->
go ~revidx ~src:raw ?hunks
(PDec.refill 0 (Cstruct.len raw) state)
| None ->
Lwt.return (Error (`Decoder_flow "Unexpected end of stream")) )
| `End (state, hash_pack) ->
Lwt.return (Ok (hash_pack, PDec.many state))
| `Error (_, err) ->
Log.err (fun l ->
l "The PACK decoder returns an error: %a." PDec.pp_error err ) ;
Lwt.return (Error (`Pack err))
| `Flush state ->
let o, n = PDec.output state in
Buffer.add buffer (Cstruct.sub o 0 n) ;
go ~revidx ~src (PDec.flush 0 (Cstruct.len o) state)
| `Hunk (state, hunk) ->
let hunks =
match hunks, hunk with
| Some hunks, PDec.Hunk.Insert raw ->
let off = Buffer.has buffer_hunks in
Buffer.add buffer_hunks raw ;
Insert (off, Cstruct.len raw) :: hunks
| Some hunks, PDec.Hunk.Copy (off, len) ->
Copy (off, len) :: hunks
| None, PDec.Hunk.Insert raw ->
let off = Buffer.has buffer_hunks in
Buffer.add buffer_hunks raw ;
[Insert (off, Cstruct.len raw)]
| None, PDec.Hunk.Copy (off, len) -> [Copy (off, len)]
in
go ~revidx ~src ~hunks (PDec.continue state)
| `Object state ->
( match PDec.kind state with
| (PDec.Commit | PDec.Tag | PDec.Tree | PDec.Blob) as kind ->
let raw = Buffer.contents buffer |> Cstruct.of_string in
Buffer.clear buffer ;
Log.debug (fun l ->
l "Retrieve a new Git object (length: %d)."
(Cstruct.len raw) ) ;
write_inflated git ~kind:(k2k kind) raw
>|= fun hash ->
Log.debug (fun l ->
l
"Add the object %a to the Git repository from the PACK \
file."
Hash.pp hash ) ;
Some hash
| PDec.Hunk hunks_descr -> (
let hunks = option_map List.rev hunks |> option_default [] in
let inflated =
Cstruct.create hunks_descr.PDec.Hunk.target_length
in
let hash_source =
match hunks_descr.PDec.Hunk.reference with
| PDec.Hunk.Hash hash -> hash
| PDec.Hunk.Offset off ->
let off = Int64.sub (PDec.offset state) off in
Revidx.find off revidx
in
Log.debug (fun l ->
l
"Catch a Hunk object which has as source the Git \
object: %a."
Hash.pp hash_source ) ;
read_inflated git hash_source
>>= function
| None ->
Log.warn (fun l ->
l "The source Git object %a does not exist yet."
Hash.pp hash_source ) ;
Queue.push
( hunks_descr
, hunks
, cstruct_copy (Buffer.unsafe_contents buffer_hunks)
, inflated
, hash_source )
queue ;
Buffer.clear buffer_hunks ;
Lwt.return None
| Some (kind, raw) -> (
Log.debug (fun l ->
l "Retrieving the source Git object %a." Hash.pp
hash_source ) ;
match
apply hunks_descr hunks
(Buffer.unsafe_contents buffer_hunks)
raw inflated
with
| Ok result ->
Buffer.clear buffer_hunks ;
write_inflated git ~kind result
>|= fun hash ->
Log.debug (fun l ->
l
"Add the object %a to the Git repository from \
the PACK file."
Hash.pp hash ) ;
Some hash
| Error err ->
Log.err (fun l ->
l
"Error when we apply the source Git object %a \
with the Hunk object: %a."
Hash.pp hash_source
(Fmt.hvbox
(Fmt.list
~sep:(Fmt.const Fmt.string ";@ ")
pp_optimized_hunk))
hunks ) ;
Lwt.fail (Failure err) ) ) )
>>= fun hash ->
let revidx =
option_map_default
(fun hash -> Revidx.add (PDec.offset state) hash revidx)
revidx hash
in
go ~revidx ~src (PDec.next_object state)
in
let rec gogo () =
match Queue.pop queue with
| exception Queue.Empty -> Lwt.return (Ok ())
| hunks_descr, hunks, buffer_hunks, inflated, hash_source -> (
read_inflated git hash_source
>>= function
| None ->
Queue.push
(hunks_descr, hunks, buffer_hunks, inflated, hash_source)
queue ;
gogo ()
| Some (kind, raw) -> (
match apply hunks_descr hunks buffer_hunks raw inflated with
| Ok result ->
write_inflated git ~kind result >>= fun _ -> gogo ()
| Error err -> Lwt.fail (Failure err) ) )
in
with_buffer git (fun {ztmp; window} ->
go ~revidx:Revidx.empty (PDec.default ztmp window) )
>>= function
| Error _ as err -> Lwt.return err
| Ok (hash_pack, n) -> (
let n = Int32.to_int n in
Queue.fold (fun acc x -> x :: acc) [] queue
|> Lwt_list.fold_left_s
(fun acc (_, _, _, _, hash) ->
mem git hash >|= function true -> acc + 1 | false -> acc )
0
>>= function
| 0 ->
if Queue.is_empty queue then Lwt.return (Ok (hash_pack, n))
else Lwt.return (Error `Unresolved_object)
| _ ->
let open Lwt_result in
gogo () >>= fun () -> Lwt.return (Ok (hash_pack, n)) )
end
module Ref = struct
module Graph = Reference.Map
let list t =
Log.debug (fun l -> l "Ref.list") ;
let graph, rest =
Hashtbl.fold
(fun k -> function `R ptr -> fun (a, r) -> a, (k, ptr) :: r
| `H hash -> fun (a, r) -> Graph.add k hash a, r )
t.refs (Graph.empty, [])
in
let graph =
List.fold_left
(fun a (k, ptr) ->
try
let v = Graph.find ptr a in
Graph.add k v a
with Not_found -> a )
graph rest
in
let r = Graph.fold (fun k v a -> (k, v) :: a) graph [] in
Lwt.return r
let mem t r =
Log.debug (fun l -> l "Ref.mem %a" Reference.pp r) ;
try
let _ = Hashtbl.find t.refs r in
Lwt.return true
with Not_found -> Lwt.return false
let rec resolve t r =
Log.debug (fun l -> l "Ref.read %a" Reference.pp r) ;
try
match Hashtbl.find t.refs r with
| `H s -> Lwt.return (Ok s)
| `R r -> resolve t r
with Not_found -> Lwt.return (Error `Not_found)
let read t r =
try
match Hashtbl.find t.refs r with
| `H hash -> Lwt.return (Ok (Reference.Hash hash))
| `R refname -> Lwt.return (Ok (Reference.Ref refname))
with Not_found -> Lwt.return (Error `Not_found)
let remove t r =
Log.debug (fun l -> l "Ref.remove %a" Reference.pp r) ;
Hashtbl.remove t.refs r ;
Lwt.return (Ok ())
let write t r value =
Log.debug (fun l -> l "Ref.write %a" Reference.pp r) ;
let head_contents =
match value with
| Reference.Hash hash -> `H hash
| Reference.Ref refname -> `R refname
in
Hashtbl.replace t.refs r head_contents ;
Lwt.return (Ok ())
end
let has_global_watches = false
let has_global_checkout = false
end
module Store = Make (Digestif.SHA1) (Inflate) (Deflate)