Git hook scripts
Recipe | Crates | Categories |
---|---|---|
Check your code before committing it | ||
pre-commit |
Git hook scripts are useful for automatically identifying simple issues, such as missing semicolons, trailing whitespace, poor formatting of the code or configuration files, when commiting in git
, prior to submission to code review or start of a CI workflow.
Check your code before committing it
cargo-husky⮳ setup Git hooks automatically for cargo projects with 🐶
Add the cargo-husky
crate to the [dev-dependencies]
section of your project's Cargo.toml
.
[dev-dependencies]
cargo-husky = "1"
Then run tests in your project directory.
cargo test
pre-commit
pre-commit
⮳ is a Python framework for managing and maintaining multi-language pre-commit hooks.
Installation
pre-commit
is written in Python. Include the following into your Dockerfile
or run the commands by hand to install pre-commit
:
# Install python3, pipx, pre-commit (Ubuntu & friends)
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get install -y python3 pipx \
&& pipx install pre-commit \
&& pipx ensurepath
- Verify that it is properly installed:
pre-commit --version
- Add a file called
.pre-commit-config.yaml
to the root of your project. Usepre-commit sample-config
for a template. - Edit it to configure your preferred hooks.
# Set up the git hook scripts
pre-commit install
# It's usually a good idea to run the hooks against all of the files when adding new hooks
# (pre-commit will only run on the changed files during git hooks)
pre-commit run --all-files
Useful links
Rust CI Tooling: Clippy, commitlint, pre‑commit
A pre-commit hook for commitlint