(* 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. *) (** Generated commit-graph records from gitformat-commit-graph(5): OID Fanout, Commit Data, Extra Edge List and Generation Data Overflow. Each input exercises topological and corrected generation numbers, octopus parents, a 34-bit date and a corrected date offset larger than 31 bits. *) open Alcobar module G = Git.Commit_graph let oid n i = let b = Bytes.init 20 (fun j -> Char.chr ((n + (i * 7) + (j * 17)) mod 251)) in Bytes.set b 0 (Char.chr (i * 256 / n)); Bytes.set_int32_be b 16 (Int32.of_int (i + 1)); Bytes.to_string b let tree i = String.init 20 (fun j -> Char.chr (((i * 7) + (j * 17)) mod 251)) let model seed = let n = 6 + (seed mod 10) in let parents = Array.init n (function | 0 -> [] | 1 -> [ 0 ] | 2 -> [ 0; 1 ] | 3 -> [ 2; 0; 1 ] | i -> [ i - 1; 0; 1; 2 ]) in let dates = Array.init n (function | 1 -> 5_000_000_000L | 2 -> 500L | i -> Int64.of_int (seed + (i * 101))) in let topo = Array.make n 0L and corrected = Array.make n 0L in Array.iteri (fun i ps -> topo.(i) <- Int64.succ (List.fold_left (fun m p -> Int64.max m topo.(p)) 0L ps); corrected.(i) <- Int64.max dates.(i) (Int64.succ (List.fold_left (fun m p -> Int64.max m corrected.(p)) (-1L) ps))) parents; (n, parents, dates, topo, corrected) let commit_data parents dates topo = let cdat = Bytes.make (Array.length parents * 36) '\000' in let edges = Buffer.create 64 in Array.iteri (fun i ps -> Bytes.blit_string (tree i) 0 cdat (i * 36) 20; let p1, p2 = match ps with | [] -> (0x70000000, 0x70000000) | [ a ] -> (a, 0x70000000) | [ a; b ] -> (a, b) | a :: tail -> let start = Buffer.length edges / 4 in List.iteri (fun j p -> let b = Bytes.create 4 in let v = if j = List.length tail - 1 then p lor 0x80000000 else p in Bytes.set_int32_be b 0 (Int32.of_int v); Buffer.add_bytes edges b) tail; (a, start lor 0x80000000) in Bytes.set_int32_be cdat ((i * 36) + 20) (Int32.of_int p1); Bytes.set_int32_be cdat ((i * 36) + 24) (Int32.of_int p2); Bytes.set_int32_be cdat ((i * 36) + 28) (Int64.to_int32 (Int64.logor (Int64.shift_left topo.(i) 2) (Int64.shift_right_logical dates.(i) 32))); Bytes.set_int32_be cdat ((i * 36) + 32) (Int64.to_int32 dates.(i))) parents; (Bytes.to_string cdat, Buffer.contents edges) let encode seed v2 = let n, parents, dates, topo, corrected = model seed in let fanout = Bytes.make 1024 '\000' in for bucket = 0 to 255 do let count = Array.fold_left (fun c i -> if i * 256 / n <= bucket then c + 1 else c) 0 (Array.init n Fun.id) in Bytes.set_int32_be fanout (bucket * 4) (Int32.of_int count) done; let cdat, edges = commit_data parents dates topo in let gda = Bytes.create (n * 4) and gdo = Bytes.create (n * 8) in for i = 0 to n - 1 do let offset = Int64.sub corrected.(i) dates.(i) in Bytes.set_int64_be gdo (i * 8) offset; Bytes.set_int32_be gda (i * 4) (if offset > 0x7fffffffL then Int32.of_int (0x80000000 lor i) else Int64.to_int32 offset) done; let chunks = [ ("OIDF", Bytes.to_string fanout); ("OIDL", String.concat "" (List.init n (oid n))); ("CDAT", cdat); ("EDGE", edges); ] @ if v2 then [ ("GDA2", Bytes.to_string gda); ("GDO2", Bytes.to_string gdo) ] else [] in let chunks = if seed mod 2 = 0 then chunks else List.rev chunks in let c = List.length chunks in let table = Bytes.make ((c + 1) * 12) '\000' in let offset = ref (8 + Bytes.length table) in List.iteri (fun i (id, data) -> Bytes.blit_string id 0 table (i * 12) 4; Bytes.set_int64_be table ((i * 12) + 4) (Int64.of_int !offset); offset := !offset + String.length data) chunks; Bytes.set_int64_be table ((c * 12) + 4) (Int64.of_int !offset); let header = "CGPH\001\001" ^ String.make 1 (Char.chr c) ^ "\000" in let body = header ^ Bytes.to_string table ^ String.concat "" (List.map snd chunks) in body ^ Git.Hash.to_raw_string (Git.Hash.digest_raw body) let parse data = match G.of_string data with Ok graph -> graph | Error (`Msg m) -> fail m let test_model seed = let n, parents, dates, topo, corrected = model seed in List.iter (fun v2 -> let graph = parse (encode seed v2) in check_eq ~eq:Int.equal n (G.count graph); check_eq ~eq:Bool.equal v2 (G.corrected_dates graph); for i = 0 to n - 1 do let hash = Git.Hash.of_raw_string (oid n i) in check_eq ~eq:(Option.equal Int.equal) (Some i) (G.find graph hash); check_eq ~eq:Git.Hash.equal hash (G.hash graph i); check_eq ~eq:String.equal (tree i) (Git.Hash.to_raw_string (G.tree graph i)); check_eq ~eq:(List.equal Int.equal) parents.(i) (G.parents graph i); check_eq ~eq:Int64.equal dates.(i) (G.commit_time graph i); check_eq ~eq:Int64.equal (if v2 then corrected.(i) else topo.(i)) (G.generation graph i) done; check_eq ~eq:(Option.equal Int.equal) None (G.find graph Git.Hash.null)) [ false; true ] let test_header seed = let valid = encode seed true in ignore (parse valid); List.iter (fun (offset, value) -> let bad = Bytes.of_string valid in Bytes.set bad offset value; match G.of_string (Bytes.to_string bad) with | Error _ -> () | Ok _ -> fail "invalid commit-graph header accepted") [ (0, 'X'); (4, '\002'); (5, '\002'); (7, '\001') ] let suite = ( "commit_graph", [ test_case "generated generation and edge records" [ uint16 ] test_model; test_case "one invalid header field" [ uint16 ] test_header; ] )