Something went wrong. Try again.
Correctness- and interoperability-oriented Tangled CLI
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192open Camlet_domain
let timeout_s = 10.let max_output_bytes = 1024 * 1024let ( let* ) = Result.bind
let external_error ~code ~message = App_error.make ~category:External_tool ~code ~message
let failure_error operation = function | Camlet_adapter_process.Process.Unavailable -> external_error ~code:"git.unavailable" ~message:"Git is not available on PATH" | Timed_out -> external_error ~code:"git.timeout" ~message:("Git timed out while " ^ operation) | Output_limit_exceeded -> external_error ~code:"git.output_limit" ~message:("Git produced too much output while " ^ operation) | Io_error message -> external_error ~code:"git.io" ~message:("Git failed while " ^ operation ^ ": " ^ message)
let run_raw ~repo ~operation args = let safety_options = [ "--no-optional-locks" ; "-c" ; "core.fsmonitor=false" ; "-c" ; "core.hooksPath=/dev/null" ] in match Camlet_adapter_process.Process.run Git ~args:(("-C" :: repo :: safety_options) @ args) ~timeout_s ~max_output_bytes with | Ok output -> Ok output | Error failure -> Error (failure_error operation failure)
let run ~repo ~operation args = match run_raw ~repo ~operation args with | Error error -> Error error | Ok { status = Exited 0; stdout; _ } -> Ok (String.trim stdout) | Ok _ -> Error (external_error ~code:"git.command_failed" ~message:("Git failed while " ^ operation))
let commit_id ~operation value = match Commit_id.of_string value with | Ok commit -> Ok commit | Error _ -> Error (external_error ~code:"git.invalid_output" ~message:("Git returned an invalid commit ID while " ^ operation))
let git_ref ~operation value = match Git_ref.of_string value with | Ok reference -> Ok reference | Error _ -> Error (external_error ~code:"git.invalid_output" ~message:("Git returned an invalid ref while " ^ operation))
let parse_count ~operation value = match int_of_string_opt value with | Some count when count >= 0 -> Ok count | _ -> Error (external_error ~code:"git.invalid_output" ~message:("Git returned an invalid revision count while " ^ operation))
let comparison ~repo ~tracking_ref = let tracking_ref_text = Git_ref.to_string tracking_ref in match run_raw ~repo ~operation:"reading a tracking reference" [ "rev-parse"; "--verify"; "--quiet"; tracking_ref_text ] with | Error error -> Error error | Ok { status = Exited 1; _ } -> Ok None | Ok { status = Exited 0; stdout; _ } -> ( let* commit = commit_id ~operation:"reading a tracking reference" (String.trim stdout) in let* counts = run ~repo ~operation:"comparing repository histories" [ "rev-list" ; "--left-right" ; "--count" ; "HEAD..." ^ tracking_ref_text ] in match String.split_on_char '\t' counts with | [ ahead; behind ] -> let* ahead = parse_count ~operation:"comparing repository histories" ahead in let* behind = parse_count ~operation:"comparing repository histories" behind in Ok (Some Camlet_application.Context_ports.{ commit; ahead; behind }) | _ -> Error (external_error ~code:"git.invalid_output" ~message:"Git returned invalid history comparison output")) | Ok _ -> Error (external_error ~code:"git.command_failed" ~message:"Git failed while reading a tracking reference")
let inspect_remote ~repo ~branch name = let* fetch_url = run ~repo ~operation:"reading a remote URL" [ "remote"; "get-url"; name ] in let* push_url = run ~repo ~operation:"reading a push URL" [ "remote"; "get-url"; "--push"; name ] in let* tracking_ref, comparison = match branch with | None -> Ok (None, None) | Some branch -> let tracking_ref_text = "refs/remotes/" ^ name ^ "/" ^ Git_ref.to_string branch in let* tracking_ref = git_ref ~operation:"constructing a tracking ref" tracking_ref_text in let* comparison = comparison ~repo ~tracking_ref in Ok (Some tracking_ref, comparison) in Ok Camlet_application.Context_ports. { name; fetch_url; push_url; tracking_ref; comparison }
let rec inspect_remotes ~repo ~branch = function | [] -> Ok [] | name :: rest -> ( match inspect_remote ~repo ~branch name with | Error error -> Error error | Ok remote -> Result.map (fun remotes -> remote :: remotes) (inspect_remotes ~repo ~branch rest))
let inspect repo = let* root = match run_raw ~repo ~operation:"locating the repository" [ "rev-parse"; "--show-toplevel" ] with | Error error -> Error error | Ok { status = Exited 0; stdout; _ } -> Ok (String.trim stdout) | Ok _ -> Error (App_error.make ~category:Configuration ~code:"git.not_repository" ~message:"The requested path is not inside a Git repository") in let* head_text = run ~repo:root ~operation:"reading HEAD" [ "rev-parse"; "HEAD" ] in let* head = commit_id ~operation:"reading HEAD" head_text in let* branch = match run_raw ~repo:root ~operation:"reading the current branch" [ "symbolic-ref"; "--quiet"; "--short"; "HEAD" ] with | Error error -> Error error | Ok { status = Exited 0; stdout; _ } -> Result.map Option.some (git_ref ~operation:"reading the current branch" (String.trim stdout)) | Ok { status = Exited 1; _ } -> Ok None | Ok _ -> Error (external_error ~code:"git.command_failed" ~message:"Git failed while reading the current branch") in let* status_text = run ~repo:root ~operation:"reading repository status" [ "status"; "--porcelain=v1"; "--untracked-files=normal" ] in let* remote_text = run ~repo:root ~operation:"listing remotes" [ "remote" ] in let remote_names = remote_text |> String.split_on_char '\n' |> List.filter (fun name -> name <> "") in let* remotes = inspect_remotes ~repo:root ~branch remote_names in Ok Camlet_application.Context_ports. { root; head; branch; dirty = status_text <> ""; remotes }