Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869(* 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. *)
let ( let* ) = Result.bind
let read r h = match Repository.read_commit r h with | Ok c -> Ok c | Error e -> Error (Git.Value.msg_of_error e)
let write ?(sign = Fun.id) r ~tree ~parents ~author ~committer message = Repository.write_commit r (sign (Git.Commit.v ~tree ~author ~committer ~parents (Some message)))
let root ?sign r ~files ~author ~committer message = let entry (name, content) = Git.Tree.entry ~perm:`Normal ~name (Repository.write_blob r content) in let tree = Repository.write_tree r (Git.Tree.v (List.map entry files)) in write ?sign r ~tree ~parents:[] ~author ~committer message
let child ?sign r ~parent ~edits ~author ~committer message = let* c = read r parent in let edit (path, content) = (path, Repository.Set (`Normal, Repository.write_blob r content)) in let* tree = Repository.overlay_paths r ~base:(Git.Commit.tree c) (List.map edit edits) in Ok (write ?sign r ~tree ~parents:[ parent ] ~author ~committer message)
let carried paths : Git.Diff.change -> _ = function | Added { path; hash; perm } | Modified { path; new_hash = hash; new_perm = perm; _ } when List.mem path paths -> Some (path, Repository.Set (perm, hash)) | Removed { path; _ } when List.mem path paths -> Some (path, Repository.Drop) | Added _ | Modified _ | Removed _ -> None
let carry r ~base ~tip ~paths ~author ~committer message = let* changes = Diff.commits r ~old_commit:base ~new_commit:tip in let* c = read r base in let* tree = Repository.overlay_paths r ~base:(Git.Commit.tree c) (List.filter_map (carried paths) changes) in Ok (write r ~tree ~parents:[ base ] ~author ~committer message, tree)
let with_tree r h ~tree = match Repository.read_commit r h with | Error e -> Fmt.invalid_arg "%a" Git.Value.pp_error e | Ok c when Git.Hash.equal tree (Git.Commit.tree c) -> h | Ok c -> Repository.write_commit r (Git.Commit.v ~tree ~author:(Git.Commit.author c) ~committer:(Git.Commit.committer c) ~parents:(Git.Commit.parents c) (Git.Commit.message c))