| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open CErrors |
| 13 |
|
| 14 |
module type Kind = |
| 15 |
sig |
| 16 |
type 'a t |
| 17 |
val master : 'a t |
| 18 |
end |
| 19 |
|
| 20 |
module type S = sig |
| 21 |
|
| 22 |
module Branch : |
| 23 |
sig |
| 24 |
type t |
| 25 |
val make : string -> t |
| 26 |
val equal : t -> t -> bool |
| 27 |
val compare : t -> t -> int |
| 28 |
val to_string : t -> string |
| 29 |
val master : t |
| 30 |
end |
| 31 |
|
| 32 |
type id |
| 33 |
type 'a kind_gen |
| 34 |
type kind = Branch.t kind_gen |
| 35 |
|
| 36 |
type branch_info = { |
| 37 |
kind : kind; |
| 38 |
root : id; |
| 39 |
pos : id; |
| 40 |
} |
| 41 |
|
| 42 |
type ('diff,'info,'property_data) t |
| 43 |
|
| 44 |
val empty : id -> ('diff,'info,'property_data) t |
| 45 |
|
| 46 |
val current_branch : ('e,'i,'c) t -> Branch.t |
| 47 |
val branches : ('e,'i,'c) t -> Branch.t list |
| 48 |
|
| 49 |
val get_branch : ('e,'i,'c) t -> Branch.t -> branch_info |
| 50 |
val reset_branch : ('e,'i,'c) t -> Branch.t -> id -> ('e,'i,'c) t |
| 51 |
val branch : |
| 52 |
('e,'i,'c) t -> ?root:id -> ?pos:id -> |
| 53 |
Branch.t -> kind -> ('e,'i,'c) t |
| 54 |
val delete_branch : ('e,'i,'c) t -> Branch.t -> ('e,'i,'c) t |
| 55 |
val merge : |
| 56 |
('diff,'i,'c) t -> id -> ours:'diff -> theirs:'diff -> ?into:Branch.t -> |
| 57 |
Branch.t -> ('diff,'i,'c) t |
| 58 |
val commit : ('diff,'i,'c) t -> id -> 'diff -> ('diff,'i,'c) t |
| 59 |
val rewrite_merge : |
| 60 |
('diff,'i,'c) t -> id -> ours:'diff -> theirs:'diff -> at:id -> |
| 61 |
Branch.t -> ('diff,'i,'c) t |
| 62 |
val checkout : ('e,'i,'c) t -> Branch.t -> ('e,'i,'c) t |
| 63 |
|
| 64 |
val set_info : ('e,'info,'c) t -> id -> 'info -> ('e,'info,'c) t |
| 65 |
val get_info : ('e,'info,'c) t -> id -> 'info option |
| 66 |
|
| 67 |
|
| 68 |
module Dag : Dag.S with type node = id |
| 69 |
val dag : ('diff,'info,'cdata) t -> ('diff,'info,'cdata) Dag.t |
| 70 |
|
| 71 |
val create_property : ('e,'i,'c) t -> id list -> 'c -> ('e,'i,'c) t |
| 72 |
val property_of : ('e,'i,'c) t -> id -> 'c Dag.Property.t list |
| 73 |
val delete_property : ('e,'i,'c) t -> 'c Dag.Property.t -> ('e,'i,'c) t |
| 74 |
|
| 75 |
|
| 76 |
val gc : ('e,'info,'c) t -> ('e,'info,'c) t * Dag.NodeSet.t |
| 77 |
val reachable : ('e,'info,'c) t -> id -> Dag.NodeSet.t |
| 78 |
|
| 79 |
end |
| 80 |
|
| 81 |
module Make(OT : Map.OrderedType)(K : Kind) = struct |
| 82 |
|
| 83 |
module Dag = Dag.Make(OT) |
| 84 |
|
| 85 |
type id = OT.t |
| 86 |
|
| 87 |
module Branch = |
| 88 |
struct |
| 89 |
type t = string |
| 90 |
let make = |
| 91 |
let bid = ref 0 in |
| 92 |
fun s -> incr bid; string_of_int !bid ^ "_" ^ s |
| 93 |
let equal = CString.equal |
| 94 |
let compare = CString.compare |
| 95 |
let to_string s = s |
| 96 |
let master = "master" |
| 97 |
end |
| 98 |
|
| 99 |
type 'a kind_gen = 'a K.t |
| 100 |
type kind = Branch.t kind_gen |
| 101 |
|
| 102 |
module BranchMap = Map.Make(Branch) |
| 103 |
|
| 104 |
type branch_info = { |
| 105 |
kind : kind; |
| 106 |
root : id; |
| 107 |
pos : id; |
| 108 |
} |
| 109 |
|
| 110 |
type ('edge,'info,'property_data) t = { |
| 111 |
cur_branch : Branch.t; |
| 112 |
heads : branch_info BranchMap.t; |
| 113 |
dag : ('edge,'info,'property_data) Dag.t; |
| 114 |
} |
| 115 |
|
| 116 |
let empty root = { |
| 117 |
cur_branch = Branch.master; |
| 118 |
heads = BranchMap.singleton Branch.master { root = root; pos = root; kind = K.master }; |
| 119 |
dag = Dag.empty; |
| 120 |
} |
| 121 |
|
| 122 |
let add_node vcs id edges = |
| 123 |
assert (not (CList.is_empty edges)); |
| 124 |
{ vcs with dag = |
| 125 |
List.fold_left (fun g (t,tgt) -> Dag.add_edge g id t tgt) vcs.dag edges } |
| 126 |
|
| 127 |
let get_branch vcs head = |
| 128 |
try BranchMap.find head vcs.heads |
| 129 |
with Not_found -> anomaly (str"head " ++ str head ++ str" not found.") |
| 130 |
|
| 131 |
let reset_branch vcs head id = |
| 132 |
let map name h = |
| 133 |
if Branch.equal name head then { h with pos = id } else h |
| 134 |
in |
| 135 |
{ vcs with heads = BranchMap.mapi map vcs.heads; } |
| 136 |
|
| 137 |
let current_branch vcs = vcs.cur_branch |
| 138 |
|
| 139 |
let branch |
| 140 |
vcs ?(root=(get_branch vcs vcs.cur_branch).pos) ?(pos=root) name kind |
| 141 |
= |
| 142 |
{ vcs with |
| 143 |
heads = BranchMap.add name { kind; root; pos } vcs.heads; |
| 144 |
cur_branch = name } |
| 145 |
|
| 146 |
let delete_branch vcs name = |
| 147 |
if Branch.equal Branch.master name then vcs else |
| 148 |
let filter n _ = not (Branch.equal n name) in |
| 149 |
{ vcs with heads = BranchMap.filter filter vcs.heads } |
| 150 |
|
| 151 |
let merge vcs id ~ours:tr1 ~theirs:tr2 ?(into = vcs.cur_branch) name = |
| 152 |
assert (not (Branch.equal name into)); |
| 153 |
let br_id = (get_branch vcs name).pos in |
| 154 |
let cur_id = (get_branch vcs into).pos in |
| 155 |
let vcs = add_node vcs id [tr1,cur_id; tr2,br_id] in |
| 156 |
let vcs = reset_branch vcs into id in |
| 157 |
vcs |
| 158 |
|
| 159 |
let del_edge id vcs tgt = { vcs with dag = Dag.del_edge vcs.dag id tgt } |
| 160 |
|
| 161 |
let rewrite_merge vcs id ~ours:tr1 ~theirs:tr2 ~at:cur_id name = |
| 162 |
let br_id = (get_branch vcs name).pos in |
| 163 |
let old_edges = List.map fst (Dag.from_node vcs.dag id) in |
| 164 |
let vcs = List.fold_left (del_edge id) vcs old_edges in |
| 165 |
let vcs = add_node vcs id [tr1,cur_id; tr2,br_id] in |
| 166 |
vcs |
| 167 |
|
| 168 |
let commit vcs id tr = |
| 169 |
let vcs = add_node vcs id [tr, (get_branch vcs vcs.cur_branch).pos] in |
| 170 |
let vcs = reset_branch vcs vcs.cur_branch id in |
| 171 |
vcs |
| 172 |
|
| 173 |
let checkout vcs name = { vcs with cur_branch = name } |
| 174 |
|
| 175 |
let set_info vcs id info = { vcs with dag = Dag.set_info vcs.dag id info } |
| 176 |
let get_info vcs id = Dag.get_info vcs.dag id |
| 177 |
|
| 178 |
let create_property vcs l i = { vcs with dag = Dag.create_property vcs.dag l i } |
| 179 |
let property_of vcs i = Dag.property_of vcs.dag i |
| 180 |
let delete_property vcs c = { vcs with dag = Dag.del_property vcs.dag c } |
| 181 |
|
| 182 |
let branches vcs = BranchMap.fold (fun x _ accu -> x :: accu) vcs.heads [] |
| 183 |
let dag vcs = vcs.dag |
| 184 |
|
| 185 |
let rec closure s d n = |
| 186 |
let l = try Dag.from_node d n with Not_found -> [] in |
| 187 |
List.fold_left (fun s (n',_) -> |
| 188 |
if Dag.NodeSet.mem n' s then s |
| 189 |
else closure s d n') |
| 190 |
(Dag.NodeSet.add n s) l |
| 191 |
|
| 192 |
let reachable vcs i = closure Dag.NodeSet.empty vcs.dag i |
| 193 |
|
| 194 |
let gc vcs = |
| 195 |
let alive = |
| 196 |
BranchMap.fold (fun b { pos } s -> closure s vcs.dag pos) |
| 197 |
vcs.heads Dag.NodeSet.empty in |
| 198 |
let dead = Dag.NodeSet.diff (Dag.all_nodes vcs.dag) alive in |
| 199 |
{ vcs with dag = Dag.del_nodes vcs.dag dead }, dead |
| 200 |
|
| 201 |
end |