Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an interface on the Git module to clarify what we want to expose #63

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 3 additions & 29 deletions lib/git.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
29 changes: 29 additions & 0 deletions lib/git.mli
Original file line number Diff line number Diff line change
@@ -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
Loading