| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Hook used to indent all source files before commiting |
| 4 | # |
| 5 | |
| 6 | # indent / format file by type |
| 7 | indent() { |
| 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 ! -f $file |
| 32 | then |
| 33 | return; |
| 34 | fi |
| 35 | |
| 36 | echo "Indenting " $file |
| 37 | echo "library(formatR);formatR::tidy_source('$file',comment=TRUE,blank=TRUE, |
| 38 | arrow=TRUE,brace.newline=TRUE,indent=2,width.cutoff=80,file='$file')" | R --slave |
| 39 | git add "$file" |
| 40 | } |
| 41 | |
| 42 | indent |