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-commithook description that executesbazel runfor the provided target -
pre_commit_hooks: bundles multiplepre_commit_hookrules together for easy usage -
pre_commit_config: acceptspre_commit_hook{,s}and generates a.pre-commit-config.yamlfile -
pre_commit_install: installs a provided.pre-commit-config.yamlfile into.git/hooks -
pre_commit_run: performspre-commit runagainst the provided.pre-commit-config.yamlfile -
pre_commit_stage: describes apre-commithook stage to be used withpre_commit_hook -
pre_commit_tag: describes apre-commithook 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_configcan parse and generate a configuration from -
PreCommitHooksInfo: contains adepsetofPreCommitHookInfoproviders -
PreCommitTagInfo: contains aidentifyfile kind tag -
PreCommitStageInfo: contains apre-commithook stage
The patch has the following public labels:
-
@pre-commit: a monkey patchedpre-committo ensure it works with Bazel effectively and provides a hermeticgitandbazelimplementation for running the hooks -
@pre-commit//pre-commit:pkg: apy_librarythat provdes thepre_commitPython module. Uses universal lockfiles and Python versionselectto provide the correct module to downstream users. -
@pre-commit//pre-commit/git: alabel_flagfor changing the hermeticgitimplementation used -
@pre-commit//pre-commit/bazel: alabel_flagfor changing the hermeticbazelimplementation 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
buildifierhooks - 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.pyunderstandable?- See the
pre-commit/README.mdfor 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-hooksmodule that provides our commonly used hooks
- Ideally, we would create a
Edited by Matthew Clarkson