package volgo

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

Source file volgo_stdlib.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
(*******************************************************************************)
(*  Volgo - a Versatile OCaml Library for Git Operations                       *)
(*  Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com>          *)
(*                                                                             *)
(*  This file is part of Volgo.                                                *)
(*                                                                             *)
(*  Volgo is free software; you can redistribute it and/or modify it under     *)
(*  the terms of the GNU Lesser General Public License as published by the     *)
(*  Free Software Foundation either version 3 of the License, or any later     *)
(*  version, with the LGPL-3.0 Linking Exception.                              *)
(*                                                                             *)
(*  Volgo is distributed in the hope that it will be useful, but WITHOUT ANY   *)
(*  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *)
(*  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and    *)
(*  the file `NOTICE.md` at the root of this repository for more details.      *)
(*                                                                             *)
(*  You should have received a copy of the GNU Lesser General Public License   *)
(*  and the LGPL-3.0 Linking Exception along with this library. If not, see    *)
(*  <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.       *)
(*******************************************************************************)

(* Some functions are copied from [Base] version [v0.17] which is released
   under MIT and may be found at [https://github.com/janestreet/base].

   See Base's LICENSE below:

   ----------------------------------------------------------------------------

   The MIT License

   Copyright (c) 2016--2024 Jane Street Group, LLC <opensource-contacts@janestreet.com>

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.

   ----------------------------------------------------------------------------

   When this is the case, we clearly indicate it next to the copied function. *)

open! Stdlib_compat
module Code_error = Code_error
module Dyn = Dyn0

module Dynable = struct
  module type S = sig
    type t

    val to_dyn : t -> Dyn.t
  end
end

let print pp = Format.printf "%a@." Pp.to_fmt pp
let print_dyn dyn = print (Dyn.pp dyn)
let phys_equal a b = a == b

module Ordering = struct
  include Ordering

  let to_dyn = function
    | Lt -> Dyn.Variant ("Lt", [])
    | Eq -> Dyn.Variant ("Eq", [])
    | Gt -> Dyn.Variant ("Gt", [])
  ;;
end

module Sexp = struct
  include Sexp

  let rec to_dyn = function
    | Sexp.Atom s -> Dyn.variant "Atom" [ Dyn.string s ]
    | Sexp.List l -> Dyn.variant "List" [ Dyn.list to_dyn l ]
  ;;
end

module type To_sexpable = sig
  type t

  val sexp_of_t : t -> Sexp.t
end

let sexp_field' (type a) (sexp_of_a : a -> Sexp.t) field a =
  Sexp.List [ Atom field; sexp_of_a a ]
;;

let sexp_field (type a) (module M : To_sexpable with type t = a) field a =
  sexp_field' M.sexp_of_t field a
;;

module Absolute_path = struct
  include Absolute_path

  let to_dyn t = Dyn.string (Absolute_path.to_string t)
end

module Array = struct
  include ArrayLabels

  let sexp_of_t = Sexplib0.Sexp_conv.sexp_of_array
  let create ~len a = make len a

  let filter_mapi t ~f =
    let out_count = ref 0 in
    let out = ref [] in
    iteri t ~f:(fun i a ->
      match f i a with
      | None -> ()
      | Some e ->
        incr out_count;
        out := e :: !out);
    match !out with
    | [] -> [||]
    | hd :: _ ->
      let out_count = !out_count in
      let res = create ~len:out_count hd in
      List.iteri (fun i a -> res.(out_count - 1 - i) <- a) !out;
      res
  ;;

  let rev a =
    let len = length a in
    let res = create ~len a.(0) in
    iteri a ~f:(fun i x -> res.(len - 1 - i) <- x);
    res
  ;;

  let sort t ~compare = sort t ~cmp:compare

  let[@tail_mod_cons] rec to_list_mapi_aux t ~f ~index ~len =
    if index >= len
    then []
    else (
      let elt = Array.unsafe_get t index in
      (* Coverage is off in the second part of the expression because the
         instrumentation breaks [@tail_mod_cons], triggering warning 71. *)
      f index elt :: (to_list_mapi_aux t ~f ~index:(index + 1) ~len [@coverage off]))
  ;;

  let to_list_mapi t ~f = to_list_mapi_aux t ~f ~index:0 ~len:(Array.length t)
end

module Bool = struct
  include Bool

  let to_dyn = Dyn.bool
end

module Char = struct
  include Char

  let is_alphanum = function
    | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' -> true
    | _ -> false
  ;;

  let is_whitespace = function
    | '\t' | '\n' | '\011' (* vertical tab *) | '\012' (* form feed *) | '\r' | ' ' ->
      true
    | _ -> false
  ;;
end

module Fsegment = struct
  include Fsegment

  let to_dyn t = Dyn.string (Fsegment.to_string t)
end

module Hashtbl = struct
  include (
    MoreLabels.Hashtbl :
      module type of MoreLabels.Hashtbl with module Make := MoreLabels.Hashtbl.Make)

  module type S_extended = sig
    include MoreLabels.Hashtbl.S

    val add_exn : 'a t -> key:key -> data:'a -> unit
    val add_multi : 'a list t -> key:key -> data:'a -> unit
    val find : 'a t -> key -> 'a option
    val set : 'a t -> key:key -> data:'a -> unit
  end

  exception E of Sexp.t

  let () =
    Sexplib0.Sexp_conv.Exn_converter.add [%extension_constructor E] (function
      | E sexp -> sexp
      | _ -> assert false)
  ;;

  module Make (H : sig
      include Hashtbl.HashedType

      val sexp_of_t : t -> Sexp.t
    end) =
  struct
    include MoreLabels.Hashtbl.Make (H)

    let add_exn t ~key ~data =
      if mem t key
      then
        raise
          (E
             (List
                [ Atom "Hashtbl.add_exn: key already present"
                ; sexp_field (module H) "key" key
                ]))
      else add t ~key ~data
    ;;

    let add_multi t ~key ~data =
      let data =
        match find_opt t key with
        | None -> [ data ]
        | Some l -> data :: l
      in
      replace t ~key ~data
    ;;

    let find = find_opt
    let set = replace
  end
end

module Int = struct
  include Int

  let incr = incr
  let max_value = max_int
  let of_string_opt = int_of_string_opt

  let to_string_hum n =
    let s = string_of_int n in
    let len = String.length s in
    let is_negative = n < 0 in
    let sign_count = if is_negative then 1 else 0 in
    let absolute_digit_count = if is_negative then len - 1 else len in
    let separator_count = absolute_digit_count / 3 in
    let initial_skip_count =
      let digit_skip = absolute_digit_count mod 3 in
      sign_count + if digit_skip > 0 then digit_skip else 3
    in
    let buffer = Buffer.create (len + separator_count) in
    let rec aux i count =
      if i < len
      then
        if count = 0
        then (
          Buffer.add_char buffer '_';
          aux i 3)
        else (
          Buffer.add_char buffer s.[i];
          aux (i + 1) (count - 1))
    in
    aux 0 initial_skip_count;
    Buffer.contents buffer
  ;;

  let to_dyn t = Dyn.Int t
  let sexp_of_t t = Sexp.Atom (to_string_hum t)
end

module List = struct
  include ListLabels

  let sexp_of_t = Sexplib0.Sexp_conv.sexp_of_list
  let concat_map t ~f = concat_map ~f t
  let dedup_and_sort t ~compare = sort_uniq t ~cmp:compare

  let hd = function
    | [] -> None
    | hd :: _ -> Some hd
  ;;

  let filter_opt t = filter_map t ~f:Fun.id
  let find t ~f = find_opt t ~f
  let find_map t ~f = find_map ~f t
  let fold t ~init ~f = fold_left ~f ~init t
  let iter t ~f = iter t ~f
  let map t ~f = map ~f t
  let mapi t ~f = mapi ~f t
  let sort t ~compare = sort t ~cmp:compare
  let count t ~f = fold t ~init:0 ~f:(fun acc e -> acc + if f e then 1 else 0)
end

module Option = struct
  include Option

  let sexp_of_t = Sexplib0.Sexp_conv.sexp_of_option
  let iter t ~f = iter f t
  let map t ~f = map f t
  let some_if cond a = if cond then Some a else None
end

module Queue = struct
  include Queue

  let enqueue t a = push a t
  let to_list t = t |> to_seq |> List.of_seq
end

module Relative_path = struct
  include Relative_path

  let to_dyn t = Dyn.string (Relative_path.to_string t)
end

module Result = struct
  type ('a, 'b) t = ('a, 'b) Result.t =
    | Ok of 'a
    | Error of 'b

  let sexp_of_t sexp_of_a sexp_of_b = function
    | Ok a -> Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Ok"; sexp_of_a a ]
    | Error b -> Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Error"; sexp_of_b b ]
  ;;

  include (Result : module type of Result with type ('a, 'b) t := ('a, 'b) t)

  module Syntax = struct
    let ( let* ) = Result.bind
  end

  let map t ~f = map f t
  let map_error t ~f = map_error f t

  let of_option t ~error =
    match t with
    | Some v -> Ok v
    | None -> Error error
  ;;

  let return = ok
end

module String = struct
  include StringLabels

  let to_dyn = Dyn.string
  let sexp_of_t = Sexplib0.Sexp_conv.sexp_of_string
  let to_string t = t

  let chop_prefix t ~prefix =
    if starts_with ~prefix t
    then (
      let prefix_len = length prefix in
      Some (sub t ~pos:prefix_len ~len:(length t - prefix_len)))
    else None
  ;;

  let chop_suffix t ~suffix =
    if ends_with ~suffix t
    then Some (sub t ~pos:0 ~len:(length t - length suffix))
    else None
  ;;

  let is_empty t = length t = 0

  let lsplit2 t ~on =
    match index_from_opt t 0 on with
    | None -> None
    | Some i -> Some (sub t ~pos:0 ~len:i, sub t ~pos:(i + 1) ~len:(length t - i - 1))
  ;;

  let rsplit2 t ~on =
    let len = length t in
    match rindex_from_opt t (len - 1) on with
    | None -> None
    | Some i -> Some (sub t ~pos:0 ~len:i, sub t ~pos:(i + 1) ~len:(len - i - 1))
  ;;

  (* The function [split_lines] below was copied from [Base.String0.split_lines]
     version [v0.17] which is released under MIT and may be found at
     [https://github.com/janestreet/base].

     The changes we made were minimal:

     - Changed references to [Char0] to [Char].

     See notice at the top of the file and project global notice for licensing
     information. *)

  let split_lines =
    let back_up_at_newline ~t ~pos ~eol =
      pos := !pos - if !pos > 0 && Char.equal t.[!pos - 1] '\r' then 2 else 1;
      eol := !pos + 1
    in
    fun t ->
      let n = length t in
      if n = 0
      then []
      else (
        (* Invariant: [-1 <= pos < eol]. *)
        let pos = ref (n - 1) in
        let eol = ref n in
        let ac = ref [] in
        (* We treat the end of the string specially, because if the string ends with a
           newline, we don't want an extra empty string at the end of the output. *)
        if Char.equal t.[!pos] '\n' then back_up_at_newline ~t ~pos ~eol;
        while !pos >= 0 do
          if not (Char.equal t.[!pos] '\n')
          then decr pos
          else (
            (* Because [pos < eol], we know that [start <= eol]. *)
            let start = !pos + 1 in
            ac := sub t ~pos:start ~len:(!eol - start) :: !ac;
            back_up_at_newline ~t ~pos ~eol)
        done;
        sub t ~pos:0 ~len:!eol :: !ac)
  ;;

  (* ---------------------------------------------------------------------------- *)

  let split t ~on = split_on_char ~sep:on t
  let strip = trim
  let uncapitalize = uncapitalize_ascii
end

module With_equal_and_dyn = struct
  module type S = sig
    type t

    val equal : t -> t -> bool
    val to_dyn : t -> Dyn.t
  end
end

let require cond = if not cond then failwith "Required condition does not hold."

let require_does_raise f =
  match f () with
  | _ -> Code_error.raise "Did not raise." []
  | exception e -> print_endline (Printexc.to_string e)
;;

let require_equal
      (type a)
      (module M : With_equal_and_dyn.S with type t = a)
      (v1 : a)
      (v2 : a)
  =
  if not (M.equal v1 v2)
  then Code_error.raise "Values are not equal." [ "v1", M.to_dyn v1; "v2", M.to_dyn v2 ]
;;

let require_not_equal
      (type a)
      (module M : With_equal_and_dyn.S with type t = a)
      (v1 : a)
      (v2 : a)
  =
  if M.equal v1 v2
  then Code_error.raise "Values are equal." [ "v1", M.to_dyn v1; "v2", M.to_dyn v2 ]
;;

(* {1 Transition API} *)

let print_s sexp = print_endline (Sexp.to_string_hum sexp)