Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103(* 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.
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. *)
(** Interop: replay real [git upload-pack] wire traces (captured by [capture.sh] from the system git) through the {!Git.Smart} codec. Proves that our advertisement parser and upload-pack response reader decode bytes a real git server emits, and that incremental negotiation (want + have + done) yields a strictly smaller pack preceded by a common-base ACK -- exactly the behaviour {!Git_eio.Serve} reproduces.
The git binary is only needed at capture time; the test replays the committed bytes. Refresh with [REGEN=1 dune build @regen]. *)
let trace file = Filename.concat "traces" file
let read_file p = let ic = open_in_bin p in Fun.protect ~finally:(fun () -> close_in ic) (fun () -> really_input_string ic (in_channel_length ic))
let reader = Bytesrw.Bytes.Reader.of_stringlet tip = String.trim (read_file (trace "tip"))let parent = String.trim (read_file (trace "parent"))
let pack_objects pack = match Git.Pack.of_string pack with | Error (`Msg m) -> Alcotest.failf "decode pack: %s" m | Ok p -> ( match Git.Pack.fold (fun ~offset:_ ~kind:_ ~data:_ n -> n + 1) 0 p with | Ok n -> n | Error (`Msg m) -> Alcotest.failf "fold: %s" m)
let read_response file = match Git.Smart.Message.Upload_pack.read_response ~side_band:true (reader (read_file (trace file))) with | Ok resp -> resp | Error (`Msg m) -> Alcotest.failf "%s: %s" file m
let ack_references h = function | Git.Smart.Message.Negotiation.ACK x | Common x | Ready x | Continue x -> Git.Hash.to_hex x = h | NAK -> false
let test_advertise () = match Git.Smart.Message.Advertisement.of_string (read_file (trace "advertise.bin")) with | Error (`Msg m) -> Alcotest.failf "advertise: %s" m | Ok adv -> Alcotest.(check bool) "real git advertises side-band-64k" true (Git.Smart.Message.Capability.mem adv.capabilities "side-band-64k"); Alcotest.(check bool) "real git advertises multi_ack_detailed" true (Git.Smart.Message.Capability.mem adv.capabilities "multi_ack_detailed"); Alcotest.(check (option string)) "HEAD resolves to the captured tip" (Some tip) (Option.map Git.Hash.to_hex (Git.Smart.Message.Advertisement.find_ref adv "HEAD"))
let test_clone () = let resp = read_response "clone-response.bin" in Alcotest.(check int) "clone delivers the whole history" 6 (pack_objects resp.pack)
let test_incremental () = let resp = read_response "incremental-response.bin" in Alcotest.(check bool) "git ACKs the offered common parent" true (List.exists (ack_references parent) resp.acks); let n = pack_objects resp.pack in Alcotest.(check int) "incremental sends only c2's new objects" 3 n; Alcotest.(check bool) "incremental pack is strictly smaller than the clone" true (n < pack_objects (read_response "clone-response.bin").pack)
let () = Alcotest.run "interop_git" [ ( "git_upload_pack", [ Alcotest.test_case "advertisement parses" `Quick test_advertise; Alcotest.test_case "clone pack decodes" `Quick test_clone; Alcotest.test_case "incremental ACK + smaller pack" `Quick test_incremental; ] ); ]