<commit_before>(*
 * Copyright (c) 2013-2017 <NAME> <<EMAIL>>
 * and <NAME> <<EMAIL>>
 *
 * 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 Make
    (Store : Store.S with type Hash.Digest.buffer = Cstruct.t)
= struct
  type t =
    | Reference of Store.Reference.t (* <refname> *)
    | Commit of id                   (* <id> *)
    | Parent of t * int              (* <rev>^<n> *)
    | Ancestor of t * int            (* <rev>~<n> *)
  and id =
    | Incomplete of string
    | Complete of Store.Hash.t

  let head = Reference Store.Reference.head

  let parent rev n   = Parent (rev, n)
  let ancestor rev n = Ancestor (rev, n)

  let from_hash hash = Commit (Complete hash)

  let rec commit git hash =
    let open Lwt.Infix in

    Store.read git hash >>= function
    | Ok (Store.Value.Commit commit) -> Lwt.return (Ok commit)
    | Ok (Store.Value.Tag tag) -> commit git (Store.Value.Tag.obj tag)
    | Ok (Store.Value.Blob _ | Store.Value.Tree _) -> Lwt.return (Error (`Expected_commit hash))
    | Error #Store.error as err -> Lwt.return err

  let rec normalize git rev =
    let open Lwt.Infix in

    match rev with
    | Commit (Complete hash) -> Lwt.return (Ok hash)
    | Commit (Incomplete _) -> assert false
    | Parent (rev, n) ->
      normalize git rev >>= (function
          | Ok hash ->
            (commit git hash >>= function
              | Ok commit ->
                Lwt.try_bind
                  (fun () -> List.nth (Store.Value.Commit.parents commit) n |> Lwt.return)
                  (fun hash -> Lwt.return (Ok hash))
                  (fun _ -> Lwt.return (Error (`Invalid_parent (hash, n))))
              | Error _ as err -> Lwt.return err)
          | Error _ as err -> Lwt.return err)
    | Ancestor (rev, n) ->
      normalize git rev >>= (function
          | Ok hash ->
            let rec go hash = function
              | 0 -> Lwt.return (Ok hash)
              | n -> commit git hash >>= function
                | Error _ as err -> Lwt.return err
                | Ok commit ->
                  Lwt.try_bind
                    (fun () -> Lwt.return (List.hd (Store.Value.Commit.parents commit)))
                    (fun hash -> go hash (n - 1))
                    (fun _ -> Lwt.return (Error (`Invalid_parent (hash, n))))
            in
            go hash n
          | Error _ as err -> Lwt.return err)
    | Reference reference ->
      Store.Ref.graph git >>= function
      | Error #Store.Ref.error as err -> Lwt.return err
      | Ok graph ->
        Store.Ref.normalize graph (Store.Reference.Ref reference) >>= function
        | Error #Store.Ref.error as err -> Lwt.return err
        | Ok _ as value-> Lwt.return value

  module Range =
  struct
    type nonrec t =
      | Include     of t (* <rev> *)
      | Exclude     of t (* ^<rev> *)
      | Diff        of t * t (* <rev1>..<rev2> différence
                                semantically, it's the same than [^<rev1> <rev2>] *)
      | Delta       of t * t (* <rev1>...<rev2> différence symétrique *)
      | Parents     of t (* <rev>^@ *)
      | PParents    of t (* <rev>^! *)

    module E = Store.Hash.Set

    let to_visit git hash =
      let open Lwt.Infix in

      Store.read git hash >|= function
      | Ok (Store.Value.Commit commit) -> Store.Value.Commit.parents commit
      | Ok (Store.Value.Tree _) -> []
      | Ok (Store.Value.Blob _) -> []
      | Ok (Store.Value.Tag tag) -> [ Store.Value.Tag.obj tag ]
      | Error _ -> []
        (* XXX(dinosaure): we silent the error. *)

    let rec walk git close rest to_visit fadd fcompute acc =
      let open Lwt.Infix in<commit_msg>Revision module needs only the minimal interface now<commit_after>(*
 * Copyright (c) 2013-2017 <NAME> <<EMAIL>>
 * and <NAME> <<EMAIL>>
 *
 * 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 Make
    (Store : Minimal.S with type Hash.Digest.buffer = Cstruct.t)
= struct
  type t =
    | Reference of Store.Reference.t (* <refname> *)
    | Commit of id                   (* <id> *)
    | Parent of t * int              (* <rev>^<n> *)
    | Ancestor of t * int            (* <rev>~<n> *)
  and id =
    | Incomplete of string
    | Complete of Store.Hash.t

  let head = Reference Store.Reference.head

  let parent rev n   = Parent (rev, n)
  let ancestor rev n = Ancestor (rev, n)

  let from_hash hash = Commit (Complete hash)

  let rec commit git hash =
    let open Lwt.Infix in

    Store.read git hash >>= function
    | Ok (Store.Value.Commit commit) -> Lwt.return (Ok commit)
    | Ok (Store.Value.Tag tag) -> commit git (Store.Value.Tag.obj tag)
    | Ok (Store.Value.Blob _ | Store.Value.Tree _) -> Lwt.return (Error (`Expected_commit hash))
    | Error err -> Lwt.return (Error (`Store err))

  let rec normalize git rev =
    let open Lwt.Infix in

    match rev with
    | Commit (Complete hash) -> Lwt.return (Ok hash)
    | Commit (Incomplete _) -> assert false
    | Parent (rev, n) ->
      normalize git rev >>= (function
          | Ok hash ->
            (commit git hash >>= function
              | Ok commit ->
                Lwt.try_bind
                  (fun () -> List.nth (Store.Value.Commit.parents commit) n |> Lwt.return)
                  (fun hash -> Lwt.return (Ok hash))
                  (fun _ -> Lwt.return (Error (`Invalid_parent (hash, n))))
              | Error _ as err -> Lwt.return err)
          | Error _ as err -> Lwt.return err)
    | Ancestor (rev, n) ->
      normalize git rev >>= (function
          | Ok hash ->
            let rec go hash = function
              | 0 -> Lwt.return (Ok hash)
              | n -> commit git hash >>= function
                | Error _ as err -> Lwt.return err
                | Ok commit ->
                  Lwt.try_bind
                    (fun () -> Lwt.return (List.hd (Store.Value.Commit.parents commit)))
                    (fun hash -> go hash (n - 1))
                    (fun _ -> Lwt.return (Error (`Invalid_parent (hash, n))))
            in
            go hash n
          | Error _ as err -> Lwt.return err)
    | Reference reference ->
      Store.Ref.list git >>= fun lst ->
      try let hash = List.assoc reference lst in Lwt.return (Ok hash)
      with Not_found -> Lwt.return (Error `Not_found)

  module Range =
  struct
    type nonrec t =
      | Include     of t (* <rev> *)
      | Exclude     of t (* ^<rev> *)
      | Diff        of t * t (* <rev1>..<rev2> différence
                                semantically, it's the same than [^<rev1> <rev2>] *)
      | Delta       of t * t (* <rev1>...<rev2> différence symétrique *)
      | Parents     of t (* <rev>^@ *)
      | PParents    of t (* <rev>^! *)

    module E = Store.Hash.Set

    let to_visit git hash =
      let open Lwt.Infix in

      Store.read git hash >|= function
      | Ok (Store.Value.Commit commit) -> Store.Value.Commit.parents commit
      | Ok (Store.Value.Tree _) -> []
      | Ok (Store.Value.Blob _) -> []
      | Ok (Store.Value.Tag tag) -> [ Store.Value.Tag.obj tag ]
      | Error _ -> []
        (* XXX(dinosaure): we silent the error. *)

    let rec walk git close rest to_visit fadd fcompute acc =
      let open Lwt.Infix in
{'fraction_non_alphanumeric': 0.11456743749266346, 'fraction_numerical': 0.0037563094259889657, 'mean_word_length': 1.9450397511234012, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3995120', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2478, 'n_words': 922}