I have the following pre-commit hook:
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
While in the past I have seen pre-commit skip over non-CPP files, a coworker just showed me an example with many .js files where it did try to format all of them - resulting in a complete mess.
How do I prevent pre-commit or the clang-format itself from running on these files?
Or preferably just have it only run on certain extensions (.c/.cpp/.h/.hpp/.cxx/etc)
Pre-commit has files
but I don't understand how to use it for this (would it be regex?), and I always seem to get something about the formatting wrong. Does it go a nested level under the clang-format line? Or at the same level as it? Or at the uppermost level of "repos"?
Would it just be the following?
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
files: '(some regex here)'
pre-commit configurations have two parts -- a providing repo (.pre-commit-hooks.yaml
) and a consuming repo (.pre-commit-config.yaml
). these configurations have a different format and you can't just paste one of them into the other one blindly
consuming repositories include configurations and code from the remote repository via a repo
entry in the .pre-commit-config.yaml
the settings for the clang-format
hook you're using in your configuration come from here -- the relevant bits for your question are:
- id: clang-format
# ...
'types_or': [c++, c, c#, cuda, java, javascript, json, objective-c, proto]
filtering by types
is outlined in detail in Filtering files with types -- the important parts being:
types
,types_or
, andfiles
are evaluated together withAND
when filtering. Tags withintypes
are also evaluated usingAND
. ... As withfiles
andexclude
, you can also excludetypes
if necessary usingexclude_types
.
in your configuration you can either override the value of types_or
by setting that on the hook with the same id
you're including
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
types_or: [c++, c]
or you can utilize exclude_types
to eliminate them
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
exclude_types: [javascript, json]
disclaimer: I wrote pre-commit