2021-01-20 15:07:20 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2021-02-15 13:34:19 +00:00
|
|
|
filter=$(git config hooks.filter)
|
2022-06-14 13:46:48 +00:00
|
|
|
validate=$(git config hooks.validate)
|
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)
|
2022-08-10 06:49:22 +00:00
|
|
|
export SRC_DIR=$(pwd)
|
|
|
|
export WORK_DIR=$(git rev-parse --show-toplevel)
|
2021-01-20 15:07:20 +00:00
|
|
|
|
2024-04-05 10:55:20 +00:00
|
|
|
export commit=commit
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:55:20 +00:00
|
|
|
get_marks() {
|
|
|
|
git status --porcelain | cut --bytes 4- | while read file
|
|
|
|
do
|
|
|
|
if [ -e "$temp/index/$file" ]
|
|
|
|
then
|
|
|
|
grep -n -i no$commit "$temp/index/$file" | sed "s/:/ /" | while read line
|
|
|
|
do
|
|
|
|
echo "$file:$line"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:22:31 +00:00
|
|
|
git checkout-index --prefix="$temp/index/" --all
|
|
|
|
|
2024-04-05 10:55:20 +00:00
|
|
|
marks=$(get_marks)
|
|
|
|
if [ -n "$marks" ]
|
|
|
|
then
|
|
|
|
/bin/echo -e "\x1b[31m'no$commit' mark(s) found:\x1b[0m"
|
|
|
|
echo $marks
|
|
|
|
rm -rf $temp
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-01-25 12:22:31 +00:00
|
|
|
cd $temp/index/
|
2021-02-15 13:34:19 +00:00
|
|
|
|
2023-12-06 09:31:16 +00:00
|
|
|
ln -s "$WORK_DIR"/.git/ .git
|
2021-02-15 13:34:19 +00:00
|
|
|
if [ -n "$filter" ]; then
|
2022-07-01 11:30:27 +00:00
|
|
|
sh -c "$filter"
|
2021-02-15 13:34:19 +00:00
|
|
|
git add --all
|
|
|
|
fi
|
|
|
|
|
2021-01-20 15:07:20 +00:00
|
|
|
case $indentation in
|
|
|
|
tab|tabs)
|
2023-12-06 09:31:16 +00:00
|
|
|
/bin/echo -e "\x1b[2mChecking indentation: \x1b[32mtabs"
|
2021-01-20 15:07:20 +00:00
|
|
|
for file in $(new); do
|
|
|
|
check '\t' $file || error=1
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
space|spaces)
|
2023-12-06 09:31:16 +00:00
|
|
|
/bin/echo -e "\x1b[2mChecking indentation: \x1b[32mspaces"
|
2021-01-20 15:07:20 +00:00
|
|
|
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
|
|
|
|
|
2023-12-06 09:31:16 +00:00
|
|
|
if [ -n "$validate" ]; then
|
|
|
|
/bin/echo -e "\x1b[33mValidating commit\x1b[0m"
|
|
|
|
/bin/echo -e "\x1b[2mλ $validate\x1b[0m"
|
2024-01-22 13:03:41 +00:00
|
|
|
if sh -c "$validate"
|
|
|
|
then
|
|
|
|
/bin/echo -e "\x1b[32mValidation Passed!\x1b[0m"
|
|
|
|
else
|
|
|
|
error=1
|
|
|
|
/bin/echo -e "\x1b[1;31mValidation Failed!\x1b[0m"
|
|
|
|
fi
|
2023-12-06 09:31:16 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
rm .git
|
|
|
|
|
2022-05-27 08:27:19 +00:00
|
|
|
if [ -n "$error" ]
|
|
|
|
then exit 1
|
|
|
|
fi
|
|
|
|
|
2023-12-06 09:31:16 +00:00
|
|
|
cd "$WORK_DIR"
|
2021-01-25 12:22:31 +00:00
|
|
|
rm -rf $temp
|
|
|
|
|
2021-02-08 12:36:02 +00:00
|
|
|
hook=$(basename $0)
|
2022-08-10 06:49:22 +00:00
|
|
|
if [ -f "$WORK_DIR/.git/hooks/$hook" ]
|
2021-02-08 12:36:02 +00:00
|
|
|
then
|
2022-09-01 10:55:30 +00:00
|
|
|
echo Running project-level hook: "$WORK_DIR"/.git/hooks/$hook
|
|
|
|
"$WORK_DIR"/.git/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
|