3

I am using pre-commit (version 2.20.0) for my C++ project with this hook:

repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
  rev: v14.0.6
  hooks:
  - id: clang-format

I just staged a few lines each from a few different .cpp/.h/cmakelists files. When I try to commit those changes, I get the following error from pre-commit:

[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.

I am confused by this. Does pre-commit not allow me to partially commit files, or to have unstaged changes at all when committing?

All the files, both staged and un-staged, have been formatted by clang-format. I've manually double-checked this.

Edit for clarity:

If I run pre-commit run --files [filename] on each file (staged and unstaged) all report back either "Passed" or "Skipped" (for the non-cpp files). If all files pass, why is there a problem?

Tyler Shellberg
  • 1,086
  • 11
  • 28

2 Answers2

11

the first part of your output is:

[WARNING] Unstaged files detected.

this means you had a partially staged commit -- this is fine, pre-commit supports this well

then after that a hook made changes:

- files were modified by this hook

these changes must've conflicted with the unstaged parts because then there's a message such as:

[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...

meaning you'll need to either make sure the unstaged parts are formatted properly (pre-commit run --files whatever.cpp) or stage those as well so they also get formatted


disclaimer: I created pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • I have manually formatted all the staged and unstaged files within visual studio using a clang-format keybind. They should all be formatted. That's the crux of my confusion. – Tyler Shellberg Oct 12 '22 at 19:01
  • If I run ```pre-commit run --files``` on each file, every one is either skipped (cmakelists.txt) or reports "Passed". If all of them passed, why is there a problem? – Tyler Shellberg Oct 12 '22 at 19:08
  • 1
    Sorry, I think I realized what's happening. I was hoping I could commit the logical parts of my changes first, and then commit the formatting changes afterwards. I'm assuming the hook is checking that the version that I commit already has all formatting changes. So I can't do it in that order. – Tyler Shellberg Oct 12 '22 at 19:44
  • 1
    the tool doesn't allow you to commit misformatted things -- that's kinda the point – anthony sottile Oct 12 '22 at 20:46
  • This is really quite a burdensome limitation. – AdamC Mar 31 '23 at 16:35
  • @AdamC why do you want to intentionally commit things which don't pass formatting? – anthony sottile Mar 31 '23 at 17:20
  • I didn't. I intentionally wanted to make separate commits, with more specific commit comments on each. – AdamC Apr 03 '23 at 01:45
  • nothing prevents you from doing that – anthony sottile Apr 03 '23 at 14:19
2

from the terminal, note:

  • files were modified by this hook

changes have been automatically made by precommit hook. All you need to do is stage the files again basically..

git add <filename> or git add .

then re-commit and you will see your pre-commit hook passing this time.

tink
  • 290
  • 1
  • 12