open Camlet_domain let safe_human value = String.escaped value let relation_name = function | Repository_context.Equal -> "equal" | Ahead -> "ahead" | Behind -> "behind" | Diverged -> "diverged" | Missing -> "missing" let agreement_name = function | Repository_context.Equal_surfaces -> "equal" | Out_of_sync -> "out_of_sync" | Incomplete -> "incomplete" | Diverged_surfaces -> "diverged" | Unconfigured -> "unconfigured" let identity_human = function | Repository_context.Tangled (Forge_url.Repository_did did) -> Repository_did.to_string did | Tangled (Repository_path path) -> path | Radicle rid -> Radicle_rid.to_string rid let forge_name = function | Repository_context.Tangled _ -> "tangled" | Radicle _ -> "radicle" let optional_revision = function | None -> "not observed" | Some revision -> Commit_id.short revision let human_surface (surface : Repository_context.surface) = let relation = match surface.relation.kind with | Equal -> "equal" | Ahead -> Printf.sprintf "ahead by %d" surface.relation.ahead | Behind -> Printf.sprintf "behind by %d" surface.relation.behind | Diverged -> Printf.sprintf "diverged (ahead %d, behind %d)" surface.relation.ahead surface.relation.behind | Missing -> "missing" in String.concat "\n" [ Printf.sprintf " %s (%s)" (forge_name surface.identity) (safe_human surface.remote) ; Human_format.field ~indent:4 "Repository" (identity_human surface.identity |> safe_human) ; Human_format.field ~indent:4 "Tracking ref" (match surface.tracking_ref with | None -> "not observed" | Some reference -> Git_ref.to_string reference) ; Human_format.field ~indent:4 "Revision" (optional_revision surface.revision) ; Human_format.field ~indent:4 "Relation" relation ] let human (context : Repository_context.t) = let git_state = if context.git.dirty then "dirty" else "clean" in let branch = match context.git.branch with | None -> "detached" | Some branch -> Git_ref.to_string branch in let jj_line = match context.jj with | None -> Human_format.section "Jujutsu" [ Human_format.field "Status" "not detected" ] | Some jj -> let bookmarks = match jj.bookmarks with | [] -> "none" | values -> values |> List.map safe_human |> String.concat ", " in Human_format.section "Jujutsu" [ Human_format.field "Commit" (Commit_id.short jj.commit) ; Human_format.field "Change" (Human_format.short ~length:12 jj.change_id |> safe_human) ; Human_format.field "Bookmarks" bookmarks ] in let surfaces = match context.surfaces with | [] -> [ Human_format.field "Status" "none configured" ] | values -> List.map human_surface values |> Human_format.blocks in Human_format.document [ Human_format.section "Repository" [ Human_format.field "Root" (safe_human context.git.root) ] ; Human_format.section "Git" [ Human_format.field "HEAD" (Commit_id.short context.git.head) ; Human_format.field "Branch" branch ; Human_format.field "Working tree" git_state ] ; jj_line ; Human_format.section "Surfaces (local tracking refs)" surfaces ; Human_format.section "Agreement" [ Human_format.field "State" (agreement_name context.agreement) ; Human_format.field "Basis" "local tracking refs only" ] ] let relation_json (relation : Repository_context.relation) = `Assoc [ ("kind", `String (relation_name relation.kind)) ; ("ahead", `Int relation.ahead) ; ("behind", `Int relation.behind) ] let identity_json = function | Repository_context.Tangled (Forge_url.Repository_did did) -> ( "tangled" , `Assoc [ ("kind", `String "did") ; ("value", `String (Repository_did.to_string did)) ] ) | Tangled (Repository_path path) -> ("tangled", `Assoc [ ("kind", `String "path"); ("value", `String path) ]) | Radicle rid -> ( "radicle" , `Assoc [ ("kind", `String "rid") ; ("value", `String (Radicle_rid.to_string rid)) ] ) let surface_json (surface : Repository_context.surface) = let forge, repository = identity_json surface.identity in `Assoc [ ("forge", `String forge) ; ("remote", `String surface.remote) ; ("repository", repository) ; ( "tracking_ref" , match surface.tracking_ref with | None -> `Null | Some reference -> `String (Git_ref.to_string reference) ) ; ( "revision" , match surface.revision with | None -> `Null | Some revision -> `String (Commit_id.to_string revision) ) ; ("relation", relation_json surface.relation) ] let jj_json = function | None -> `Null | Some (jj : Repository_context.jj) -> `Assoc [ ("commit", `String (Commit_id.to_string jj.commit)) ; ("change_id", `String jj.change_id) ; ( "bookmarks" , `List (List.map (fun value -> `String value) jj.bookmarks) ) ] let json (context : Repository_context.t) = `Assoc [ ("schema", `String "org.camlet.context.v1") ; ( "result" , `Assoc [ ("repository", `Assoc [ ("root", `String context.git.root) ]) ; ( "git" , `Assoc [ ("head", `String (Commit_id.to_string context.git.head)) ; ( "branch" , match context.git.branch with | None -> `Null | Some branch -> `String (Git_ref.to_string branch) ) ; ("dirty", `Bool context.git.dirty) ] ) ; ("jujutsu", jj_json context.jj) ; ("surfaces", `List (List.map surface_json context.surfaces)) ; ("agreement", `String (agreement_name context.agreement)) ; ("observation", `String "local_tracking_refs") ] ) ] |> Yojson.Safe.to_string