forked from
gazagnaire.org/irmin
Persistent store with Git semantics: lazy reads, delayed writes, content-addressing
1(** Import command - import data from external formats. *)
2
3let import_car ~config ~fs data file =
4 let header, blocks = Atp.Car.of_string ~cid_format:`Atproto data in
5 let irmin_dir = Filename.concat config.Config.store_path ".irmin" in
6 (try Unix.mkdir irmin_dir 0o755 with Unix.Unix_error _ -> ());
7 let store_path = Eio.Path.(fs / irmin_dir / "blocks") in
8 let blockstore = Atp.Blockstore.filesystem store_path in
9 let count = ref 0 in
10 List.iter
11 (fun (cid, block_data) ->
12 blockstore#put cid block_data;
13 incr count)
14 blocks;
15 blockstore#sync;
16 Common.success "Imported %d blocks from %a" !count Common.styled_cyan file;
17 ignore header;
18 0
19
20let run ~repo ~branch file =
21 let config = Config.load ~repo () in
22 Eio_main.run @@ fun env ->
23 let fs = Eio.Stdenv.cwd env in
24 Eio.Switch.run @@ fun sw ->
25 let file_path = Eio.Path.(fs / file) in
26 let data = Eio.Path.load file_path in
27 let is_car = Filename.check_suffix file ".car" in
28 if is_car then import_car ~config ~fs data file
29 else begin
30 let (module B : Common.BACKEND) = Common.backend_of_config config in
31 let store = B.open_store ~sw ~fs ~config in
32 let tree =
33 match B.checkout store ~branch with
34 | None -> B.empty_tree store
35 | Some t -> t
36 in
37 let path = Common.path_of_string file in
38 let tree = B.tree_add tree path data in
39 let parents =
40 match B.head store ~branch with None -> [] | Some h -> [ h ]
41 in
42 let message = "Import " ^ file in
43 let hash =
44 B.commit store ~tree ~parents ~message ~author:"irmin <irmin@local>"
45 in
46 B.set_head store ~branch hash;
47 Common.success "Imported %a" Common.styled_cyan file;
48 0
49 end