From dbf21f049a82f5dbdef2ab67eb7b6914391e12e7 Mon Sep 17 00:00:00 2001 From: Ashine Foster Date: Mon, 31 Jul 2023 13:57:39 -0500 Subject: [PATCH] Add the offline capability using result monad --- src/ocamlorg_package/lib/opam_repository.ml | 29 ++++++++++++--------- src/ocamlorg_package/lib/pull.ml | 5 ---- 2 files changed, 16 insertions(+), 18 deletions(-) delete mode 100644 src/ocamlorg_package/lib/pull.ml diff --git a/src/ocamlorg_package/lib/opam_repository.ml b/src/ocamlorg_package/lib/opam_repository.ml index aa7dbe87d3..ea9f7b29a0 100644 --- a/src/ocamlorg_package/lib/opam_repository.ml +++ b/src/ocamlorg_package/lib/opam_repository.ml @@ -16,20 +16,13 @@ module Process = struct let check_status cmd = function | Unix.WEXITED 0 -> () - | Unix.WEXITED 1 -> - if Pull.is_pull () then - prerr_endline - "\n\ - Pulling from opam repository was not possible. \n\ - We will continue by building the package state using the existing \ - local state of opam-repository.\n\ - Some functionality might not work properly.\n" - else Fmt.failwith "%a %a" pp_cmd cmd pp_status (Unix.WEXITED 1) | status -> Fmt.failwith "%a %a" pp_cmd cmd pp_status status let exec cmd = let proc = Lwt_process.open_process_none cmd in - proc#status >|= check_status cmd + let open Lwt.Syntax in + let+ status = proc#status in + match status with Unix.WEXITED 0 -> Ok () | s -> Error (cmd, s) let pread cmd = let proc = Lwt_process.open_process_in cmd in @@ -60,7 +53,7 @@ let last_commit () = let clone () = let open Lwt.Syntax in - let* () = + let* res = Process.exec ( "git", [| @@ -70,14 +63,24 @@ let clone () = Fpath.to_string clone_path; |] ) in + (match res with Ok () -> () | Error (c, s) -> Process.check_status c s); last_commit () let pull () = - let () = Pull.pull := true in let open Lwt.Syntax in - let* () = + let* pul = Process.exec (git_cmd [ "pull"; "-q"; "--ff-only"; "origin"; "master" ]) in + (match pul with + | Ok () -> () + | Error (_, Unix.WEXITED 1) -> + prerr_endline + "\n\ + Pulling from opam repository was not possible. \n\ + We will continue by building\n\ + the package state using the existing local state of opam-repository.\n\ + Some functionality might not work properly.\n" + | Error (c, s) -> Process.check_status c s); last_commit () let fold_dir f acc directory = diff --git a/src/ocamlorg_package/lib/pull.ml b/src/ocamlorg_package/lib/pull.ml deleted file mode 100644 index e45b313b09..0000000000 --- a/src/ocamlorg_package/lib/pull.ml +++ /dev/null @@ -1,5 +0,0 @@ -(* A value to tell if the Opam_repository.pull () function was called. *) -let pull = ref false - -(* Returns true if Opam_repository.pull () was called. *) -let is_pull () = !pull