pfn*_*sel 5 git hook git-commit
我有一个git钩子可以防止提交超过 72 个字符的消息:
#!/usr/bin/env bash
# Hook to make sure that no commit message line exceeds 72 characters
while read line; do
if [ ${#line} -ge 72 ]; then
echo "Commit messages are limited to 72 characters."
echo "The following commit message has ${#line} characters."
echo "${line}"
exit 1
fi
done < "${1}"
exit 0
Run Code Online (Sandbox Code Playgroud)
到目前为止,这一切都运行良好。我尝试对提交进行变基并更改其提交消息,然后git正确地告诉我:
Commit messages are limited to 72 characters.
The following commit message has 81 characters.
# You are currently editing a commit while rebasing branch 'master' on '984734a'.
Could not amend commit after successfully picking 19b8030dc0ad2fc8186df5159b91e0efe862b981... Fill SUSY decay dictionary on the fly when needed
This is most likely due to an empty commit message, or the pre-commit hook
failed. If the pre-commit hook failed, you may need to resolve the issue before
you are able to reword the commit.
Run Code Online (Sandbox Code Playgroud)
我使用的方法不是很聪明。我该如何正确地做到这一点?
只需跳过注释(以 开头的行#就可以了):
#!/usr/bin/env bash
# Hook to make sure that no commit message line exceeds 72 characters
while read line; do
# Skip comments
if [ "${line:0:1}" == "#" ]; then
continue
fi
if [ ${#line} -ge 72 ]; then
echo "Commit messages are limited to 72 characters."
echo "The following commit message has ${#line} characters."
echo "${line}"
exit 1
fi
done < "${1}"
exit 0
Run Code Online (Sandbox Code Playgroud)