Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183type diff = | Modified of string | Created of string | Renamed of string * string | Removed of string[@@deriving repr]
let diff_jsont = let jsont__Removed = Jsont.Object.Case.map "Removed" (Jsont.Object.map ~kind:"Removed" Stdlib.Fun.id |> Jsont.Object.mem "v" ~doc:"Wrapper for Removed" Jsont.string ~enc:Fun.id |> Jsont.Object.finish) ~dec:(fun arg0 -> Removed arg0) and jsont__Renamed = Jsont.Object.Case.map "Renamed" (Jsont.Object.map ~kind:"Renamed" Stdlib.Fun.id |> Jsont.Object.mem "v" ~doc:"Wrapper for Renamed" (let get_or_raise = function | Ok r -> r | Error err -> raise (Jsont.Error err) in let enc f acc (e0, e1) = let e0 = Jsont.Json.encode' Jsont.string e0 |> get_or_raise in let e1 = Jsont.Json.encode' Jsont.string e1 |> get_or_raise in [ (0, e0); (1, e1) ] |> Stdlib.List.fold_left (fun acc (i, e) -> f acc i e) acc in let dec_empty () = (None, None) in let dec_add i elt (e0, e1) = match i with | 0 -> let e = Jsont.Json.decode' Jsont.string elt |> get_or_raise in (Some e, e1) | 1 -> let e = Jsont.Json.decode' Jsont.string elt |> get_or_raise in (e0, Some e) | _ -> Jsont.Error.msgf Jsont.Meta.none "Too many elements for tuple." in let dec_finish meta _ (e0, e1) = let get_or_raise i o = match o with | Some v -> v | None -> Jsont.Error.msgf meta "Missing tuple member #%i" i in (get_or_raise 0 e0, get_or_raise 1 e1) in Jsont.Array.map ~enc:{ enc } ~dec_empty ~dec_add ~dec_finish Jsont.json |> Jsont.Array.array) ~enc:Fun.id |> Jsont.Object.finish) ~dec:(fun (arg0, arg1) -> Renamed (arg0, arg1)) and jsont__Created = Jsont.Object.Case.map "Created" (Jsont.Object.map ~kind:"Created" Stdlib.Fun.id |> Jsont.Object.mem "v" ~doc:"Wrapper for Created" Jsont.string ~enc:Fun.id |> Jsont.Object.finish) ~dec:(fun arg0 -> Created arg0) and jsont__Modified = Jsont.Object.Case.map "Modified" (Jsont.Object.map ~kind:"Modified" Stdlib.Fun.id |> Jsont.Object.mem "v" ~doc:"Wrapper for Modified" Jsont.string ~enc:Fun.id |> Jsont.Object.finish) ~dec:(fun arg0 -> Modified arg0) in Jsont.Object.map ~kind:"Diff" Fun.id |> Jsont.Object.case_mem "type" ~doc:"Cases for Diff" Jsont.string ~enc:Fun.id ~enc_case:(function | Removed arg0 -> Jsont.Object.Case.value jsont__Removed arg0 | Renamed (arg0, arg1) -> Jsont.Object.Case.value jsont__Renamed (arg0, arg1) | Created arg0 -> Jsont.Object.Case.value jsont__Created arg0 | Modified arg0 -> Jsont.Object.Case.value jsont__Modified arg0) [ Jsont.Object.Case.make jsont__Removed; Jsont.Object.Case.make jsont__Renamed; Jsont.Object.Case.make jsont__Created; Jsont.Object.Case.make jsont__Modified; ] |> Jsont.Object.finish
let path = function | Modified p -> p | Created p -> p | Renamed (p, _) -> p | Removed p -> p
type t = diff list [@@deriving repr]
let jsont = Jsont.list diff_jsont
let truncate_path s = match Astring.String.cut ~sep:"rootfs" s with Some (_, p) -> p | None -> s
let parse_row = function | [ "M"; s ] -> let path = truncate_path s in if String.equal path "" then None else Some (Modified path) | [ "+"; s ] -> let path = truncate_path s in if String.equal path "" then None else Some (Created path) | [ "R"; a; b ] -> let a_path = truncate_path a in let b_path = truncate_path b in Some (Renamed (a_path, b_path)) | [ "-"; s ] -> let path = truncate_path s in if String.equal path "" then None else Some (Removed path) | s -> Fmt.invalid_arg "Unknown ZFS diff: %a" (Fmt.list ~sep:Fmt.comma Fmt.string) s
let of_zfs s : t = let lines = String.split_on_char '\n' s in let tsv = List.map (String.split_on_char '\t') lines |> List.map (List.filter (fun s -> not (String.equal "" s))) |> List.filter (function [] -> false | _ -> true) in List.filter_map parse_row tsv
type tree = Leaf of diff | Dir of string * tree list
let rec insert modified path_components tree = match (path_components, tree) with | [], _ -> tree | [ file ], nodes -> if List.exists (function Leaf f -> path f = file | _ -> false) nodes then nodes else let diff = match modified with | Modified _ -> Modified file | Created _ -> Created file | Renamed (_, to_) -> Renamed (file, to_) | Removed _ -> Removed file in Leaf diff :: nodes | dir :: rest, nodes -> let rec insert_into_dir acc = function | [] -> Dir (dir, insert modified rest []) :: List.rev acc | Dir (name, children) :: tl when name = dir -> List.rev_append acc (Dir (name, insert modified rest children) :: tl) | x :: tl -> insert_into_dir (x :: acc) tl in insert_into_dir [] nodes
let to_tree (diffs : diff list) = let paths = List.map (fun v -> (v, String.split_on_char '/' (path v))) diffs in List.fold_left (fun acc (m, p) -> insert m p acc) [] paths
let leaves = let rec loop acc acc2 = function | Leaf (Modified v) -> Modified (Filename.concat acc v) :: acc2 | Leaf (Created v) -> Created (Filename.concat acc v) :: acc2 | Leaf (Removed v) -> Removed (Filename.concat acc v) :: acc2 | Leaf (Renamed (r1, r2)) -> Renamed (Filename.concat acc r1, r2) :: acc2 | Dir (p, cs) -> List.fold_left (fun lvs v -> loop (Filename.concat acc p) lvs v) acc2 cs in loop "" []
let pp_diff fmt = function | Modified v -> Logger.pp_colored `Yellow Fmt.string fmt ("~ /" ^ v) | Created v -> Logger.pp_colored `Green Fmt.string fmt ("+ /" ^ v) | Removed v -> Logger.pp_colored `Red Fmt.string fmt ("- /" ^ v) | Renamed (v, _) -> Logger.pp_colored `Magenta Fmt.string fmt ("| /" ^ v)
let pp fmt diffs = let tree = to_tree diffs in let lvs = List.fold_left (fun acc v -> leaves v @ acc) [] tree in if lvs = [] then Fmt.nop fmt () else Fmt.pf fmt "%a" Fmt.(list ~sep:Format.pp_force_newline pp_diff) lvs