(* 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 sha_arg = let doc = "Hex SHA-1 of the commit to split." in Arg.(required & pos 0 (some string) None & info [] ~docv:"SHA" ~doc) let run ~repo_path ~sha ~ref_name () = Common.with_repo repo_path @@ fun repo -> match Common.resolve_target_ref repo ref_name with | Error (`Msg m) -> Common.pp_error Fmt.stderr m; `Error (false, m) | Ok ref_name -> ( match Common.parse_sha ~repo sha with | Error (`Msg m) -> Common.pp_error Fmt.stderr m; `Error (false, m) | Ok commit -> Git_eio.Rewrite.split_commit repo ~commit ~ref_name |> Common.report_rewrite_result) let term = Term.( ret (const (fun () repo_path sha ref_name -> run ~repo_path ~sha ~ref_name ()) $ Common.setup $ Common.repo_path $ sha_arg $ Common.target_ref_arg)) let cmd = let info = Cmd.info "split" ~doc:"Split a commit into one commit per top-level directory it modifies." ~man: [ `S Manpage.s_description; `P "Replace $(b,SHA) with a chain of N commits, one per top-level \ entry whose contents differ between $(b,SHA) and its parent. Each \ split commit modifies a single top-level entry; their union \ reproduces the original tree exactly."; `P "Useful for un-bundling an accidental $(b,git commit -a) that \ touched several monorepo packages at once. All split commits \ inherit $(b,SHA)'s author, committer, and original message; \ reword them afterwards with $(b,git-x commit reword)."; `P "Refuses to split root commits, merge commits, or commits that \ only modify zero or one top-level entry."; `S Manpage.s_examples; `P "Split a bundled commit on main:"; `Pre " $(tname) abc1234"; ] in Cmd.v info term