Source file commit.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
open Lwt.Infix
open Merge.Infix
let src = Logs.Src.create "irmin.commit" ~doc:"Irmin commits"
module Log = (val Logs.src_log src : Logs.LOG)
module Make (K : Type.S) = struct
type hash = K.t
type t = { node : hash; parents : hash list; info : Info.t }
let parents t = t.parents
let node t = t.node
let info t = t.info
let v ~info ~node ~parents =
let parents = List.fast_sort (Type.compare K.t) parents in
{ node; parents; info }
let t =
let open Type in
record "commit" (fun node parents info -> { node; parents; info })
|+ field "node" K.t (fun t -> t.node)
|+ field "parents" (list K.t) (fun t -> t.parents)
|+ field "info" Info.t (fun t -> t.info)
|> sealr
let hash_t = K.t
end
module Store
(N : S.NODE_STORE) (S : sig
include S.CONTENT_ADDRESSABLE_STORE with type key = N.key
module Key : S.HASH with type t = key
module Val : S.COMMIT with type t = value and type hash = key
end) =
struct
module Node = N
type 'a t = 'a N.t * 'a S.t
type key = S.key
type value = S.value
let add (_, t) = S.add t
let unsafe_add (_, t) = S.unsafe_add t
let mem (_, t) = S.mem t
let find (_, t) = S.find t
let merge_node (t, _) = Merge.f (N.merge t)
let pp_key = Type.pp S.Key.t
let err_not_found k =
Fmt.kstrf invalid_arg "Commit.get: %a not found" pp_key k
let get (_, t) k =
S.find t k >>= function None -> err_not_found k | Some v -> Lwt.return v
let empty_if_none (n, _) = function
| None -> N.add n N.Val.empty
| Some node -> Lwt.return node
let equal_opt_keys = Type.(equal (option S.Key.t))
let merge_commit info t ~old k1 k2 =
get t k1 >>= fun v1 ->
get t k2 >>= fun v2 ->
if List.mem k1 (S.Val.parents v2) then Merge.ok k2
else if List.mem k2 (S.Val.parents v1) then Merge.ok k1
else
(old () >>= function
| Error (`Conflict msg) ->
Log.debug (fun f -> f "old: conflict %s" msg);
Lwt.return_none
| Ok o -> Lwt.return o)
>>= fun old ->
if equal_opt_keys old (Some k1) then Merge.ok k2
else if equal_opt_keys old (Some k2) then Merge.ok k1
else
let old () =
match old with
| None -> Merge.ok None
| Some old ->
get t old >>= fun vold ->
Merge.ok (Some (Some (S.Val.node vold)))
in
merge_node t ~old (Some (S.Val.node v1)) (Some (S.Val.node v2))
>>=* fun node ->
empty_if_none t node >>= fun node ->
let parents = [ k1; k2 ] in
let commit = S.Val.v ~node ~parents ~info:(info ()) in
add t commit >>= fun key -> Merge.ok key
let merge t ~info = Merge.(option (v S.Key.t (merge_commit info t)))
module Key = Hash.Typed (S.Key) (S.Val)
module Val = S.Val
end
module History (S : S.COMMIT_STORE) = struct
type commit = S.key
type node = S.Node.key
type 'a t = 'a S.t
type v = S.Val.t
let commit_t = S.Key.t
let merge t ~info =
let f ~old c1 c2 =
let somify = Merge.map_promise (fun x -> Some x) in
let merge = S.merge t ~info in
Merge.f merge ~old:(somify old) (Some c1) (Some c2) >>=* function
| None -> Merge.conflict "History.merge"
| Some x -> Merge.ok x
in
Merge.v S.Key.t f
let v t ~node ~parents ~info =
let commit = S.Val.v ~node ~parents ~info in
S.add t commit >|= fun hash -> (hash, commit)
let pp_key = Type.pp S.Key.t
let parents t c =
Log.debug (fun f -> f "parents %a" pp_key c);
S.find t c >|= function None -> [] | Some c -> S.Val.parents c
module Graph =
Object_graph.Make (S.Node.Contents.Key) (S.Node.Metadata) (S.Node.Key)
(S.Key)
(struct
type t = unit
let t = Type.unit
end)
let edges t =
Log.debug (fun f -> f "edges");
[ `Node (S.Val.node t) ] @ List.map (fun k -> `Commit k) (S.Val.parents t)
let closure t ~min ~max =
Log.debug (fun f -> f "closure");
let pred = function
| `Commit k -> (
S.find t k >|= function Some r -> edges r | None -> [] )
| _ -> Lwt.return_nil
in
let min = List.map (fun k -> `Commit k) min in
let max = List.map (fun k -> `Commit k) max in
Graph.closure ~pred ~min ~max () >|= fun g ->
List.fold_left
(fun acc -> function `Commit k -> k :: acc | _ -> acc)
[] (Graph.vertex g)
module K = struct
type t = S.Key.t
let compare = Type.compare S.Key.t
let hash = S.Key.short_hash
let equal = Type.equal S.Key.t
end
module KSet = Set.Make (K)
module KHashtbl = Hashtbl.Make (K)
let read_parents t commit =
S.find t commit >|= function
| None -> KSet.empty
| Some c -> KSet.of_list (S.Val.parents c)
let equal_keys = Type.equal S.Key.t
let str_key k = String.sub (Type.to_string S.Key.t k) 0 4
let pp_key = Fmt.of_to_string str_key
let pp_keys ppf keys =
let keys = KSet.elements keys in
Fmt.pf ppf "[%a]" Fmt.(list ~sep:(unit " ") pp_key) keys
let str_keys = Fmt.to_to_string pp_keys
let lca_calls = ref 0
let rec unqueue todo seen =
if Queue.is_empty todo then None
else
let ((_, commit) as pop) = Queue.pop todo in
if KSet.mem commit seen then unqueue todo seen else Some pop
let traverse_bfs t ~f ~pp:_ ~check ~init ~return =
let todo = Queue.create () in
let add_todo d x = Queue.add (d, x) todo in
KSet.iter (add_todo 0) init;
let rec aux seen =
match check () with
| (`Too_many_lcas | `Max_depth_reached) as x -> Lwt.return_error x
| `Stop -> return ()
| `Continue -> (
match unqueue todo seen with
| None -> return ()
| Some (depth, commit) ->
let seen = KSet.add commit seen in
read_parents t commit >>= fun parents ->
let () = f depth commit parents in
let parents = KSet.diff parents seen in
KSet.iter (add_todo (depth + 1)) parents;
aux seen )
in
aux KSet.empty
type mark =
| Seen1
| Seen2
| SeenBoth
| LCA
let _pp_mark = function
| Seen1 -> "seen1"
| Seen2 -> "seen2"
| SeenBoth -> "seenBoth"
| LCA -> "LCA"
type state = {
marks : mark KHashtbl.t;
parents : KSet.t KHashtbl.t;
layers : (int, KSet.t) Hashtbl.t;
c1 : S.key;
c2 : S.key;
mutable depth : int;
mutable lcas : int;
mutable complete : bool;
}
let pp_state t =
lazy
(let pp m =
KHashtbl.fold
(fun k v acc -> if v = m then str_key k :: acc else acc)
t.marks []
|> String.concat " "
in
Fmt.strf "d: %d, seen1: %s, seen2: %s, seenboth: %s, lcas: %s (%d) %s"
t.depth (pp Seen1) (pp Seen2) (pp SeenBoth) (pp LCA) t.lcas
(String.concat " | "
(Hashtbl.fold
(fun d ks acc -> Fmt.strf "(%d: %s)" d (str_keys ks) :: acc)
t.layers [])))
let get_mark_exn t elt = KHashtbl.find t.marks elt
let get_mark t elt = try Some (get_mark_exn t elt) with Not_found -> None
let set_mark t elt mark = KHashtbl.replace t.marks elt mark
let get_layer t d =
try Hashtbl.find t.layers d with Not_found -> KSet.empty
let add_to_layer t d k =
Hashtbl.replace t.layers d (KSet.add k (get_layer t d))
let add_parent t c p = KHashtbl.add t.parents c p
let get_parent t c =
try KHashtbl.find t.parents c with Not_found -> KSet.empty
let incr_lcas t = t.lcas <- t.lcas + 1
let decr_lcas t = t.lcas <- t.lcas - 1
let both_seen t k =
match get_mark t k with
| None | Some Seen1 | Some Seen2 -> false
| _ -> true
let empty_state c1 c2 =
let t =
{
marks = KHashtbl.create 10;
parents = KHashtbl.create 10;
layers = Hashtbl.create 10;
c1;
c2;
depth = 0;
lcas = 0;
complete = false;
}
in
set_mark t c1 Seen1;
set_mark t c2 Seen2;
t
let update_mark t mark commit =
let new_mark =
match (mark, get_mark t commit) with
| Seen1, Some Seen1 | Seen1, None -> Seen1
| Seen2, Some Seen2 | Seen2, None -> Seen2
| SeenBoth, Some LCA ->
decr_lcas t;
SeenBoth
| SeenBoth, _ -> SeenBoth
| Seen1, Some Seen2 | Seen2, Some Seen1 ->
incr_lcas t;
LCA
| _, Some LCA -> LCA
| _ -> SeenBoth
in
let is_init () = equal_keys commit t.c1 || equal_keys commit t.c2 in
let is_shared () = new_mark = SeenBoth || new_mark = LCA in
if is_shared () && is_init () then (
Log.debug (fun f -> f "fast-forward");
t.complete <- true );
set_mark t commit new_mark;
new_mark
let update_ancestors_marks t mark commit =
let todo = Queue.create () in
Queue.add commit todo;
let rec loop mark =
if Queue.is_empty todo then ()
else
let a = Queue.pop todo in
let old_mark = get_mark t a in
let mark = update_mark t mark a in
let () =
match old_mark with
| Some (SeenBoth | LCA) -> ()
| Some old when old = mark -> ()
| _ -> KSet.iter (fun x -> Queue.push x todo) (get_parent t a)
in
loop (if mark = LCA then SeenBoth else mark)
in
loop mark
let update_parents t depth commit parents =
add_parent t commit parents;
add_to_layer t depth commit;
if depth <> t.depth then (
assert (depth = t.depth + 1);
let layer = get_layer t t.depth in
let complete = KSet.for_all (both_seen t) layer in
if complete then t.complete <- true else t.depth <- depth );
let mark = get_mark_exn t commit in
KSet.iter (update_ancestors_marks t mark) parents
let lcas t =
KHashtbl.fold (fun k v acc -> if v = LCA then k :: acc else acc) t.marks []
let check ~max_depth ~n t =
if t.depth > max_depth then `Max_depth_reached
else if t.lcas > n then `Too_many_lcas
else if t.lcas = n || t.complete then `Stop
else `Continue
let lcas t ?(max_depth = max_int) ?(n = max_int) c1 c2 =
incr lca_calls;
if max_depth < 0 then Lwt.return_error `Max_depth_reached
else if n <= 0 then Lwt.return_error `Too_many_lcas
else if equal_keys c1 c2 then Lwt.return_ok [ c1 ]
else
let init = KSet.of_list [ c1; c2 ] in
let s = empty_state c1 c2 in
let check () = check ~max_depth ~n s in
let pp () = pp_state s in
let return () = Lwt.return_ok (lcas s) in
let t0 = Sys.time () in
Lwt.finalize
(fun () ->
traverse_bfs t ~f:(update_parents s) ~pp ~check ~init ~return)
(fun () ->
let t1 = Sys.time () -. t0 in
Log.debug (fun f ->
f "lcas %d: depth=%d time=%.4fs" !lca_calls s.depth t1);
Lwt.return_unit)
let rec three_way_merge t ~info ?max_depth ?n c1 c2 =
Log.debug (fun f -> f "3-way merge between %a and %a" pp_key c1 pp_key c2);
if equal_keys c1 c2 then Merge.ok c1
else
lcas t ?max_depth ?n c1 c2 >>= fun lcas ->
let old () =
match lcas with
| Error `Too_many_lcas -> Merge.conflict "Too many lcas"
| Error `Max_depth_reached -> Merge.conflict "Max depth reached"
| Ok [] -> Merge.ok None
| Ok (old :: olds) ->
let rec aux acc = function
| [] -> Merge.ok (Some acc)
| old :: olds ->
three_way_merge t ~info acc old >>=* fun acc -> aux acc olds
in
aux old olds
in
let merge =
merge t ~info
|> Merge.with_conflict (fun msg ->
Fmt.strf "Recursive merging of common ancestors: %s" msg)
|> Merge.f
in
merge ~old c1 c2
let lca_aux t ~info ?max_depth ?n c1 c2 =
if equal_keys c1 c2 then Merge.ok (Some c1)
else
lcas t ?max_depth ?n c1 c2 >>= function
| Error `Too_many_lcas -> Merge.conflict "Too many lcas"
| Error `Max_depth_reached -> Merge.conflict "Max depth reached"
| Ok [] -> Merge.ok None
| Ok [ x ] -> Merge.ok (Some x)
| Ok (c :: cs) ->
let rec aux acc = function
| [] -> Merge.ok (Some acc)
| c :: cs -> (
three_way_merge t ~info ?max_depth ?n acc c >>= function
| Error (`Conflict _) -> Merge.ok None
| Ok acc -> aux acc cs )
in
aux c cs
let rec lca t ~info ?max_depth ?n = function
| [] -> Merge.conflict "History.lca: empty"
| [ c ] -> Merge.ok (Some c)
| c1 :: c2 :: cs -> (
lca_aux t ~info ?max_depth ?n c1 c2 >>=* function
| None -> Merge.ok None
| Some c -> lca t ~info ?max_depth ?n (c :: cs) )
end
module V1 (C : S.COMMIT) = struct
module K = struct
let h = Type.string_of `Int64
let size_of ? x =
Type.size_of ?headers h (Type.to_bin_string C.hash_t x)
let encode_bin ? e k =
Type.encode_bin ?headers h (Type.to_bin_string C.hash_t e) k
let decode_bin ? buf off =
let n, v = Type.decode_bin ?headers h buf off in
( n,
match Type.of_bin_string C.hash_t v with
| Ok v -> v
| Error (`Msg e) -> Fmt.failwith "decode_bin: %s" e )
let t = Type.like C.hash_t ~bin:(encode_bin, decode_bin, size_of)
end
type hash = C.hash
let hash_t = K.t
type t = { parents : hash list; c : C.t }
let import c = { c; parents = C.parents c }
let export t = t.c
let node t = C.node t.c
let parents t = t.parents
let info t = C.info t.c
let v ~info ~node ~parents = { parents; c = C.v ~node ~parents ~info }
let make = v
let info_t : Info.t Type.t =
let open Type in
record "info" (fun date author message -> Info.v ~date ~author message)
|+ field "date" int64 (fun t -> Info.date t)
|+ field "author" (string_of `Int64) (fun t -> Info.author t)
|+ field "message" (string_of `Int64) (fun t -> Info.message t)
|> sealr
let t : t Type.t =
let open Type in
record "commit" (fun node parents info -> make ~info ~node ~parents)
|+ field "node" K.t node
|+ field "parents" (list ~len:`Int64 K.t) parents
|+ field "info" info_t info |> sealr
end