Source file value.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
(*
 * Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org>
 * and Romain Calascibetta <romain.calascibetta@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

let src =
  Logs.Src.create "git.value" ~doc:"logs git's internal value computation"

module Log = (val Logs.src_log src : Logs.LOG)

module type S = sig
  module Hash : S.HASH
  module Inflate : S.INFLATE
  module Deflate : S.DEFLATE
  module Blob : Blob.S with module Hash := Hash
  module Commit : Commit.S with module Hash := Hash
  module Tree : Tree.S with module Hash := Hash
  module Tag : Tag.S with module Hash := Hash

  type t =
    | Blob of Blob.t
    | Commit of Commit.t
    | Tree of Tree.t
    | Tag of Tag.t

  val blob : Blob.t -> t
  val commit : Commit.t -> t
  val tree : Tree.t -> t
  val tag : Tag.t -> t
  val kind : t -> [`Commit | `Blob | `Tree | `Tag]
  val pp_kind : [`Commit | `Blob | `Tree | `Tag] Fmt.t

  module MakeMeta (Meta : Encore.Meta.S) : sig
    val commit : t Meta.t
    val blob : t Meta.t
    val tree : t Meta.t
    val tag : t Meta.t
    val p : t Meta.t
  end

  module A : sig
    include S.DESC with type 'a t = 'a Angstrom.t and type e = t

    val kind : [`Commit | `Blob | `Tree | `Tag] t
    val length : int64 t
  end

  module M : S.DESC with type 'a t = 'a Encore.Encoder.t and type e = t

  module D :
    S.DECODER
    with type t = t
     and type init = Inflate.window * Cstruct.t * Cstruct.t
     and type error = [Error.Decoder.t | `Inflate of Inflate.error]

  module E :
    S.ENCODER
    with type t = t
     and type init = Cstruct.t * t * int * Cstruct.t
     and type error = [`Deflate of Deflate.error]

  include S.DIGEST with type t := t and type hash := Hash.t
  include S.BASE with type t := t

  val length : t -> int64
end

module type RAW = sig
  module Hash : S.HASH
  module Inflate : S.INFLATE
  module Deflate : S.DEFLATE

  module Value :
    S
    with module Hash := Hash
     and module Inflate := Inflate
     and module Deflate := Deflate

  include module type of Value

  module EncoderRaw :
    S.ENCODER
    with type t = t
     and type init = Cstruct.t * t
     and type error = Error.never

  module DecoderRaw :
    S.DECODER
    with type t = t
     and type init = Cstruct.t
     and type error = Error.Decoder.t

  module EncoderWithoutHeader :
    S.ENCODER
    with type t = t
     and type init = Cstruct.t * t
     and type error = Error.never

  val to_deflated_raw :
       raw:Cstruct.t
    -> etmp:Cstruct.t
    -> ?level:int
    -> ztmp:Cstruct.t
    -> t
    -> (string, E.error) result

  val to_raw : raw:Cstruct.t -> etmp:Cstruct.t -> t -> (string, EncoderRaw.error) result

  val to_raw_without_header :
    raw:Cstruct.t -> etmp:Cstruct.t -> t -> (string, EncoderWithoutHeader.error) result

  val of_raw :
       kind:[`Commit | `Blob | `Tree | `Tag]
    -> Cstruct.t
    -> (t, Error.Decoder.t) result

  val of_raw_with_header : Cstruct.t -> (t, DecoderRaw.error) result
end

module Make (Hash : S.HASH) (Inflate : S.INFLATE) (Deflate : S.DEFLATE) :
  S
  with module Hash := Hash
   and module Inflate := Inflate
   and module Deflate := Deflate
   and module Blob = Blob.Make(Hash)
   and module Commit = Commit.Make(Hash)
   and module Tree = Tree.Make(Hash)
   and module Tag = Tag.Make(Hash) = struct
  module Blob = Blob.Make (Hash)
  module Commit = Commit.Make (Hash)
  module Tree = Tree.Make (Hash)
  module Tag = Tag.Make (Hash)

  type t =
    | Blob of Blob.t
    | Commit of Commit.t
    | Tree of Tree.t
    | Tag of Tag.t

  let blob blob = Blob blob

  (* blob *)
  let commit commit = Commit commit
  let tree tree = Tree tree
  let tag tag = Tag tag

  let kind = function
    | Commit _ -> `Commit
    | Blob _ -> `Blob
    | Tree _ -> `Tree
    | Tag _ -> `Tag

  let pp_kind ppf = function
    | `Commit -> Fmt.pf ppf "commit"
    | `Tree -> Fmt.pf ppf "tree"
    | `Tag -> Fmt.pf ppf "tag"
    | `Blob -> Fmt.pf ppf "blob"

  let pp ppf = function
    | Blob blob -> Fmt.pf ppf "(Blob %a)" (Fmt.hvbox Blob.pp) blob
    | Commit commit -> Fmt.pf ppf "(Commit %a)" (Fmt.hvbox Commit.pp) commit
    | Tree tree -> Fmt.pf ppf "(Tree %a)" (Fmt.hvbox Tree.pp) tree
    | Tag tag -> Fmt.pf ppf "(Tag %a)" (Fmt.hvbox Tag.pp) tag

  module MakeMeta (Meta : Encore.Meta.S) = struct
    type e = t

    open Helper.BaseIso

    module Iso = struct
      open Encore.Bijection

      let kind =
        make_exn
          ~fwd:(function
            | "tree" -> `Tree
            | "blob" -> `Blob
            | "commit" -> `Commit
            | "tag" -> `Tag
            | _ -> Exn.fail ())
          ~bwd:(function
            | `Tree -> "tree"
            | `Blob -> "blob"
            | `Tag -> "tag"
            | `Commit -> "commit")

      let value =
        make_exn
          ~fwd:(fun (kind, _, value) ->
            match kind, value with
            | `Tree, Tree _ -> value
            | `Commit, Commit _ -> value
            | `Blob, Blob _ -> value
            | `Tag, Tag _ -> value
            | _, _ -> Exn.fail ())
          ~bwd:(function
            | Tree tree -> `Tree, Tree.length tree, Tree tree
            | Commit commit -> `Commit, Commit.length commit, Commit commit
            | Tag tag -> `Tag, Tag.length tag, Tag tag
            | Blob blob -> `Blob, Blob.length blob, Blob blob)
    end

    module Commit = Commit.MakeMeta (Meta)
    module Blob = Blob.MakeMeta (Meta)
    module Tree = Tree.MakeMeta (Meta)
    module Tag = Tag.MakeMeta (Meta)

    type 'a t = 'a Meta.t

    module Meta = Encore.Meta.Make (Meta)
    open Encore.Bijection
    open Meta

    let is_digit = function '0' .. '9' -> true | _ -> false
    let length = int64 <$> while0 is_digit

    let kind =
      Iso.kind
      <$> (const "tree" <|> const "commit" <|> const "blob" <|> const "tag")

    let commit =
      make_exn
        ~fwd:(fun commit -> Commit commit)
        ~bwd:(function
          | Commit commit -> commit | _ -> Exn.fail ())
      <$> Commit.p

    let blob =
      make_exn
        ~fwd:(fun blob -> Blob blob)
        ~bwd:(function Blob blob -> blob | _ -> Exn.fail ())
      <$> Blob.p

    let tree =
      make_exn
        ~fwd:(fun tree -> Tree tree)
        ~bwd:(function Tree tree -> tree | _ -> Exn.fail ())
      <$> Tree.p

    let tag =
      make_exn
        ~fwd:(fun tag -> Tag tag)
        ~bwd:(function Tag tag -> tag | _ -> Exn.fail ())
      <$> Tag.p

    let p =
      let value kind p =
        Iso.kind
        <$> const kind
        <* (char_elt ' ' <$> any)
        <*> (length <* (char_elt '\000' <$> any))
        <*> p
      in
      Exn.compose obj3 Iso.value
      <$> ( value "commit" commit
          <|> value "tree" tree
          <|> value "blob" blob
          <|> value "tag" tag )
  end

  module A = MakeMeta (Encore.Proxy_decoder.Impl)
  module M = MakeMeta (Encore.Proxy_encoder.Impl)

  let length = function
    | Commit commit -> Commit.length commit
    | Tag tag -> Tag.length tag
    | Tree tree -> Tree.length tree
    | Blob blob -> Blob.length blob

  module D = Helper.MakeInflater (Inflate) (A)
  module E = Helper.MakeDeflater (Deflate) (M)

  let digest = function
    | Blob blob -> Blob.digest blob
    | Commit commit -> Commit.digest commit
    | Tree tree -> Tree.digest tree
    | Tag tag -> Tag.digest tag

  let equal = ( = )
  let hash = Hashtbl.hash

  let int_of_kind = function
    | Commit _ -> 0
    | Tree _ -> 1
    | Blob _ -> 2
    | Tag _ -> 3

  let compare a b =
    match a, b with
    | Commit a, Commit b -> Commit.compare a b
    | Blob a, Blob b -> Blob.compare a b
    | Tree a, Tree b -> Tree.compare a b
    | Tag a, Tag b -> Tag.compare a b
    | ( ((Commit _ | Blob _ | Tree _ | Tag _) as a)
      , ((Commit _ | Blob _ | Tree _ | Tag _) as b) ) ->
        if int_of_kind a > int_of_kind b then -1
        else if int_of_kind a < int_of_kind b then 1
        else if length a > length b then -1
        else if length a < length b then 1
        else Stdlib.compare a b

  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

module Raw (Hash : S.HASH) (Inflate : S.INFLATE) (Deflate : S.DEFLATE) = struct
  module Value = Make (Hash) (Inflate) (Deflate)
  include Value
  module DecoderRaw = Helper.MakeDecoder (A)
  module EncoderRaw = Helper.MakeEncoder (M)

  module MakeWithoutHeader (Meta : Encore.Meta.S) = struct
    type e = t
    type 'a t = 'a Meta.t

    module Meta = Encore.Meta.Make (Meta)
    module ValueMeta = MakeMeta (Meta)
    open Meta
    open ValueMeta

    let p = tag <|> commit <|> tree <|> blob
  end

  module DecoderWithoutHeader =
    Helper.MakeDecoder (MakeWithoutHeader (Encore.Proxy_decoder.Impl))

  module EncoderWithoutHeader =
    Helper.MakeEncoder (MakeWithoutHeader (Encore.Proxy_encoder.Impl))

  module type ENCODER = sig
    type state
    type raw
    type result
    type error

    val raw_length : raw -> int
    val raw_sub : raw -> int -> int -> raw

    val eval :
         raw
      -> state
      -> [`Flush of state | `End of state * result | `Error of state * error]

    val used : state -> int
    val flush : int -> int -> state -> state
  end

  type ('state, 'raw, 'result, 'error) encoder =
    (module
     ENCODER
       with type state = 'state
        and type raw = 'raw
        and type result = 'result
        and type error = 'error)

  let to_ (type state res err_encoder)
      (encoder : (state, Cstruct.t, res, err_encoder) encoder)
      (buffer : Cstruct_buffer.t) (raw : Cstruct.t) (state : state) :
      (string, err_encoder) result =
    let module E = ( val encoder
                       : ENCODER
                       with type state = state
                        and type raw = Cstruct.t
                        and type result = res
                        and type error = err_encoder )
    in
    let rec go state =
      match E.eval raw state with
      | `Error (_, err) -> Error err
      | `End (state, _) ->
          if E.used state > 0 then
            Cstruct_buffer.add buffer (E.raw_sub raw 0 (E.used state)) ;
          Ok (Cstruct_buffer.contents buffer)
      | `Flush state ->
          if E.used state > 0 then
            Cstruct_buffer.add buffer (E.raw_sub raw 0 (E.used state)) ;
          go (E.flush 0 (E.raw_length raw) state)
    in
    go state

  let to_deflated_raw ~raw ~etmp ?(level = 4) ~ztmp value =
    let encoder = E.default (etmp, value, level, ztmp) in
    let buffer = Cstruct_buffer.create (Int64.to_int (length value)) in
    (* XXX(dinosaure): it's an heuristic to consider than the size of the
       result is lower than [F.length value]. In most of cases, it's true but
       sometimes, a deflated Git object can be bigger than a serialized Git
       object. *)
    let module SpecializedEncoder = struct
      type state = E.encoder
      type raw = Cstruct.t
      type result = int
      type error = E.error

      let raw_length = Cstruct.len
      let raw_sub = Cstruct.sub

      type rest = [`Flush of state | `End of state * result]

      let eval raw state =
        match E.eval raw state with
        | #rest as rest -> rest
        | `Error err -> `Error (state, err)

      let used = E.used
      let flush = E.flush
    end in
    to_ (module SpecializedEncoder) buffer raw encoder

  let to_raw ~raw ~etmp value =
    let encoder = EncoderRaw.default (etmp, value) in
    let buffer = Cstruct_buffer.create (Int64.to_int (length value)) in
    (* XXX(dinosaure): we are sure than the serialized object has the size
       [F.length value]. So, the [buffer] should not growth. *)
    let module SpecializedEncoder = struct
      type state = EncoderRaw.encoder
      type raw = Cstruct.t
      type result = int
      type error = EncoderRaw.error

      let raw_length = Cstruct.len
      let raw_sub = Cstruct.sub

      type rest = [`Flush of state | `End of state * result]

      let eval raw state =
        match EncoderRaw.eval raw state with
        | #rest as rest -> rest
        | `Error err -> `Error (state, err)

      let used = EncoderRaw.used
      let flush = EncoderRaw.flush
    end in
    to_ (module SpecializedEncoder) buffer raw encoder

  let to_raw_without_header ~raw ~etmp value =
    let encoder = EncoderWithoutHeader.default (etmp, value) in
    let buffer = Cstruct_buffer.create (Int64.to_int (length value)) in
    (* XXX(dinosaure): we are sure than the serialized object has the size
       [F.length value]. So, the [buffer] should not growth. *)
    let module SpecializedEncoder = struct
      type state = EncoderWithoutHeader.encoder
      type raw = Cstruct.t
      type result = int
      type error = EncoderWithoutHeader.error

      let raw_length = Cstruct.len
      let raw_sub = Cstruct.sub

      type rest = [`Flush of state | `End of state * result]

      let eval raw state =
        match EncoderWithoutHeader.eval raw state with
        | #rest as rest -> rest
        | `Error err -> `Error (state, err)

      let used = EncoderWithoutHeader.used
      let flush = EncoderWithoutHeader.flush
    end in
    to_ (module SpecializedEncoder) buffer raw encoder

  let of_raw_with_header inflated = DecoderRaw.to_result inflated

  let of_raw ~kind inflated =
    match kind with
    | `Commit ->
        Rresult.R.map
          (fun commit -> Commit commit)
          (Value.Commit.D.to_result inflated)
    | `Tree ->
        Rresult.R.map (fun tree -> Tree tree) (Value.Tree.D.to_result inflated)
    | `Tag ->
        Rresult.R.map (fun tag -> Tag tag) (Value.Tag.D.to_result inflated)
    | `Blob ->
        let blob blob = Blob blob in
        Rresult.R.(get_ok (Value.Blob.D.to_result inflated) |> blob |> ok)
end