Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596(* 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 src = Logs.Src.create "git-x.tree.split" ~doc:"git-x tree split"
module Log = (val Logs.src_log src : Logs.LOG)
let prefix_arg = let doc = "Subtree prefix to split. This is the subdirectory whose history will be \ extracted into a standalone commit chain." in Arg.(required & pos 0 (some string) None & info [] ~docv:"PREFIX" ~doc)
let rev_arg = let doc = "Revision to split from. Defaults to HEAD. Can be a branch name, tag, or \ commit hash." in Arg.(value & opt string "HEAD" & info [ "r"; "rev" ] ~docv:"REV" ~doc)
let report_split_result prefix = function | Error (`Msg msg) -> Common.cmd_error msg | Ok None -> Log.warn (fun m -> m "No commits touch prefix '%s'." prefix); `Ok () | Ok (Some hash) -> Fmt.pr "%s@." (Git.Hash.to_hex hash); `Ok ()
let report_rev_error rev = let shown = "Could not resolve revision '" ^ rev ^ "'." in let error = "could not resolve revision '" ^ rev ^ "'" in Common.pp_error Fmt.stderr shown; `Error (false, error)
let run ~repo_path ~prefix ~rev () = Common.with_repo repo_path @@ fun repo -> match Common.resolve_rev repo rev with | Error (`Msg m) -> Common.pp_error Fmt.stderr m; `Error (false, m) | Ok None -> report_rev_error rev | Ok (Some head_hash) -> Log.info (fun m -> m "Splitting prefix '%s' from %a" prefix Git.Hash.pp head_hash); report_split_result prefix (Git_eio.Subtree.split repo ~prefix ~head:head_hash ())
let term = Term.( ret (const (fun () repo_path prefix rev -> run ~repo_path ~prefix ~rev ()) $ Common.setup $ Common.repo_path $ prefix_arg $ rev_arg))
let cmd = let info = Cmd.info "split" ~doc:"Extract subtree history into standalone commits" ~man: [ `S Manpage.s_description; `P "Extract the history of a subdirectory into a standalone commit \ chain. Each commit that touches the prefix is rewritten so the \ subtree becomes the root tree, preserving author, committer, and \ message."; `P "This is a fast, native replacement for $(b,git subtree split), \ avoiding the per-commit subprocess overhead of the shell script."; `P "A persistent cache is stored in $(b,.git/subtree-cache/<prefix>). \ Subsequent runs only process new commits, making incremental \ splits near-instantaneous."; `S Manpage.s_examples; `P "Split the $(b,ocaml-git) subdirectory:"; `Pre " $(tname) ocaml-git"; `P "Split from a specific branch:"; `Pre " $(tname) --rev feature-branch ocaml-git"; `P "Use in a push workflow (equivalent to $(b,git subtree push)):"; `Pre " git push <remote> \\$(git-x tree split lib):refs/heads/main"; `S "COMPLEXITY"; `P "Let $(i,n) be the number of commits, $(i,d) the prefix depth, and \ $(i,k) the max parents per commit."; `P "First run: O($(i,n) x ($(i,d) + $(i,k))). Cached run: O(1). \ Incremental ($(i,m) new commits): O($(i,m) x ($(i,d) + $(i,k)))."; ] in Cmd.v info term