Last active
April 1, 2019 22:11
-
-
Save wookietreiber/afdb946625c6090f96012ee1da316a73 to your computer and use it in GitHub Desktop.
git pre-commit hook to check R scripts with lintr via littler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires the following R packages: