Home > Software design >  Pre-commit complains that "Stashed changes conflicted with hook auto-fixes" despite manual
Pre-commit complains that "Stashed changes conflicted with hook auto-fixes" despite manual

Time:10-13

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?

CodePudding user response:

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

CodePudding user response:

If there are indeed some local moifications that aren't staged, you can try the following workaround :

# stash your modifications, but keep your index as is:
git stash --keep

git commit

# get back the content of your repo as it was before you ran `git stash`,
# without trying to fix any conflicts
git restore -s=stash .

#  *warning* : using `git restore` to restore the worktree is one of those few
# destructive commands, that is allowed to discard changes and delete files
# from your disk without them being stored in git (a lot like
# 'git reset --hard').
# In the current situation, there shouldn't be any uncommitted changes, though.

# you shouldn't need that stash anymore
git stash drop
  • Related