diff --git a/lib/dune b/lib/dune index 773e84b..17c20f7 100644 --- a/lib/dune +++ b/lib/dune @@ -62,7 +62,7 @@ (name git) (public_name bob.git) (modules git) - (libraries bob.fiber bob.stream cstruct digestif)) + (libraries bob.fiber bob.stream cstruct carton digestif)) (library (name pack) diff --git a/lib/git.ml b/lib/git.ml index 82d0345..e22d2c2 100644 --- a/lib/git.ml +++ b/lib/git.ml @@ -7,11 +7,7 @@ module Log = (val Logs.src_log src : Logs.LOG) module SHA1 = struct include Digestif.SHA1 - let hash x = Hashtbl.hash x let length = digest_size - let feed = feed_bigstring - let null = digest_string "" - let compare a b = String.compare (to_raw_string a) (to_raw_string b) let sink_bigstring ?(ctx = empty) () = Stream.Sink.make ~init:(Fiber.always ctx) @@ -57,6 +53,9 @@ let tree_of_string ?path str = let v_space = Cstruct.string " " let v_null = Cstruct.string "\x00" +type elt = [ `Reg of Bob_fpath.t * SHA1.t | `Dir of Bob_fpath.t * SHA1.t ] +type tree = elt list + let tree_of_cstruct ?path contents = let path_with ~name = match path with @@ -114,31 +113,6 @@ module Filesystem = struct try Sys.readdir (Bob_fpath.to_string d) with _exn -> [||] in Array.to_list <.> readdir - - let rec traverse ~get ~add visited stack ~f acc = - match stack with - | [] -> Fiber.return acc - | x :: r -> - if List.exists (Bob_fpath.equal x) visited then - traverse ~get ~add visited r ~f acc - else - let open Fiber in - let contents = get x in - traverse ~get ~add (x :: visited) (add contents stack) ~f acc >>= f x - - let fold ?(dotfiles = false) ~f acc paths = - let dir_child d acc bname = - if (not dotfiles) && bname.[0] = '.' then acc - else Bob_fpath.(d / bname) :: acc - in - let add stack vs = vs @ stack in - let get path = - let entries = readdir path in - List.fold_left (dir_child path) [] entries - in - traverse ~get ~add [] paths ~f acc - - let fold ?dotfiles ~f acc d = fold ?dotfiles ~f acc [ d ] end let serialize_directory entries = diff --git a/lib/git.mli b/lib/git.mli new file mode 100644 index 0000000..a5d9d92 --- /dev/null +++ b/lib/git.mli @@ -0,0 +1,29 @@ +type elt = + [ `Reg of Bob_fpath.t * Digestif.SHA1.t + | `Dir of Bob_fpath.t * Digestif.SHA1.t ] + +type tree = elt list + +val tree_of_string : ?path:Bob_fpath.t -> string -> tree +val tree_of_cstruct : ?path:Bob_fpath.t -> Stdbob.bigstring -> elt Stream.source + +val digest : + kind:Carton.kind -> + ?off:int -> + ?len:int -> + Stdbob.bigstring -> + Digestif.SHA1.t + +val serialize_directory : + (Bob_fpath.t * Digestif.SHA1.t) list -> string Stream.stream + +val hash_of_root : + real_length:int -> root:Bob_fpath.t -> Digestif.SHA1.t -> Digestif.SHA1.t + +val hash_of_directory : + root:Bob_fpath.t -> + (Bob_fpath.t, Digestif.SHA1.t * [ `Dir | `Reg | `Root ]) Hashtbl.t -> + Bob_fpath.t -> + Digestif.SHA1.t Fiber.t + +val hash_of_filename : Bob_fpath.t -> Digestif.SHA1.t Fiber.t