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?
Asked
Active
Viewed 1,873 times
5
-
you could create an after commit hook to enforce it, but is it really worth it? – phoet Feb 08 '14 at 02:31
-
2A *pre*-commit hook seems like the way to go. An after (*post*-) commit hook cannot enforce anything. – larsks Feb 08 '14 at 03:20
-
1Congrats on keeping your question's text brief :) – 2540625 Aug 15 '14 at 23:29
-
You could use an editor which supports this, such as emacs and the Git-Commit minor mode. – Dec 11 '15 at 16:43
1 Answers
3
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
-
The right hook to check the message is `commit-msg`, not `pre-commit`; that's what the linked blog post uses. – Nickolay Jan 19 '16 at 09:52