Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125(* 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. *)
(** Commit-graph files (gitformat-commit-graph(5)).
A commit-graph lists commits by name with, for each, its root tree, its parents as positions in the graph, its commit time and its generation number, so that a walk learns a commit's place in the history without reading the commit object. A graph is one file, or a chain of layers (the files named by [objects/info/commit-graphs/commit-graph-chain]), each layer numbering its commits after those of the layers below it.
A commit is found by its position, an integer from [0] to [count t - 1] across the whole chain. The graph is closed under reachability: every parent of a commit in the graph is in the graph. *)
type t(** The type for commit-graphs, one layer and the layers below it. *)
val of_read : ?base:t -> length:int -> (int -> int -> string) -> (t, [ `Msg of string ]) result(** [of_read ?base ~length read] is the graph file of [length] bytes that [read o n] reads [n] bytes of at [o], as the layer above [base] (default: none, the file is a whole graph). It is [Error] if the file is not a version 1 SHA-1 commit-graph, a required chunk is missing or does not fit the number of commits, it names a number of base layers other than [base]'s, or its Base Graphs List does not name [base]'s layers. *)
val of_string : ?base:t -> string -> (t, [ `Msg of string ]) result(** [of_string ?base s] is {!of_read} over [s]. *)
val checksum : t -> Object_hash.t(** [checksum t] is the trailing checksum of [t]'s top layer, the name of its file in a chain. *)
val layers : t -> int(** [layers t] is the number of files [t] is made of. *)
val count : t -> int(** [count t] is the number of commits of [t], across its layers. *)
val find : t -> Object_hash.t -> int option(** [find t h] is the position of the commit [h] in [t], if [t] has it. *)
val probed : t -> (unit -> unit) -> t(** [probed t f] is [t] calling [f] at each {!find}: a store counts with it the commits a walk looks up in its graph, which read no object. *)
(** {1:commits Commits}
Each function raises [Invalid_argument] if the position is out of bounds, or if the file gives a parent position out of bounds. *)
val hash : t -> int -> Object_hash.t(** [hash t i] is the name of the commit at [i]. *)
val tree : t -> int -> Object_hash.t(** [tree t i] is the root tree of the commit at [i]. *)
val parents : t -> int -> int list(** [parents t i] is the positions of the parents of the commit at [i], in the commit's order. *)
val commit_time : t -> int -> int64(** [commit_time t i] is the committer date of the commit at [i], in seconds since the epoch (34 bits). *)
val generation : t -> int -> int64(** [generation t i] is the generation number of the commit at [i]: its corrected commit date when every layer of [t] carries Generation Data (the GDA2 chunk, with GDO2 for offsets past 31 bits), and its topological level otherwise. A commit cannot reach a commit of a greater generation. [0] is a generation the writer did not compute. *)
val corrected_dates : t -> bool(** [corrected_dates t] is [true] iff {!generation} is the corrected commit date, [false] if it is the topological level. *)
(** {1:layers Layers} *)
val below : t -> t option(** [below t] is [t] without its top layer, [None] when [t] is one file. *)
val generation_data : t -> bool(** [generation_data t] is [true] iff the top layer of [t] carries Generation Data (the GDA2 chunk), whatever the layers below it carry. *)
(** {1:writing Writing} *)
type commit = { hash : Object_hash.t; tree : Object_hash.t; parents : Object_hash.t list; time : int64; (** The committer date, in seconds since the epoch. *)}(** The type for the commits a layer holds, as their objects give them. *)
val encode : ?base:t -> commit list -> string(** [encode ?base cs] is the file of the layer above [base] (default: none, a whole graph) holding the commits [cs], byte for byte as [git commit-graph write] writes it (commit-graph.c, write_commit_graph_file): the chunks OIDF, OIDL, CDAT, GDA2 (with GDO2 for offsets past 31 bits) when [base] is [None] or {!generation_data} of [base], EDGE for an octopus, and BASE naming [base]'s layers, then the SHA-1 of what precedes it. The topological levels and corrected commit dates of [cs] are computed from their parents', read from [base] for a parent [base] holds.
@raise Invalid_argument if a commit repeats, is in [base], or has a parent neither in [cs] nor in [base]. *)