add automatic code formatter following https://google.github.io/styleguide/Rguide.xml
[valse.git] / hooks / pre-commit
CommitLineData
7a56cc18
BA
1#!/bin/sh
2#
3# Hook used to indent all source files before commiting
4#
5
6# indent / format file by type
7indent() {
8 # getting against as the current commit
9 if git rev-parse --verify HEAD >/dev/null 2>&1
10 then
11 local against=HEAD
12 else
13 # Initial commit: diff against an empty tree object
14 local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
15 fi
16
17 # loop on modified files
18 git diff --cached --name-only $against |while read file;
19 do
20 local ext=$(expr "$file" : ".*\(\..*\)")
21 case $ext in
22 .R|.r)
23 __indent_R;
24 ;;
25 esac
26 done
27}
28
29# Indent the file with `indent' if this is a R file
30__indent_R() {
31 if test ! -x "$INDENT"
32 then
33 return;
34 fi
35 if test ! -f $file
36 then
37 return;
38 fi
39
40 echo "Indenting " $file
41 echo "library(formatR);formatR::tidy_source('$file',comment=TRUE,blank=TRUE,
42 arrow=TRUE,brace.newline=TRUE,indent=2,width.cutoff=80,file='$file')" | R --slave
43 git add "$file"
44}
45
46indent