Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394(* Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org> Copyright (c) 2017-2024 Romain Calascibetta <romain.calascibetta@gmail.com> Copyright (c) 2024-2026 Thomas Gazagnaire <thomas@gazagnaire.org>
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. *)
(** Git object hashes (SHA-1 or SHA-256). *)
type t = Hash.SHA1.t
let digest_size = Hash.SHA1.digest_sizelet pp = Hash.SHA1.pplet equal = Hash.SHA1.equal
let rec equal_list a b = match (a, b) with | [], [] -> true | x :: xs, y :: ys -> equal x y && equal_list xs ys | _ -> false
let compare = Hash.SHA1.unsafe_compare
let of_raw_string s = if String.length s <> digest_size then invalid_arg "Object_hash.of_raw_string: invalid length"; Hash.SHA1.of_raw_string s
let to_raw_string = Hash.SHA1.to_raw_stringlet of_hex s = Hash.SHA1.of_hex slet to_hex t = Hash.SHA1.to_hex tlet is_hex_digit = function '0' .. '9' | 'a' .. 'f' -> true | _ -> false
let to_loose_name t = let hex = to_hex t in (String.sub hex 0 2, String.sub hex 2 (String.length hex - 2))
let of_loose_name fan name = if String.length fan = 2 && String.length name = (2 * digest_size) - 2 && String.for_all is_hex_digit fan && String.for_all is_hex_digit name then Some (of_hex (fan ^ name)) else None
let null = let s = String.make digest_size '\x00' in of_raw_string s
let v t = Hashtbl.hash (to_raw_string t)
module Set = Set.Make (struct type nonrec t = t
let compare = compareend)
module Map = Map.Make (struct type nonrec t = t
let compare = compareend)
(** Compute the hash of a git object. *)let digest ~kind ~length contents = let kind_str = match kind with | `Blob -> "blob" | `Tree -> "tree" | `Commit -> "commit" | `Tag -> "tag" in let header = Fmt.str "%s %d\x00" kind_str length in let ctx = Hash.SHA1.empty in let ctx = Hash.SHA1.feed_string ctx header in let ctx = Hash.SHA1.feed_string ctx contents in Hash.SHA1.get ctx
let digest_string ~kind s = digest ~kind ~length:(String.length s) s
let digest_raw ?(off = 0) ?len s = let len = match len with Some l -> l | None -> String.length s - off in Hash.SHA1.(get (feed_string empty ~off ~len s))