Change pre-commit hook to use temp directory

This commit is contained in:
Talia 2021-01-25 13:22:31 +01:00
parent 16334905ab
commit 1480cbbd96
1 changed files with 8 additions and 15 deletions

View File

@ -1,22 +1,13 @@
#!/bin/sh #!/bin/sh
indentation=$(git config hooks.indentation) indentation=$(git config hooks.indentation)
temp=$(mktemp -p /dev/shm -d -t git-hook.XXXX)
export GIT_DIR=$(git rev-parse --show-toplevel)/.git
new() { new() {
git status --porcelain | grep -E '^AM|^M|^A' | sed 's/^[^ ]* *//' git status --porcelain | grep -E '^AM|^M|^A' | sed 's/^[^ ]* *//'
} }
prepare() {
index=$(git write-tree) > /dev/null
git stash push --keep-index --include-untracked > /dev/null
}
reset() {
git checkout stash -- '*'
git stash drop > /dev/null
git read-tree "$index"
}
check() { check() {
file=$2 file=$2
luajit -e " luajit -e "
@ -28,22 +19,21 @@ check() {
end" end"
} }
git checkout-index --prefix="$temp/index/" --all
cd $temp/index/
case $indentation in case $indentation in
tab|tabs) tab|tabs)
prepare
echo "Checking tab indentation" echo "Checking tab indentation"
for file in $(new); do for file in $(new); do
check '\t' $file || error=1 check '\t' $file || error=1
done done
reset
;; ;;
space|spaces) space|spaces)
prepare
echo "Checking space indentation" echo "Checking space indentation"
for file in $(new); do for file in $(new); do
check ' ' $file || error=1 check ' ' $file || error=1
done done
reset
;; ;;
""|off) ""|off)
echo "Skipping indentation check" echo "Skipping indentation check"
@ -54,6 +44,9 @@ case $indentation in
;; ;;
esac esac
cd $GIT_DIR
rm -rf $temp
if [ -z "$error" ] if [ -z "$error" ]
then exit 0 then exit 0
else exit 1 else exit 1