Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125(* Copyright (c) 2024-2026 Thomas Gazagnaire <thomas@gazagnaire.org>
Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. *)
open Cmdliner
let url_arg = let doc = "Smart-HTTP(S) or ssh:// URL of the remote repository, or the path of one \ on this machine." in Arg.(required & pos 0 (some string) None & info [] ~docv:"URL" ~doc)
let dir_arg = let doc = "Directory to clone into. Defaults to the repository name derived from URL." in Arg.(value & pos 1 (some string) None & info [] ~docv:"DIR" ~doc)
let ref_arg = let doc = "Ref to check out (branch, tag, HEAD, or hash). Defaults to HEAD." in Arg.(value & pos 2 string "HEAD" & info [] ~docv:"REF" ~doc)
let depth_arg = let doc = "Truncate history to $(docv) commits from the fetched tip, recording the \ cut in .git/shallow. Cheap when only the tip's tree is wanted." in Arg.(value & opt (some int) None & info [ "depth" ] ~docv:"N" ~doc)
(* The last path segment of [url], minus trailing slashes and a ".git" suffix. *)let dir_of_url url = let url = let n = ref (String.length url) in while !n > 0 && url.[!n - 1] = '/' do decr n done; String.sub url 0 !n in let base = match String.rindex_opt url '/' with | Some i -> String.sub url (i + 1) (String.length url - i - 1) | None -> ( match String.rindex_opt url ':' with | Some i -> String.sub url (i + 1) (String.length url - i - 1) | None -> url) in if Filename.check_suffix base ".git" then Filename.chop_suffix base ".git" else base
(* [Git_eio.Repository.init] mkdir's the whole path and needs an absolute one. *)let absolute dir = Result.map (Fpath.append (Fpath.v (Sys.getcwd ()))) (Fpath.of_string dir)
let finish ~dir ~url = function | Error f -> Fmt.kstr Common.cmd_error "%a" Git_net.Fetch.pp_failure f | Ok fetched -> Fmt.pr "Cloned %s into %a@." url Fpath.pp dir; Fmt.pr "HEAD is now at %a@." Git.Hash.pp fetched; `Ok ()
let clone_ssh ~dir ~ref_name ~url ~insecure ~depth (e : Common.ssh_endpoint) = match Common.load_ssh_identity () with | Error (`Msg m) -> Common.cmd_error m | Ok key -> ( Common.with_env @@ fun ~env ~sw ~pool -> let net = Eio.Stdenv.net env in let mono = Eio.Stdenv.mono_clock env in let fs = Eio.Stdenv.fs env in match Common.ssh_authenticator ~fs ~insecure ~host:e.host ?port:e.port () with | Error (`Msg m) -> Common.cmd_error m | Ok authenticator -> let fetch dst = Result.map_error (function | `Msg _ as m -> Git_net.Fetch.failure_of_msg m (* As a TLS handshake that fails, no session was made: a refused host key, or an authentication the server ended. *) | (`Host_key _ | `Auth _) as e -> Fmt.kstr (fun s -> Git_net.Fetch.Unreachable s) "%a" Git_ssh.pp_error e) (Git_ssh.fetch ~sw ~net ~mono ~authenticator ?port:e.port ?depth ~user:e.user (`Pubkey key) ~host:e.host ~dst ~ref_name e.path) in finish ~dir ~url (Git_net.Clone.into ~pool ~sw ~fs ~dir ~fetch ()))
let clone_url ~dir ~ref_name ~url ~depth = Common.with_env @@ fun ~env ~sw ~pool -> finish ~dir ~url (Git_net.Clone.v ~mono:env#mono_clock ~pool ?depth ~ref_name ~sw ~net:env#net ~clock:env#clock ~fs:(Eio.Stdenv.fs env) ~dir url)
let run ~repo_dir ~url ~ref_name ~insecure ~depth () = match absolute (match repo_dir with Some d -> d | None -> dir_of_url url) with | Error (`Msg m) -> Common.cmd_error m | Ok dir -> ( match Common.parse_ssh_url url with | Some e -> clone_ssh ~dir ~ref_name ~url ~insecure ~depth e | None -> clone_url ~dir ~ref_name ~url ~depth)
let term = Term.( ret (const (fun () url repo_dir ref_name insecure depth -> run ~repo_dir ~url ~ref_name ~insecure ~depth ()) $ Common.setup $ url_arg $ dir_arg $ ref_arg $ Common.insecure_ssh_host_key $ depth_arg))
let cmd = Cmd.v (Cmd.info "clone" ~doc: "Clone a smart-HTTP or ssh:// remote, or a repository on this \ machine, into a new directory") term