Git object storage and pack files for Eio
at main 68 lines 2.2 kB view raw
1(* Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org> 2 Copyright (c) 2017-2024 Romain Calascibetta <romain.calascibetta@gmail.com> 3 Copyright (c) 2024-2026 Thomas Gazagnaire <thomas@gazagnaire.org> 4 5 Permission to use, copy, modify, and distribute this software for any 6 purpose with or without fee is hereby granted, provided that the above 7 copyright notice and this permission notice appear in all copies. 8 9 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) 16 17(** Git object hashes (SHA-1 or SHA-256). *) 18 19type t = Digestif.SHA1.t 20 21let digest_size = Digestif.SHA1.digest_size 22let pp = Digestif.SHA1.pp 23let equal = Digestif.SHA1.equal 24let compare = Digestif.SHA1.unsafe_compare 25 26let of_raw_string s = 27 if String.length s <> digest_size then 28 invalid_arg "Hash.of_raw_string: invalid length"; 29 Digestif.SHA1.of_raw_string s 30 31let to_raw_string = Digestif.SHA1.to_raw_string 32let of_hex s = Digestif.SHA1.of_hex s 33let to_hex t = Digestif.SHA1.to_hex t 34 35let null = 36 let s = String.make digest_size '\x00' in 37 of_raw_string s 38 39let v t = Hashtbl.hash (to_raw_string t) 40 41module Set = Set.Make (struct 42 type nonrec t = t 43 44 let compare = compare 45end) 46 47module Map = Map.Make (struct 48 type nonrec t = t 49 50 let compare = compare 51end) 52 53(** Compute the hash of a git object. *) 54let digest ~kind ~length contents = 55 let kind_str = 56 match kind with 57 | `Blob -> "blob" 58 | `Tree -> "tree" 59 | `Commit -> "commit" 60 | `Tag -> "tag" 61 in 62 let header = Fmt.str "%s %d\x00" kind_str length in 63 let ctx = Digestif.SHA1.empty in 64 let ctx = Digestif.SHA1.feed_string ctx header in 65 let ctx = Digestif.SHA1.feed_string ctx contents in 66 Digestif.SHA1.get ctx 67 68let digest_string ~kind s = digest ~kind ~length:(String.length s) s