/precommit

pre-commit hooks for R projects

Primary LanguageRGNU General Public License v3.0GPL-3.0

Useful git pre-commit hooks for R related projects

Travis build status Lifecycle: maturing

Pre-commit hooks are tests that run each time you attempt to commit. If the tests pass, the commit will be made, otherwise not. A very basic test is to check if the code is parsable, making sure you have not forgotten a comma, brace or quote. To learn more about available hooks and why to use them consult the online documentation.

Installation

Please make sure you have conda installed. The rest can be handled from R:

# once
remotes::install_github("lorenzwalthert/precommit")
precommit::install_precommit()

# in every project
precommit::use_precommit()

This installs pre-commit and performs some other set-up tasks. If you want more control over the installation, see vignette("manual-installation").

Usage

The next time you run git commit, the hooks listed in your .pre-commit-config.yaml will get executed before the commit. The helper function precommit::open_config() let’s you open the .pre-commit-config.yaml conveniently from the RStudio console. When any file is changed due to running a hook, the commit will fail. You can inspect the changes introduced by the hook and if satisfied, you can attempt to commit again. If all hooks pass, the commit is made. You can also temporarily disable hooks. If you succeed, it should look like this:

See the hooks provided by this repo under vignette("available-hooks"). You can also add other hooks from other repos, by extending the .pre-commit-config.yaml file, e.g. like this:

-   repo: https://github.com/pre-commit/precommit
    rev: v1.2.3
    hooks: 
    -   id: check-added-large-files

To update the hook revisions, just run pre-commit autoupdate in your terminal of precommit::autoupdate().

Caution

Do not abort while hooks are running. Non-staged changes are stashed to a temp directory when the hooks are run and may not easily be recovered afterwards.