2021-01-20 15:07:20 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2021-02-15 13:34:19 +00:00
|
|
|
filter=$(git config hooks.filter)
|
|
|
|
validation=$(git config hooks.validation)
|
2021-01-20 15:07:20 +00:00
|
|
|
indentation=$(git config hooks.indentation)
|
2021-01-25 12:22:31 +00:00
|
|
|
temp=$(mktemp -p /dev/shm -d -t git-hook.XXXX)
|
|
|
|
export GIT_DIR=$(git rev-parse --show-toplevel)/.git
|
2021-01-20 15:07:20 +00:00
|
|
|
|
|
|
|
new() {
|
|
|
|
git status --porcelain | grep -E '^AM|^M|^A' | sed 's/^[^ ]* *//'
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
|
|
|
file=$2
|
|
|
|
luajit -e "
|
|
|
|
for line in assert(io.open('$file')):lines() do
|
|
|
|
if not line:match('^%s*'):match('^$1*$') then
|
|
|
|
print('File \x1b[31m$file\x1b[00m is incorrectly indented!')
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
end"
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:22:31 +00:00
|
|
|
git checkout-index --prefix="$temp/index/" --all
|
|
|
|
|
|
|
|
cd $temp/index/
|
2021-02-15 13:34:19 +00:00
|
|
|
|
|
|
|
if [ -n "$filter" ]; then
|
|
|
|
ln -s $GIT_DIR/ .git
|
|
|
|
$filter
|
|
|
|
git add --all
|
2022-06-14 13:16:19 +00:00
|
|
|
rm .git
|
2021-02-15 13:34:19 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$validation" ]; then
|
|
|
|
$validation || error=1
|
|
|
|
fi
|
|
|
|
|
2021-01-20 15:07:20 +00:00
|
|
|
case $indentation in
|
|
|
|
tab|tabs)
|
|
|
|
echo "Checking tab indentation"
|
|
|
|
for file in $(new); do
|
|
|
|
check '\t' $file || error=1
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
space|spaces)
|
|
|
|
echo "Checking space indentation"
|
|
|
|
for file in $(new); do
|
|
|
|
check ' ' $file || error=1
|
|
|
|
done
|
|
|
|
;;
|
2021-01-21 13:20:52 +00:00
|
|
|
""|off)
|
2021-01-20 15:07:20 +00:00
|
|
|
echo "Skipping indentation check"
|
|
|
|
;;
|
2021-01-21 13:20:52 +00:00
|
|
|
*)
|
2021-01-20 15:07:20 +00:00
|
|
|
echo "Cannot check for indentation type \033[31m'$indentation'\033[00m"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2022-05-27 08:27:19 +00:00
|
|
|
if [ -n "$error" ]
|
|
|
|
then exit 1
|
|
|
|
fi
|
|
|
|
|
2021-01-25 12:22:31 +00:00
|
|
|
cd $GIT_DIR
|
|
|
|
rm -rf $temp
|
|
|
|
|
2021-02-08 12:36:02 +00:00
|
|
|
hook=$(basename $0)
|
2022-05-27 08:27:19 +00:00
|
|
|
if [ -f "$GIT_DIR/hooks/$hook" ]
|
2021-02-08 12:36:02 +00:00
|
|
|
then
|
|
|
|
echo Running project-level hook: $GIT_DIR/hooks/$hook
|
|
|
|
$GIT_DIR/hooks/$hook
|
2022-05-27 08:27:19 +00:00
|
|
|
error=$?
|
|
|
|
if [ -n "$error" ]
|
|
|
|
then exit $error
|
|
|
|
fi
|
2021-02-08 12:36:02 +00:00
|
|
|
fi
|