Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223(* Copyright (c) 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.
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. *)
(* What a store says of an object it does not hold names no object, as a repository on disk says it. *)let not_found = Error (`Msg "object not found")let err_missing h = Fmt.error_msg "%a: object not found" Git.Hash.pp h
(* Objects in memory, with the facts they keep and the computations they count. *)module Store = struct type t = { objects : (Git.Hash.t, Git.Value.t) Hashtbl.t; facts : Git.Commit_facts.t; mutable changes : int; mutable walks : int; }
let v ?budget () = { objects = Hashtbl.create 64; facts = Git.Commit_facts.v ?budget (); changes = 0; walks = 0; }
let read t h = match Hashtbl.find_opt t.objects h with Some v -> Ok v | None -> not_found
let commit_graph _ = None let facts t = t.facts
let computed t : Git.Commit_facts.computation -> unit = function | Change -> t.changes <- t.changes + 1 | Merge_base -> t.walks <- t.walks + 1end
module F = Git.Commit_facts.Make (Store)module D = Git.Diff.Make (Store)
let hash = Alcotest.testable Git.Hash.pp Git.Hash.equallet msg = Alcotest.testable (fun ppf (`Msg m) -> Fmt.string ppf m) ( = )
let add (s : Store.t) v = let h = Git.Value.digest v in Hashtbl.replace s.objects h v; h
(* The tree of the files [files], each a name and its content, at the root. *)let tree s files = let entry (name, content) = let blob = add s (Git.Value.blob (Git.Blob.of_string content)) in Git.Tree.entry ~perm:`Normal ~name blob in add s (Git.Value.Tree (Git.Tree.v (List.map entry files)))
let date = ref 0L
let commit s ~parents files = date := Int64.add !date 10L; let user = Git.User.v ~name:"Test" ~email:"test@test.com" ~date:!date () in add s (Git.Value.commit (Git.Commit.v ~tree:(tree s files) ~author:user ~committer:user ~parents (Some "c")))
let get = function Ok v -> v | Error (`Msg m) -> Alcotest.fail m
(* A commit's paths, patch id, a pair's changed paths and merge base, each asked twice: the answers are those of the diff and the walk, and the second asks compute nothing. Catches a fact computed again at each ask, which held each round of a merge queue's reading for about 40 s. *)let test_computed_once () = let s = Store.v () in let base = commit s ~parents:[] [ ("a", "0\n"); ("b", "0\n") ] in let one = commit s ~parents:[ base ] [ ("a", "1\n"); ("b", "0\n") ] in let two = commit s ~parents:[ base ] [ ("a", "0\n"); ("c", "2\n") ] in let diff = get (D.commits s ~old_commit:base ~new_commit:one) in let id = Git.Diff.patch_id ~read:(fun h -> match Store.read s h with | Ok (Git.Value.Blob b) -> Git.Blob.to_string b | _ -> Alcotest.fail "a blob") diff in let ask () = Alcotest.(check (option (list string))) "the paths one changes" (Some [ "a" ]) (get (F.paths s one)); Alcotest.(check (option hash)) "its patch id" id (get (F.patch_id s one)); Alcotest.(check (list string)) "the paths between one and two" [ "a"; "b"; "c" ] (get (F.changed s ~was:one ~now:two)); Alcotest.(check (option hash)) "their merge base" (Some base) (F.merge_base s one two) in ask (); let changes = s.changes and walks = s.walks in Alcotest.(check int) "a diff for each of three facts" 3 changes; Alcotest.(check int) "a walk" 1 walks; ask (); Alcotest.(check int) "no diff asked again" changes s.changes; Alcotest.(check int) "no walk asked again" walks s.walks
(* A root's paths are its tree's; a merge's are none, computed from no diff, and so is its patch id. *)let test_root_and_merge () = let s = Store.v () in let root = commit s ~parents:[] [ ("b", "0\n"); ("a", "0\n") ] in let side = commit s ~parents:[ root ] [ ("a", "1\n"); ("b", "0\n") ] in let merge = commit s ~parents:[ root; side ] [ ("a", "1\n"); ("b", "1\n") ] in Alcotest.(check (option (list string))) "a root's paths" (Some [ "a"; "b" ]) (get (F.paths s root)); let changes = s.changes in Alcotest.(check (option (list string))) "a merge's paths" None (get (F.paths s merge)); Alcotest.(check (option hash)) "a merge's patch id" None (get (F.patch_id s merge)); Alcotest.(check int) "a merge diffs nothing" changes s.changes
(* An object that is not there is an error naming it, and no fact: asked again once it is there, the answer is computed. A blob the patch id reads that is not there is named too, so a caller tells which object of a change is missing. *)let test_unreadable () = let s = Store.v () in let root = commit s ~parents:[] [ ("a", "0\n") ] in let v = Hashtbl.find s.objects root in Hashtbl.remove s.objects root; Alcotest.(check (result (option (list string)) msg)) "a missing commit" (err_missing root) (F.paths s root); Hashtbl.replace s.objects root v; Alcotest.(check (option (list string))) "read once it is there" (Some [ "a" ]) (get (F.paths s root)); let blob = Git.Value.digest (Git.Value.blob (Git.Blob.of_string "1\n")) in let child = commit s ~parents:[ root ] [ ("a", "1\n") ] in Hashtbl.remove s.objects blob; Alcotest.(check (result (option hash) msg)) "a missing blob" (err_missing blob) (F.patch_id s child)
(* A line of [n] commits after a root, each changing the [p] files it holds: the paths of each asked, then asked again. *)let line s ~n ~p = let files i = List.init p (fun j -> (Fmt.str "file-%04d" j, Fmt.str "%d\n" i)) in let rec go acc parent i = if i > n then List.rev acc else let c = commit s ~parents:[ parent ] (files i) in go (c :: acc) c (i + 1) in go [] (commit s ~parents:[] (files 0)) 1
(* What a store keeps of the paths of a history stays under the budget at two sizes an order of magnitude apart, 20 commits of 10 paths and 200 of 100, each under 64 KiB of facts: the small one is kept whole, so asking it again computes nothing, and the large one, a hundred times heavier than the budget, holds no more than the budget. Catches a store whose facts grow with the history it is asked about. *)let test_bounded () = let budget = 64 * 1024 in List.iter (fun (n, p, whole) -> let s = Store.v ~budget () in let commits = line s ~n ~p in let ask () = List.iter (fun c -> ignore (get (F.paths s c))) commits in ask (); Alcotest.(check bool) (Fmt.str "%d commits of %d paths weigh at most the budget" n p) true (Git.Commit_facts.weight s.facts <= budget); let changes = s.changes in ask (); Alcotest.(check bool) (Fmt.str "%d commits of %d paths asked again compute %s" n p (if whole then "nothing" else "again")) whole (changes = s.changes)) [ (20, 10, true); (200, 100, false) ]
(* A commit whose paths alone weigh more than the budget is answered and not kept: the store holds nothing, and asking again computes it again. *)let test_heavier_than_budget () = let s = Store.v ~budget:(4 * 1024) () in let files = List.init 200 (fun j -> (Fmt.str "file-%04d" j, "x\n")) in let root = commit s ~parents:[] files in Alcotest.(check int) "its paths" 200 (List.length (Option.get (get (F.paths s root)))); Alcotest.(check int) "nothing kept" 0 (Git.Commit_facts.weight s.facts); ignore (get (F.paths s root)); Alcotest.(check int) "computed again" 2 s.changes
let suite = ( "commit_facts", [ Alcotest.test_case "each fact is computed once" `Quick test_computed_once; Alcotest.test_case "a root's and a merge's paths" `Quick test_root_and_merge; Alcotest.test_case "an unreadable commit is no fact" `Quick test_unreadable; Alcotest.test_case "the facts stay within the budget" `Quick test_bounded; Alcotest.test_case "a fact heavier than the budget is not kept" `Quick test_heavier_than_budget; ] )