Skip to content

Instantly share code, notes, and snippets.

@metanivek
Created August 14, 2023 14:55
Show Gist options
  • Save metanivek/2e59fe6971d5ce2ab1d42fc9eaf23ca7 to your computer and use it in GitHub Desktop.
Save metanivek/2e59fe6971d5ce2ab1d42fc9eaf23ca7 to your computer and use it in GitHub Desktop.
open Lwt.Syntax
(* irmin-pack setup from [examples/irmin-pack/kv.ml] *)
let src =
Logs.Src.create "irmin-pack.unix/examples/kv"
~doc:"irmin-pack.unix/examples/kv"
module Log = (val Logs.src_log src : Logs.LOG)
(* Compile-time configurations passed to a functor. *)
module Conf = struct
let entries = 32
let stable_hash = 256
let contents_length_header = Some `Varint
let inode_child_order = `Seeded_hash
let forbid_empty_dir_persistence = true
end
(* Run-time configurations passed when instantiating a repository. *)
module Repo_config = struct
(** The number of entries to cache in the index log (an in-memory and on-disk
data store). Default is [2_500_000] *)
let index_log_size = 2_500_000
(** Choose what to do when the index log is full and a merge is in-progress.
- [Block_writes] (the default) will block writing until the merge is
complete
- [Overcommit_memory] will increase the in-memory cache indefinitely *)
let merge_throttle = `Block_writes
(** Must use minimal indexing strategy to use GC *)
let indexing_strategy = Irmin_pack.Indexing_strategy.minimal
(** Location on disk to save the repository
Note: irmin-pack will not create the entire path, only the final directory *)
let root = "./irmin-pack-git-sync"
(** See {!Irmin_pack.Conf} for more keys that can be used when initialising
the repository config *)
(** Create new repository every time. Use [false] (the defalut) to use
existing repository. *)
let fresh = true
(** Create config for our repository *)
let config =
Irmin_pack.config ~fresh ~index_log_size ~merge_throttle ~indexing_strategy
root
end
module StoreMaker = Irmin_pack_unix.KV (Conf)
module Store = StoreMaker.Make (Irmin.Contents.String)
(* Modified [examples/sync.ml] code to attempt syncing to a local irmin-pack store *)
(* let info = Irmin_git_unix.info *)
let path =
if Array.length Sys.argv = 2 then Sys.argv.(1)
else "git://github.com/mirage/ocaml-git.git"
module Git_store = Irmin_git_unix.FS.KV (Irmin.Contents.String)
module Sync = Irmin.Sync.Make (Store)
let test () =
(* Config.init (); *)
(* let config = Irmin_git.config Config.root in *)
let* repo = Store.Repo.v Repo_config.config in
let* t = Store.of_branch repo "master" in
let* upstream = Git_store.remote path in
let* _ = Sync.pull_exn t upstream `Set in
let* readme = Store.get t [ "README.md" ] in
let* tree = Store.get_tree t [] in
let* tree = Store.Tree.add tree [ "BAR.md" ] "Hoho!" in
let* tree = Store.Tree.add tree [ "FOO.md" ] "Hihi!" in
let+ () = Store.set_tree_exn t ~info:(fun () -> Store.Info.empty) [] tree in
Printf.printf "%s\n%!" readme
let () = Lwt_main.run (test ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment