(* Copyright (c) 2024-2026 Thomas Gazagnaire 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) URL of the remote repository." in Arg.(required & pos 0 (some string) None & info [] ~docv:"URL" ~doc) let ref_arg = let doc = "Ref to fetch (branch, tag, HEAD, or hash). Defaults to HEAD." in Arg.(value & pos 1 string "HEAD" & info [] ~docv:"REF" ~doc) let fetch_ssh ?on_progress ~repo_path ~ref_name ~insecure (e : Common.ssh_endpoint) = match Common.load_ssh_identity () with | Error (`Msg m) -> Common.cmd_error m | Ok key -> ( Common.with_repo_env repo_path @@ fun ~env ~sw repo -> 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 -> ( match Git_ssh.fetch ?on_progress ~sw ~net ~mono ~authenticator ?port:e.port ~user:e.user (`Pubkey key) ~host:e.host ~dst:repo ~ref_name e.path with | Ok h -> Fmt.pr "fetched %s -> %a@." ref_name Git.Hash.pp h; `Ok () | Error e -> Fmt.kstr Common.cmd_error "%a" Git_ssh.pp_error e)) let run ?on_progress ~repo_path ~url ~ref_name ~insecure () = match Common.parse_ssh_url url with | Some e -> fetch_ssh ?on_progress ~repo_path ~ref_name ~insecure e | None -> ( match Git_eio.Fetch.parse_url url with | `File _ -> Common.cmd_error "fetch: use a local source via 'git-x' file ops" | `Other u when String.starts_with ~prefix:"http" u -> ( Common.with_repo_env repo_path @@ fun ~env ~sw repo -> match Git_net.Fetch.http ?on_progress ~mono:env#mono_clock ~sw ~net:env#net ~clock:env#clock ~dst:repo ~ref_name u with | Ok h -> Fmt.pr "fetched %s -> %a@." ref_name Git.Hash.pp h; `Ok () | Error (`Msg m) -> Common.cmd_error m (* Name the transport failure rather than letting it escape as a backtrace. *) | exception (Eio.Io _ as exn) -> Fmt.kstr Common.cmd_error "%a" Git_net.Fetch.pp_failure (Git_net.Fetch.classify exn)) | `Other _ -> Common.cmd_error "fetch: only http(s) and ssh:// URLs are supported") let term = Term.( ret (const (fun () repo_path url ref_name insecure on_progress -> run ?on_progress ~repo_path ~url ~ref_name ~insecure ()) $ Common.setup $ Common.repo_path $ url_arg $ ref_arg $ Common.insecure_ssh_host_key $ Common.progress)) let cmd = Cmd.v (Cmd.info "fetch" ~doc:"Fetch a ref from a smart-HTTP remote into FETCH_HEAD") term