【问题标题】:How do I enforce the 50 character commit summary line in Sublime?如何在 Sublime 中强制执行 50 个字符的提交摘要行?
【发布时间】:2014-02-08 02:16:53
【问题描述】:

作为一个天生健谈的人,我的摘要行几乎总是超过 50 个字符。如何在 sublime 中对提交消息和仅提交消息的标题行强制执行 50 个字符的限制?有没有办法自定义设置?

【问题讨论】:

  • 你可以创建一个提交后挂钩来强制执行它,但这真的值得吗?
  • 一个 pre-commit 钩子似乎是要走的路。 after (post-) 提交钩子不能强制执行任何操作。
  • 恭喜你问题的文字简短:)
  • 您可以使用支持此功能的编辑器,例如 emacs 和 Git-Commit 次要模式。

标签: git sublimetext commit


【解决方案1】:

sublime-text-git 这样的插件会强制使用soft wrap on git commit message

{
  "rulers": [70]
}

但这适用于所有行,而不仅仅是第一行。

您可以修改该插件以在您键入时更改颜色(但这需要一些 python 编程);这就是an editor like vim does:

请记住,对于之前的冗长消息,您可以通过 LESS 选项以适当的长度查看它们。见“How to wrap git commit comments?”。


否则,作为 larskscommentedcommit-msg hook like this one(来自 Addam Hardy,在 python 中)是真正执行该策略的唯一解决方案.

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

【讨论】:

  • 查看消息的正确钩子是commit-msg,而不是pre-commit;这就是链接的博客文章使用的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多