feat: implement `pre_commit` macro
This is a huge patch that implements the functionality to use Bazel run targets as pre-commit
hooks.
The patch provides the following rules:
-
pre_commit
: a macro that bundles the below rules -
pre_commit_hook
: a macro to select a specific Bazel command rule such aspre_commit_hook_run
-
pre_commit_hook_run
: creates apre-commit
hook description that executesbazel run
for the provided target -
pre_commit_hooks
: bundles multiplepre_commit_hook
rules together for easy usage -
pre_commit_config
: acceptspre_commit_hook{,s}
and generates a.pre-commit-config.yaml
file -
pre_commit_install
: installs a provided.pre-commit-config.yaml
file into.git/hooks
-
pre_commit_run
: performspre-commit run
against the provided.pre-commit-config.yaml
file -
pre_commit_stage
: describes apre-commit
hook stage to be used withpre_commit_hook
-
pre_commit_tag
: describes apre-commit
hook file type tag to be used withpre_commit_hook
It has the following providers:
-
PreCommitConfigInfo
: a generated configuration file -
PreCommitHookJSONInfo
: a provider that can be serialized to JSON for ingestion intopre_commit_config
-
PreCommitHookInfo
: a generated JSON file thatpre_commit_config
can parse and generate a configuration from -
PreCommitHooksInfo
: contains adepset
ofPreCommitHookInfo
providers -
PreCommitTagInfo
: contains aidentify
file kind tag -
PreCommitStageInfo
: contains apre-commit
hook stage
The patch has the following public labels:
-
@pre-commit
: a monkey patchedpre-commit
to ensure it works with Bazel effectively and provides a hermeticgit
andbazel
implementation for running the hooks -
@pre-commit//pre-commit:pkg
: apy_library
that provdes thepre_commit
Python module. Uses universal lockfiles and Python versionselect
to provide the correct module to downstream users. -
@pre-commit//pre-commit/git
: alabel_flag
for changing the hermeticgit
implementation used -
@pre-commit//pre-commit/bazel
: alabel_flag
for changing the hermeticbazel
implementation used -
@pre-commit//pre-commit/stage:*
: stages to be used withpre_commit_hook#stages
-
@pre-commit//pre-commit/tag:*
: tags to be used withpre_commit_hook#types{,_or}
The README.md
is a good place to start to understand the module.
Areas that require more-indepth review:
- Do the various rules have a sensible API?
- Look at
buildifier
hooks - See how they are used in the
hooks
- Look at
- Does the MR work for yourself?
- Try cloning the MR and run
bazelisk run -- hooks:install
/bazelisk run -- hooks
- Try cloning the MR and run
- Is the monkey patching in
pre-commit/__main.py
understandable?- See the
pre-commit/README.md
for information about the monkey patching
- See the
- Is it clear how to use the module for creating hooks downstream?
- Ideally, we would create a
@pre-commit-hooks
module that provides our commonly used hooks
- Ideally, we would create a
Edited by Matthew Clarkson