Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248(* 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. *)
type state = Set | Unset | Value of string | Unspecifiedtype merge_driver = Text_merge | Binary_merge | Union_merge | Driver of string
(* One line of a file, as git's attr.c keeps it: a pattern with the states it gives, or a macro with its expansion. [!name] gives [Unspecified]. *)type line = | Rule of { spec : Pathspec.t; base : string; states : (string * state) list } | Macro of { name : string; states : (string * state) list }
type t = line list
let pp ppf t = Fmt.pf ppf "gitattributes[%d]" (List.length t)let empty = []let append = ( @ )
(* git's ATTR_MAX_LINE_LENGTH: a longer line is ignored. *)let max_line = 2048let blank c = c = ' ' || c = '\t' || c = '\r' || c = '\n'
(* attr.c attr_name_valid and attr_name_reserved. *)let valid_name name = name <> "" && name.[0] <> '-' && String.for_all (function | '-' | '.' | '_' | '0' .. '9' | 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false) name && not (String.starts_with ~prefix:"builtin_" name)
(* One state token: [name], [-name], [!name] or [name=value]. *)let state_of_token token = let negation = if token = "" then None else match token.[0] with | '-' -> Some Unset | '!' -> Some Unspecified | _ -> None in let body = if negation = None then token else String.sub token 1 (String.length token - 1) in let name, value = match String.index_opt body '=' with | None -> (body, None) | Some i -> ( String.sub body 0 i, Some (String.sub body (i + 1) (String.length body - i - 1)) ) in if not (valid_name name) then None else match (negation, value) with | Some state, _ -> Some (name, state) | None, None -> Some (name, Set) | None, Some value -> Some (name, Value value)
let tokens s = String.split_on_char ' ' (String.map (fun c -> if blank c then ' ' else c) s) |> List.filter (fun s -> s <> "")
(* A whole line's states, or [None] when one token is invalid: git drops the line. *)let states_of s = List.fold_left (fun acc token -> match (acc, state_of_token token) with | Some acc, Some state -> Some (state :: acc) | _ -> None) (Some []) (tokens s) |> Option.map List.rev
(* git's unquote_c_style: the quoted pattern at the start of [s] and the rest of the line, or [None] when [s] holds no well-formed quoted string. *)let unquote s = let n = String.length s in let b = Buffer.create n in let octal c = c >= '0' && c <= '7' in let rec go i = if i >= n then None else match s.[i] with | '"' -> Some (Buffer.contents b, String.sub s (i + 1) (n - i - 1)) | '\\' when i + 1 < n -> ( let simple c = Buffer.add_char b c; go (i + 2) in match s.[i + 1] with | 'a' -> simple '\007' | 'b' -> simple '\b' | 'f' -> simple '\012' | 'n' -> simple '\n' | 'r' -> simple '\r' | 't' -> simple '\t' | 'v' -> simple '\011' | ('\\' | '"') as c -> simple c | '0' .. '3' when i + 3 < n && octal s.[i + 2] && octal s.[i + 3] -> let digit k = Char.code s.[i + k] - Char.code '0' in Buffer.add_char b (Char.chr ((digit 1 * 64) + (digit 2 * 8) + digit 3)); go (i + 4) | _ -> None) | c -> Buffer.add_char b c; go (i + 1) in go 1
(* [line] cut at its first blank. *)let cut line = let n = String.length line in let rec find i = if i < n && not (blank line.[i]) then find (i + 1) else i in let i = find 0 in (String.sub line 0 i, String.sub line i (n - i))
(* The pattern at the start of [line], C-quoted or not, and the rest. *)let split_pattern line = if line.[0] = '"' then match unquote line with Some split -> split | None -> cut line else cut line
let macro_prefix = "[attr]"
(* attr.c parse_attr_line. A macro is allowed only in the root file; a negative pattern and a directory pattern match no file and are dropped. *)let compile_one ~base raw = let line = let n = String.length raw in let rec skip i = if i < n && blank raw.[i] then skip (i + 1) else i in let i = skip 0 in String.sub raw i (n - i) in if String.length raw > max_line || line = "" || line.[0] = '#' then None else if String.starts_with ~prefix:macro_prefix line then if base <> "" then None else let rest = String.sub line (String.length macro_prefix) (String.length line - String.length macro_prefix) in let name, after = cut rest in if not (valid_name name) then None else Option.map (fun states -> Macro { name; states }) (states_of after) else let pattern, rest = split_pattern line in if pattern = "" || pattern.[0] = '!' || pattern.[String.length pattern - 1] = '/' then None else match (Pathspec.compile pattern, states_of rest) with | Some spec, Some states -> Some (Rule { spec; base; states }) | _ -> None
let of_file_content ~base content = String.split_on_char '\n' content |> List.filter_map (compile_one ~base)
let of_dirs ~read dir = let load acc dir = match read dir with | None -> acc | Some content -> append acc (of_file_content ~base:dir content) in let root = load empty "" in if dir = "" then root else let rec go acc prefix = function | [] -> acc | x :: rest -> let prefix = if prefix = "" then x else prefix ^ "/" ^ x in go (load acc prefix) prefix rest in go root "" (String.split_on_char '/' dir)
let builtin_macros = [ ("binary", [ ("diff", Unset); ("merge", Unset); ("text", Unset) ]) ]
(* attr.c fill: the lines are read from the last to the first and each line's states from its last to its first; an attribute keeps the first state it is given, and a macro given [Set] gives its expansion's attributes the states they do not hold yet. The last definition of a macro wins. *)let resolve t ~path = let macros = Hashtbl.create 8 in List.iter (fun (name, states) -> Hashtbl.replace macros name states) builtin_macros; List.iter (function | Macro { name; states } -> Hashtbl.replace macros name states | Rule _ -> ()) t; let filled = Hashtbl.create 8 in let rec give (name, state) = if not (Hashtbl.mem filled name) then ( Hashtbl.replace filled name state; match (state, Hashtbl.find_opt macros name) with | Set, Some states -> List.iter give (List.rev states) | _ -> ()) in List.iter (function | Rule { spec; base; states } when Pathspec.matches spec ~base ~path -> List.iter give (List.rev states) | Rule _ | Macro _ -> ()) (List.rev t); fun name -> match Hashtbl.find_opt filled name with | Some state -> state | None -> Unspecified
let check t ~path name = resolve t ~path name
type autocrlf = True | Input | False
(* convert.c: [text] decides; with it unspecified, an [eol] makes the file text. *)let normalizes_eol rules ~autocrlf ~path ~content = let is_text_content () = not (String.contains content '\000') in let state = resolve rules ~path in match state "text" with | Unset -> false | Set -> true | Value "auto" -> is_text_content () | Value _ | Unspecified -> ( match state "eol" with | Value ("lf" | "crlf") -> true | _ -> ( match autocrlf with | True | Input -> is_text_content () | False -> false))
let merge_driver rules ~path = match check rules ~path "merge" with | Set | Unspecified | Value "text" -> Text_merge | Unset | Value "binary" -> Binary_merge | Value "union" -> Union_merge | Value name -> Driver name