将钩子放在($PREFIX/share/git-core/templates/hooks,其中$PREFIX可能/usr/local或/usr)下即可,注意chmod a+x FILENAME
commit 前代码indent钩子pre-commit:
#!/bin/bash
IFS=""
function get_type()
{
file "$1" | awk -F'[:,]' '{ print $2 }'
}
function get_status()
{
echo "$1" | awk '{ print $1 }'
}
function get_name()
{
echo "$1" | awk '{ print $2 }'
}
git status -s | while read -r line ; do
if [ "$(get_status "$line")" == "??" ] ; then
# echo "未被跟踪"
:
else
if [ "${line:0:1}" == " " ] ; then
# "没有新添加,啥也不是"
:
elif [ "${line:0:1}" != "D" ] ; then
# "有新添加,但不是删除"
file="$(get_name "$line")"
if [ "${file##*.}" == "c" ] || [ "${file##*.}" == "cpp" ] || [ "${file##*.}" == "h" ] || [ "${file##*.}" == "hpp" ] ; then
echo Mandatory indent: "$file"
indent -linux -l1000 "$file" > /dev/null 2>&1
git add "$file" > /dev/null 2>&1
elif [ "$(get_type "$file")" == " Bourne-Again shell script" ] ; then
echo Mandatory check "$file"
if shellcheck "$file" ; then
exit 1
fi
fi
fi
fi
done
注意indent对cpp不友好,出现问题可在出现问题的代码块前后加宏
/* *INDENT-OFF* */
extern void cam_depth_global_dtor(void) __attribute__((destructor));
/* *INDENT-ON* */