type t = string let forbidden_character = function | '\000' .. '\032' | '\127' | '~' | '^' | ':' | '?' | '*' | '[' | '\\' -> true | _ -> false let component_is_invalid component = component = "" || String.starts_with ~prefix:"." component || String.ends_with ~suffix:".lock" component let of_string value = if value = "" then Error "Git ref must not be empty" else if value = "@" then Error "Git ref must not be @" else if String.starts_with ~prefix:"-" value then Error "Git ref must not start with a dash" else if String.ends_with ~suffix:"." value then Error "Git ref must not end with a dot" else if String.exists forbidden_character value then Error "Git ref contains a forbidden character" else if String.starts_with ~prefix:"/" value || String.ends_with ~suffix:"/" value then Error "Git ref must not start or end with a slash" else if String.starts_with ~prefix:"." value || String.ends_with ~suffix:".lock" value then Error "Git ref has an invalid component" else let components = String.split_on_char '/' value in if List.exists component_is_invalid components then Error "Git ref has an invalid component" else let rec has_forbidden_sequence index = if index + 1 >= String.length value then false else match (value.[index], value.[index + 1]) with | '.', '.' | '@', '{' -> true | _ -> has_forbidden_sequence (index + 1) in if has_forbidden_sequence 0 then Error "Git ref contains a forbidden sequence" else Ok value let to_string value = value