Skip to content

Commit

Permalink
docs: info on pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 18, 2024
1 parent a7a4415 commit 8efdb29
Showing 1 changed file with 94 additions and 4 deletions.
98 changes: 94 additions & 4 deletions docs/dev-guide/pre-commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Git has **hooks**, which can run code on various events:
- Before rebasing (pre-rebase).
- Etc.

## Pre-Commit
### Pre-Commit Hook

- Pre-commit Git hooks run checks before a commit is accepted.
- Pre-commit is a package to automate the setup.
Expand All @@ -21,12 +21,102 @@ Git has **hooks**, which can run code on various events:

## Pre-Commit (Python Tool)

...
- Pre-commit is a Python tool for simplifying and applying Git pre-commit hooks.
- Hooks can be configured via a YAML file, then applied on each attempted commit.
- There are many hooks available from different sources.

### Add pre-commit-config.yaml

...
A best practice config file, taken from FMTM:

```yaml
repos:
# Versioning: Commit messages & changelog
- repo: https://github.com/commitizen-tools/commitizen
rev: v3.13.0
hooks:
- id: commitizen
stages: [commit-msg]

# Autoformat: Python code
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black
files: ^src/backend/(?:.*/)*.*$
args: [--target-version=py39]

# Lint / autoformat: Python code
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.13"
hooks:
- id: ruff
files: ^src/backend/(?:.*/)*.*$
args: [--fix, --exit-non-zero-on-fix]

# Autoformat: YAML, JSON, Markdown, etc.
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
args:
[
--ignore-unknown,
--no-error-on-unmatched-pattern,
"!CHANGELOG.md",
"!CONTRIBUTING.md",
"!LICENSE.md",
"!src/frontend/pnpm-lock.yaml",
]

# Lint: Bash scripts
- repo: https://github.com/openstack-dev/bashate.git
rev: 2.1.1
hooks:
- id: bashate

# Lint: Shell scripts
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.9.0.6
hooks:
- id: shellcheck
args: ["-x"]

# Lint: Markdown
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.38.0
hooks:
- id: markdownlint
args:
[
--fix,
--ignore,
LICENSE.md,
--ignore,
CHANGELOG.md,
--ignore,
.github,
]
```

> Note: the config above is for a monorepo configuration.
>
> Your repo may not require both Python and JS code formatting.
### Add Hooks

...
Run

```bash
# Standard install for most hooks
pre-commit install

# Additional commit-msg hook (for the commitizen hook above)
pre-commit install --hook-type commit-msg
```

Now when you attempt to commit to the repo:

- Code will be auto-formatted.
- An error will show if linting fails.
- An error will show if commit messages are in the wrong format.

0 comments on commit 8efdb29

Please sign in to comment.