Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334module Make (Ctx : Types.Context) = struct type 'entry irmin = | Store : ((module Irmin.S with type t = 'a and type Schema.Branch.t = string and type Schema.Contents.t = 'entry and type Schema.Path.t = string list and type Schema.Path.step = string) * 'a) -> 'entry irmin
type ctx = Zfs_store.t type entry = Ctx.ctx type store = entry list irmin
let history_key = [ "history" ]
type s = { store : store; ctx : ctx } type t = s option
let get_store t = t.store let get_ctx t = t.ctx let v store ctx = { store; ctx } let get_store_opt = Option.map get_store
let no_store () = Fmt.failwith "Trying to access store, but it has not been initialised."
let get_ctx_exn = function None -> no_store () | Some s -> s.ctx
let history s = match s with | None -> no_store () | Some (Store ((module S), store) : store) -> ( let repo = S.repo store in match S.Head.find store with | None -> [] | Some hd -> let rec linearize c = match S.Commit.parents c |> List.map (S.Commit.of_hash repo) with | [ Some p ] -> c :: linearize p | _ -> [ c ] in let commits = linearize hd in let get_diff_content t1 t2 = match S.Tree.diff t1 t2 with | [ (_, `Added (c, _)) ] -> c | [ (_, `Updated ((_, _), (c, _))) ] -> c | lst -> let pp_diff = Repr.pp (Irmin.Diff.t (Repr.pair Repr.(list Ctx.ctx_t) S.metadata_t)) in Fmt.epr "Get diff (%i) content %a%!" (List.length lst) Fmt.( list ~sep:Fmt.comma (Fmt.pair (Repr.pp S.path_t) pp_diff)) lst; invalid_arg "Get diff should only have a single difference." in let hash c = S.Commit.hash c |> S.Hash.to_raw_string in let rec diff_calc = function | [] -> [] | [ x ] -> let diff = get_diff_content (S.Tree.empty ()) (S.Commit.tree x) in [ (hash x, diff) ] | c :: p :: rest -> let diff = get_diff_content (S.Commit.tree p) (S.Commit.tree c) in (hash c, diff) :: diff_calc (p :: rest) in diff_calc commits)
let with_latest ~default s f = match history s with [] -> default () | (_, hd) :: _ -> f hd
let latest s = with_latest ~default:(fun _ -> Fmt.failwith "No latest!") s List.hd
let text c = Fmt.(styled (`Fg c) string)
let sessions s = match get_store_opt s with | None -> no_store () | Some (Store ((module S), store) : store) -> S.Branch.list (S.repo store)
let commit ~message clock s v = match get_store_opt s with | None -> no_store () | Some (Store ((module S), store) : store) -> let info () = S.Info.v ~message (Eio.Time.now clock |> Int64.of_float) in S.set_exn ~info store history_key v
let commit_info s = match get_store_opt s with | None -> no_store () | Some (Store ((module S), store) : store) -> let history = history (Some (Store ((module S), store))) in let repo = S.repo store in List.fold_left (fun acc (commit, _) -> let commit = S.Hash.unsafe_of_raw_string commit |> S.Commit.of_hash repo |> Option.get in let info = S.Commit.info commit |> S.Info.message in let hash = S.Commit.hash commit |> Repr.to_string S.Hash.t in (String.sub hash 0 7, info) :: acc) [] history
(* TODO: We could improve performance by not setting the HEAD each time. *) let rec reset_hard_aux n s = match get_store_opt s with | None -> no_store () | Some (Store ((module S), store) : store) -> ( match List.filter_map (S.Commit.of_hash (S.repo store)) @@ S.Commit.parents (S.Head.get store) with | [] -> Ok s | [ p ] -> S.Head.set store p; if n <= 1 then Ok s else reset_hard_aux (n - 1) s | ps -> Error (Fmt.str "Not exactly one parent: %a" Import.Fmt.(lst (Repr.pp (S.commit_t (S.repo store)))) ps))
let reset_hard ?(n = 1) s = reset_hard_aux n s
let save_execution s env new_entry diff = match get_store_opt s with | None -> no_store () | Some (Store ((module S), store) : store) -> ( match new_entry with | Error e -> Error e | Ok (`Reset c) -> ( match S.Hash.unsafe_of_raw_string c |> S.Commit.of_hash (S.repo store) with | None -> Fmt.epr "Resetting to existing entry failed...\n%!"; Ok s | Some c -> S.Head.set store c; Ok s) | Ok (`Entry (Exit.Nonzero _, _)) -> Ok s | Ok (`Entry (Exit.Zero entry, path)) -> (* Set diff *) let current = Option.value ~default:[] @@ S.find store history_key in let new_entry = Ctx.with_ctx entry ~diff in (* Commit if RW *) if Ctx.mode entry = RW then ( commit ~message: (Fmt.str "evaluating `%a'" Ast_pp.complete_commands (Ctx.command new_entry)) env#clock s (new_entry :: current); (* Save the commit hash for easy restoring later *) let hash = S.Head.get store |> S.Commit.hash |> S.Hash.to_raw_string in Eio.Path.save ~create:(`If_missing 0o644) Eio.Path.(env#fs / path / "hash") hash); Ok s)
let with_directory s env fn = match get_store_opt s with | None -> no_store () | Some (Store ((module S), store) : store) -> ( let repo = S.repo store in let config = S.Repo.config repo in match Irmin.Backend.Conf.find_root config with | None -> failwith "No directory!" | Some p -> fn Eio.Path.(env#fs / p))
let save_branch_name s env ~name = with_directory s env @@ fun dir -> Eio.Path.(save ~create:(`If_missing 0o644) (dir / "runtime-branch") name)
let which_branch_from_file s env = with_directory s env @@ fun dir -> Eio.Path.(load (dir / "runtime-branch"))
let which_branch s env = match get_store_opt s with | None -> no_store () | Some (Store ((module S), store) : store) -> ( let branches = sessions s in let repo = S.repo store in let heads = List.map (fun b -> (S.Branch.find repo b, b)) branches in let head = S.Head.find store in let head_hash = Option.map (fun hash -> String.sub (Fmt.str "%a" S.Commit.pp_hash hash) 0 7) head in let branches = List.find_all (fun (h, _) -> Repr.(unstage (equal (Repr.option (S.Commit.t repo)))) head h) heads in match branches with | [] -> (head_hash, None) | [ (_, c) ] -> (head_hash, Some c) | _ -> (* The case where we have _just_ created a new branch and cannot distinguish it from the branch we created it from *) (head_hash, Some (which_branch_from_file s env)))
(* New sessions *) let set_session env s m = match s with | None -> no_store () | Some { store = (Store ((module S), store) : store); ctx } -> let sesh = S.of_branch (S.repo store) m in save_branch_name s env ~name:m; { store = Store ((module S), sesh); ctx }
let check_branch t env ~existing = match which_branch t env with | _, Some br -> String.equal br existing | _ -> false
let replay exec s env existing_branch = match s with | None -> no_store () | Some { store = (Store ((module S), store) : store); ctx } -> ( let seshes = sessions s in if not (List.exists (String.equal existing_branch) seshes) then ( Fmt.epr "%s does not exist!\n%!" existing_branch; Ok s) else if check_branch s env ~existing:existing_branch then ( Fmt.epr "Cannot replay current branch onto itself\n%!"; Ok s) else let repo = S.repo store in let from = S.of_branch repo existing_branch in match S.lcas ~n:1 store from with | Error lcas_error -> Fmt.epr "Replay LCAS: %a" (Repr.pp S.lca_error_t) lcas_error; Ok s | Ok [ lcas ] -> ( let all_commits = history (Some (Store ((module S), from))) in let lcas_hash = S.Commit.hash lcas |> S.Hash.to_raw_string in let rec collect = function | [] -> [] | (x, _) :: _ when String.equal lcas_hash x -> [] | v :: vs -> v :: collect vs in let commits_to_apply = collect all_commits |> List.rev in match commits_to_apply with | [] -> Error (`Shell (`Msg "no commits")) | (h, first) :: rest -> let _, latest_current = history (Some (Store ((module S), store))) |> List.hd in let first = List.hd first in let current_head = List.hd latest_current in let new_first = Ctx.with_ctx ~build:(Ctx.build current_head) first :: latest_current in let commits_to_apply = (h, new_first) :: rest in let get_build = function | Ok (`Entry ((c : entry Exit.t), _)) -> Ctx.build (Exit.value c) | Error _ -> Fmt.failwith "TODO: Thread through failed replays" | _ -> assert false in let _, res = List.fold_left (fun (build, last) (_, entry) -> let entry_to_apply = List.hd entry in let entry_to_apply : entry = Ctx.with_ctx entry_to_apply ~build in match last with | Error _ as e -> (build, e) | Ok st -> let new_entry, diff = exec st entry_to_apply in ( get_build new_entry, save_execution st env new_entry diff )) ( Ctx.build current_head, Ok (Some { store = Store ((module S), store); ctx }) ) commits_to_apply in res) | _ -> assert false (* Because n = 1 *))
let merge s env branch_name = match s with | None -> no_store () | Some { store = (Store ((module S), store) : store); _ } -> let message = Fmt.str "merged %s" branch_name in let info () = S.Info.v ~message (Eio.Time.now env#clock |> Int64.of_float) in let branch = S.of_branch (S.repo store) branch_name in S.merge_into ~info ~into:store branch
let fork ?(detach = false) env s ~new_branch = match s with | None -> no_store () | Some { store = (Store ((module S), session) : store); ctx } -> ( let repo = S.repo session in match (S.Head.find session, S.Branch.find repo new_branch) with | _, Some _ -> Error (`Msg (new_branch ^ " already exists, try @ session " ^ new_branch)) | None, _ -> let new_store = S.of_branch (S.repo session) new_branch in save_branch_name s env ~name:new_branch; let store = Store ((module S), new_store) in Ok { store; ctx } | Some commit, None -> let new_store = S.of_branch (S.repo session) new_branch in if not detach then S.Branch.set repo new_branch commit; save_branch_name s env ~name:new_branch; let store = Store ((module S), new_store) in Ok { store; ctx })end