Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392(* 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 push and a fetch tell their [on_progress], against the bytes they sent and received. *)
module Reader = Bytesrw.Bytes.Reader
let advertisement_of ?service ?receive repo = match Git_eio.Serve.advertise ?service ?receive repo with | Ok a -> a | Error (`Msg m) -> Alcotest.fail m
let with_temp_repo = Test_helpers.with_temp_repolet commit = Test_helpers.commitlet event = Alcotest.testable Git.Progress.pp Git.Progress.equallet te name h = Git.Tree.entry ~perm:`Normal ~name h
(* Every event [on_progress] is told, in order. *)let recorder () = let events = ref [] in ((fun e -> events := e :: !events), fun () -> List.rev !events)
(* A commit of two files: two blobs, a tree and the commit, four objects. *)let fixture repo = let b1 = Git_eio.Repository.write_blob repo "one\n" in let b2 = Git_eio.Repository.write_blob repo "two\n" in let tree = Git_eio.Repository.write_tree repo (Git.Tree.v [ te "a" b1; te "b" b2 ]) in let c = commit ~repo ~tree ~parents:[] ~message:"two files" in Git_eio.Repository.write_ref repo "refs/heads/main" c; c
(* A commit of one blob of 200000 bytes that zlib cannot shrink, from a linear congruential generator: its pack spans four side-band-64k packets of at most 65515 bytes (protocol-capabilities(5), "side-band, side-band-64k"), so a count of bytes received that does not add up is seen. *)let large_fixture repo = let state = ref 12345 in let byte _ = state := ((!state * 1103515245) + 12345) land 0x7fffffff; Char.chr ((!state lsr 16) land 0xff) in let b = Git_eio.Repository.write_blob repo (String.init 200_000 byte) in let tree = Git_eio.Repository.write_tree repo (Git.Tree.v [ te "large" b ]) in let c = commit ~repo ~tree ~parents:[] ~message:"large" in Git_eio.Repository.write_ref repo "refs/heads/main" c; c
(* The pack of a receive-pack request: what follows the flush-pkt that ends its commands (gitprotocol-pack(5), "Reference Update Request and Packfile Transfer"). *)let pack_of_request body = let rec skip pos = match int_of_string ("0x" ^ String.sub body pos 4) with | 0 -> pos + 4 | len -> skip (pos + len) in let start = skip 0 in String.sub body start (String.length body - start)
(* The object count of a pack's header: the 4-byte network-order number after the signature and the version (gitformat-pack(5), "pack-*.pack files"). *)let pack_count pack = Int32.to_int (String.get_int32_be pack 8)
(* [Counted 1] to [Counted n]: one event as the walk counts each object, the last the total, as git's "Counting objects: k" counts. A count told once, after the walk, would tell nothing while it runs, so a caller that reads the events as the transfer's movement would read a long count as a stall. *)let counted n = List.init n (fun i -> Git.Progress.Counted (i + 1))let written n = List.init n (fun i -> Git.Progress.Written (i + 1))
let sent = List.filter_map (function Git.Progress.Sent n -> Some n | _ -> None)
let advertised server = match Git.Smart.Client.Push.advertisement Git.Smart.Client.Push.v (Reader.of_string (advertisement_of ~service:"git-receive-pack" server)) with | Error (`Msg m) -> Alcotest.failf "advertise: %s" m | Ok v -> v
let negotiate_err = function | `Msg m -> Alcotest.fail m | `Rejected (r, _, _) -> Alcotest.failf "rejected: %s" r | `Declined why -> Alcotest.failf "declined: %s" why
(* A push over the smart protocol to an in-process receive-pack: the objects counted are the count in the header of the pack the server took, each entry is reported written as it goes into the pack, as git's "Writing objects: k/n" counts, and the bytes sent end at that pack's length. A count taken before the remote's objects are left out, or bytes counted from the request's commands, would differ; a count written once the pack ended would report no Written before the trailer's bytes. *)let test_negotiate_counts () = with_temp_repo @@ fun sw fs src_dir -> with_temp_repo @@ fun _sw _fs srv_dir -> let src = Git_eio.Repository.init ~sw ~fs src_dir in let server = Test_helpers.init_bare ~sw ~fs srv_dir in let _ = fixture src in let request = ref "" in let send body = request := Reader.to_string body; match Git_eio.Serve.receive_pack server (Reader.of_string !request) with | Ok status -> Ok (Reader.of_string status) | Error _ as e -> e in let on_progress, events = recorder () in let client, adv = advertised server in (match Git_eio.Push.negotiate ~on_progress ~client ~adv ~src ~src_hash:"refs/heads/main" ~dst_ref:"refs/heads/main" ~force:false ~send () with | Ok _ -> () | Error e -> negotiate_err e); let pack = pack_of_request !request in let n = pack_count pack in Alcotest.(check int) "the pack holds the four objects" 4 n; let events = events () in Alcotest.(check (list event)) "each object counted, then each entry written" (counted n @ written 4) (List.filter (function Git.Progress.Sent _ -> false | _ -> true) events); Alcotest.(check (list string)) "the header's bytes, then each entry written before its bytes, then the \ trailer's" [ "counted"; "counted"; "counted"; "counted"; "sent"; "written"; "sent"; "written"; "sent"; "written"; "sent"; "written"; "sent"; "sent"; ] (List.map (function | Git.Progress.Counted _ -> "counted" | Written _ -> "written" | Sent _ -> "sent" | Received _ | Remote _ | Hook _ -> "other") events); let sent = sent events in Alcotest.(check int) "the last count sent is the pack's length" (String.length pack) (List.nth sent (List.length sent - 1)); Alcotest.(check bool) "each count sent is above the one before" true (snd (List.fold_left (fun (prev, ok) n -> (n, ok && n > prev)) (0, true) sent))
(* A local push copies objects: the objects counted, then each one copied. *)let test_local_counts () = with_temp_repo @@ fun sw fs src_dir -> with_temp_repo @@ fun _sw _fs dst_dir -> let src = Git_eio.Repository.init ~sw ~fs src_dir in let dst = Test_helpers.init_bare ~sw ~fs dst_dir in let _ = fixture src in let on_progress, events = recorder () in (match Git_eio.Push.local ~on_progress ~src ~dst ~src_hash:"main" ~dst_ref:"refs/heads/main" ~force:false () with | Ok _ -> () | Error (`Msg m | `Non_fast_forward m) -> Alcotest.fail m); Alcotest.(check (list event)) "four objects counted one by one, then copied one by one" (counted 4 @ written 4) (events ())
(* A local fetch copies objects: the objects counted, then each one copied, as a local push reports them. *)let test_local_fetch_counts () = with_temp_repo @@ fun sw fs src_dir -> with_temp_repo @@ fun _sw _fs dst_dir -> let src = Git_eio.Repository.init ~sw ~fs src_dir in let dst = Test_helpers.init_bare ~sw ~fs dst_dir in let _ = fixture src in let on_progress, events = recorder () in (match Git_eio.Fetch.local ~on_progress ~src ~dst ~ref_name:"main" () with | Ok _ -> () | Error (`Msg m) -> Alcotest.fail m); Alcotest.(check (list event)) "four objects counted one by one, then copied one by one" (counted 4 @ written 4) (events ())
(* A fetch of two refs whose histories share their first commit counts the objects of both once: a count of each ref's history apart, before either is copied, would count the shared commit's four objects twice. *)let test_local_refs_counts () = with_temp_repo @@ fun sw fs src_dir -> with_temp_repo @@ fun _sw _fs dst_dir -> let src = Git_eio.Repository.init ~sw ~fs src_dir in let dst = Test_helpers.init_bare ~sw ~fs dst_dir in let first = fixture src in let b = Git_eio.Repository.write_blob src "three\n" in let tree = Git_eio.Repository.write_tree src (Git.Tree.v [ te "c" b ]) in let next = commit ~repo:src ~tree ~parents:[ first ] ~message:"three" in Git_eio.Repository.write_ref src "refs/heads/next" next; let on_progress, events = recorder () in (match Git_eio.Fetch.local_refs ~on_progress ~src ~dst ~select:Option.some () with | Ok _ -> () | Error (`Msg m) -> Alcotest.fail m); Alcotest.(check (list event)) "seven objects counted one by one, then copied one by one" (counted 7 @ written 7) (events ())
(* The remote's answer to a push, on side bands (gitprotocol-pack(5), "side-band, side-band-64k"; protocol-capabilities(5)): band 2 is the remote's progress, which redraws a meter with CR, ends a line with LF and may split one across two packets; band 1 is the report-status. Each packet below is spelled out with its pkt-line length. *)let side_band_answer = String.concat "" [ "0022\002Resolving deltas: 50% (1/2)\r"; "0029\002Resolving deltas: 100% (2/2), done.\n"; "0018\002Total 3 (delta 0), "; "0018\002reused 0 (delta 0)\n"; "0012\002\027[31mred\027[0m\n"; "002e\001000eunpack ok\n0017ok refs/heads/main\n0000"; "0000"; ]
let test_remote_lines () = with_temp_repo @@ fun sw fs src_dir -> with_temp_repo @@ fun _sw _fs srv_dir -> let src = Git_eio.Repository.init ~sw ~fs src_dir in let server = Test_helpers.init_bare ~sw ~fs srv_dir in let _ = fixture src in let send body = let _ : string = Reader.to_string body in Ok (Reader.of_string side_band_answer) in let on_progress, events = recorder () in let client, adv = advertised server in (match Git_eio.Push.negotiate ~on_progress ~client ~adv ~src ~src_hash:"refs/heads/main" ~dst_ref:"refs/heads/main" ~force:false ~send () with | Ok _ -> () | Error e -> negotiate_err e); Alcotest.(check (list event)) "each line, split on CR and LF, its escape sequence shown" Git.Progress. [ Remote "Resolving deltas: 50% (1/2)"; Remote "Resolving deltas: 100% (2/2), done."; Remote "Total 3 (delta 0), reused 0 (delta 0)"; Remote "\\x1b[31mred\\x1b[0m"; ] (List.filter (function Git.Progress.Remote _ -> true | _ -> false) (events ()))
(* A fetch over the smart protocol from an in-process upload-pack: the bytes received grow with each band-1 packet and end at the length of the pack the server sent, and the objects written are the count in its header. A count of each packet alone, not of all so far, ends below the pack's length. *)let test_fetch_counts () = with_temp_repo @@ fun sw fs srv_dir -> with_temp_repo @@ fun _sw _fs dst_dir -> let server = Git_eio.Repository.init ~sw ~fs srv_dir in let dst = Git_eio.Repository.init ~sw ~fs dst_dir in let _ = large_fixture server in let response = ref "" in let send body = match Git_eio.Serve.upload_pack server (Reader.of_string body) with | Ok r -> response := r; Ok (Reader.of_string r) | Error _ as e -> e in let on_progress, events = recorder () in (match Git.Smart.Client.Fetch.advertisement Git.Smart.Client.Fetch.v (Reader.of_string (advertisement_of ~service:"git-upload-pack" server)) with | Error (`Msg m) -> Alcotest.failf "advertise: %s" m | Ok (client, adv) -> ( match Git_eio.Fetch.negotiate ~on_progress ~client ~adv ~ref_name:"main" ~send dst with | Ok _ -> () | Error (`Msg m) -> Alcotest.fail m)); let pack = match Git.Smart.Message.Upload_pack.read_response ~side_band:true (Reader.of_string !response) with | Ok r -> r.pack | Error (`Msg m) -> Alcotest.failf "the server's response: %s" m in let events = events () in let received = List.filter_map (function Git.Progress.Received n -> Some n | _ -> None) events in Alcotest.(check int) "the last count received is the pack's length" (String.length pack) (List.nth received (List.length received - 1)); Alcotest.(check bool) "the pack came in more than one piece" true (List.length received > 1); Alcotest.check event "the objects of the pack, last" (Git.Progress.Written (pack_count pack)) (List.nth events (List.length events - 1))
(* The remote's text arrives in pieces, on side band 2 or on an SSH session's standard error. A line ends at a CR or an LF, as git's sideband.c ends one, so each step of a meter the remote redraws with CR is a line; a line split across two pieces is given whole once its end arrives, the text after the last end waits for the next piece or for flush, and an escape sequence the remote writes is shown, not run. The counts are totals so far, and a pre-push hook's lines print as the hook's. *)let test_lines () = let seen = ref [] in let lines = Git.Progress.lines (fun e -> seen := e :: !seen) in Git.Progress.feed lines "Counting objects: 50% (1/2)\rCounting objects: 100% (2/2), done.\n\ Total 2, "; Git.Progress.feed lines "reused 0\n\n\027[31mred"; Git.Progress.flush lines; let shown = List.map (Fmt.str "%a" Git.Progress.pp) in Alcotest.(check (list string)) "each line, whole, its escape shown" [ "remote: Counting objects: 50% (1/2)"; "remote: Counting objects: 100% (2/2), done."; "remote: Total 2, reused 0"; "remote: \\x1b[31mred"; ] (shown (List.rev !seen)); Alcotest.(check (list string)) "the counts" [ "4 objects counted"; "4 objects written"; "312 bytes sent"; "4096 bytes received"; "hook: lint: 0 issues"; ] (shown Git.Progress. [ Counted 4; Written 4; Sent 312; Received 4096; Hook "lint: 0 issues"; ])
let suite = ( "progress", [ Alcotest.test_case "a push counts each object, then writes its pack" `Quick test_negotiate_counts; Alcotest.test_case "a local push counts each object, then copies it" `Quick test_local_counts; Alcotest.test_case "a local fetch counts each object, then copies it" `Quick test_local_fetch_counts; Alcotest.test_case "a local fetch of refs counts each object once" `Quick test_local_refs_counts; Alcotest.test_case "the remote's lines" `Quick test_remote_lines; Alcotest.test_case "a fetch counts its pack" `Quick test_fetch_counts; Alcotest.test_case "lines and counts" `Quick test_lines; ] )