(* 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. *) (* commit_write.mli against git: each commit is read back by native Git (git ls-tree -r, git rev-parse), the independent reader, so a tree laid out wrong, a mode lost or a parent misnamed shows as Git's text. *) module R = Git_eio.Repository module W = Git_eio.Commit_write let with_repo f = Eio_main.run @@ fun env -> Eio.Switch.run @@ fun sw -> let fs = Eio.Stdenv.fs env in let dir = Fpath.v (Filename.temp_dir "git-commit-write-" "") in Fun.protect ~finally:(fun () -> Eio.Path.rmtree ~missing_ok:true Eio.Path.(fs / Fpath.to_string dir)) (fun () -> f (Fpath.to_string dir) (R.init ~sw ~fs dir)) (* What native Git prints for [args] in [dir]. *) let git dir args = let out = Filename.temp_file "git-commit-write" "" in let command = String.concat " " (List.map Filename.quote ("/usr/bin/git" :: "-C" :: dir :: args)) ^ " > " ^ Filename.quote out in Fun.protect ~finally:(fun () -> Sys.remove out) @@ fun () -> match Sys.command command with | 0 -> In_channel.with_open_bin out In_channel.input_all | _ -> Alcotest.fail ("Git command failed: " ^ command) let who = Git.User.v ~name:"A U Thor" ~email:"a@example.test" ~date:0L () let ok = function Ok x -> x | Error (`Msg m) -> Alcotest.fail m let blob s = Git.Hash.to_hex (Git.Hash.digest_string ~kind:`Blob s) let ls_tree dir c = String.split_on_char '\n' (git dir [ "ls-tree"; "-r"; Git.Hash.to_hex c ]) |> List.filter (( <> ) "") let line ?(mode = "100644") path content = Fmt.str "%s blob %s\t%s" mode (blob content) path let test_root_and_child () = with_repo @@ fun dir r -> let root = ok (W.root r ~files:[ ("a", "a\n"); ("b", "b\n") ] ~author:who ~committer:who "root") in Alcotest.(check (list string)) "the files at the top" [ line "a" "a\n"; line "b" "b\n" ] (ls_tree dir root); Alcotest.(check string) "no parent" (Git.Hash.to_hex root) (String.trim (git dir [ "rev-list"; "--parents"; "-n1"; Git.Hash.to_hex root ])); let child = ok (W.child r ~parent:root ~edits:[ ("b", "B\n"); ("d/e", "e\n") ] ~author:who ~committer:who "child") in Alcotest.(check (list string)) "the parent's tree, edited" [ line "a" "a\n"; line "b" "B\n"; line "d/e" "e\n" ] (ls_tree dir child); Alcotest.(check string) "its one parent" (Git.Hash.to_hex root) (String.trim (git dir [ "rev-parse"; Git.Hash.to_hex child ^ "^" ])); (* A signer runs on the commit before it is written: the hash names the signed object. *) let marked = ref 0 in let sign c = incr marked; Ok c in ignore (ok (W.root ~sign r ~files:[] ~author:who ~committer:who "signed")); Alcotest.(check int) "signed once" 1 !marked; (* A signer that does not sign is the write's error, never a commit written unsigned in its place. *) let refuse _ = Error (`Msg "no signing key") in match W.root ~sign:refuse r ~files:[] ~author:who ~committer:who "refused" with | Ok c -> Alcotest.failf "%a written unsigned" Git.Hash.pp c | Error (`Msg m) -> Alcotest.(check string) "the signer's" "no signing key" m (* carry: [base]'s tree with the listed paths as [tip] holds them, a mode change and a removal included, and every other path [tip] changes left as [base] has it. An implementation that took [tip]'s whole tree keeps c; one that skipped removals keeps b. *) let test_carry () = with_repo @@ fun dir r -> let base = ok (W.root r ~files:[ ("a", "a\n"); ("b", "b\n") ] ~author:who ~committer:who "base") in let tip = ok (W.child r ~parent:base ~edits:[ ("a", "A\n"); ("c", "c\n") ] ~author:who ~committer:who "tip") in let tip_tree = Git.Commit.tree (Result.get_ok (R.read_commit r tip)) in let tip_tree = ok (R.overlay_paths r ~base:tip_tree [ ("b", R.Drop); ("x", R.Set (`Exec, R.write_blob r "x\n")) ]) in let tip = W.with_tree r tip ~tree:tip_tree in let carried, tree = ok (W.carry r ~base ~tip ~paths:[ "a"; "b"; "x" ] ~author:who ~committer:who "carried") in Alcotest.(check (list string)) "the paths carried, the others as base has them" [ line "a" "A\n"; line ~mode:"100755" "x" "x\n" ] (ls_tree dir carried); Alcotest.(check string) "its tree" (Git.Hash.to_hex tree) (String.trim (git dir [ "rev-parse"; Git.Hash.to_hex carried ^ "^{tree}" ])); Alcotest.(check string) "on base" (Git.Hash.to_hex base) (String.trim (git dir [ "rev-parse"; Git.Hash.to_hex carried ^ "^" ])) (* with_tree keeps a commit whose tree is already the one given, and otherwise writes one that differs in its tree alone. *) let test_with_tree () = with_repo @@ fun dir r -> let c = ok (W.root r ~files:[ ("a", "a\n") ] ~author:who ~committer:who "c") in let tree = Git.Commit.tree (Result.get_ok (R.read_commit r c)) in Alcotest.(check string) "the same tree, the same commit" (Git.Hash.to_hex c) (Git.Hash.to_hex (W.with_tree r c ~tree)); let other = R.write_tree r Git.Tree.empty in let d = W.with_tree r c ~tree:other in let header h = String.split_on_char '\n' (git dir [ "cat-file"; "commit"; Git.Hash.to_hex h ]) |> List.filter (fun l -> not (String.starts_with ~prefix:"tree " l)) in Alcotest.(check (list string)) "all but the tree kept" (header c) (header d); Alcotest.(check (list string)) "the new tree" [] (ls_tree dir d) let suite = ( "commit_write", [ Alcotest.test_case "root and child" `Quick test_root_and_child; Alcotest.test_case "carry" `Quick test_carry; Alcotest.test_case "with_tree" `Quick test_with_tree; ] )