-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support running pre-commit hook against staged files #69
Comments
Would this include the possibility to run |
hi @sirwindfield 👋 The change described in this issue is really about the behavior and context that Right now the commands you specify in your This is fine for most hooks, but for the pre-commit hook it creates a scenario where a user could potentially still commit changes that would fail the pre-commit hook. This only happens when a user stages changes that would fail the hook (for example if your pre-commit hook is running rustfmt, then staging changes with bad formatting), but then fixes the files on disk and commits, without staging the fixes. Contrived example: $ cat src/lib.rs
fn foo( ) { }
$ git add lib.rs
$ echo -e "fn foo() {}" > src/lib.rs
$ git commit -m "oops" This would pass the pre-commit hook, because rustfmt would be executed with the fixed src/lib.rs on disk, but the original/misformatted src/lib.rs version would be committed. The same type of situation could occur with other pre-commit hooks too ( What we'd like to have |
As it relates to rustfmt in general: rusty-hook isn't affiliated with rustfmt in any capacity, but I also do some work over on rustfmt so will share some rustfmt-specific info and personal preferences. An important thing to note with v1.x of rustfmt is that it formats entire projects/crates by default. When running The Neither So if you use [hooks]
pre-commit = "cargo fmt -- --check"
Personally, if I intend to use rustfmt to maintain consistent formatting in my project, I like to enable that up front along with a CI check that will completely prevent misformatted code from sneaking in to the main branch. I also like to use Trying to retroactively add rustfmt to an existing project is a bit trickier, especially if the project is large and/or highly active and/or has a lot of code rustfmt would change. The best approach for this IMO is to incrementally adopt rustfmt within the codebase by leveraging some of rustfmt's config options (like The |
I found this works for me: [hooks]
pre-commit = "cargo +nightly fmt -- --check; cargo +nightly fmt" The check fails, then, re-run |
I have the following piece of code to run migrations, only when there are new/changed files in there: # If there are any staged changes in `migrations/`,
# regenerate models to check whether we forgot to generate them.
git diff --cached --quiet -- "migrations/**" || ./shortcuts/generate.sh |
No description provided.
The text was updated successfully, but these errors were encountered: