(* Copyright (c) 2024-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. *) (** Tree and commit diffs for Eio-backed Git repositories. *) open! Git (** Tree and commit diffs. A diff is the list of changes between two trees or commits. *) (** {1:types Types} *) (** The type for changes. *) type change = Git.Diff.change = | Added of { path : string; hash : Hash.t; perm : Tree.perm } (** A file or directory added. *) | Removed of { path : string; hash : Hash.t; perm : Tree.perm } (** A file or directory removed. *) | Modified of { path : string; old_hash : Hash.t; new_hash : Hash.t; old_perm : Tree.perm; new_perm : Tree.perm; } (** A file whose content, and possibly permissions, changed. *) type t = change list (** The type for diffs. *) (** {1:ops Diffs} *) val trees : Repository.t -> old_tree:Hash.t -> new_tree:Hash.t -> (t, [ `Msg of string ]) result (** [trees repo ~old_tree ~new_tree] is the changes that turn [old_tree] into [new_tree], in lexicographic order of path. *) val commits : Repository.t -> old_commit:Hash.t -> new_commit:Hash.t -> (t, [ `Msg of string ]) result (** [commits repo ~old_commit ~new_commit] is {!trees} on the trees of [old_commit] and [new_commit]. *) val nearer : Repository.t -> old_tree:Hash.t -> new_tree:Hash.t -> (bool, [ `Msg of string ]) result (** [nearer repo ~old_tree ~new_tree] is whether {!trees} on [old_tree] and [new_tree] has fewer changes than [new_tree] has files: whether moving a work tree from [old_tree] to [new_tree] by their diff writes fewer paths than checking [new_tree] out afresh. It reads the subtrees that differ, and of the others only as many as the answer needs. *) val empty_of_tree : Repository.t -> Hash.t -> (t, [ `Msg of string ]) result (** [empty_of_tree repo tree] is every entry of [tree] as an {!constructor-Added} change. *) val tree_of_empty : Repository.t -> Hash.t -> (t, [ `Msg of string ]) result (** [tree_of_empty repo tree] is every entry of [tree] as a {!constructor-Removed} change. *) val of_commit : Repository.t -> Hash.t -> (t, [ `Msg of string ]) result (** [of_commit repo commit] is {!Git.Diff.Make.of_commit}: the changes [commit] makes to its first parent, or every entry of its tree for a root commit. *) (** {1:filtering Filtering} *) val filter_by_path : prefix:string -> t -> t (** [filter_by_path ~prefix diff] is the changes of [diff] under [prefix]. *) val blobs : t -> (string * Hash.t) list (** [blobs diff] is {!Git.Diff.blobs}: the path and blob of each file or symbolic link a change of [diff] leaves. *) (** {1:stats Statistics} *) type stats = Git.Diff.stats = { additions : int; (** The number of paths added. *) deletions : int; (** The number of paths removed. *) modifications : int; (** The number of paths modified. *) } (** The type for diff statistics. *) val stats : t -> stats (** [stats diff] is the statistics of [diff]. *) (** {1:numstat Line counts} *) type lines = Git.Diff.lines = { added : int; deleted : int } (** The type for the lines a change adds and deletes. *) type numstat = Git.Diff.numstat = { path : string; (** The path the change leaves, or the path it removed. *) source : string option; (** The path a rename moved the file from. *) hash : Hash.t; (** The object at {!field-path} after the change, or the one removed. *) lines : lines option; (** The lines changed, [None] for a binary file. *) } (** The type for the line counts of one file, a line of [git diff --numstat]. *) val numstat : ?renames:bool -> ?minimum:int -> ?limit:int -> Repository.t -> Hash.t -> (numstat list, [ `Msg of string ]) result (** [numstat ~renames ~minimum ~limit repo commit] is the line counts of the changes [commit] makes to its first parent, or to the empty tree for a root commit, as [git diff-tree -r -M --numstat --root] writes them: renames found at [minimum] (defaults to git's 50 per cent) unless [renames] is [false], as {!Git.Diff.numstat} counts them. It is [Error] when [commit] or an object it reaches cannot be read. *) val pp_numstat : numstat Fmt.t (** [pp_numstat] is {!Git.Diff.pp_numstat}. *) (** {1:fmt Formatting} *) val pp_change : change Fmt.t (** [pp_change] formats a single change. *) val pp : t Fmt.t (** [pp] formats a diff as a list of changes. *) val pp_stats : stats Fmt.t (** [pp_stats] formats diff statistics. *)