Difference between revisions of "Git/pre-commit"
< Git
Jump to navigation
Jump to search
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Install = | = Install = | ||
<source lang=bash> | <source lang=bash> | ||
pip install pre-commit | pip install pre-commit==2.19.0 | ||
pip install pre-commit --upgrade | |||
</source> | </source> | ||
= Sample <code>.pre-commit-config.yaml</code> = | = Sample <code>.pre-commit-config.yaml</code> = | ||
< | <source lang=yaml> | ||
# verified with pre-commit v2.19.0 | |||
repos: | repos: | ||
- repo: git://github.com/pre-commit/pre-commit-hooks | - repo: git://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.0 | rev: v4.1.0 | ||
hooks: | hooks: | ||
- id: fix-byte-order-marker | - id: fix-byte-order-marker | ||
Line 19: | Line 21: | ||
- id: mixed-line-ending | - id: mixed-line-ending | ||
- id: trailing-whitespace | - id: trailing-whitespace | ||
- repo: | - repo: https://github.com/antonbabenko/pre-commit-terraform | ||
rev: v1.62.3 | rev: v1.62.3 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases | ||
hooks: | hooks: | ||
- id: terraform_fmt | - id: terraform_fmt # it modifies the files | ||
- id: | args: | ||
- "--args=-recursive" | |||
- repo: local | |||
hooks: | |||
- id: terraform-docs | |||
name: terraform-docs | |||
language: docker_image | |||
entry: quay.io/terraform-docs/terraform-docs:0.16.0 | |||
args: ["--config", ".terraform-docs.yml", "markdown", "."] | |||
pass_filenames: false | |||
- repo: https://github.com/gruntwork-io/pre-commit | - repo: https://github.com/gruntwork-io/pre-commit | ||
rev: v0.1.17 | rev: v0.1.17 | ||
hooks: | hooks: | ||
- id: shellcheck | - id: shellcheck | ||
- id: terraform-fmt # duplication of antonbabenko/pre-commit-terraform, it does not modify files by defaults | |||
args: | |||
- "-recursive" | |||
- id: terraform-validate | |||
- repo: https://github.com/dnephin/pre-commit-golang.git | - repo: https://github.com/dnephin/pre-commit-golang.git | ||
rev: v0.4.0 | rev: v0.4.0 | ||
Line 39: | Line 54: | ||
hooks: | hooks: | ||
- id: gitleaks | - id: gitleaks | ||
</ | |||
</source> | |||
= Operations = | = Operations = | ||
Operation of running any checks is done only on tracked or staged files. The untracked files are skipped. | |||
<source lang=bash> | <source lang=bash> | ||
pre-commit install # install all hooks | pre-commit install # install all hooks | ||
Line 47: | Line 64: | ||
pre-commit autoupdate # update all hooks to their latest versions, next time you `run` they will get downloaded | pre-commit autoupdate # update all hooks to their latest versions, next time you `run` they will get downloaded | ||
</source> | </source> | ||
{{Note| Pre-commit automatically adds a mount of the current code to <code>/src</code> if run via <code>language: docker_image</code>}} | |||
= Hooks = | = Hooks = | ||
* [https://github.com/antonbabenko/pre-commit-terraform Collection of git hooks for Terraform] | * [https://github.com/antonbabenko/pre-commit-terraform Collection of git hooks for Terraform] |
Latest revision as of 09:09, 10 May 2022
Install
pip install pre-commit==2.19.0 pip install pre-commit --upgrade
Sample .pre-commit-config.yaml
# verified with pre-commit v2.19.0 repos: - repo: git://github.com/pre-commit/pre-commit-hooks rev: v4.1.0 hooks: - id: fix-byte-order-marker - id: check-case-conflict - id: check-merge-conflict - id: detect-aws-credentials args: ['--allow-missing-credentials'] - id: detect-private-key - id: end-of-file-fixer - id: mixed-line-ending - id: trailing-whitespace - repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.62.3 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases hooks: - id: terraform_fmt # it modifies the files args: - "--args=-recursive" - repo: local hooks: - id: terraform-docs name: terraform-docs language: docker_image entry: quay.io/terraform-docs/terraform-docs:0.16.0 args: ["--config", ".terraform-docs.yml", "markdown", "."] pass_filenames: false - repo: https://github.com/gruntwork-io/pre-commit rev: v0.1.17 hooks: - id: shellcheck - id: terraform-fmt # duplication of antonbabenko/pre-commit-terraform, it does not modify files by defaults args: - "-recursive" - id: terraform-validate - repo: https://github.com/dnephin/pre-commit-golang.git rev: v0.4.0 hooks: - id: go-fmt - id: go-imports - id: go-lint - id: go-mod-tidy - repo: https://github.com/zricethezav/gitleaks rev: v8.2.4 hooks: - id: gitleaks
Operations
Operation of running any checks is done only on tracked or staged files. The untracked files are skipped.
pre-commit install # install all hooks pre-commit run --all-files # run all hooks/checks pre-commit autoupdate # update all hooks to their latest versions, next time you `run` they will get downloaded
Note: Pre-commit automatically adds a mount of the current code to /src
if run via language: docker_image