How do I enforce the 50 character commit summary line in Sublime?

不打扰是莪最后的温柔 提交于 2019-12-05 14:02:06
VonC

A plugin like sublime-text-git would impose a soft wrap on git commit message:

{
  "rulers": [70]
}

But that applies to all the lines, not just the first one.

You could modify that plugin to change colors when you type (but that would require some python programming); this is what an editor like vim does:

Remember that for previous lengthy message, you can view them with the appropriate length through a LESS option. See "How to wrap git commit comments?".


Otherwise, as commented by larsks, a commit-msg hook like this one (from Addam Hardy, in python) is the only solution to really enforce that policy.

#!/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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!