(* Copyright (c) 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. *) module Hash = Object_hash (* A blob {!Diff.patch_id} reads that is not there. *) exception Unreadable of string let fail_unreadable fmt = Fmt.kstr (fun m -> raise (Unreadable m)) fmt (* What an entry holds besides its paths: its key, a hash or two, and the table's own words. *) let entry = 128 let weigh_paths = List.fold_left (fun n p -> n + String.length p + 32) entry let weigh_option = function None -> entry | Some paths -> weigh_paths paths let constant _ = entry (* Each kind of fact by what it is of: a commit by its raw hash, a pair by both, each kind within a quarter of the budget. *) type t = { paths : (string, string list option) Cache.t; patch_ids : (string, Hash.t option) Cache.t; changed : (string * string, string list) Cache.t; merge_bases : (string * string, Hash.t option) Cache.t; } let v ?(budget = 16 * 1024 * 1024) () = let budget = budget / 4 in { paths = Cache.v ~budget ~weight:weigh_option; patch_ids = Cache.v ~budget ~weight:constant; changed = Cache.v ~budget ~weight:weigh_paths; merge_bases = Cache.v ~budget ~weight:constant; } let weight t = Cache.weight t.paths + Cache.weight t.patch_ids + Cache.weight t.changed + Cache.weight t.merge_bases type computation = Change | Merge_base module type S = sig include Rev_list.S type facts val facts : t -> facts val computed : t -> computation -> unit end with type facts := t module Make (S : S) = struct module D = Diff.Make (S) module W = Rev_list.Make (S) let ( let* ) = Result.bind (* [f ()], kept under [key] in [cache]. *) let kept cache key f = match Cache.find cache key with | Some fact -> Ok fact | None -> let* fact = f () in Cache.add cache key fact; Ok fact let path : Diff.change -> string = function | Added { path; _ } | Removed { path; _ } | Modified { path; _ } -> path (* The change of the commit [c] against its one parent, every entry of a root's tree, [None] for a merge. *) let change s c = match S.read s c with | Error (`Msg m) -> Fmt.error_msg "%a: %s" Hash.pp c m | Ok (Value.Commit commit) -> ( match Commit.parents commit with | [ parent ] -> S.computed s Change; Result.map Option.some (D.commits s ~old_commit:parent ~new_commit:c) | [] -> S.computed s Change; Result.map Option.some (D.empty_of_tree s (Commit.tree commit)) | _ -> Ok None) | Ok _ -> Fmt.error_msg "%a is not a commit" Hash.pp c let paths s c = kept (S.facts s).paths (Hash.to_raw_string c) @@ fun () -> let* d = change s c in Ok (Option.map (fun d -> List.sort_uniq String.compare (List.map path d)) d) let blob s h = match S.read s h with | Ok (Value.Blob b) -> Blob.to_string b | Ok _ -> fail_unreadable "%a is not a blob" Hash.pp h | Error (`Msg m) -> fail_unreadable "%a: %s" Hash.pp h m let patch_id s c = kept (S.facts s).patch_ids (Hash.to_raw_string c) @@ fun () -> let* d = change s c in match d with | None -> Ok None | Some d -> ( match Diff.patch_id ~read:(blob s) d with | id -> Ok id | exception Unreadable m -> Error (`Msg m)) let changed s ~was ~now = kept (S.facts s).changed (Hash.to_raw_string was, Hash.to_raw_string now) @@ fun () -> S.computed s Change; let* d = D.commits s ~old_commit:was ~new_commit:now in Ok (List.map path d) let merge_base s a b = let bases = (S.facts s).merge_bases and key = (Hash.to_raw_string a, Hash.to_raw_string b) in match Cache.find bases key with | Some base -> base | None -> S.computed s Merge_base; let base = W.merge_base s a b in Cache.add bases key base; base end