Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Last active April 1, 2019 22:11
Show Gist options
  • Save wookietreiber/afdb946625c6090f96012ee1da316a73 to your computer and use it in GitHub Desktop.
Save wookietreiber/afdb946625c6090f96012ee1da316a73 to your computer and use it in GitHub Desktop.
git pre-commit hook to check R scripts with lintr via littler
suppressMessages(library(lintr))
files <- commandArgs(trailingOnly = T)
messages <- function(file) {
result <- lint(file)
print(result)
return(length(result))
}
msgs <- sapply(files, messages)
if (sum(msgs) > 0) {
q(status = 1)
}
#!/bin/bash
if git rev-parse --verify HEAD &> /dev/null ; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
files=$(
git diff-index --cached --name-only $against |
grep -E '\.[rR]$'
)
if [[ -n $files ]] ; then
if ! command -v Rscript &> /dev/null ; then
echo -e "\e[1m\e[33m[warn]\e[0m \e[1mRscript\e[0m not installed" >&2
exit 0
fi
if ! Rscript --vanilla -e 'library(lintr)' &> /dev/null ; then
echo -e "\e[1m\e[33m[warn]\e[0m R package \e[1mlintr\e[0m not installed" >&2
exit 0
fi
fi
for file in $files ; do
tmp_lintr=$(mktemp)
git show ":$file" > "$tmp_lintr"
Rscript --vanilla git-hook-lintr.r "$tmp_lintr" || exit 1
rm -f "$tmp_lintr"
done
@wookietreiber
Copy link
Author

Requires the following R packages:

  • littler
  • lintr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment