[ANN] ocamlformat pre-commit hook

This is kind of trivial but I figured it might be useful for other people. We created a hook config for using ocamlformat with pre-commit:

pre-commit is a tool that makes it easier to run checks on changed files before commiting them, and this makes it so you can auto-run ocamlformat and ensure no unformatted code gets into your repo.

  1. Install pre-commit like pip install pre-commit
  2. In your repo, add a .pre-commit-config.yaml like:
    ---
    repos:
      - repo: https://github.com/arenadotio/pre-commit-ocamlformat
        rev: master # or pick a commit sha I guess
        hooks:
         - id: ocamlformat
    
  3. Run pre-commit install
  4. Now every time you run git commit (or pre-commit run), it will run every staged OCaml file through ocamlformat and complain if there are any changes:
    $ pre-commit run ocamlformat
    ocamlformat.....Failed
    - hook id: ocamlformat
    - files were modified by this hook
    $ git add .
    $ pre-commit run ocamlformat
    ocamlformat.....Passed
    
3 Likes