/
irmin_git.ml
1163 lines (930 loc) · 32.7 KB
/
irmin_git.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
open Lwt.Infix
module Metadata = struct
module X = struct
type t = [ `Normal | `Exec | `Link | `Everybody ]
let t =
Irmin.Type.enum "metadata"
[
("normal", `Normal);
("exec", `Exec);
("link", `Link);
("everybody", `Everybody);
]
end
include X
let default = `Normal
let merge = Irmin.Merge.default X.t
end
let src = Logs.Src.create "irmin.git" ~doc:"Irmin Git-format store"
module Log = (val Logs.src_log src : Logs.LOG)
module Conf = struct
let root = Irmin.Private.Conf.root
let reference =
let parse str = Ok (Git.Reference.of_string str) in
let print ppf name = Fmt.string ppf (Git.Reference.to_string name) in
(parse, print)
let head =
Irmin.Private.Conf.key ~doc:"The main branch of the Git repository." "head"
Irmin.Private.Conf.(some reference)
None
let bare =
Irmin.Private.Conf.key ~doc:"Do not expand the filesystem on the disk."
"bare" Irmin.Private.Conf.bool false
let level =
Irmin.Private.Conf.key ~doc:"The Zlib compression level." "level"
Irmin.Private.Conf.(some int)
None
let buffers =
Irmin.Private.Conf.key ~doc:"The number of 4K pre-allocated buffers."
"buffers"
Irmin.Private.Conf.(some int)
None
let dot_git =
Irmin.Private.Conf.key
~doc:
"The location of the .git directory. By default set to [$root/.git]."
"dot-git"
Irmin.Private.Conf.(some string)
None
end
let config ?(config = Irmin.Private.Conf.empty) ?head ?bare ?level ?dot_git
root =
let module C = Irmin.Private.Conf in
let config = C.add config Conf.root (Some root) in
let config =
match bare with
| None -> C.add config Conf.bare (C.default Conf.bare)
| Some b -> C.add config Conf.bare b
in
let config = C.add config Conf.head head in
let config = C.add config Conf.level level in
let config = C.add config Conf.dot_git dot_git in
config
module Make_private (G : Git.S) (C : Irmin.Contents.S) (P : Irmin.Path.S) =
struct
module H = Irmin.Hash.Make (G.Hash)
module type V = sig
type t
val type_eq : [ `Commit | `Blob | `Tree | `Tag ] -> bool
val to_git : t -> G.Value.t
val of_git : G.Value.t -> t option
end
module Content_addressable (V : V) = struct
type 'a t = G.t
type key = H.t
type value = V.t
let pp_key = Irmin.Type.pp H.t
let mem t key =
Log.debug (fun l -> l "mem %a" pp_key key);
G.mem t key >>= function
| false -> Lwt.return_false
| true -> (
G.read t key >>= function
| Error `Not_found -> Lwt.return_false
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok v -> Lwt.return (V.type_eq (G.Value.kind v)) )
let find t key =
Log.debug (fun l -> l "find %a" pp_key key);
G.read t key >>= function
| Error `Not_found -> Lwt.return_none
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok v -> Lwt.return (V.of_git v)
let add t v =
let v = V.to_git v in
G.write t v >>= function
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok (k, _) ->
Log.debug (fun l -> l "add %a" pp_key k);
Lwt.return k
let unsafe_add t k v =
add t v >|= fun k' ->
if Irmin.Type.equal H.t k k' then ()
else
Fmt.failwith
"[Git.unsafe_append] %a is not a valid key. Expecting %a instead.\n"
pp_key k pp_key k'
end
module Raw = Git.Value.Raw (G.Hash) (G.Inflate) (G.Deflate)
module XContents = struct
module GitContents = struct
type t = C.t
let type_eq = function `Blob -> true | _ -> false
let of_git = function
| G.Value.Blob b -> (
let str = G.Value.Blob.to_string b in
match Irmin.Type.of_string C.t str with
| Ok x -> Some x
| Error (`Msg e) -> Fmt.invalid_arg "error %s" e )
| _ -> None
let to_git b =
let str = Irmin.Type.to_string C.t b in
G.Value.blob (G.Value.Blob.of_string str)
end
include Content_addressable (GitContents)
module Val = struct
include C
let to_bin t =
let raw, etmp = (Cstruct.create 0x100, Cstruct.create 0x100) in
match Raw.to_raw ~raw ~etmp (GitContents.to_git t) with
| Error _ -> assert false
| Ok s -> s
let encode_bin ?headers:_ (t : t) k = k (to_bin t)
let decode_bin ?headers:_ buf off =
Log.debug (fun l -> l "Content.decode_bin");
let buf = Cstruct.of_string buf in
let buf = Cstruct.shift buf off in
match Raw.of_raw_with_header buf with
| Ok g -> (
match GitContents.of_git g with
| Some g -> (off + Cstruct.len buf, g)
| None -> failwith "wrong object kind" )
| Error e -> Fmt.invalid_arg "error %a" Raw.DecoderRaw.pp_error e
let size_of ?headers:_ _ = None
let t = Irmin.Type.like ~bin:(encode_bin, decode_bin, size_of) t
end
module Key = H
end
module Contents = Irmin.Contents.Store (XContents)
module XNode = struct
module Key = H
module Path = P
module Val = struct
module Metadata = Metadata
type t = G.Value.Tree.t
type metadata = Metadata.t
type hash = Key.t
type step = Path.step
type value = [ `Node of hash | `Contents of hash * metadata ]
let metadata_t = Metadata.t
let hash_t = Key.t
let step_t = Path.step_t
let default = Metadata.default
let value_t =
let open Irmin.Type in
variant "Tree.value" (fun node contents ->
function `Node n -> node n | `Contents c -> contents c)
|~ case1 "node" hash_t (fun n -> `Node n)
|~ case1 "contents" (pair hash_t metadata_t) (fun c -> `Contents c)
|> sealv
let of_step = Irmin.Type.to_string P.step_t
let to_step str =
match Irmin.Type.of_string P.step_t str with
| Ok x -> x
| Error (`Msg e) -> failwith e
let list t =
List.fold_left
(fun acc { G.Value.Tree.perm; name; node } ->
let name = to_step name in
match perm with
| `Dir -> (name, `Node node) :: acc
| `Commit -> acc (* FIXME *)
| #Metadata.t as p -> (name, `Contents (node, p)) :: acc)
[] (G.Value.Tree.to_list t)
|> List.rev
let find t s =
let s = of_step s in
let rec aux = function
| [] -> None
| x :: xs when x.G.Value.Tree.name <> s -> aux xs
| { G.Value.Tree.perm; node; _ } :: _ -> (
match perm with
| `Dir -> Some (`Node node)
| `Commit -> None (* FIXME *)
| #Metadata.t as p -> Some (`Contents (node, p)) )
in
aux (G.Value.Tree.to_list t)
let remove t step = G.Value.Tree.remove ~name:(of_step step) t
let is_empty = G.Value.Tree.is_empty
let add t name value =
let name = of_step name in
let entry =
match value with
| `Node node -> G.Value.Tree.entry name `Dir node
| `Contents (node, perm) ->
G.Value.Tree.entry name (perm :> G.Value.Tree.perm) node
in
(* FIXME(samoht): issue in G.Value.Tree.add *)
let entries = G.Value.Tree.to_list t in
match List.find (fun e -> e.G.Value.Tree.name = name) entries with
| exception Not_found -> G.Value.Tree.of_list (entry :: entries)
| e ->
let equal x y =
x.G.Value.Tree.perm = y.G.Value.Tree.perm
&& x.name = y.name && G.Hash.equal x.node y.node
in
if equal e entry then t
else
let entries =
List.filter (fun e -> e.G.Value.Tree.name <> name) entries
in
G.Value.Tree.of_list (entry :: entries)
let empty = G.Value.Tree.of_list []
let to_git perm (name, node) =
G.Value.Tree.entry (of_step name) perm node
let v alist =
let alist =
List.rev_map
(fun (l, x) ->
let v k = (l, k) in
match x with
| `Node n -> to_git `Dir (v n)
| `Contents (c, perm) -> to_git (perm :> G.Value.Tree.perm) (v c))
alist
in
(* Tree.of_list will sort the list in the right order *)
G.Value.Tree.of_list alist
let alist t =
let mk_n k = `Node k in
let mk_c k metadata = `Contents (k, metadata) in
List.fold_left
(fun acc -> function
| { G.Value.Tree.perm = `Dir; name; node } ->
(to_step name, mk_n node) :: acc
| { G.Value.Tree.perm = `Commit; name; _ } ->
(* Irmin does not support Git submodules; do not follow them,
just consider *)
Log.warn (fun l -> l "skipping Git submodule: %s" name);
acc
| { G.Value.Tree.perm = #Metadata.t as perm; name; node; _ } ->
(to_step name, mk_c node perm) :: acc)
[] (G.Value.Tree.to_list t)
|> List.rev
module N = Irmin.Private.Node.Make (H) (P) (Metadata)
let to_n t = N.v (alist t)
let of_n n = v (N.list n)
let to_bin t =
let raw, etmp = (Cstruct.create 0x100, Cstruct.create 0x100) in
match Raw.to_raw ~raw ~etmp (G.Value.tree t) with
| Error _ -> assert false
| Ok s -> s
let encode_bin ?headers:_ (t : t) k =
Log.debug (fun l -> l "Tree.encode_bin");
k (to_bin t)
let decode_bin ?headers:_ buf off =
Log.debug (fun l -> l "Tree.decode_bin");
let buf = Cstruct.of_string buf in
let buf = Cstruct.shift buf off in
match Raw.of_raw_with_header buf with
| Ok (G.Value.Tree t) -> (off + Cstruct.len buf, t)
| Ok _ -> failwith "wrong object kind"
| Error e -> Fmt.invalid_arg "error %a" Raw.DecoderRaw.pp_error e
let size_of ?headers:_ _ = None
let t =
Irmin.Type.map ~bin:(encode_bin, decode_bin, size_of) N.t of_n to_n
end
include Content_addressable (struct
type t = Val.t
let type_eq = function `Tree -> true | _ -> false
let to_git t = G.Value.tree t
let of_git = function G.Value.Tree t -> Some t | _ -> None
end)
end
module Node = Irmin.Private.Node.Store (Contents) (P) (Metadata) (XNode)
module XCommit = struct
module Val = struct
type t = G.Value.Commit.t
type hash = H.t
let hash_t = H.t
let info_of_git author message =
let id = author.Git.User.name in
let date, _ = author.Git.User.date in
(* FIXME: tz offset is ignored *)
Irmin.Info.v ~date ~author:id message
let name_email name =
let name = String.trim name in
try
let i = String.rindex name ' ' in
let email = String.sub name (i + 1) (String.length name - i - 1) in
if
String.length email > 0
&& email.[0] = '<'
&& email.[String.length email - 1] = '>'
then
let email = String.sub email 1 (String.length email - 2) in
let name = String.trim (String.sub name 0 i) in
(name, email)
else (name, "irmin@openmirage.org")
with Not_found -> (name, "irmin@openmirage.org")
let of_git g =
let node = G.Value.Commit.tree g in
let parents = G.Value.Commit.parents g in
let author = G.Value.Commit.author g in
let message = G.Value.Commit.message g in
let message =
if String.length message > 0 && message.[0] = '\n' then
String.sub message 1 (String.length message - 1)
else message
in
let info = info_of_git author message in
(info, node, parents)
let to_git info node parents =
let tree = node in
let parents = List.fast_sort G.Hash.compare parents in
let author =
let date = Irmin.Info.date info in
let name, email = name_email (Irmin.Info.author info) in
Git.User.{ name; email; date = (date, None) }
in
let message = Irmin.Info.message info in
let message = if message = "" then "" else "\n" ^ message in
G.Value.Commit.make (* FIXME: should be v *) ~tree ~parents ~author
~committer:author message
let v ~info ~node ~parents = to_git info node parents
let xnode g = G.Value.Commit.tree g
let node t = xnode t
let parents g = G.Value.Commit.parents g
let info g =
let author = G.Value.Commit.author g in
let message = G.Value.Commit.message g in
info_of_git author message
module C = Irmin.Private.Commit.Make (H)
let of_c c = to_git (C.info c) (C.node c) (C.parents c)
let to_c t =
let info, node, parents = of_git t in
C.v ~info ~node ~parents
let to_bin t =
let raw, etmp = (Cstruct.create 0x100, Cstruct.create 0x100) in
match Raw.to_raw ~raw ~etmp (G.Value.commit t) with
| Error _ -> assert false
| Ok s -> s
let encode_bin ?headers:_ (t : t) k =
Log.debug (fun l -> l "Commit.encode_bin");
k (to_bin t)
let decode_bin ?headers:_ buf off =
Log.debug (fun l -> l "Commit.decode_bin");
let buf = Cstruct.of_string buf in
let buf = Cstruct.shift buf off in
match Raw.of_raw_with_header buf with
| Ok (G.Value.Commit t) -> (off + Cstruct.len buf, t)
| Ok _ -> failwith "wrong object kind"
| Error e -> Fmt.invalid_arg "error %a" Raw.DecoderRaw.pp_error e
let size_of ?headers:_ _ = None
let t =
Irmin.Type.map ~bin:(encode_bin, decode_bin, size_of) C.t of_c to_c
end
module Key = H
include Content_addressable (struct
type t = Val.t
let type_eq = function `Commit -> true | _ -> false
let of_git = function G.Value.Commit c -> Some c | _ -> None
let to_git c = G.Value.commit c
end)
end
module Commit = Irmin.Private.Commit.Store (Node) (XCommit)
end
module type BRANCH = sig
include Irmin.Branch.S
val pp_ref : t Fmt.t
val of_ref : string -> (t, [ `Msg of string ]) result
end
module Branch (B : Irmin.Branch.S) : BRANCH with type t = B.t = struct
open Astring
include B
let pp = Irmin.Type.pp B.t
let pp_ref ppf b = Fmt.pf ppf "refs/heads/%a" pp b
let of_ref str =
match String.cuts ~sep:"/" str with
| "refs" :: "heads" :: b ->
Irmin.Type.of_string B.t (String.concat ~sep:"/" b)
| _ -> Error (`Msg (Fmt.strf "%s is not a valid branch" str))
end
module Irmin_branch_store (G : Git.S) (B : BRANCH) = struct
module Key = B
module Val = Irmin.Hash.Make (G.Hash)
module W = Irmin.Private.Watch.Make (Key) (Val)
type t = {
bare : bool;
dot_git : Fpath.t;
git_head : G.Reference.head_contents;
t : G.t;
w : W.t;
m : Lwt_mutex.t;
}
let watches = Hashtbl.create 10
type key = Key.t
type value = Val.t
type watch = W.watch * (unit -> unit Lwt.t)
let branch_of_git r =
let str = String.trim @@ G.Reference.to_string r in
match B.of_ref str with Ok r -> Some r | Error (`Msg _) -> None
let git_of_branch r = G.Reference.of_string (Fmt.to_to_string B.pp_ref r)
let pp_key = Irmin.Type.pp Key.t
let mem { t; _ } r =
Log.debug (fun l -> l "mem %a" pp_key r);
G.Ref.mem t (git_of_branch r)
let find { t; _ } r =
Log.debug (fun l -> l "find %a" pp_key r);
G.Ref.resolve t (git_of_branch r) >>= function
| Error `Not_found -> Lwt.return_none
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok k -> Lwt.return_some k
let listen_dir t =
let ( / ) = Filename.concat in
if G.has_global_watches then
let dir = Fpath.(to_string @@ (t.dot_git / "refs")) in
let key file =
match B.of_ref ("refs" / file) with
| Ok x -> Some x
| Error (`Msg e) ->
Log.err (fun l -> l "listen: file %s: %s" file e);
None
in
W.listen_dir t.w dir ~key ~value:(find t)
else Lwt.return (fun () -> Lwt.return_unit)
let watch_key t key ?init f =
Log.debug (fun l -> l "watch_key %a" pp_key key);
listen_dir t >>= fun stop ->
W.watch_key t.w key ?init f >|= fun w -> (w, stop)
let watch t ?init f =
Log.debug (fun l -> l "watch");
listen_dir t >>= fun stop ->
W.watch t.w ?init f >|= fun w -> (w, stop)
let unwatch t (w, stop) = stop () >>= fun () -> W.unwatch t.w w
let v ?lock ~head ~bare t =
let m = match lock with None -> Lwt_mutex.create () | Some l -> l in
let dot_git = G.dotgit t in
let write_head head =
let head = G.Reference.Ref head in
(( if G.has_global_checkout then
Lwt_mutex.with_lock m (fun () -> G.Ref.write t G.Reference.head head)
else Lwt.return_ok () )
>|= function
| Error e -> Log.err (fun l -> l "Cannot create HEAD: %a" G.pp_error e)
| Ok () -> ())
>|= fun () -> head
in
( match head with
| Some h -> write_head h
| None -> (
G.Ref.read t G.Reference.head >>= function
| Error `Not_found -> write_head (git_of_branch B.master)
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok r -> Lwt.return r ) )
>|= fun git_head ->
let w =
try Hashtbl.find watches (G.dotgit t)
with Not_found ->
let w = W.v () in
Hashtbl.add watches (G.dotgit t) w;
w
in
{ git_head; bare; t; w; dot_git; m }
let list { t; _ } =
Log.debug (fun l -> l "list");
G.Ref.list t >|= fun refs ->
List.fold_left
(fun acc (r, _) ->
match branch_of_git r with None -> acc | Some r -> r :: acc)
[] refs
let write_index t gr gk =
Log.debug (fun l -> l "write_index");
if G.has_global_checkout then Log.debug (fun f -> f "write_index");
let git_head = G.Reference.Ref gr in
Log.debug (fun f ->
f "write_index/if bare=%b head=%a" t.bare G.Reference.pp gr);
if (not t.bare) && git_head = t.git_head then (
Log.debug (fun f -> f "write cache (%a)" G.Reference.pp gr);
(* FIXME G.write_index t.t gk *)
let _ = gk in
Lwt.return_unit )
else Lwt.return_unit
let pp_branch = Irmin.Type.pp B.t
let set t r k =
Log.debug (fun f -> f "set %a" pp_branch r);
let gr = git_of_branch r in
Lwt_mutex.with_lock t.m (fun () -> G.Ref.write t.t gr (G.Reference.Hash k))
>>= function
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok () -> W.notify t.w r (Some k) >>= fun () -> write_index t gr k
let remove t r =
Log.debug (fun f -> f "remove %a" pp_branch r);
Lwt_mutex.with_lock t.m (fun () -> G.Ref.remove t.t (git_of_branch r))
>>= function
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok () -> W.notify t.w r None
let eq_head_contents_opt x y =
match (x, y) with
| None, None -> true
| Some x, Some y -> G.Reference.equal_head_contents x y
| _ -> false
let test_and_set t r ~test ~set =
Log.debug (fun f -> f "test_and_set %a" pp_branch r);
let gr = git_of_branch r in
let c = function None -> None | Some h -> Some (G.Reference.Hash h) in
let ok = function
| Ok () -> Lwt.return_true
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
in
Lwt_mutex.with_lock t.m (fun () ->
(G.Ref.read t.t gr >>= function
| Error `Not_found -> Lwt.return_none
| Ok x -> Lwt.return_some x
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e)
>>= fun x ->
( if not (eq_head_contents_opt x (c test)) then Lwt.return_false
else
match c set with
| None -> G.Ref.remove t.t gr >>= ok
| Some h -> G.Ref.write t.t gr h >>= ok )
>>= fun b ->
(if b then W.notify t.w r set else Lwt.return_unit) >>= fun () ->
( if
(* We do not protect [write_index] because it can take a long
time and we don't want to hold the lock for too long. Would
be safer to grab a lock, although the expanded filesystem
is not critical for Irmin consistency (it's only a
convenience for the user). *)
b
then
match set with
| None -> Lwt.return_unit
| Some v -> write_index t gr v
else Lwt.return_unit )
>|= fun () -> b)
end
module Irmin_sync_store
(G : Git.S)
(S : Git.Sync.S with module Store := G)
(B : Irmin.Branch.S) =
struct
module H = Irmin.Hash.Make (G.Hash)
type t = G.t
type commit = H.t
type branch = B.t
type endpoint = S.Endpoint.t
let git_of_branch_str str = G.Reference.of_string ("refs/heads/" ^ str)
let git_of_branch r = git_of_branch_str (Irmin.Type.to_string B.t r)
let o_head_of_git = function None -> Ok None | Some k -> Ok (Some k)
let fetch t ?depth e br =
let uri = S.Endpoint.uri e in
Log.debug (fun f -> f "fetch %a" Uri.pp_hum uri);
let _deepen = depth in
(* FIXME: need to be exposed in the Git API *)
let reference = git_of_branch br in
let result refs =
let key =
try Some (G.Reference.Map.find reference refs) with Not_found -> None
in
o_head_of_git key
in
let references =
(* remote *)
( reference,
(* local *)
[
G.Reference.of_string
("refs/remotes/origin/" ^ Irmin.Type.to_string B.t br);
reference;
] )
in
S.fetch_one t e ~reference:references >|= function
| Error e -> Fmt.kstrf (fun e -> Error (`Msg e)) "%a" S.pp_error e
| Ok res -> (
Log.debug (fun f -> f "fetch result: %a" S.pp_fetch_one res);
match res with `Sync refs -> result refs | `AlreadySync -> Ok None )
let push t ?depth:_ e br =
let uri = S.Endpoint.uri e in
Log.debug (fun f -> f "push %a" Uri.pp_hum uri);
let reference = git_of_branch br in
let result refs =
let errors = ref [] in
List.iter
(function
| Ok _ -> ()
| Error (r, e) ->
errors := Fmt.strf "%a: %s" G.Reference.pp r e :: !errors)
refs;
if !errors = [] then Ok ()
else
Fmt.kstrf
(fun e -> Error (`Msg e))
"%a"
Fmt.(list ~sep:(unit "@.") string)
!errors
in
let references =
G.Reference.Map.singleton (* local *) reference
(* remote *) [ reference ]
in
S.update_and_create t ~references e >|= function
| Error e -> Fmt.kstrf (fun e -> Error (`Msg e)) "%a" S.pp_error e
| Ok r ->
Log.debug (fun f -> f "push result: %a" S.pp_update_and_create r);
result r
end
type reference =
[ `Branch of string | `Remote of string | `Tag of string | `Other of string ]
module Reference : BRANCH with type t = reference = struct
open Astring
type t =
[ `Branch of string
| `Remote of string
| `Tag of string
| `Other of string ]
let pp_ref ppf = function
| `Branch b -> Fmt.pf ppf "refs/heads/%s" b
| `Remote r -> Fmt.pf ppf "refs/remotes/%s" r
| `Tag t -> Fmt.pf ppf "refs/tags/%s" t
| `Other o -> Fmt.pf ppf "refs/%s" o
let path l = String.concat ~sep:"/" l
let of_ref str =
match String.cuts ~sep:"/" str with
| "refs" :: "heads" :: b -> Ok (`Branch (path b))
| "refs" :: "remotes" :: r -> Ok (`Remote (path r))
| "refs" :: "tags" :: t -> Ok (`Tag (path t))
| "refs" :: o -> Ok (`Other (path o))
| _ -> Error (`Msg (Fmt.strf "%s is not a valid reference" str))
let t =
let open Irmin.Type in
variant "reference" (fun branch remote tag other ->
function
| `Branch x -> branch x
| `Remote x -> remote x
| `Tag x -> tag x
| `Other x -> other x)
|~ case1 "branch" string (fun t -> `Branch t)
|~ case1 "remote" string (fun t -> `Remote t)
|~ case1 "tag" string (fun t -> `Tag t)
|~ case1 "other" string (fun t -> `Other t)
|> sealv
let t = Irmin.Type.like t ~cli:(pp_ref, of_ref)
let master = `Branch Irmin.Branch.String.master
let is_valid = function
| `Branch s | `Tag s | `Remote s | `Other s ->
Irmin.Branch.String.is_valid s
end
module type S = sig
module Git : Git.S
include Irmin.S with type metadata = Metadata.t and type hash = Git.Hash.t
val git_commit : Repo.t -> commit -> Git.Value.Commit.t option Lwt.t
val git_of_repo : Repo.t -> Git.t
val repo_of_git :
?head:Git.Reference.t ->
?bare:bool ->
?lock:Lwt_mutex.t ->
Git.t ->
Repo.t Lwt.t
end
module type G = sig
include Git.S
val v :
?dotgit:Fpath.t ->
?compression:int ->
?buffers:buffer Lwt_pool.t ->
Fpath.t ->
(t, error) result Lwt.t
end
module Make_ext
(G : G)
(S : Git.Sync.S with module Store := G)
(C : Irmin.Contents.S)
(P : Irmin.Path.S)
(B : BRANCH) =
struct
module R = Irmin_branch_store (G) (B)
type r = { config : Irmin.config; g : G.t; b : R.t }
module P = struct
module Hash = Irmin.Hash.Make (G.Hash)
module XSync = struct
include Irmin_sync_store (G) (S) (R.Key)
let v repo = Lwt.return repo.g
end
include Make_private (G) (C) (P)
module Branch = R
module Slice = Irmin.Private.Slice.Make (Contents) (Node) (Commit)
module Sync = XSync
module Repo = struct
type t = r
let branch_t t = t.b
let contents_t t = t.g
let node_t t = (contents_t t, t.g)
let commit_t t = (node_t t, t.g)
let batch t f = f (contents_t t) (node_t t) (commit_t t)
type config = {
root : string;
dot_git : string option;
level : int option;
buffers : int option;
head : G.Reference.t option;
bare : bool;
}
let config c =
let root =
match Irmin.Private.Conf.get c Conf.root with
| None -> "."
| Some d -> d
in
let dot_git = Irmin.Private.Conf.get c Conf.dot_git in
let level = Irmin.Private.Conf.get c Conf.level in
let head = Irmin.Private.Conf.get c Conf.head in
let bare = Irmin.Private.Conf.get c Conf.bare in
let buffers = Irmin.Private.Conf.get c Conf.buffers in
{ root; dot_git; level; head; buffers; bare }
let fopt f = function None -> None | Some x -> Some (f x)
let v conf =
let { root; dot_git; level; head; bare; buffers } = config conf in
let dotgit = fopt Fpath.v dot_git in
let root = Fpath.v root in
let buffers =
fopt
(fun n -> Lwt_pool.create n (fun () -> Lwt.return (G.buffer ())))
buffers
in
G.v ?dotgit ?compression:level ?buffers root >>= function
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok g -> R.v ~head ~bare g >|= fun b -> { g; b; config = conf }
let close _ = Lwt.return_unit
end
end
include Irmin.Of_private (P)
module Git = G
let git_commit (repo : Repo.t) (h : commit) : Git.Value.Commit.t option Lwt.t
=
let h = Commit.hash h in
Git.read repo.g h >|= function
| Ok (Git.Value.Commit c) -> Some c
| _ -> None
let git_of_repo r = r.g
let repo_of_git ?head ?(bare = true) ?lock g =
R.v ?lock ~head ~bare g >|= fun b ->
{ config = Irmin.Private.Conf.empty; g; b }
end
module Mem = struct
include Git.Mem.Store
let confs = Hashtbl.create 10
let find_conf c = Hashtbl.find_opt confs c
let add_conf c t =
Hashtbl.replace confs c t;
t
let v' ?dotgit ?compression ?buffers root =
let buffer =
match buffers with None -> None | Some p -> Some (Lwt_pool.use p)
in
v ?dotgit ?compression ?buffer root
let v ?dotgit ?compression ?buffers root =
let conf = (root, dotgit, compression, buffers) in
match find_conf conf with
| Some x -> Lwt.return x
| None -> v' ?dotgit ?compression ?buffers root >|= add_conf conf
end
module Make
(G : G)
(S : Git.Sync.S with module Store := G)
(C : Irmin.Contents.S)
(P : Irmin.Path.S)
(B : Irmin.Branch.S) =
Make_ext (G) (S) (C) (P) (Branch (B))
module No_sync (G : Git.S) = struct
(* XXX(samoht): so much boilerplate... *)
module Store = G
module Endpoint = struct
type t = unit
let uri _ = assert false
end
type error = unit
let pp_error _ _ = assert false
type command =
[ `Create of Store.Hash.t * Store.Reference.t
| `Delete of Store.Hash.t * Store.Reference.t