Git object storage and pack files for Eio
1(* Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org>
2 Copyright (c) 2017-2024 Romain Calascibetta <romain.calascibetta@gmail.com>
3 Copyright (c) 2024-2026 Thomas Gazagnaire <thomas@gazagnaire.org>
4
5 Permission to use, copy, modify, and distribute this software for any
6 purpose with or without fee is hereby granted, provided that the above
7 copyright notice and this permission notice appear in all copies.
8
9 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *)
16
17(** Git values (union of all object types). *)
18
19type t =
20 | Blob of Blob.t
21 | Commit of Commit.t
22 | Tree of Tree.t
23 | Tag of Tag.t (** The type of git values. *)
24
25val blob : Blob.t -> t
26(** Create a blob value. *)
27
28val commit : Commit.t -> t
29(** Create a commit value. *)
30
31val tree : Tree.t -> t
32(** Create a tree value. *)
33
34val tag : Tag.t -> t
35(** Create a tag value. *)
36
37val kind : t -> [ `Blob | `Commit | `Tree | `Tag ]
38(** Get the kind of a value. *)
39
40val pp : t Fmt.t
41(** Pretty-print a value. *)
42
43val equal : t -> t -> bool
44(** Equality on values. *)
45
46val compare : t -> t -> int
47(** Total ordering on values. *)
48
49val hash : t -> int
50(** Hash function for use with Hashtbl. *)
51
52val digest : t -> Hash.t
53(** Compute the git hash of a value. *)
54
55val length : t -> int
56(** The length of the value content (without header). *)
57
58val to_string_without_header : t -> string
59(** Encode to git format without the header. *)
60
61val to_string : t -> string
62(** Encode to git format with header ("type length\x00content"). *)
63
64val of_string :
65 kind:[ `Blob | `Commit | `Tree | `Tag ] ->
66 string ->
67 (t, [ `Msg of string ]) result
68(** Parse from git format (content only, no header). *)
69
70val of_string_exn : kind:[ `Blob | `Commit | `Tree | `Tag ] -> string -> t
71(** [of_string_exn ~kind s] is like {!of_string} but raises on error. *)
72
73val of_string_with_header : string -> (t, [ `Msg of string ]) result
74(** Parse from git format with header. *)
75
76val of_string_with_header_exn : string -> t
77(** [of_string_with_header_exn s] is like {!of_string_with_header} but raises on
78 error. *)
79
80(** {1 Bytesrw support} *)
81
82module Reader = Bytesrw.Bytes.Reader
83module Writer = Bytesrw.Bytes.Writer
84module Slice = Bytesrw.Bytes.Slice
85
86val read_header :
87 Reader.t ->
88 ([ `Blob | `Commit | `Tree | `Tag ] * int, [ `Msg of string ]) result
89(** Read a git object header from a reader. Returns the kind and content length.
90*)
91
92val of_reader :
93 kind:[ `Blob | `Commit | `Tree | `Tag ] ->
94 Reader.t ->
95 (t, [ `Msg of string ]) result
96(** [of_reader ~kind r] parses a value directly from a reader positioned at the
97 start of the object body. Commits and trees are parsed without materialising
98 the full content string; blobs and tags still go through {!of_string}. *)
99
100val read : Reader.t -> (t, [ `Msg of string ]) result
101(** Read a git object from a bytesrw reader (parses the header then the body).
102*)
103
104val write : Writer.t -> t -> unit
105(** Write a git object to a bytesrw writer (with header). *)
106
107val write_content : Writer.t -> t -> unit
108(** Write only the content (without header) to a bytesrw writer. *)
109
110module Set : Set.S with type elt = t
111module Map : Map.S with type key = t