Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465(* 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) URL of the remote repository." in Arg.(required & pos 0 (some string) None & info [] ~docv:"URL" ~doc)
let src_arg = let doc = "Local ref/branch/hash to push. Defaults to HEAD." in Arg.(value & pos 1 string "HEAD" & info [] ~docv:"SRC" ~doc)
let dst_arg = let doc = "Remote ref to update. Defaults to refs/heads/main." in Arg.(value & opt string "refs/heads/main" & info [ "to" ] ~docv:"DST" ~doc)
let force_arg = Arg.( value & flag & info [ "f"; "force" ] ~doc:"Force a non-fast-forward push.")
let run ?on_progress ~repo_path ~url ~src ~dst_ref ~force () = match Git_eio.Fetch.parse_url url with | `Other u when String.starts_with ~prefix:"http" u -> ( Common.with_repo_env repo_path @@ fun ~env ~sw repo -> match Git_net.Push.http ?on_progress ~mono:env#mono_clock ~sw ~net:env#net ~clock:env#clock ~src:repo ~src_hash:src ~dst_ref ~force u with | Ok (h, Up_to_date) -> Fmt.pr "%s already at %a@." dst_ref Git.Hash.pp h; `Ok () | Ok (h, (Created | Fast_forwarded _)) -> Fmt.pr "pushed %a -> %s@." Git.Hash.pp h dst_ref; `Ok () | Ok (h, Forced old) -> Fmt.pr "pushed %a -> %s, replacing %a (forced)@." Git.Hash.pp h dst_ref Git.Hash.pp old; `Ok () | Error (`Rejected (r, remote, local)) -> Fmt.kstr Common.cmd_error "push: %s rejected: the remote holds %a, which %a does not descend \ from; pass --force to replace it" r Git.Hash.pp remote Git.Hash.pp local | Error (`Declined why) -> Fmt.kstr Common.cmd_error "push: the remote refused it: %s" why | Error (`Msg m) -> Common.cmd_error m) | _ -> Common.cmd_error "push: only http(s) URLs are supported"
let term = Term.( ret (const (fun () repo_path url src dst_ref force on_progress -> run ?on_progress ~repo_path ~url ~src ~dst_ref ~force ()) $ Common.setup $ Common.repo_path $ url_arg $ src_arg $ dst_arg $ force_arg $ Common.progress))
let cmd = Cmd.v (Cmd.info "push" ~doc:"Push a ref to a smart-HTTP remote (git-receive-pack)") term