{"id":16865413,"url":"https://github.com/pocc/pre-commit-hooks","last_synced_at":"2025-04-13T00:49:23.431Z","repository":{"id":40250805,"uuid":"182405929","full_name":"pocc/pre-commit-hooks","owner":"pocc","description":"C/C++ hooks to integrate with pre-commit","archived":false,"fork":false,"pushed_at":"2024-03-20T15:32:01.000Z","size":367,"stargazers_count":349,"open_issues_count":19,"forks_count":70,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-13T00:49:18.879Z","etag":null,"topics":["clang","clang-format","clang-tidy","cppcheck","cpplint","iwyu","oclint","pre-commit-hooks","uncrustify"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pocc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-20T12:54:26.000Z","updated_at":"2025-04-01T06:29:56.000Z","dependencies_parsed_at":"2024-06-21T16:53:16.993Z","dependency_job_id":"b79ed72e-f8ab-4b0a-8e60-0175a7c8bfb6","html_url":"https://github.com/pocc/pre-commit-hooks","commit_stats":{"total_commits":146,"total_committers":8,"mean_commits":18.25,"dds":0.08904109589041098,"last_synced_commit":"336fdd7c3cab698ead0b1c95157b9e74d3906b62"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocc%2Fpre-commit-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocc%2Fpre-commit-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocc%2Fpre-commit-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocc%2Fpre-commit-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pocc","download_url":"https://codeload.github.com/pocc/pre-commit-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650436,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clang","clang-format","clang-tidy","cppcheck","cpplint","iwyu","oclint","pre-commit-hooks","uncrustify"],"created_at":"2024-10-13T14:47:00.315Z","updated_at":"2025-04-13T00:49:23.408Z","avatar_url":"https://github.com/pocc.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pre-commit hooks\n\n![Ubuntu Build](https://github.com/pocc/pre-commit-hooks/actions/workflows/gh_actions_ubuntu.yml/badge.svg)\n![Macos Build](https://github.com/pocc/pre-commit-hooks/actions/workflows/gh_actions_macos.yml/badge.svg)\n![Windows Build](https://github.com/pocc/pre-commit-hooks/actions/workflows/gh_actions_windows.yml/badge.svg)\n\n\nThis is a [pre-commit](https://pre-commit.com) hooks repo that\nintegrates two C/C++ code formatters:\n\u003e [clang-format](https://clang.llvm.org/docs/ClangFormatStyleOptions.html),\n[uncrustify](http://uncrustify.sourceforge.net/),\n\nand five C/C++ static code analyzers:\n\u003e [clang-tidy](https://clang.llvm.org/extra/clang-tidy/),\n[oclint](http://oclint.org/),\n[cppcheck](http://cppcheck.sourceforge.net/),\n[cpplint](https://github.com/cpplint/cpplint),\n[include-what-you-use](https://github.com/include-what-you-use/include-what-you-use)\n\nThis repo's hooks do more than passthrough arguments to provide these features:\n\n* Relay correct pass/fail to pre-commit, even when some commands exit 0 when they should not. Some versions of oclint, clang-tidy, and cppcheck have this behavior.\n* Honor `--` arguments, which pre-commit [has problems with](https://github.com/pre-commit/pre-commit/issues/1000)\n* Optionally [enforce a command version](https://github.com/pocc/pre-commit-hooks#special-flags-in-this-repo) so your team gets code formatted/analyzed the same way\n* Formatters clang-format and uncrustify will error with diffs of what has changed\n\n## Example Usage\n\nWith this [err.c](tests/test_repo/err.c)\n\n```c\n#include \u003cstdio.h\u003e\nint main(){int i;return;}\n```\n\nand using this `.pre-commit-config.yaml`:\n\n```yaml\nfail_fast: false\nrepos:\n  - repo: https://github.com/pocc/pre-commit-hooks\n    rev: master\n    hooks:\n      - id: clang-format\n        args: [--style=Google]\n      - id: clang-tidy\n      - id: oclint\n      - id: uncrustify\n      - id: cppcheck\n      - id: cpplint\n      - id: include-what-you-use\n```\n\nAll seven linters should fail on commit with these messages.\nFull text is at [media/all_failed.txt](media/all_failed.txt).\n\n\u003cdetails\u003e\n  \u003csummary\u003eclang-format error (indentation)\u003c/summary\u003e\n\n```\nclang-format.............................................................Failed\n- hook id: clang-format\n- exit code: 1\n\nerr.c\n====================\n--- original\n\n+++ formatted\n\n@@ -1,3 +1,6 @@\n\n #include \u003cstdio.h\u003e\n-int main(){int i;return;}\n+int main() {\n+  int i;\n+  return;\n+}\n\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003eclang-tidy error (non-void main should return a value)\u003c/summary\u003e\n\n```\nclang-tidy...............................................................Failed\n- hook id: clang-tidy\n- exit code: 1\n\n/tmp/temp/err.c:2:18: error: non-void function 'main' should return a value [clang-diagnostic-return-type]\nint main(){int i;return;}\n                 ^\n1 error generated.\nError while processing /tmp/temp/err.c.\nFound compiler error(s).\n\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003eoclint error (non-void main should return a value)\u003c/summary\u003e\n\n```\noclint...................................................................Failed\n- hook id: oclint\n- exit code: 6\n\nCompiler Errors:\n(please be aware that these errors will prevent OCLint from analyzing this source code)\n\n/tmp/temp/err.c:2:18: non-void function 'main' should return a value\n\nClang Static Analyzer Results:\n\n/tmp/temp/err.c:2:18: non-void function 'main' should return a value\n\n\nOCLint Report\n\nSummary: TotalFiles=0 FilesWithViolations=0 P1=0 P2=0 P3=0\n\n\n[OCLint (https://oclint.org) v21.05]\n\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003euncrustify error (indentation)\u003c/summary\u003e\n\n```\nuncrustify...............................................................Failed\n- hook id: uncrustify\n- exit code: 1\n\nerr.c\n====================\n--- original\n\n+++ formatted\n\n@@ -1,3 +1,5 @@\n\n #include \u003cstdio.h\u003e\n-int main(){int i;return;}\n+int main(){\n+  int i; return;\n+}\n\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003ecppcheck error (unused variable i)\u003c/summary\u003e\n\n```\ncppcheck.................................................................Failed\n- hook id: cppcheck\n- exit code: 1\n\nerr.c:2:16: style: Unused variable: i [unusedVariable]\nint main(){int i;return;}\n               ^\n\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003ecpplint error (no copyright message, bad whitespace)\u003c/summary\u003e\n\n```\ncpplint..................................................................Failed\n- hook id: cpplint\n- exit code: 1\n\nDone processing err.c\nTotal errors found: 4\nerr.c:0:  No copyright message found.  You should have a line: \"Copyright [year] \u003cCopyright Owner\u003e\"  [legal/copyright] [5]\nerr.c:2:  More than one command on the same line  [whitespace/newline] [0]\nerr.c:2:  Missing space after ;  [whitespace/semicolon] [3]\nerr.c:2:  Missing space before {  [whitespace/braces] [5]\n\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003einclude-what-you-use error (remove unused #include \u003cstdio.h\u003e)\u003c/summary\u003e\n\n```\ninclude-what-you-use.....................................................Failed\n- hook id: include-what-you-use\n- exit code: 3\n\nerr.c:2:18: error: non-void function 'main' should return a value [-Wreturn-type]\nint main(){int i;return;}\n                 ^\n\nerr.c should add these lines:\n\nerr.c should remove these lines:\n- #include \u003cstdio.h\u003e  // lines 1-1\n\nThe full include-list for err.c:\n---\n\n```\n\u003c/details\u003e\n\n_Note that for your config yaml, you can supply your own args or remove the args line entirely,\ndepending on your use case._\n\nYou can also clone this repo and then run the test_repo to see all of the linters at work to produce this output:\n\n```bash\ngit clone https://github.com/pocc/pre-commit-hooks\ncp -r pre-commit-hooks/tests/test_repo .\ncd test_repo\ngit init\ngit add .\npre-commit run\n```\n\nNote that we are copying the test_repo outside of the pre-commit-hooks repo\nso that pre-commit doesn't get confused by nested github repositories.\n\n## Using this repo\n\n### Special flags in this repo\n\nThere are 2 flags, `--version` and `--no-diff` that can be added to `args:` for a pre-commit hook.\nThey will be removed and not be passed on to the command.\n\nSome linters change behavior between versions. To enforce a linter version\n8.0.0, for example, add `--version=8.0.0` to `args:` for that linter. Note that\nthis is a pre-commit hook arg and will be filtered before args are passed to the linter.\n\nYou can add `--no-diff` to the `args:` for clang-format and uncrustify\nif you would like there to be no diff output for these commands.\n\n### Default Options\n\nThese options are automatically added to enable all errors or are required.\n\n* oclint: `[\"-enable-global-analysis\", \"-enable-clang-static-analyzer\", \"-max-priority-3\", \"0\"]`\n* uncrustify: `[\"-c\", \"defaults.cfg\", \"-q\"]` (options added, and a defaults.cfg generated, if -c is missing)\n* cppcheck: `[\"-q\" , \"--error-exitcode=1\", \"--enable=all\", \"--suppress=unmatchedSuppression\", \"--suppress=missingIncludeSystem\", \"--suppress=unusedFunction\"]` (See https://github.com/pocc/pre-commit-hooks/pull/30)\n* cpplint: `[\"--verbose=0\"]`\n\nIf you supply any of these options in `args:`, your options will override the above defaults (use `-\u003cflag\u003e=\u003coption\u003e` if possible when overriding).\n\n### Compilation Database\n\n`clang-tidy` and `oclint` both expect a\n[compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html).\nBoth of the hooks for them will ignore the error for not having one.\n\nYou can generate with one `cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \u003cdir\u003e` if you\nhave a cmake-based project.\n\n## Information about the Commands\n\nPython3.6+ is required to use these hooks as all 5 invoking scripts are written in it.\nAs this is also the minimum version of pre-commit, this should not be an issue.\n\n### Installation\n\nUse these commands to install some or all of the linters used in this project:\n\n* Linux\n    * `brew install llvm uncrustify cppcheck include-what-you-use` [1] [2]\n    * `oclint`: See note below for installation [3]\n* MacOS\n    * `brew install llvm uncrustify cppcheck include-what-you-use oclint`\n* Windows\n    * `choco install llvm uncrustify cppcheck inlcude-what-you-use`\n* Cross-platform\n    * `pip install cpplint`\n    * `pipx install clang-format` [4]\n\n[1]: llvm includes tools like `clang-format` and `clang-tidy`.\n\n[2]: While it's possible to use other package managers to install these utilities on Linux, I recommend\n  using brew to avoid dependency issues between llvm and linters that use it, such as include-what-you-use.\n\n[3]: You can install on MacOS with `brew install oclint`. Oclint's github page also provides compiled\n  binary packages (and zip of source code to compile) for Macos/Linux:\n  [releases](https://github.com/oclint/oclint/releases). oclint is not available on windows.\n\n[4]: This will download the latest version. Versions from `10.0.1` and up are supported.\n  To pin to a specific version like `13.0.0`, use `pipx install clang-format==13.0.0`.\n  Check out the [clang-format-wheel repository](https://github.com/ssciwr/clang-format-wheel) for information on\n  how to download the clang-format binary as part of a CI pre-commit build.\n\n### Hook Info\n\n| Hook Info                                                                | Type                 | Languages                             |\n| ------------------------------------------------------------------------ | -------------------- | ------------------------------------- |\n| [clang-format](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) | Formatter            | C, C++, ObjC, ObjC++, Java            |\n| [clang-tidy](https://clang.llvm.org/extra/clang-tidy/)                   | Static code analyzer | C, C++, ObjC                          |\n| [oclint](http://oclint.org/)                                             | Static code analyzer | C, C++, ObjC                          |\n| [uncrustify](http://uncrustify.sourceforge.net/)                         | Formatter            | C, C++, C#, ObjC, D, Java, Pawn, Vala |\n| [cppcheck](http://cppcheck.sourceforge.net/)                             | Static code analyzer | C, C++                                |\n| [cpplint](https://github.com/cpplint/cpplint)                            | Style checker        | C, C++                                |\n| [include-what-you-use](https://github.com/include-what-you-use/include-what-you-use) | Static code analyzer | C, C++                    |\n\n### Hook Option Comparison\n\n| Hook Options                                                             | Fix In Place | Enable all Checks                             | Set key/value |\n| ------------------------------------------------------------------------ | ------------ | --------------------------------------------- | --------------- |\n| [clang-format](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) | `-i`         |                   | |\n| [clang-tidy](https://clang.llvm.org/extra/clang-tidy/)                   | `--fix-errors` [1] | `-checks=*` `-warnings-as-errors=*` [2] | |\n| [oclint](http://oclint.org/)                                             |  | `-enable-global-analysis` `-enable-clang-static-analyzer` `-max-priority-3 0` [3] | `-rc=\u003ckey\u003e=\u003cvalue\u003e` |\n| [uncrustify](http://uncrustify.sourceforge.net/)                         | `--replace` `--no-backup` [4] |  | `--set key=value` |\n| [cppcheck](http://cppcheck.sourceforge.net/)                             |  | `-enable=all` | |\n| [cpplint](https://github.com/cpplint/cpplint)                            |  | `--verbose=0` |  |\n| [include-what-you-use](https://github.com/include-what-you-use/include-what-you-use) | | `--verbose=3` | |\n\n[1]: `-fix` will fail if there are compiler errors. `-fix-errors` will `-fix`\nand fix compiler errors if it can, like missing semicolons.\n\n[2]: Be careful with `-checks=*`.  can have self-contradictory rules in newer versions of llvm (9+):\nmodernize wants to use [trailing return type](https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-trailing-return-type.html)\nbut Fuchsia [disallows it](https://clang.llvm.org/extra/clang-tidy/checks/fuchsia-trailing-return.html).\n*Thanks to @rambo.*\n\n[3]: The oclint pre-commit hook does the equivalent of `-max-priority-3 0` by default, which returns an error code when any check fails.\nSee [oclint error codes](https://docs.oclint.org/en/stable/manual/oclint.html#exit-status-options) for more info on partially catching failed checks.\n\n[4]: By definition, if you are using `pre-commit`, you are using version control.\nTherefore, it is recommended to avoid needless backup creation by using `--no-backup`.\n\n## Development\n\nSee [README_dev.md](README_dev.md)\n\n## Additional Resources\n\n### clang-format\n\n* [Official Docs](https://clang.llvm.org/docs/ClangFormatStyleOptions.html)\n* [clang-format\n  Guide](https://embeddedartistry.com/blog/2017/10/23/creating-and-enforcing-a-coding-standard-with-clang-format) -\n  a good overview and a great place to get started\n* [clang-format Configurator](https://zed0.co.uk/clang-format-configurator/) - Website to\n  interactively design your config while\n* [clang-format Options Explorer](https://zed0.co.uk/clang-format-configurator/) - Website to interactively\n  understand various options\n* [Source Code](https://github.com/llvm-mirror/clang/tree/master/tools/clang-format)\n\n### clang-tidy\n\n* [Official Docs](https://clang.llvm.org/extra/clang-tidy/)\n* [clang-tidy\n  guide](https://www.kdab.com/clang-tidy-part-1-modernize-source-code-using-c11c14/) -\n  Good place to start\n* [Example\n  usage](https://github.com/KratosMultiphysics/Kratos/wiki/How-to-use-Clang-Tidy-to-automatically-correct-code) -\n  Explanation of how to use clang-tidy by the creators of Kratos\n* [Add your own\n  checks](https://devblogs.microsoft.com/cppblog/exploring-clang-tooling-part-1-extending-clang-tidy/) -\n  Function names must be _awesome_!\n* [Source Code](https://github.com/llvm-mirror/clang-tools-extra/tree/master/clang-tidy)\n\n### oclint\n\n* [Official Docs](http://oclint.org/)\n* [Fastlane Integration](https://docs.fastlane.tools/actions/oclint/)\n* [Source Code](https://github.com/oclint/oclint)\n\n### uncrustify\n\n* [Official Docs](http://uncrustify.sourceforge.net/)\n* [Getting Started with Uncrustify](https://patrickhenson.com/2018/06/07/uncrustify-configuration.html)\n* [Source Code](https://github.com/uncrustify/uncrustify)\n\n### cppcheck\n\n* [Official Docs](http://cppcheck.sourceforge.net/)\n* [Using Cppcheck](https://katecpp.wordpress.com/2015/08/04/cppcheck/)\n* [Source Code](https://github.com/danmar/cppcheck)\n\n### cpplint\n\n* [Google C++ style guide (basis of cpplint)](https://google.github.io/styleguide/cppguide.html)\n* [Source Code](https://github.com/cpplint/cpplint)\n\n### include-what-you-use\n\n* [Official Docs](https://include-what-you-use.org/)\n* [Using include-what-you-use](https://www.incredibuild.com/blog/include-what-you-use-how-to-best-utilize-this-tool-and-avoid-common-issues)\n* [Source Code](https://github.com/include-what-you-use/include-what-you-use)\n\n## License\n\nApache 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpocc%2Fpre-commit-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpocc%2Fpre-commit-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpocc%2Fpre-commit-hooks/lists"}