package git

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file collector.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
(*
 * 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.
 *)

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

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

  module PEnc : Pack.P with module Hash := Hash and module Deflate := Deflate

  type t
  type error = private [> `Delta of PEnc.Delta.error]
  type kind = [`Commit | `Tree | `Tag | `Blob]

  val pp_error : error Fmt.t
  val read_inflated : t -> Hash.t -> (kind * Cstruct.t) option Lwt.t
  val contents : t -> ((Hash.t * Value.t) list, error) result Lwt.t
end

module Make (S : STORE) = struct
  module Store = S

  module Log = struct
    let src = Logs.Src.create "git.gc" ~doc:"logs git's GC event"

    include (val Logs.src_log src : Logs.LOG)
  end

  let cstruct_copy cs =
    let ln = Cstruct.len cs in
    let rs = Cstruct.create ln in
    Cstruct.blit cs 0 rs 0 ln ; rs

  let delta ?(window = `Object 10) ?(depth = 50) git objects =
    let names = Hashtbl.create 1024 in
    let memory, window =
      match window with `Memory v -> true, v | `Object v -> false, v
    in
    let make (hash, value) =
      let name = try Some (Hashtbl.find names hash) with Not_found -> None in
      let kind =
        match value with
        | Store.Value.Commit _ -> Pack.Kind.Commit
        | Store.Value.Tree _ -> Pack.Kind.Tree
        | Store.Value.Tag _ -> Pack.Kind.Tag
        | Store.Value.Blob _ -> Pack.Kind.Blob
      in
      let entry =
        S.PEnc.Entry.make hash ?name kind (Store.Value.length value)
      in
      Log.debug (fun l ->
          l ~header:"delta" "Add the object %a in the new PACK file." S.Hash.pp
            hash ) ;
      entry
    in
    let canonicalize entries =
      List.fold_left
        (fun acc o ->
          let hash = Store.Value.digest o in
          S.Hash.Map.add hash o acc )
        S.Hash.Map.empty entries
      |> S.Hash.Map.bindings
    in
    let ( >?= ) a f = Lwt_result.map_err f a in
    canonicalize objects
    |> fun objects ->
    List.iter
      (function
        | _, Store.Value.Tree tree ->
            Store.Value.Tree.iter
              (fun entry ->
                Hashtbl.add names entry.Store.Value.Tree.node
                  entry.Store.Value.Tree.name )
              tree
        | _ -> ())
      objects
    |> fun () ->
    List.map make objects
    |> fun entries ->
    S.PEnc.Delta.deltas ~memory entries
      Lwt.Infix.(
        fun hash ->
          Log.debug (fun l ->
              l ~header:"delta" "Ask to try to delta-ify the object %a."
                S.Hash.pp hash ) ;
          Store.read_inflated git hash
          >|= function
          | Some (_, raw) -> Some (cstruct_copy raw) | None -> None)
      (fun _ -> false)
      window depth
    >?= fun err -> (`Delta err :> S.error)

  let make ?window ?depth git objects =
    let open Lwt.Infix in
    let ztmp = Cstruct.create 0x8000 in
    delta ?window ?depth git objects
    >>= function
    | Ok entries ->
        let state = S.PEnc.default ztmp entries in
        Lwt.return (Ok state)
    | Error _ as err -> Lwt.return err

  exception PackEncoder of S.PEnc.error

  type stream = unit -> Cstruct.t option Lwt.t

  let make_stream git ?(window = `Object 10) ?(depth = 50) objects =
    let open Lwt.Infix in
    make git ~window ~depth objects
    >>= function
    | Error _ as err -> Lwt.return err
    | Ok state ->
        let dtmp = Cstruct.create 0x8000 in
        let empty = Cstruct.create 0 in
        let value ~default = function Some x -> x | None -> default in
        let mvar = Lwt_mvar.create_empty () in
        let src = ref None in
        let state = ref state in
        let write = ref 0 in
        let rec stream () =
          match S.PEnc.eval (value ~default:empty !src) dtmp !state with
          | `Flush state' ->
              write := !write + S.PEnc.used_out state' ;
              state := S.PEnc.flush 0 (Cstruct.len dtmp) state' ;
              Lwt.return (Some (Cstruct.sub dtmp 0 (S.PEnc.used_out state')))
          | `End (state', _) ->
              if S.PEnc.used_out state' > 0 then (
                state := S.PEnc.flush 0 0 state' ;
                Lwt.return (Some (Cstruct.sub dtmp 0 (S.PEnc.used_out state'))) )
              else (
                state := state' ;
                let graph =
                  S.Hash.Map.fold
                    (fun key value acc -> S.Hash.Map.add key value acc)
                    (S.PEnc.idx state') S.Hash.Map.empty
                in
                (* XXX(dinosaure): can be optimized because structurally the
                   same. *)
                Lwt_mvar.put mvar graph >>= fun () -> Lwt.return None )
          | `Error (state', err) ->
              state := state' ;
              Lwt.fail (PackEncoder err)
          | `Await state' -> (
            match !src with
            | Some _ ->
                src := None ;
                state := S.PEnc.finish state' ;
                stream ()
            | None ->
                let expect = S.PEnc.expect state' in
                Store.read_inflated git expect
                >>= (function
                      | Some (_, raw) ->
                          src := Some raw ;
                          Lwt.return raw
                      | None -> assert false)
                >>= fun raw ->
                state := S.PEnc.refill 0 (Cstruct.len raw) state' ;
                stream () )
        in
        Lwt.return (Ok (stream, mvar))
end