(* 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 base_arg = let doc = "Exclusive lower bound of the range to sort -- the tool reorders the \ commits in $(b,($(docv), ref head])." in Arg.(required & pos 0 (some string) None & info [] ~docv:"BASE" ~doc) (* Mutually exclusive axis flags; [run] enforces exactly one. *) let by_dir_flag = let doc = "Cluster commits by the top-level directory they touch (relative to each \ commit's parent)." in Arg.(value & flag & info [ "by-dir" ] ~doc) let by_prefix_flag = let doc = "Cluster commits by the subject prefix before $(b,:) (conventional commits \ style: $(b,feat:), $(b,fix:), ...)." in Arg.(value & flag & info [ "by-prefix" ] ~doc) let by_author_flag = let doc = "Cluster commits by author (name and email)." in Arg.(value & flag & info [ "by-author" ] ~doc) let by_trailer_opt = let doc = "Cluster commits by the value of the given trailer (e.g. \ $(b,Co-Authored-By))." in Arg.(value & opt (some string) None & info [ "by-trailer" ] ~docv:"NAME" ~doc) let pick_axis ~by_dir ~by_prefix ~by_author ~by_trailer = let candidates = [ (if by_dir then Some Git_eio.Rewrite.Dir else None); (if by_prefix then Some Git_eio.Rewrite.Prefix else None); (if by_author then Some Git_eio.Rewrite.Author else None); Option.map (fun n -> Git_eio.Rewrite.Trailer n) by_trailer; ] in let chosen = List.filter_map Fun.id candidates in match chosen with | [] -> Error (`Msg "sort requires one of --by-dir, --by-prefix, --by-author, \ --by-trailer=NAME") | [ a ] -> Ok a | _ -> Error (`Msg "sort accepts exactly one --by- flag; pick one axis at a time") let run ~repo_path ~base_sha ~by_dir ~by_prefix ~by_author ~by_trailer ~ref_name () = Common.with_repo repo_path @@ fun repo -> let open Result.Syntax in match let* ref_name = Common.resolve_target_ref repo ref_name in let* axis = pick_axis ~by_dir ~by_prefix ~by_author ~by_trailer in let* base = Common.parse_sha ~repo base_sha in Ok (Git_eio.Rewrite.sort_commits repo ~base ~axis ~ref_name ()) with | Error (`Msg m) -> Common.cmd_error m | Ok result -> Common.report_rewrite_result result let term = Term.( ret (const (fun () repo_path base_sha by_dir by_prefix by_author by_trailer ref_name -> run ~repo_path ~base_sha ~by_dir ~by_prefix ~by_author ~by_trailer ~ref_name ()) $ Common.setup $ Common.repo_path $ base_arg $ by_dir_flag $ by_prefix_flag $ by_author_flag $ by_trailer_opt $ Common.target_ref_arg )) let cmd = let info = Cmd.info "sort" ~doc:"Reorder a commit range so same-key commits cluster adjacent." ~man: [ `S Manpage.s_description; `P "Stable-reorders the commits in $(b,(BASE, ref head]) by a \ user-chosen axis. The relative order within each cluster is \ preserved. Reordered commits are replayed onto their predecessor \ via an in-memory 3-way merge; a conflict aborts the sort without \ touching the object database or the ref."; `P "Forms the middle step of the $(b,split) -> $(b,sort) -> \ $(b,squash) pipeline for tidying a branch: split produces one \ atomic commit per directory, sort clusters same-directory \ commits, squash folds each cluster into a single commit."; `S Manpage.s_examples; `P "Cluster commits by top-level directory:"; `Pre " $(tname) 1ef1dd0 --by-dir"; `P "Cluster by conventional-commits prefix:"; `Pre " $(tname) 1ef1dd0 --by-prefix"; `P "Cluster by a trailer (e.g. Co-Authored-By):"; `Pre " $(tname) 1ef1dd0 --by-trailer Co-Authored-By"; ] in Cmd.v info term