package mgit

  1. Overview
  2. Docs

Source file mgit_object.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
(* NOTE(dinosaure): ocaml-git for a poor man... *)
(* NOTE(dinosaure): One lesson learned from ocaml-git is that it isn't really
   necessary to define an abstract [type t] for all the Git objects you're
   working with; instead, it's better to be a bit more direct and extract the
   information you need from the objects as they exist in memory or on the
   block device. *)

module SHA1 = Digestif.SHA1

type error = [ `Msg of string ]

let pp_error ppf = function `Msg msg -> Fmt.string ppf msg
let error_msgf fmt = Fmt.kstr (fun msg -> Error (`Msg msg)) fmt
let ( let* ) = Result.bind
let ref_length = SHA1.digest_size

let uid_of_hex hex =
  match Ohex.decode hex with
  | str when String.length str = ref_length ->
      Ok (Carton.Uid.unsafe_of_string str)
  | _ -> error_msgf "Invalid object identifier: %S" hex
  | exception _ -> error_msgf "Invalid object identifier: %S" hex

let uid_of_hex_exn hex =
  match uid_of_hex hex with
  | Ok uid -> uid
  | Error (`Msg msg) -> invalid_arg msg

let hex_of_uid (uid : Carton.Uid.t) = Ohex.encode (uid :> string)
let pp_uid ppf uid = Fmt.string ppf (hex_of_uid uid)
let decode_opt str = try Some (Ohex.decode str) with _exn -> None

let string_of_kind = function
  | `A -> "commit"
  | `B -> "tree"
  | `C -> "blob"
  | `D -> "tag"

let kind_of_string = function
  | "commit" -> Ok `A
  | "tree" -> Ok `B
  | "blob" -> Ok `C
  | "tag" -> Ok `D
  | str -> error_msgf "Invalid object kind: %S" str

(* The Git object identifier: SHA-1 of the {i loose} framing
   ["<kind> <length>\000"] followed by the payload — exactly what
   [git hash-object] computes. *)
let digest ~kind payload =
  let hdr = Fmt.str "%s %d\000" (string_of_kind kind) (String.length payload) in
  let ctx = SHA1.feed_string SHA1.empty hdr in
  let ctx = SHA1.feed_string ctx payload in
  Carton.Uid.unsafe_of_string (SHA1.to_raw_string (SHA1.get ctx))

let identify =
  let init kind (len : Carton.Size.t) =
    let hdr = Fmt.str "%s %d\000" (string_of_kind kind) (len :> int) in
    SHA1.feed_string SHA1.empty hdr
  in
  let feed bstr ctx = SHA1.feed_bigstring ctx bstr in
  let serialize ctx =
    Carton.Uid.unsafe_of_string (SHA1.to_raw_string (SHA1.get ctx))
  in
  { Carton.First_pass.init; feed; serialize }

let digest_pack () =
  let feed_bytes buf ~off ~len ctx = SHA1.feed_bytes ctx ~off ~len buf in
  let feed_bigstring bstr ctx = SHA1.feed_bigstring ctx bstr in
  let serialize ctx = SHA1.to_raw_string (SHA1.get ctx) in
  let hash =
    {
      Carton.First_pass.feed_bytes
    ; feed_bigstring
    ; serialize
    ; length= SHA1.digest_size
    }
  in
  Carton.First_pass.Digest (hash, SHA1.empty)

(* Users *)

module User = struct
  type t = { name: string; email: string; date: int * int option }

  let string_of_tz = function
    | None -> "+0000"
    | Some minutes ->
        let sign = if minutes < 0 then '-' else '+' in
        let minutes = abs minutes in
        Fmt.str "%c%02d%02d" sign (minutes / 60) (minutes mod 60)

  let to_string { name; email; date= secs, tz } =
    Fmt.str "%s <%s> %d %s" name email secs (string_of_tz tz)

  let tz_of_string str =
    if String.length str <> 5 then None
    else
      let sign =
        match str.[0] with '-' -> Some (-1) | '+' -> Some 1 | _ -> None
      in
      match
        ( sign
        , int_of_string_opt (String.sub str 1 2)
        , int_of_string_opt (String.sub str 3 2) )
      with
      | Some sign, Some hh, Some mm -> Some (sign * ((hh * 60) + mm))
      | _ -> None

  let of_string str =
    match (String.index_opt str '<', String.rindex_opt str '>') with
    | Some i, Some j when i < j ->
        let name = String.trim (String.sub str 0 i) in
        let email = String.sub str (i + 1) (j - i - 1) in
        let rest = String.sub str (j + 1) (String.length str - j - 1) in
        let fn = ( <> ) "" in
        let rest =
          List.filter fn (String.split_on_char ' ' (String.trim rest))
        in
        begin match rest with
        | [] -> Ok { name; email; date= (0, None) }
        | secs :: rest ->
            begin match int_of_string_opt secs with
            | None -> error_msgf "Invalid date in %S" str
            | Some secs ->
                let tz =
                  match rest with tz :: _ -> tz_of_string tz | [] -> None
                in
                Ok { name; email; date= (secs, tz) }
            end
        end
    | _ -> error_msgf "Invalid user: %S" str

  let pp ppf t = Fmt.string ppf (to_string t)
end

(* Trees *)

module Tree = struct
  type perm = [ `Normal | `Exec | `Everybody | `Link | `Dir | `Commit ]
  type entry = { perm: perm; name: string; node: Carton.Uid.t }
  type t = entry list (* kept sorted, see [order] *)

  let int_of_perm = function
    | `Normal -> 0o100644
    | `Exec -> 0o100755
    | `Everybody -> 0o100664
    | `Link -> 0o120000
    | `Dir -> 0o040000
    | `Commit -> 0o160000

  let perm_of_int = function
    | 0o100644 -> Ok `Normal
    | 0o100755 -> Ok `Exec
    | 0o100664 -> Ok `Everybody
    | 0o120000 -> Ok `Link
    | 0o040000 -> Ok `Dir
    | 0o160000 -> Ok `Commit
    | perm -> error_msgf "Invalid permission: %o" perm

  let key { perm; name; _ } = match perm with `Dir -> name ^ "/" | _ -> name
  let order a b = String.compare (key a) (key b)
  let empty = []
  let v entries = List.sort_uniq order entries
  let to_list t = t
  let is_empty t = t = []
  let find t name = List.find_opt (fun entry -> entry.name = name) t
  let remove t name = List.filter (fun entry -> entry.name <> name) t
  let add t entry = List.merge order [ entry ] (remove t entry.name)
  let uids t = List.map (fun { node; _ } -> node) t

  let of_string str =
    let rec go acc pos =
      if pos >= String.length str then Ok (v (List.rev acc))
      else
        match String.index_from_opt str pos '\000' with
        | None -> error_msgf "Malformed tree object"
        | Some nul ->
            if nul + ref_length >= String.length str then
              error_msgf "Malformed tree object"
            else
              begin match String.index_from_opt str pos ' ' with
              | Some sp when sp < nul ->
                  let perm = String.sub str pos (sp - pos) in
                  let name = String.sub str (sp + 1) (nul - sp - 1) in
                  let node = String.sub str (nul + 1) ref_length in
                  let node = Carton.Uid.unsafe_of_string node in
                  begin match int_of_string_opt ("0o" ^ perm) with
                  | None -> error_msgf "Invalid permission %S" perm
                  | Some perm ->
                      let* perm = perm_of_int perm in
                      go ({ perm; name; node } :: acc) (nul + 1 + ref_length)
                  end
              | _ -> error_msgf "Malformed tree object"
              end
    in
    go [] 0

  let to_string t =
    let buf = Buffer.create 0x100 in
    let fn { perm; name; node } =
      Buffer.add_string buf (Fmt.str "%o" (int_of_perm perm));
      Buffer.add_char buf ' ';
      Buffer.add_string buf name;
      Buffer.add_char buf '\000';
      Buffer.add_string buf (node :> string)
    in
    List.iter fn t; Buffer.contents buf

  let digest t = digest ~kind:`B (to_string t)
end

(* Headers, kind of similar to RFC 822 *)

let headers_of_string str =
  let hdrs, message =
    let rec go pos =
      match String.index_from_opt str pos '\n' with
      | None -> (String.sub str 0 (String.length str), None)
      | Some nl when nl = pos ->
          let msg = String.sub str (nl + 1) (String.length str - nl - 1) in
          (String.sub str 0 pos, Some msg)
      | Some nl -> go (nl + 1)
    in
    go 0
  in
  let lines =
    if hdrs = "" then []
    else
      let trimmed =
        if String.length hdrs > 0 && hdrs.[String.length hdrs - 1] = '\n' then
          String.sub hdrs 0 (String.length hdrs - 1)
        else hdrs
      in
      String.split_on_char '\n' trimmed
  in
  let rec fold acc = function
    | [] -> List.rev acc
    | line :: rest when String.length line > 0 && line.[0] = ' ' ->
        let value = String.sub line 1 (String.length line - 1) in
        begin match acc with
        | (key, values) :: acc -> fold ((key, value :: values) :: acc) rest
        | [] -> fold [ ("", [ value ]) ] rest
        end
    | line :: rest ->
        begin match String.index_opt line ' ' with
        | None -> fold ((line, []) :: acc) rest
        | Some sp ->
            let key = String.sub line 0 sp in
            let value =
              String.sub line (sp + 1) (String.length line - sp - 1)
            in
            fold ((key, [ value ]) :: acc) rest
        end
  in
  let hdrs = fold [] lines in
  (List.map (fun (key, values) -> (key, List.rev values)) hdrs, message)

let string_of_headers hdrs =
  let buf = Buffer.create 0x100 in
  let fn (key, values) =
    match values with
    | [] -> Buffer.add_string buf (key ^ "\n")
    | value :: rest ->
        Buffer.add_string buf (key ^ " " ^ value ^ "\n");
        List.iter (fun value -> Buffer.add_string buf (" " ^ value ^ "\n")) rest
  in
  List.iter fn hdrs; Buffer.contents buf

let single key hdrs =
  match List.assoc_opt key hdrs with
  | Some [ value ] -> Ok value
  | Some _ -> error_msgf "Multiple %S headers" key
  | None -> error_msgf "Missing %S header" key

(* Commits *)

module Commit = struct
  type t = {
      tree: Carton.Uid.t
    ; parents: Carton.Uid.t list
    ; author: User.t
    ; committer: User.t
    ; extra: (string * string list) list
    ; message: string option
  }

  let make ~tree ?(parents = []) ~author ~committer ?(extra = []) message =
    { tree; parents; author; committer; extra; message }

  let reserved = [ "tree"; "parent"; "author"; "committer" ]

  let of_string str =
    let hdrs, message = headers_of_string str in
    let* tree = single "tree" hdrs in
    let* tree = uid_of_hex tree in
    let parents =
      List.filter_map
        (function
          | "parent", [ hex ] -> Result.to_option (uid_of_hex hex) | _ -> None)
        hdrs
    in
    let* author = single "author" hdrs in
    let* author = User.of_string author in
    let* committer = single "committer" hdrs in
    let* committer = User.of_string committer in
    let extra =
      List.filter (fun (key, _) -> not (List.mem key reserved)) hdrs
    in
    Ok { tree; parents; author; committer; extra; message }

  let to_string { tree; parents; author; committer; extra; message } =
    let hdrs =
      [ ("tree", [ hex_of_uid tree ]) ]
      @ List.map (fun uid -> ("parent", [ hex_of_uid uid ])) parents
      @ [
          ("author", [ User.to_string author ])
        ; ("committer", [ User.to_string committer ])
        ]
      @ extra
    in
    let hdrs = string_of_headers hdrs in
    match message with None -> hdrs | Some message -> hdrs ^ "\n" ^ message

  let digest t = digest ~kind:`A (to_string t)
end

(* Tags *)

module Tag = struct
  type t = {
      obj: Carton.Uid.t
    ; kind: Carton.Kind.t
    ; tag: string
    ; tagger: User.t option
    ; message: string option
  }

  let of_string str =
    let hdrs, message = headers_of_string str in
    let* obj = single "object" hdrs in
    let* obj = uid_of_hex obj in
    let* kind = single "type" hdrs in
    let* kind = kind_of_string kind in
    let* tag = single "tag" hdrs in
    let* tagger =
      match List.assoc_opt "tagger" hdrs with
      | Some [ tagger ] ->
          let* tagger = User.of_string tagger in
          Ok (Some tagger)
      | _ -> Ok None
    in
    Ok { obj; kind; tag; tagger; message }

  let to_string { obj; kind; tag; tagger; message } =
    let hdrs =
      [
        ("object", [ hex_of_uid obj ]); ("type", [ string_of_kind kind ])
      ; ("tag", [ tag ])
      ]
      @
      match tagger with
      | Some tagger -> [ ("tagger", [ User.to_string tagger ]) ]
      | None -> []
    in
    let hdrs = string_of_headers hdrs in
    match message with None -> hdrs | Some message -> hdrs ^ "\n" ^ message

  let digest t = digest ~kind:`D (to_string t)
end

(* Fast decoders *)

(* tree <hex>\n *)
let tree_of_commit str =
  match String.index_opt str '\n' with
  | None -> None
  | Some eol ->
      begin match String.split_on_char ' ' (String.sub str 0 eol) with
      | [ "tree"; hex ] ->
          decode_opt hex |> Option.map Carton.Uid.unsafe_of_string
      | _ -> None
      end

(* parent <hex>\n, right after the [tree] line *)
let parents_of_commit str =
  let rec go acc pos =
    match String.index_from_opt str pos '\n' with
    | None -> List.rev acc
    | Some eol ->
        begin match
          String.split_on_char ' ' (String.sub str pos (eol - pos))
        with
        | [ "parent"; hex ] ->
            begin match decode_opt hex with
            | Some uid -> go (Carton.Uid.unsafe_of_string uid :: acc) (eol + 1)
            | None -> List.rev acc
            end
        | [ "tree"; _ ] -> go acc (eol + 1)
        | _ -> List.rev acc
        end
  in
  go [] 0

(* object <hex>\n *)
let target_of_tag str =
  match String.index_opt str '\n' with
  | None -> None
  | Some eol ->
      begin match String.split_on_char ' ' (String.sub str 0 eol) with
      | [ "object"; hex ] ->
          decode_opt hex |> Option.map Carton.Uid.unsafe_of_string
      | _ -> None
      end

let entries str = Result.map Tree.to_list (Tree.of_string str)

let links ~kind str =
  match kind with
  | `A ->
      let tree = Option.to_list (tree_of_commit str) in
      tree @ parents_of_commit str
  | `B ->
      begin match Tree.of_string str with
      | Ok tree -> Tree.uids tree
      | Error _ -> []
      end
  | `C -> []
  | `D -> Option.to_list (target_of_tag str)