# pre-commit-hooks **Repository Path**: btcong/pre-commit-hooks ## Basic Information - **Project Name**: pre-commit-hooks - **Description**: No description available - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-04-20 - **Last Updated**: 2022-04-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pre-commit hooks ![Ubuntu Build](https://github.com/pocc/pre-commit-hooks/actions/workflows/gh_actions_ubuntu.yml/badge.svg) ![Macos Build](https://github.com/pocc/pre-commit-hooks/actions/workflows/gh_actions_macos.yml/badge.svg) ![Windows Build](https://github.com/pocc/pre-commit-hooks/actions/workflows/gh_actions_windows.yml/badge.svg) This is a [pre-commit](https://pre-commit.com) hooks repo that integrates two C/C++ code formatters: > [clang-format](https://clang.llvm.org/docs/ClangFormatStyleOptions.html), [uncrustify](http://uncrustify.sourceforge.net/), and five C/C++ static code analyzers: > [clang-tidy](https://clang.llvm.org/extra/clang-tidy/), [oclint](http://oclint.org/), [cppcheck](http://cppcheck.sourceforge.net/), [cpplint](https://github.com/cpplint/cpplint), [include-what-you-use](https://github.com/include-what-you-use/include-what-you-use) This repo's hooks do more than passthrough arguments to provide these features: * 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. * Honor `--` arguments, which pre-commit [has problems with](https://github.com/pre-commit/pre-commit/issues/1000) * 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 * Formatters clang-format and uncrustify will error with diffs of what has changed ## Example Usage With this [err.c](tests/test_repo/err.c) ```c #include int main(){int i;return;} ``` and using this `.pre-commit-config.yaml`: ```yaml fail_fast: false repos: - repo: https://github.com/pocc/pre-commit-hooks rev: master hooks: - id: clang-format args: [--style=Google] - id: clang-tidy - id: oclint - id: uncrustify - id: cppcheck - id: cpplint - id: include-what-you-use ``` All seven linters should fail on commit with these messages. Full text is at [media/all_failed.txt](media/all_failed.txt).
clang-format error (indentation) ``` clang-format.............................................................Failed - hook id: clang-format - exit code: 1 err.c ==================== --- original +++ formatted @@ -1,3 +1,6 @@ #include -int main(){int i;return;} +int main() { + int i; + return; +} ```
clang-tidy error (non-void main should return a value) ``` clang-tidy...............................................................Failed - hook id: clang-tidy - exit code: 1 /tmp/temp/err.c:2:18: error: non-void function 'main' should return a value [clang-diagnostic-return-type] int main(){int i;return;} ^ 1 error generated. Error while processing /tmp/temp/err.c. Found compiler error(s). ```
oclint error (non-void main should return a value) ``` oclint...................................................................Failed - hook id: oclint - exit code: 6 Compiler Errors: (please be aware that these errors will prevent OCLint from analyzing this source code) /tmp/temp/err.c:2:18: non-void function 'main' should return a value Clang Static Analyzer Results: /tmp/temp/err.c:2:18: non-void function 'main' should return a value OCLint Report Summary: TotalFiles=0 FilesWithViolations=0 P1=0 P2=0 P3=0 [OCLint (https://oclint.org) v21.05] ```
uncrustify error (indentation) ``` uncrustify...............................................................Failed - hook id: uncrustify - exit code: 1 err.c ==================== --- original +++ formatted @@ -1,3 +1,5 @@ #include -int main(){int i;return;} +int main(){ + int i; return; +} ```
cppcheck error (unused variable i) ``` cppcheck.................................................................Failed - hook id: cppcheck - exit code: 1 err.c:2:16: style: Unused variable: i [unusedVariable] int main(){int i;return;} ^ ```
cpplint error (no copyright message, bad whitespace) ``` cpplint..................................................................Failed - hook id: cpplint - exit code: 1 Done processing err.c Total errors found: 4 err.c:0: No copyright message found. You should have a line: "Copyright [year] " [legal/copyright] [5] err.c:2: More than one command on the same line [whitespace/newline] [0] err.c:2: Missing space after ; [whitespace/semicolon] [3] err.c:2: Missing space before { [whitespace/braces] [5] ```
include-what-you-use error (remove unused #include ) ``` include-what-you-use.....................................................Failed - hook id: include-what-you-use - exit code: 3 err.c:2:18: error: non-void function 'main' should return a value [-Wreturn-type] int main(){int i;return;} ^ err.c should add these lines: err.c should remove these lines: - #include // lines 1-1 The full include-list for err.c: --- ```
_Note that for your config yaml, you can supply your own args or remove the args line entirely, depending on your use case._ You can also clone this repo and then run the test_repo to see all of the linters at work to produce this output: ```bash git clone https://github.com/pocc/pre-commit-hooks cp -r pre-commit-hooks/tests/test_repo . cd test_repo git init git add . pre-commit run ``` Note that we are copying the test_repo outside of the pre-commit-hooks repo so that pre-commit doesn't get confused by nested github repositories. ## Using this repo ### Special flags in this repo There are 2 flags, `--version` and `--no-diff` that can be added to `args:` for a pre-commit hook. They will be removed and not be passed on to the command. Some linters change behavior between versions. To enforce a linter version 8.0.0, for example, add `--version=8.0.0` to `args:` for that linter. Note that this is a pre-commit hook arg and will be filtered before args are passed to the linter. You can add `--no-diff` to the `args:` for clang-format and uncrustify if you would like there to be no diff output for these commands. ### Default Options These options are automatically added to enable all errors or are required. * oclint: `["-enable-global-analysis", "-enable-clang-static-analyzer", "-max-priority-3", "0"]` * uncrustify: `["-c", "defaults.cfg", "-q"]` (options added, and a defaults.cfg generated, if -c is missing) * cppcheck: `["-q" , "--error-exitcode=1", "--enable=all", "--suppress=unmatchedSuppression", "--suppress=missingIncludeSystem", "--suppress=unusedFunction"]` (See https://github.com/pocc/pre-commit-hooks/pull/30) * cpplint: `["--verbose=0"]` If you supply any of these options in `args:`, your options will override the above defaults (use `-=