(* Copyright (c) 2013-2017 Thomas Gazagnaire Copyright (c) 2017-2024 Romain Calascibetta Copyright (c) 2024-2026 Thomas Gazagnaire 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 = Digestif.SHA1.t let digest_size = Digestif.SHA1.digest_size let pp = Digestif.SHA1.pp let equal = Digestif.SHA1.equal let compare = Digestif.SHA1.unsafe_compare let of_raw_string s = if String.length s <> digest_size then invalid_arg "Hash.of_raw_string: invalid length"; Digestif.SHA1.of_raw_string s let to_raw_string = Digestif.SHA1.to_raw_string let of_hex s = Digestif.SHA1.of_hex s let to_hex t = Digestif.SHA1.to_hex t 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 = compare end) module Map = Map.Make (struct type nonrec t = t let compare = compare end) (** 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 = Digestif.SHA1.empty in let ctx = Digestif.SHA1.feed_string ctx header in let ctx = Digestif.SHA1.feed_string ctx contents in Digestif.SHA1.get ctx let digest_string ~kind s = digest ~kind ~length:(String.length s) s