Skip to content

Commit

Permalink
Merge pull request #63 from dinosaure/improve-git
Browse files Browse the repository at this point in the history
Add an interface on the Git module to clarify what we want to expose
  • Loading branch information
dinosaure authored Feb 2, 2024
2 parents 2222b70 + b5b8869 commit 3a96113
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
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

0 comments on commit 3a96113

Please sign in to comment.