Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317(* Copyright (c) 2024-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. *)
(** Git fetch, over any {!Store.S}.
The object graph walk and the smart-protocol negotiations do no I/O. The HTTP and SSH transports that give [send] and [channel] are in [nox-git-net].*)
val validate_closure : read:(Object_hash.t -> (Value.t, [ `Msg of string ]) result) -> max_objects:int -> max_bytes:int -> Object_hash.t -> (Object_hash.t list, [ `Msg of string ]) result(** Validate every commit, tree and blob reachable from a commit. The reader must bound each object before allocation. The two ceilings bound the number and aggregate decoded size of values examined; Git submodule links are external repositories and are not followed. The result is in child-before-parent order. *)
val validate_trees : read:(Object_hash.t -> (Value.t, [ `Msg of string ]) result) -> max_objects:int -> max_bytes:int -> Object_hash.t list -> (Object_hash.t list, [ `Msg of string ]) result(** Validate every tree and blob reachable from the supplied tree roots, with one visited set and aggregate object/byte bounds. Commit parents are not traversed. The reader must bound each object before allocation. *)
val validate_trees_bounded : read:(max_bytes:int -> Object_hash.t -> (Value.t, [ `Msg of string ]) result) -> max_objects:int -> max_bytes:int -> Object_hash.t list -> (Object_hash.t list, [ `Msg of string ]) result(** Like {!validate_trees}, passing the remaining aggregate decoded-byte allowance to the reader before each object is materialized. *)
exception Unreadable_object of Object_hash.t * string(** [Unreadable_object (h, reason)] is raised by {!reachable_from} when the object [h] cannot be read, or does not hash to [h], for [reason]. *)
val reachable_from : ?shallow:(Object_hash.t -> bool) -> ?counted:(unit -> unit) -> read:(Object_hash.t -> (Value.t, [ `Msg of string ]) result) -> exists:(Object_hash.t -> bool) -> Object_hash.t -> Object_hash.t list(** [reachable_from ?shallow ?counted ~read ~exists root] is every object reachable from [root] that [exists] does not already have, bottom-up (children before parents). A commit for which [shallow] is [true] is a root of the history, as a commit of a [shallow] file is (gitrepository-layout(5)): its tree is walked and its parents are not. [shallow] defaults to none. A missing or hash-mismatched source object raises {!Unreadable_object} rather than returning an incomplete graph. [counted ()] is called as each object joins the result, so a caller sees a long walk move; it defaults to nothing. *)
val reachable_named : ?shallow:(Object_hash.t -> bool) -> ?edge:(Object_hash.t -> unit) -> ?counted:(unit -> unit) -> read:(Object_hash.t -> (Value.t, [ `Msg of string ]) result) -> exists:(Object_hash.t -> bool) -> Object_hash.t -> Store.pack_object list(** [reachable_named ?shallow ?edge ?counted ~read ~exists root] is {!reachable_from}'s objects, in its order, each named by the path git's rev-list --objects names it by: a tree or a blob by its path from the root tree of the commit that first reaches it, the root tree by [""], and a commit or a tag by none. A blob's type is the one its tree entry's mode gives. [edge] is told, once each, the parents [exists] holds of the commits of the walk: the edge git's rev-list --objects-edge marks. [counted] and what it raises are as in {!reachable_from}. *)
val preferred_bases : ?window:int -> read:(Object_hash.t -> (Value.t, [ `Msg of string ]) result) -> edges:Object_hash.t list -> Store.pack_object list -> Store.pack_object list(** [preferred_bases ~window ~read ~edges objects] is the objects git's pack-objects --thin holds as preferred bases for a pack of [objects] (builtin/pack-objects.c, [add_preferred_base_object]): for the first of [objects] of each name hash, the object at its path in the tree of each of the first [window] (defaults to [10]) of [edges], the commits the receiver holds; none that [objects] holds, nor a gitlink, nor one under a tree that [read] cannot read. *)
val parse_url : string -> [ `File of string | `Other of string ](** [parse_url s] is [`File p] for a bare path or a [file://] URL, and [`Other s] otherwise. *)
val resolve_want : Smart.Message.Advertisement.t -> string -> Object_hash.t option(** [resolve_want adv ref_name] is the object to [want] for [ref_name] in the protocol v0 and v1 advertisement [adv]: a ref, a short branch or tag name, or a 40-character object ID. *)
val resolve_v2 : Smart.Message.V2.ls_ref list -> string -> Object_hash.t option(** [resolve_v2 refs ref_name] is like {!resolve_want} for the result of a protocol v2 [ls-refs]. A 40-character hexadecimal ID takes priority. *)
type channel = { write : string -> unit; (** Send bytes to the server and flush. *) reader : Bytesrw.Bytes.Reader.t; (** The bytes from the server. *) eof : unit -> unit; (** End the client's side of the stream: once a v0 fetch has its pack, as git's fetch keeps its side open until then (transport.c, [fetch_refs_via_pack]), or once the session asks nothing more. *)}(** The type for the persistent two-way connections of an upload-pack session.*)
module Make (S : Store.S) : sig val local : ?on_progress:(Progress.event -> unit) -> ?budget:Store.pack_budget -> src:S.t -> dst:S.t -> ref_name:string -> unit -> (Object_hash.t, [ `Msg of string ]) result (** [local ~src ~dst ~ref_name ()] copies to [dst] the objects of [ref_name] in [src] that [dst] lacks, sets [FETCH_HEAD] and the remote-tracking ref, and is the fetched commit. [on_progress] is told each object to copy as it is counted, then each one copied, as {!Push.Make.local} tells it. [budget] holds every read of [src] to its per-object limit and charges each object copied to its decoded total once, so a repository on this machine costs what one fetched over a network may; it reads no wire, so its byte allowance is not spent. *)
val local_refs : ?on_progress:(Progress.event -> unit) -> src:S.t -> dst:S.t -> select:(string -> string option) -> unit -> ((string * Object_hash.t) list, [ `Msg of string ]) result (** [local_refs ~src ~dst ~select ()] copies to [dst] the objects that [dst] lacks of each ref [r] of [src] for which [select r] is [Some l], and is each such [(l, value)], in the order of the names of [src]. It writes no ref of [dst]. [on_progress] is told each object to copy as it is counted, once however many refs reach it, then each one copied. *)
val import_pack : S.t -> string -> (int, [ `Msg of string ]) result (** [import_pack dst pack] stores the self-contained [pack] in [dst] and is its number of objects. *)
val negotiate : ?budget:Store.pack_budget -> ?on_progress:(Progress.event -> unit) -> ?depth:int -> client:Smart.Client.Fetch.t -> adv:Smart.Message.Advertisement.t -> ref_name:string -> send:(string -> (Bytesrw.Bytes.Reader.t, [ `Msg of string ]) result) -> S.t -> (Object_hash.t, [ `Msg of string ]) result (** [negotiate ?depth ~client ~adv ~ref_name ~send dst] runs a stateless multi-round upload-pack fetch over any transport, streaming the pack into [dst]. [budget] bounds bytes stored and decoded by [dst]; the transport must separately bound bytes it reads before handing over the pack.
[depth] limits the history to that many commits from each tip ([deepen]). The shallow boundary of the server is added to that of [dst] with {!Store.S.write_shallow}, and the boundary [dst] already has is sent, so a deeper fetch fills it in. Without [depth] the whole history is fetched.
[on_progress] is told the bytes of the pack received so far ({!Progress.Received}), each line the remote writes on side band 2 while it sends the pack ({!Progress.Remote}), and the objects of the pack once [dst] stored them ({!Progress.Written}). *)
val negotiate_refs : ?budget:Store.pack_budget -> ?on_progress:(Progress.event -> unit) -> client:Smart.Client.Fetch.t -> adv:Smart.Message.Advertisement.t -> select:(string -> string option) -> send:(string -> (Bytesrw.Bytes.Reader.t, [ `Msg of string ]) result) -> S.t -> ((string * Object_hash.t) list, [ `Msg of string ]) result (** [negotiate_refs ?budget ~client ~adv ~select ~send dst] is {!local_refs} over stateless upload-pack rounds: the request wants every advertised ref [r] for which [select r] is [Some _] and that [dst] lacks, and the pack is streamed into [dst] as {!negotiate} does, [on_progress] told as it tells it. It writes no ref of [dst], and is [Error] if the pack lacks a wanted object. *)
val negotiate_stateful : ?budget:Store.pack_budget -> ?on_progress:(Progress.event -> unit) -> ?depth:int -> client:Smart.Client.Fetch.t -> adv:Smart.Message.Advertisement.t -> ref_name:string -> channel:channel -> S.t -> (Object_hash.t, [ `Msg of string ]) result (** [negotiate_stateful ?depth ~client ~adv ~ref_name ~channel dst] runs a multi-round [multi_ack_detailed] fetch over a persistent connection, its haves in rounds newest first, pruning acknowledged ancestors. [budget], [depth] and [on_progress] are as in {!negotiate}. *)
val negotiate_v2 : ?budget:Store.pack_budget -> ?on_progress:(Progress.event -> unit) -> ?depth:int -> ref_name:string -> send: (buffered:bool -> string -> (Bytesrw.Bytes.Reader.t, [ `Msg of string ]) result) -> S.t -> (Object_hash.t, [ `Msg of string ]) result (** [negotiate_v2 ?depth ~ref_name ~send dst] runs a protocol-v2 fetch over any transport ([ls-refs] then [fetch]). [budget], [depth] and [on_progress] are as in {!negotiate}; the request asks the server not to send its progress ([no-progress]) unless [on_progress] is given. *)
val ls_refs_v2 : prefixes:string list -> send: (buffered:bool -> string -> (Bytesrw.Bytes.Reader.t, [ `Msg of string ]) result) -> (Smart.Message.V2.ls_ref list, [ `Msg of string ]) result (** [ls_refs_v2 ~prefixes ~send] is every ref of a protocol v2 server, read with one ls-refs command, with HEAD's symbolic target and each tag's peeled value, as [git ls-remote] lists them. Each of [prefixes] is sent as a [ref-prefix] argument (gitprotocol-v2(5), "ls-refs"): the server then lists the refs that start with one of them, and the answer costs what they name, not every ref the server holds; it is a hint, so a caller still picks the refs it reads by name. With none, [[]], every ref. *)
val negotiate_refs_v2 : ?budget:Store.pack_budget -> ?on_progress:(Progress.event -> unit) -> refs:Smart.Message.V2.ls_ref list -> select:(string -> string option) -> send: (buffered:bool -> string -> (Bytesrw.Bytes.Reader.t, [ `Msg of string ]) result) -> S.t -> ((string * Object_hash.t) list, [ `Msg of string ]) result (** [negotiate_refs_v2 ?budget ~refs ~select ~send dst] is {!negotiate_refs} over protocol v2: fetch commands, their haves in rounds newest first, pruning acknowledged ancestors, want every ref [r] of [refs] for which [select r] is [Some _] and that [dst] lacks, and is each such [(l, value)]. It writes no ref of [dst], and is [Error] if the pack lacks a wanted object. [on_progress] is as in {!negotiate_v2}. *)
(** {1:session An upload-pack session}
A session with the [git-upload-pack] at the other end of a {!channel}, which the transport asked for protocol v2 as it asks it ({{:https://git-scm.com/docs/gitprotocol-v2}gitprotocol-v2(5)}, "Initial Client Request"). The server's first answer says which protocol it speaks: a v2 greeting, after which each command is sent on the channel, which stays open for the next, and the session ends with a flush-pkt; or a v0 or v1 advertisement. An error packet as that first answer is the refusal it names; [explain m] (default [m]) is the error reported when that first answer cannot be read otherwise, [m] saying why. *)
val session_ls_refs : ?explain:(string -> string) -> ?prefixes:string list -> channel -> (Smart.Message.Advertisement.t, [ `Msg of string ]) result (** [session_ls_refs c] is the listing of the server: from one ls-refs command ({!ls_refs_v2}) in v2, asked for [prefixes], the advertisement itself in v0, after which the session ends with a flush-pkt. *)
val session_fetch_refs : ?budget:Store.pack_budget -> ?on_progress:(Progress.event -> unit) -> ?explain:(string -> string) -> ?prefixes:string list -> select:(string -> string option) -> channel -> S.t -> ((string * Object_hash.t) list, [ `Msg of string ]) result (** [session_fetch_refs ~select c dst] is {!negotiate_refs_v2} after {!ls_refs_v2} in v2 asked for [prefixes], and in v0 the refs {!negotiate_refs} selects, fetched in rounds over [c] as {!negotiate_stateful} fetches, [on_progress] told as they tell it. *)
val session_fetch : ?budget:Store.pack_budget -> ?on_progress:(Progress.event -> unit) -> ?depth:int -> ?explain:(string -> string) -> ref_name:string -> channel -> S.t -> (Object_hash.t, [ `Msg of string ]) result (** [session_fetch ~ref_name c dst] is {!negotiate_v2} in v2, and {!negotiate_stateful} over [c] in v0, [on_progress] told as they tell it. *)end