Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As a naturally talkative soul, my summary lines almost always exceed 50 characters. How do I enforce a 50 character limit on my title lines in sublime for commit messages and commit messages only? Is there a way to customize the settings?

like image 819
Tara Roys Avatar asked Oct 21 '22 15:10

Tara Roys


1 Answers

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:

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

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
like image 182
VonC Avatar answered Oct 24 '22 12:10

VonC