Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262(* Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org> Copyright (c) 2017-2024 Romain Calascibetta <romain.calascibetta@gmail.com> 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. *)
open! Gitopen Handleopen Object_storeopen Ref_lookupopen Stagingopen Tree_checkout
(** {1 The status of the work tree}
Each function reads the facts of [t], its index, HEAD and configuration, and leaves the decision to {!Git.Status.Make}. *)
let join_path prefix name = if prefix = "" then name else prefix ^ "/" ^ name
(* [List.filter_map f l], or the first error of [f]. *)let filter_map_ok f l = let rec go acc = function | [] -> Ok (List.rev acc) | x :: rest -> ( match f x with | Ok None -> go acc rest | Ok (Some y) -> go (y :: acc) rest | Error _ as error -> error) in go [] l
(* The index as git reads it before it looks at the working tree, each probe of an entry's stat data counted. An index that cannot be read caches nothing, and every file is read. *)let stat_cache_of t ~stamp index = let probed () = t.stats.worktree_stats <- t.stats.worktree_stats + 1 in Work_status.cache ?stamp ~probed (work_tree t) index
let stat_cache t = let stamp = index_stamp t in let index = match read_index t with Ok i -> i | Error _ -> Index.empty in stat_cache_of t ~stamp index
let lstat_many t paths = Stat.lstat_many Eio.Path.(t.fs / t.work_dir) paths
(* The tree of HEAD, its files listed only when the index does not hold it. *)let head_of t = Option.map (fun tree -> { Status.tree; files = lazy (Result.map_error (fun e -> `Msg (Fmt.str "%a" Git.Tree_walk.pp_error e)) (tree_files t tree)); }) (Option.bind (head_exn t) (commit_tree t))
let excludes t = lazy (base_ignore t)let tracked t cache ~scope = Work_status.tracked cache ~head:(head_of t) ~scope
(* git status reports a directory holding a .git that the index does not track as one untracked entry, [dir/] (dir.c, treat_directory): the walk does not descend into it, and lists it apart from the files. *)let untracked t = match read_index t with | Error _ as error -> error | Ok index -> let tracked = Hashtbl.create 1024 in List.iter (fun (e : Index.entry) -> Hashtbl.replace tracked e.name ()) (Index.entries index); let ( let* ) = Result.bind in let* ignore = base_ignore t in let* files, repos = Work_status.walk (work_tree t) ~ignore "" in let untracked paths = List.sort String.compare (List.filter (fun p -> not (Hashtbl.mem tracked p)) paths) in Ok (untracked files, untracked repos)
let is_path_dirty t ~path = Work_status.dirty (attributes t) (stat_cache t) ~excludes:(excludes t) ~head:(head_of t) ~scope:path
let is_dirty t = is_path_dirty t ~path:""
let index_at t commit = match (commit_tree t commit, read_index t) with | Some tree, Ok index -> Option.equal Hash.equal (Status.index_tree index) (Some tree) | _ -> false
let is_clean_at ?index t commit = let cache = match index with | Some { index; stamp } -> stat_cache_of t ~stamp index | None -> stat_cache t in match commit_tree t commit with | None -> Ok false | Some tree -> ( match Work_status.clean_at (attributes t) cache ~tree with | Ok _ as ok -> ok | Error (`Msg m) -> Error (`Msg m))
let parent_dir p = match String.rindex_opt p '/' with Some i -> String.sub p 0 i | None -> ""
(* The rules in force in a directory, kept for the function's life: the rules of a directory are its parent's with its own [.gitignore], so each ignore file is read once, and one that does not read fails every directory under it. *)let rules_by_dir t = let rules = Hashtbl.create 16 in let rec rules_in dir = match Hashtbl.find_opt rules dir with | Some r -> r | None -> let above = if dir = "" then base_ignore t else rules_in (parent_dir dir) in let r = Result.bind above @@ fun above -> Result.map (Gitignore.append above) (Work_status.ignore_rules (work_tree t) dir) in Hashtbl.replace rules dir r; r in rules_in
(* The verdict on each directory, kept by the predicate with the rules. *)let ignored t = let ( let* ) = Result.bind in let rules_in = rules_by_dir t and dirs = Hashtbl.create 16 in let excluded dir ~path ~is_dir = Result.map (fun rules -> Gitignore.is_ignored rules ~path ~is_dir) (rules_in dir) in (* Each ancestor directory first: Git does not look inside one it ignores. *) let rec dir_ignored d = match Hashtbl.find_opt dirs d with | Some v -> v | None -> let parent = parent_dir d in let v = let* above = if parent = "" then Ok false else dir_ignored parent in if above then Ok true else excluded parent ~path:d ~is_dir:true in Hashtbl.replace dirs d v; v in fun ~path ~is_dir -> match List.filter (( <> ) "") (String.split_on_char '/' path) with | [] -> Ok false | parts -> let p = String.concat "/" parts in let parent = parent_dir p in let* above = if parent = "" then Ok false else dir_ignored parent in if above then Ok true else excluded parent ~path:p ~is_dir
let is_ignored t ~path ~is_dir = ignored t ~path ~is_dir
type change_kind = Status.change_kind = Modified | Deleted | Untrackedtype change = Status.change = { kind : change_kind; path : string }
let changes t ?(path = "") () = Work_status.changes (attributes t) (stat_cache t) ~excludes:(excludes t) ~head:(head_of t) ~scope:path
(* Scopes arrive from the command line: shell completion leaves a trailing '/' on directories and users write "./path"; both would defeat the exact and prefix matching in [path_in_scope]. *)let normalize_scope p = let p = if String.starts_with ~prefix:"./" p then String.sub p 2 (String.length p - 2) else p in let n = String.length p in let p = if n > 0 && p.[n - 1] = '/' then String.sub p 0 (n - 1) else p in if p = "." then "" else p
let changes_under t ~paths = let seen = Hashtbl.create 16 in let rec go acc = function | [] -> Ok (List.sort (fun a b -> String.compare a.path b.path) acc) | scope :: rest -> ( match changes t ~path:(normalize_scope scope) () with | Error _ as error -> error | Ok changes -> let fresh = List.filter (fun c -> if Hashtbl.mem seen c.path then false else begin Hashtbl.replace seen c.path (); true end) changes in go (List.rev_append fresh acc) rest) in go [] paths
let file_state t index = let cache = stat_cache_of t ~stamp:(index_stamp t) index in let attributes = attributes t in fun ~path ~mode ~hash -> match Work_status.file_state attributes cache ~path ~mode ~hash with | Ok _ as ok -> ok | Error (`Msg m) -> Error (`Msg m)
let diff_files ?paths ?index t = let read = match index with Some read -> Ok read | None -> read_stamped_index t in Result.bind read @@ fun { index; stamp } -> Work_status.diff_files ?paths (attributes t) (stat_cache_of t ~stamp index)
(* The index is written only when an entry changed, with the entries read here verified. *)let refresh_index t = locked_msg @@ Index_lock.with_lock t @@ fun lock -> match read_index t with | Error (`Msg m) -> Error (`Msg m) | Ok index -> ( let cache = stat_cache_of t ~stamp:(index_stamp t) index in match Work_status.refresh (attributes t) cache with | Error (`Msg m) -> Error (`Msg m) | Ok None -> Ok () | Ok (Some (index, verified)) -> Index_lock.commit ~verified lock index)
let read_changes t ~paths = let tree = Option.bind (head_exn t) (commit_tree t) in let entry path = Option.bind tree (fun tree -> tree_entry_at_path t tree (prefix_segments path)) in match Work_status.read_changes (attributes t) (stat_cache t) ~entry (List.sort_uniq String.compare (List.map normalize_scope paths)) with | Ok _ as ok -> ok | Error (`Msg m) -> Error (`Msg m)