Skip to content

Commit

Permalink
Merge pull request #628 from bclement-ocp/bclement/precommit
Browse files Browse the repository at this point in the history
Better pre-commit hook
  • Loading branch information
bclement-ocp authored Jun 8, 2023
2 parents 5bec9d4 + 18a4955 commit da0d114
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[Makefile]
indent_style = tab
36 changes: 27 additions & 9 deletions rsc/extra/pre-commit--git-hook
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
#!/bin/sh

git_repo=`git rev-parse --show-toplevel`
cd $git_repo/rsc/extra
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi

res=`./check_style.sh`
echo $res
# Redirect output to stderr.
exec 1>&2

if [ "$res" = "All files have correct style" ] ; then
./check_indentation.sh
exit $?
else
exit 1
# Reindent files using ocp-indent
errors=0
files=$(git diff --cached --name-only --diff-filter=ACMR -z HEAD | grep -Ez '\.ml[ily]?$' | xargs -0)
for f in $files; do
cmp -s <(git show ":$f") <(git show ":$f" | ocp-indent)
if [[ $? -ne 0 ]]; then
echo "$f: indentation errors."
ocp-indent -i "$f"
git diff -- "$f"
errors=$((errors+1))
fi
done

if [[ $errors -ne 0 ]]; then
exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

0 comments on commit da0d114

Please sign in to comment.