(* Copyright (c) 2015 Daniel C. Bünzli Copyright (c) 2020-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 references (branches, tags, HEAD). *) type t = string let head = "HEAD" let master = "refs/heads/master" let main = "refs/heads/main" let of_string s = if String.length s = 0 then Error (`Msg "Empty reference") else if String.contains s '\x00' then Error (`Msg "Reference contains null byte") else if s.[0] = '/' then Error (`Msg "Absolute reference not allowed") else (* Collapse consecutive slashes *) let buf = Buffer.create (String.length s) in let last_was_slash = ref false in String.iter (fun c -> if c = '/' then ( if not !last_was_slash then Buffer.add_char buf c; last_was_slash := true) else ( Buffer.add_char buf c; last_was_slash := false)) s; Ok (Buffer.contents buf) let of_string_exn s = match of_string s with Ok r -> r | Error (`Msg m) -> invalid_arg m let v = of_string_exn let to_string t = t let pp ppf t = Fmt.string ppf t let equal = String.equal let compare = String.compare let hash = Hashtbl.hash let segs t = String.split_on_char '/' t |> List.filter (fun s -> String.length s > 0) let ( / ) t seg = if String.contains seg '\x00' || String.contains seg '/' then invalid_arg "Invalid segment"; if t.[String.length t - 1] = '/' then t ^ seg else t ^ "/" ^ seg let ( // ) t1 t2 = if t2.[0] = '/' then t2 else if t1.[String.length t1 - 1] = '/' then t1 ^ t2 else t1 ^ "/" ^ t2 (** Reference contents: either a direct hash or a symbolic reference. *) type contents = Hash of Hash.t | Ref of t let contents_equal a b = match (a, b) with | Hash a, Hash b -> Hash.equal a b | Ref a, Ref b -> equal a b | _ -> false let contents_compare a b = match (a, b) with | Hash a, Hash b -> Hash.compare a b | Ref a, Ref b -> compare a b | Hash _, Ref _ -> -1 | Ref _, Hash _ -> 1 let pp_contents ppf = function | Hash h -> Hash.pp ppf h | Ref r -> Fmt.pf ppf "ref: %s" r (** Parse reference file contents. *) let contents_of_string s = let s = String.trim s in if String.length s = 0 then Error (`Msg "Empty reference contents") else if String.length s >= 5 && String.sub s 0 5 = "ref: " then let ref_path = String.sub s 5 (String.length s - 5) in Result.map (fun r -> Ref r) (of_string ref_path) else (* Try to parse as a hex hash *) try Ok (Hash (Hash.of_hex s)) with Invalid_argument _ -> Error (`Msg ("Invalid reference contents: " ^ s)) let contents_of_string_exn s = match contents_of_string s with Ok c -> c | Error (`Msg m) -> failwith m (** Encode reference contents. *) let contents_to_string = function | Hash h -> Hash.to_hex h ^ "\n" | Ref r -> "ref: " ^ r ^ "\n" module Set = Set.Make (String) module Map = Map.Make (String)