Skip to content

pre-commit fail even if the problem is fixed automatically #1434

Closed
@bearomorphism

Description

@bearomorphism

Description

Every time I try to commit something and hope the format should be fixed automatically, the pre-commit Format fails.

Yes, the format issue is fixed, but the commit fails.

Then I either have to git commit -a or cz c -a --retry to try again.

I haven't encountered similar issues when I worked on npm projects with husky pre-commit hooks + something like prettier --write.

commitizen-py3.13➜  commitizen git:(tmp) ✗ git commit -a
Check hooks apply to the repository.......................(no files to check)Skipped
Check for useless excludes................................(no files to check)Skipped
check vcs permalinks..........................................................Passed
fix end of files..............................................................Passed
trim trailing whitespace......................................................Passed
debug statements (python).....................................................Passed
don't commit to branch........................................................Passed
check for merge conflicts.....................................................Passed
check toml................................................(no files to check)Skipped
check yaml................................................(no files to check)Skipped
detect private key............................................................Passed
blacken-docs..................................................................Passed
Run codespell to check for common misspellings in files.......................Passed
Format........................................................................Failed
- hook id: format
- files were modified by this hook

Poe => ruff check --fix commitizen tests
All checks passed!
Poe => ruff format commitizen tests
1 file reformatted, 108 files left unchanged

Linters.......................................................................Passed

Image

Steps to reproduce

  1. Switch to an unprotected branch, ex git switch -c tmp
  2. Randomly insert some blank lines in a .py file to break the format
  3. Run git commit -am "doc: xyz" --allow-empty

Current behavior

The format is fixed, but no new git commit.

Desired behavior

The format should be fixed and there should be a new commit doc: xyz with the fixed change.

Screenshots

No response

Environment

Commitizen Version: 4.7.2
Python Version: 3.13.2 (main, May 12 2025, 02:57:09) [Clang 17.0.0 (clang-1700.0.13.3)]
Operating System: Darwin

Activity

bearomorphism

bearomorphism commented on May 25, 2025

@bearomorphism
ContributorAuthor

ChatGPT's response:


Thanks for the detailed report. You're encountering a very common pre-commit behavior that surprises many developers transitioning from Husky + Prettier (--write) setups in Node.js projects.


💡 The Core Reason

pre-commit intentionally aborts the commit if a hook modifies files — even if it's just formatting.

This is by design: it wants you to review the changes before committing.


Solutions (Pick one)

✅ Option 1: Use git commit -a or cz c -a consistently

You're already doing this — it's a safe and simple solution:

git commit -a -m "doc: xyz"
# or if using Commitizen:
cz c -a

Because -a auto-adds modified files (e.g., after ruff format), this is often enough.


✅ Option 2: Use pre-commit's --hook-stage manual trick (not ideal for Git hooks)

You could disable auto-commits on pre-commit and run them manually before committing:

pre-commit run --all-files
git commit -am "doc: xyz"

But this breaks the spirit of auto-format-on-commit.


✅ Option 3 (Best UX): Use a commit wrapper that retries if formatting occurs

You can write a small script like czc:

#!/bin/bash

# Commitizen commit with auto-format retry
cz c -a
if [ $? -ne 0 ]; then
  echo "🔁 Format hook modified files, retrying commit..."
  git add -u
  cz c -a
fi

Make it executable and use ./czc instead of cz c.


✅ Option 4: Use staged formatting (like lint-staged)

This emulates the Husky+Prettier behavior more closely:

This adds complexity and doesn't integrate well with Python tooling like poe, ruff, or black.


🔁 Why pre-commit Doesn't Auto-Commit?

By design:

  • It doesn't assume you want to commit auto-fixed files blindly.
  • It prevents accidental commits with unintended changes (e.g., during a rebase or hotfix).

This is different from Husky's philosophy — which optimizes for seamlessness rather than caution.


✅ TL;DR Fix Recommendation

If you're happy with auto-fix-on-commit:

🔧 Just run:

cz c -a

Or define an alias/script:

alias cz-auto='cz c -a || (git add -u && cz c -a)'

Would you like me to help convert this into a shell alias or Git hook that matches your desired workflow exactly?

Lee-W

Lee-W commented on May 25, 2025

@Lee-W
Member

It's more like a feature/issue of Ruff than a commitizen one, and I'm -1 to this setup. TBH, I'm quite surprised prettier is doing something like that 🤔 I still want the developer to do the manual check again, so I'll mark it as won't fix and close it unless @woile or @noirbizarre think otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      `pre-commit` fail even if the problem is fixed automatically · Issue #1434 · commitizen-tools/commitizen