From a06a611c8ab46104a0ec6651c5d1823c0bc74ca3 Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Wed, 9 Jun 2021 11:09:22 +0200 Subject: [PATCH] pre-commit: use ocp-indent with --lines instead of diff of diffs, #236 --- scripts/hooks/pre-commit | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit index 308e36871a..ce829120ba 100755 --- a/scripts/hooks/pre-commit +++ b/scripts/hooks/pre-commit @@ -59,11 +59,22 @@ git diff-index --check --cached $against -- || exit 1 fail="false" for f in $(git diff --cached --name-only | grep -E ".*\.ml(i|l|y)?$"); do # echo "Checking $f" - diff <(diff <(git show HEAD:$f) <(git show :$f)) <(diff <(git show HEAD:$f | ocp-indent) <(git show :$f | ocp-indent)) | sed "/^ //" || (echo "Problems in $f"; false) - # can't set fail above since it's in some subshell, so we return false and set it below: - [ $? = 1 ] && fail="true" + # diff <(diff <(git show HEAD:$f) <(git show :$f)) <(diff <(git show HEAD:$f | ocp-indent) <(git show :$f | ocp-indent)) | sed "/^ //" || (echo "Problems in $f"; false) + for l in $(git diff --cached -U0 -- $f | sed -n "s/^@@ -\S* +\(\S*\) @@.*/\1/p"); do + a=$(echo $l | cut -d',' -f1) + b=$(echo $l | cut -d',' -f2) + if [ "$a" = "$b" ]; then + lines=$a + else + b=$(expr $a + $b - 1) + lines="$a-$b" + fi + echo "ocp-indent file: $f, lines: $lines" + diff $f <(ocp-indent --lines=$lines $f) || fail="true" + done done if [ "$fail" == "true" ]; then + echo echo "You shall not commit! ocp-indent complains about the indentation of your changes (see diffs above). Please fix! You can bypass this check with git commit -n." exit 1 fi