-1

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)'
Tyler Shellberg
  • 1,086
  • 11
  • 28

2 Answers2

1

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, and files are evaluated together with AND when filtering. Tags within types are also evaluated using AND. ... As with files and exclude, you can also exclude types if necessary using exclude_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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
0

This is due to the type_or option in this file: https://github.com/pre-commit/mirrors-clang-format/blob/main/.pre-commit-hooks.yaml#L6

-   id: clang-format
    name: clang-format
    description: ''
    entry: clang-format -i
    language: python
    'types_or': [c++, c, c#, cuda, java, javascript, json, objective-c, proto]
    args: ["-style=file"]
    require_serial: false
    additional_dependencies: []
    minimum_pre_commit_version: '2.9.2'

You can override it in your own pre-commit configuration.

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48
  • Could I make it only include those from "files" or do I need to exclude them with "exclude" ? – Tyler Shellberg Oct 20 '22 at 18:17
  • It looks like there is an "exclude_types" option, but I don't see how it's supposed to be formatted. Are the options comma-seperated? Regex? Slash-seperated? Bar-seperated? Grouped with parentheses? Are they extensions or names? – Tyler Shellberg Oct 20 '22 at 18:23
  • @TylerShellberg try it, the error message will tell you - or read the docs on https://pre-commit.com – anthony sottile Oct 20 '22 at 18:28
  • @AnthonySottile Maybe I'm mis-using the website, but I can't find anything on exclude_types other than it exists. It just says "optional) file types to exclude.". Clicking on it doesn't go anywhere. No description of how it works. In any case, it doesn't work anyways, I get "unexpected key". So I'm stuck with 'exclude', which also doesn't seem to explain how to use it. – Tyler Shellberg Oct 20 '22 at 18:31
  • I attempted to make my own configuration that was a copy of the repo's version minus the javascript. However, I get an error that "support for top level list will be removed in a future version". So what, you can't have a configuration that isn't reliant on a repo? – Tyler Shellberg Oct 20 '22 at 18:52