检查提交消息的长度
Check length of commit message
我有一个 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 < ""
exit 0
到目前为止一切正常。我尝试重新提交并更改其提交消息,然后 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.
我用的方法不是很聪明。我该如何正确执行此操作?
您可以使用从 this post 复制的脚本:
What I want this script to do:
- Verify I have a summary line on my commit
- Verify the summary line is not over 50 characters
- Verify no line is over 72 characters
- If there are any errors, reject my commit and ask me to reformat
- If I choose to reformat my commit, bring me back into the commit editor and - show me what exactly was wrong with my commit in comments on the commit message
#!/usr/bin/python
import sys, os
from subprocess import call
print os.environ.get('EDITOR')
if os.environ.get('EDITOR') != 'none':
editor = os.environ['EDITOR']
else:
editor = "vim"
message_file = sys.argv[1]
def check_format_rules(lineno, line):
real_lineno = lineno + 1
if lineno == 0:
if len(line) > 50:
return "Error %d: First line should be less than 50 characters " \
"in length." % (real_lineno,)
if lineno == 1:
if line:
return "Error %d: Second line should be empty." % (real_lineno,)
if not line.startswith('#'):
if len(line) > 72:
return "Error %d: No line should be over 72 characters long." % (
real_lineno,)
return False
while True:
commit_msg = list()
errors = list()
with open(message_file) as commit_fd:
for lineno, line in enumerate(commit_fd):
stripped_line = line.strip()
commit_msg.append(line)
e = check_format_rules(lineno, stripped_line)
if e:
errors.append(e)
if errors:
with open(message_file, 'w') as commit_fd:
commit_fd.write('%s\n' % '# GIT COMMIT MESSAGE FORMAT ERRORS:')
for error in errors:
commit_fd.write('# %s\n' % (error,))
for line in commit_msg:
commit_fd.write(line)
re_edit = raw_input('Invalid git commit message format. Press y to edit and n to cancel the commit. [y/n]')
if re_edit.lower() in ('n','no'):
sys.exit(1)
call('%s %s' % (editor, message_file), shell=True)
continue
break
简单地跳过注释(以 #
开头的行起到了作用):
#!/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 < ""
exit 0
我有一个 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 < ""
exit 0
到目前为止一切正常。我尝试重新提交并更改其提交消息,然后 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.
我用的方法不是很聪明。我该如何正确执行此操作?
您可以使用从 this post 复制的脚本:
What I want this script to do:
- Verify I have a summary line on my commit
- Verify the summary line is not over 50 characters
- Verify no line is over 72 characters
- If there are any errors, reject my commit and ask me to reformat
- If I choose to reformat my commit, bring me back into the commit editor and - show me what exactly was wrong with my commit in comments on the commit message
#!/usr/bin/python
import sys, os
from subprocess import call
print os.environ.get('EDITOR')
if os.environ.get('EDITOR') != 'none':
editor = os.environ['EDITOR']
else:
editor = "vim"
message_file = sys.argv[1]
def check_format_rules(lineno, line):
real_lineno = lineno + 1
if lineno == 0:
if len(line) > 50:
return "Error %d: First line should be less than 50 characters " \
"in length." % (real_lineno,)
if lineno == 1:
if line:
return "Error %d: Second line should be empty." % (real_lineno,)
if not line.startswith('#'):
if len(line) > 72:
return "Error %d: No line should be over 72 characters long." % (
real_lineno,)
return False
while True:
commit_msg = list()
errors = list()
with open(message_file) as commit_fd:
for lineno, line in enumerate(commit_fd):
stripped_line = line.strip()
commit_msg.append(line)
e = check_format_rules(lineno, stripped_line)
if e:
errors.append(e)
if errors:
with open(message_file, 'w') as commit_fd:
commit_fd.write('%s\n' % '# GIT COMMIT MESSAGE FORMAT ERRORS:')
for error in errors:
commit_fd.write('# %s\n' % (error,))
for line in commit_msg:
commit_fd.write(line)
re_edit = raw_input('Invalid git commit message format. Press y to edit and n to cancel the commit. [y/n]')
if re_edit.lower() in ('n','no'):
sys.exit(1)
call('%s %s' % (editor, message_file), shell=True)
continue
break
简单地跳过注释(以 #
开头的行起到了作用):
#!/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 < ""
exit 0