Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172(* 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 ref_arg = let doc = "Branch to pull. Defaults to HEAD." in Arg.(value & pos 1 string "HEAD" & info [] ~docv:"REF" ~doc)
let pp_kept ppf path = Fmt.pf ppf " %a %s (kept your version; upstream also changed it -- reconcile with \ git diff)@." Console.(Style.styled Style.(fg Color.yellow) Fmt.string) "kept" path
(* Report a fast-forward pull, listing the files left untouched. *)let report ~fetched = function | Git_eio.Pull.Up_to_date -> Fmt.pr "Already up to date.@." | Git_eio.Pull.Fast_forwarded (r : Git_eio.Checkout.report) -> Fmt.pr "Fast-forwarded to %a@." Git.Hash.pp fetched; if r.applied <> [] || r.deleted <> [] then Fmt.pr " %d updated, %d removed@." (List.length r.applied) (List.length r.deleted); List.iter (pp_kept Fmt.stdout) r.kept | Git_eio.Pull.Not_fast_forward { local; fetched } -> Fmt.pr "Not a fast-forward: %a is not an ancestor of %a@." Git.Hash.pp local Git.Hash.pp fetched; Fmt.pr "Fetched to FETCH_HEAD; reconcile manually.@."
(* Fetch, then fast-forward the current branch and working tree. A locally modified file is never overwritten. *)let run ~repo_path ~url ~ref_name () = 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.Fetch.http ~mono:env#mono_clock ~sw ~net:env#net ~clock:env#clock ~dst:repo ~ref_name u with | Error (`Msg m) -> Common.cmd_error m (* As in [fetch]: name the transport failure. *) | exception (Eio.Io _ as exn) -> Fmt.kstr Common.cmd_error "%a" Git_net.Fetch.pp_failure (Git_net.Fetch.classify exn) | Ok fetched -> ( match Git_eio.Pull.fast_forward repo ~fetched with | Error (`Msg m) -> Common.cmd_error m | Ok outcome -> report ~fetched outcome; `Ok ())) | _ -> Common.cmd_error "pull: only http(s) URLs are supported"
let term = Term.( ret (const (fun () repo_path url ref_name -> run ~repo_path ~url ~ref_name ()) $ Common.setup $ Common.repo_path $ url_arg $ ref_arg))
let cmd = Cmd.v (Cmd.info "pull" ~doc:"Fetch from a smart-HTTP remote and fast-forward the current branch") term