2014-02-08 17 views
4

作为一个自然健谈的灵魂,我的摘要行几乎总是超过50个字符。如何在标题行上强制执行50个字符的限制,仅针对提交消息和提交消息进行升级?有没有办法自定义设置?如何在Sublime中执行50个字符提交摘要行?

+0

你可以创建一个after commit hook来强制执行它,但它真的值得吗? – phoet

+2

A * pre * -commit hook似乎是要走的路。 after(* post * - )提交挂钩无法执行任何操作。 – larsks

+1

祝贺你保持你的问题的文本简短:) – jtheletter

回答

2

sublime-text-git一个插件将处以soft wrap on git commit message

{ 
    "rulers": [70] 
} 

但是,这适用于所有的行,不只是第一个。

你可以修改该插件来改变颜色,当你键入(但这将需要一些python编程);这是an editor like vim does

http://kparal.files.wordpress.com/2011/08/git-vim-commit.png?w=595

请记住,以前冗长的消息,您可以用适当的长度,通过一个较低的选择查看。参见“How to wrap git commit comments?”。


否则,commentedlarsks,一个commit-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 
+0

检查消息的右钩是'commit-msg',而不是'pre-commit';这就是链接的博客帖子使用的内容。 – Nickolay