CONTRIBUTING.md

General

Before making a pull request, discuss your ideas in an issue.

Adding new hooks

To create a new hook, have a look at the official documentation on creating new hooks, then have a look at existing hooks in this repo. The actual executables are defined in inst/hooks/. In the script, you can expect the passed command line arguments to be all options, finally the files that should be processed with the hook.

For the scripts to become a hook, they need to be registered in .pre-commit-hooks.yaml. As of pre-commit 2.11, R is a supported language of pre-commit. Hence, it should have language: r in .pre-commit-hooks.yaml and then (for compatibility) a shebang in the entrypoint script.

Testing hooks

Hooks should be tested by checking both the positive outcome (hook passes) and the negative outcome (hook fails) by adding two run_test() statements to ./tests/testthat/test-hooks.R. Look at existing examples and the documentation of run_test(). Note that this won't interact with pre-commit. It will simply run Rscript path/to/script.R (whereas with pre-commit, a {renv} will be activated before running the script).

Also, there are tests to ensure that hooks are correctly registered in .pre-commit-hooks.yaml, which you have to adapt if you add a hook.

You can also test them with pre-commit try-repo as described in the documentation.

Summary



lorenzwalthert/precommit documentation built on Feb. 7, 2025, 4:31 p.m.