From ea305b7ad6d74ad711c4c7397c2a3bc69f6c8698 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Wed, 21 Sep 2022 14:43:41 +0200 Subject: [PATCH] Add the diff impl. and use it into the pull function --- src/git_kv.ml | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/git_kv.ml b/src/git_kv.ml index 8f4c7b5..8544d0f 100644 --- a/src/git_kv.ml +++ b/src/git_kv.ml @@ -36,6 +36,37 @@ let split_url s = | _ -> Smart_git.Endpoint.of_string s |> to_invalid, main +let fpath_to_key ~root v = + Mirage_kv.Key.v (Fpath.to_string (Option.get (Fpath.relativize ~root v))) + +let diff store commit0 commit1 = + let open Lwt.Infix in + let root = Fpath.v "." in + let tbl0 = Hashtbl.create 0x10 in + Store.fold store (fun () ?name ~length:_ hash _value -> + Option.iter (fun name -> Hashtbl.add tbl0 (fpath_to_key ~root name) hash) name ; + Lwt.return ()) () ~path:root commit0 >>= fun () -> + let tbl1 = Hashtbl.create 0x10 in + Store.fold store (fun () ?name ~length:_ hash _value -> + Option.iter (fun name -> Hashtbl.add tbl1 (fpath_to_key ~root name) hash) name ; + Lwt.return ()) () ~path:root commit1 >>= fun () -> + let diff = Hashtbl.fold (fun name hash diff -> match Hashtbl.find_opt tbl1 name with + | Some hash' when not (Digestif.SHA1.equal hash hash') -> `Change name :: diff + | Some _ -> diff + | None -> `Remove name :: diff) tbl0 [] in + let diff = Hashtbl.fold (fun name _hash diff -> + if Hashtbl.mem tbl0 name + then `Add name :: diff else diff) tbl1 diff in + Lwt.return diff + +let diff store commit0 commit1 = match commit0 with + | Some commit0 -> diff store commit0 commit1 + | None -> + let root = Fpath.v "." in + Store.fold store (fun diff ?name ~length:_ _hash _value -> match name with + | None -> Lwt.return diff + | Some name -> Lwt.return (`Add (fpath_to_key ~root name) :: diff)) [] ~path:root commit1 + let pull t = let open Lwt.Infix in Sync.fetch ~capabilities ~ctx:t.ctx t.edn t.store ~deepen:(`Depth 1) `All >>= fun r -> @@ -56,8 +87,9 @@ let pull t = Store.pp_error e)) r |> to_invalid in + diff t.store t.head head >>= fun diff -> t.head <- Some head; - Lwt.return (Ok []) + Lwt.return (Ok diff) let connect ctx endpoint = let open Lwt.Infix in @@ -77,8 +109,6 @@ type change = [ | `Change of key ] -let pull _ = assert false - type error = Mirage_kv.error let pp_error ppf = Mirage_kv.pp_error ppf