检查提交消息的长度

时间:2016-01-07 16:23:47

标签: 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

直到现在这个工作正常。我试图重新提交一个提交并更改其提交消息,然后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.

我使用的方法不是很聪明。我该怎么做呢?

2 个答案:

答案 0 :(得分:3)

只需跳过评论(以#开头的行就可以了):

#!/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

答案 1 :(得分:1)

您可以使用从this post复制的脚本:

  

我希望这个脚本做什么:

     
      
  • 确认我的提交中有摘要行
  •   
  • 验证摘要行不超过50个字符
  •   
  • 确认没有行超过72个字符
  •   
  • 如果有任何错误,请拒绝我的提交并要求我重新格式化
  •   
  • 如果我选择重新格式化我的提交,请将我带回提交编辑器 - 并告诉我在提交消息的评论中我提交的确切错误
  •   
#!/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