treefmt icon indicating copy to clipboard operation
treefmt copied to clipboard

Add --pre-commit mode

Open zimbatm opened this issue 3 years ago • 6 comments

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.

zimbatm avatar Feb 27 '21 16:02 zimbatm

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!

ocharles avatar May 11 '22 15:05 ocharles

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?

zimbatm avatar May 11 '22 17:05 zimbatm

It formats the code, and then because files have changed pre-commit aborts the commit.

ocharles avatar May 11 '22 17:05 ocharles

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

loicreynier avatar May 11 '22 17:05 loicreynier

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 avatar May 12 '22 09:05 zimbatm

@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.

nikolay avatar Oct 07 '23 01:10 nikolay