Something went wrong. Try again.
My own corner of monopam
Something went wrong. Try again.
2.9 kB · 72 lines
OCaml
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273open Cmdliner
let install_one_dir ctx ~dry_run ~force ~hooks ~count ~already_configured s d = let needs_fmt = hooks.Precommit.fmt && not s.Precommit.has_pre_commit in let needs_merlint = hooks.Precommit.merlint && not s.Precommit.has_merlint in let needs_ai = hooks.Precommit.ai && not s.Precommit.has_commit_msg in if needs_fmt || needs_merlint || needs_ai then begin Common.or_die (Precommit.init_in_dir ctx ~dry_run ~force ~hooks d); incr count; if dry_run then Common.info "Would initialise %a" Fmt.(styled `Bold string) d else Common.success "Initialised %a" Fmt.(styled `Bold string) d end else incr already_configured
let init_one_dir ctx ~dry_run ~force ~hooks ~count ~skipped_not_ocaml ~already_configured d = let s = Precommit.status_in_dir ctx d in if not s.is_git_repo then () else if not (force || s.is_ocaml_project) then incr skipped_not_ocaml else install_one_dir ctx ~dry_run ~force ~hooks ~count ~already_configured s d
let init_impl ctx ~dry_run ~force hooks dirs = let dirs = Common.collect_dirs ctx dirs in let count = ref 0 in let skipped_not_ocaml = ref 0 in let already_configured = ref 0 in List.iter (init_one_dir ctx ~dry_run ~force ~hooks ~count ~skipped_not_ocaml ~already_configured) dirs; if !count > 0 then Common.success "Processed %d director%s" !count (if !count = 1 then "y" else "ies") else if !already_configured > 0 then Common.info "All directories already have hooks installed" else if !skipped_not_ocaml > 0 then begin Common.info "No OCaml projects found (use --force to install anyway)"; exit 1 end
let cmd = let doc = "Initialise pre-commit hooks for OCaml projects." in let man = [ `S Manpage.s_description; `P "Install git hooks that run $(b,dune fmt) and $(b,merlint --build .) \ before commit and remove Claude attribution from commit messages. \ Also creates $(b,.ocamlformat) if missing (unless $(b,--force) is \ used)."; `S Manpage.s_examples; `P "Initialise hooks in the current directory:"; `Pre " precommit init"; `P "Initialise hooks in all projects under src/:"; `Pre " precommit init src/"; `P "Preview what would be done:"; `Pre " precommit init -n"; `P "Install only the AI attribution hook in a non-OCaml project:"; `Pre " precommit init -f --hooks ai"; `P "Install only the dune fmt hook:"; `Pre " precommit init --hooks fmt"; ] in let info = Cmd.info "init" ~doc ~man in Cmd.v info Term.( const (fun chdir dry_run force hooks dirs () -> Common.with_ctx chdir (fun ctx -> init_impl ctx ~dry_run ~force hooks dirs)) $ Common.chdir $ Common.dry_run $ Common.force $ Common.hooks $ Common.dirs $ Common.setup)