treefmt
treefmt copied to clipboard
Add --pre-commit mode
Some developers like to invoke the formatters just before committing the code.
When invoked, it should only format the code that has been staged for the commit and not touch the rest.
This can be done like this: https://stackoverflow.com/a/26911078 And also probably check for unresolved merge commits before and abort if that's the case.
I've ended up using pre-commit
with this:
repos:
- repo: local
hooks:
- id: treefmt
name: treefmt
entry: treefmt
language: system
always_run: true
Seems to work well!
What behaviour does it have if the commit is not fully formatted? Is it updating the commit with the fixed code, or aborting with an error?
It formats the code, and then because files have changed pre-commit
aborts the commit.
A .pre-commit-hooks.yaml
file could also be added to this repository such as
- id: treefmt
name: treefmt
description: Format code with treefmt
entry: treefmt
language: rust
minimum_pre_commit_version: 1.18.1
so that pre-commit
could download and install (with Cargo and from crates) treefmt
with
repos:
- repo: https://github.com/numtide/treefmt
rev: master
hooks:
- id: treefmt
The pre-commit framework is a bit heavy for my taste so I wrote a bash script to replace it (see #166). I think both approaches are valid, depending on if you use nix to pull the dependencies or not. If you don't then it's nice to have all the machinery that's provided by the pre-commit python code.
@zimbatm We do use it though, and I've personally been waiting for Lefthook to take over, but it hasn't happened yet, and pre-commit is the king still. Adding a simple YAML file here in the repo would solve elegantly the problem for those of us having to use pre-commit for the time being.