Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100(* 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. *)
let ref_read = Test_helpers.ref_read
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 host_key = Ssh.Keys.of_seed `Ed25519 "nox-git-ssh-server-seed-32bytes!!"let client_key = Ssh.Keys.of_seed `Ed25519 "nox-git-ssh-client-seed-32bytes!!"
let pseudo_random_payload len = let b = Bytes.create len in let state = ref 0x1234567 in for i = 0 to len - 1 do state := ((!state * 1103515245) + 12345) land 0x7fffffff; Bytes.set b i (Char.chr ((!state lsr 16) land 0xff)) done; Bytes.unsafe_to_string b
(* A repository whose main holds one file of [size] bytes that do not compress, so the pack the server sends is at least [size] bytes. *)let repo_with_blob ~sw ~fs size = let srepo = Test_helpers.init_bare ~sw ~fs (Fpath.v (Filename.temp_dir "gsb" "")) in let blob = Git_eio.Repository.write_blob srepo (pseudo_random_payload size) in let tree = Git_eio.Repository.write_tree srepo (Git.Tree.v [ Git.Tree.entry ~perm:`Normal ~name:"F" blob ]) in let a = Git.User.v ~name:"n" ~email:"n@e" ~date:0L () in let c = Git_eio.Repository.write_commit srepo (Git.Commit.v ~tree ~author:a ~committer:a (Some "c\n")) in Git_eio.Repository.write_ref srepo "refs/heads/main" c; srepo
let budget bytes = Git.Store.pack_budget ~max_bytes:bytes ~max_object_size:(1 lsl 30) ~max_total_object_size:(1 lsl 30)
let allow user = function | Ssh.Server.Pubkey p -> Ssh.Server.is_pubkey_query p || Ssh.Server.verify_pubkeyauth ~user p | Ssh.Server.Authnone -> false | Ssh.Server.Password _ -> false
(* Run [f] as a client of a server for [srepo] on the [port] it announces, on a switch of its own that must end within ten seconds: a session that fails leaves nothing running for its caller to wait on. *)let against ?on_request ?accept_git_protocol srepo f = Eio_main.run @@ fun env -> Eio.Switch.run @@ fun sw -> let net = Eio_net_mem.v ~sw () in let mono = Eio.Stdenv.mono_clock env in let clock = Eio.Stdenv.clock env in let fs = Eio.Stdenv.fs env in let result = ref None in let srepo = srepo ~sw ~fs in let ready, announce = Eio.Promise.create () in Eio.Fiber.first (fun () -> Eio.Switch.run @@ fun ssw -> Git_ssh.serve ?on_request ?accept_git_protocol ~max_connections:64 ~sw:ssw ~on_listen:(Eio.Promise.resolve announce) ~net ~mono ~host_key ~authenticate:allow ~addr:(`Tcp (Eio.Net.Ipaddr.V4.loopback, 0)) srepo) (fun () -> let port = Test_helpers.announced_port ~clock ready in result := Some (Eio.Time.with_timeout_exn clock 10. (fun () -> Eio.Switch.run @@ fun sw -> f ~sw ~net ~mono ~fs ~port))); Option.get !result
let read_file path = In_channel.with_open_bin path In_channel.input_all
let contains ~sub s = let n = String.length sub in let rec go i = i + n <= String.length s && (String.sub s i n = sub || go (i + 1)) in go 0