git hooks guide - ganong-noel/lab_manual GitHub Wiki

We use git hooks for two purposes:

  1. To check that commits are appropriately linking issues numbers
  2. To automate checking that code conforms to the style guide

Requires: lintr (install as you would normally install an R package)

How to set up locally

If someone has already added .githooks/pre-commit and .githooks/commit-msg to the repo, all you have to do is tell git where to look:

Tell git to look for hooks in .githooks

By default, git looks for hooks in the director .git/hooks/ which cannot be tracked by git. Our solution is to make a new folder .githooks in the root directory of the repo that contains hooks and can be tracked. We need to tell git to look there by updating the config file, which can be handled through the command line like so:

git config core.hooksPath .githooks

By default, files in the .githooks folder are non-executable. To make them executable, run the following command:

chmod ug+x .githooks/*

(FYI: The chmod is a command that changes permissions, the x option tells the command to make files executable and u, g stand for user and group.)

To revert to the defaults, type git config core.hooksPath .git/hooks/.

You will need to do this step each time you clone a repo.

For Windows: Writing a .profile file to set up environmental variables

In R Studio console run R.home() to return the R home directory and Sys.getenv("R_LIBS_USER") to return the libraries for user.

Once you locate these paths:

In GitBash you will run the two lines of code below. Remember to replace /c/Users/User02/Documents/R/win-library/3.5 and /c/Program Files/R/R-3.5.2/bin/x64 with your specific paths attained above. Be sure to edit the file paths given by R so that they're formatted like the file paths below -- forward slash, lower case c, etc. Also, if R gave you an abbreviated path that used the ~, you might have to type out the whole path for the hooks to run.

echo 'export PATH="${PATH:+${PATH}:}/c/Program Files/R/R-3.5.2/bin/x64"' >> ~/.profile
echo 'export R_LIBS_USER=/c/Users/User02/Documents/R/win-library/3.5' >> ~/.profile

Use cat ~/.profile in git bash to see the contents of this .profile file. After editing the .profile file, exit the git bash window and restart so the new environment variables are set properly.

Add exceptions and change defaults in .lintr

Some style issues are impossible to fix (e.g. library(RColorBrewer) fails the default camel_case_linter test). We can address this with a .lintr file.

  1. In the base directory of your repo add a file named .lintr
  2. Add exceptions as needed

Alternatively - in script before committing put RColorBrewer in quotes: library("RColorBrewer") or include # nolint: library(RColorBrewer) # nolint to pass object_name_lintr.

Automating style code in RStudio

In RStudio you can highlight your code and press ctrl + shift + A to format your code.

If this is insufficient, consider using styler is a package that will automatically format your code.

  1. Install styler in R.
  2. In RStudio automatically style your file by: Tools > Addins > Browse Addins, then type in styler and select "Style active package". (If you figure out a non-GUI way to do this, feel free to update the wiki!)

How to set-up from scratch

Set-up step 1: Move .git/hooks/ to git trackable location

# From the root of the repo
mkdir .githooks
cp .git/hooks/* .githooks

Set-up step 2: Tell git to look for hooks in .githooks

git config core.hooksPath .githooks

Set-up step 3: Add pre-commit to .githooks

  1. Create a file called pre-commit in .githooks
  2. Copy code from here into the file. This is a bash file that calls lintr on staged R files.
  3. Make the file executable. chmod u+x pre-commit

Double check this works by git adding an R file with some style issues. When you git commit, you will be told where the style issues are and that in order to commit you must fix them.

Set-up step 4: Add commit-msg to .githooks

  1. Create a file called commit-msg in .githooks
  2. Copy the code from below
  3. Make the file executable. chmod u+x commit-msg
#!/bin/sh

commit_regex='(#[0-9]+|Merg)'
error_msg="\n\e[0;31mAborting commit. Your commit message is missing an issue number.\n\e[0m"

echo $fil 
if ! grep -iE $commit_regex $1; then
    printf "$error_msg" >&2
    exit 1
fi

Setting up githooks inside Chase

  • You will need to implement the step "For Windows: Writing a .profile file to set up environmental variables" above but with one difference. Specifically, I had to add this to ~/.profile:
export PATH=$PATH:/apps/ANACONDA-GTIPAAS/4.6.14/3/lib/R
  • Additionally, if you are creating new githooks from scratch, you need to change the line
#!/bin/sh

to

#!/bin/bash

everywhere.