Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123(* githooks(5), reference-transaction; Git v2.54.0 hook.h, RUN_HOOKS_OPT_INIT.stdout_to_stderr = 1. A real native hook cannot prepend diagnostic bytes to the command's protocol stream, even when it refuses. *)module G = Git_eio.Repository
let branch = "refs/heads/result"
let config_env = Git.Config_layers.env (function | "GIT_CONFIG_NOSYSTEM" -> Some "1" | _ -> None)
let msg = function Ok v -> v | Error (`Msg why) -> Alcotest.fail why
let child root target = Eio_main.run @@ fun env -> Eio.Switch.run @@ fun sw -> let fs = Eio.Stdenv.fs env in let repo = G.open_bare ~sw ~fs ~config_env root in let expected = msg (G.read_ref_strict repo branch) in let context = { G.proc = Eio.Stdenv.process_mgr env; clock = Eio.Stdenv.clock env; timeout = None; env = None; own = None; } in let reply = match G.compare_and_swap_ref ~hook_context:context repo branch ~expected target with | Ok () -> "updated\n" | Error (`Hook_rejected _) -> "refused\n" | Error `Raced -> failwith "test reference changed before publication" | Error (`Locked why) -> failwith why in Eio.Flow.copy_string reply (Eio.Stdenv.stdout env)
let commit repo message = let author = Git.User.v ~name:"Hook fixture" ~email:"fixture@example.org" ~date:0L () in G.write_commit repo (Git.Commit.v ~tree:Git.Tree.empty_digest ~author ~committer:author (Some message))
let executable () = let path = Fpath.v Sys.executable_name in if Fpath.is_abs path then path else Fpath.(v (Sys.getcwd ()) // path)
let test_output ~refused () = Eio_main.run @@ fun env -> Eio.Switch.run @@ fun sw -> let fs = Eio.Stdenv.fs env in let root = Eio.Path.( Eio.Stdenv.cwd env / if refused then "hook-refused.git" else "hook-accepted.git") in let directory = Fpath.v (Eio.Path.native_exn root) in let repo = G.init_bare ~sw ~fs ~config_env directory in Fun.protect ~finally:(fun () -> Eio.Path.rmtree root) @@ fun () -> let old = commit repo "original\n" and target = commit repo "target\n" in G.write_ref repo branch old; let hooks = Eio.Path.(root / "hooks") in Eio.Path.mkdirs ~exists_ok:true ~perm:0o700 hooks; let exit = if refused then "exit 1\n" else "exit 0\n" in Eio.Path.save ~create:(`Exclusive 0o700) Eio.Path.(hooks / "reference-transaction") ("#!/bin/sh\nprintf 'hook-out %s\\n' \"$1\"\n" ^ "printf 'hook-err %s\\n' \"$1\" >&2\n" ^ exit); let stdout = Buffer.create 128 and stderr = Buffer.create 128 in Eio.Process.run ~stdin:(Eio.Flow.string_source "") ~stdout:(Eio.Flow.buffer_sink stdout) ~stderr:(Eio.Flow.buffer_sink stderr) (Eio.Stdenv.process_mgr env) [ Fpath.to_string (executable ()); "--child"; Fpath.to_string directory; Git.Hash.to_hex target; ]; let actual = msg (G.read_ref_strict (G.reopen ~sw repo) branch) in Alcotest.(check (option (testable Git.Hash.pp Git.Hash.equal))) "the exact ref publishes on success and survives refusal" (Some (if refused then old else target)) actual; let diagnostics = String.split_on_char '\n' (Buffer.contents stderr) in Alcotest.(check bool) "the real hook ran and wrote its stderr marker" true (List.mem "hook-err preparing" diagnostics); Alcotest.(check string) "a native hook cannot prepend bytes to the caller's stdout" (if refused then "refused\n" else "updated\n") (Buffer.contents stdout); Alcotest.(check bool) "the hook stdout marker is preserved on stderr" true (List.mem "hook-out preparing" diagnostics)
let () = if Array.length Sys.argv = 4 && Sys.argv.(1) = "--child" then let root = match Fpath.of_string Sys.argv.(2) with | Ok path -> path | Error (`Msg why) -> failwith why in child root (Git.Hash.of_hex Sys.argv.(3)) else Alcotest.run "reference hook streams" [ ( "stdio", [ Alcotest.test_case "successful update preserves protocol stdout" `Quick (test_output ~refused:false); Alcotest.test_case "refused update preserves protocol stdout" `Quick (test_output ~refused:true); ] ); ]