-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit-msg
More file actions
executable file
·425 lines (343 loc) · 13.9 KB
/
commit-msg
File metadata and controls
executable file
·425 lines (343 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python
"""
This script will try to enforce the tips given in
http://chris.beams.io/posts/git-commit/.
Line length violations will cause the commit to fail.
* The subject should be shorter than 50 characters
* The line length of the body should be shorter than 72 characters
The script will ensure the following rules, by inserting them in the message.
* Capitalize the subject line
* Remove any punctuation from the subject line
* Seperate the subject line and the body with an empty line
In case of a failed commit the commit message will be saved in the .git folder
in order to used as a templete in the next commit.
The first paramter given to the 'commit-msg' git hook is the name
of the file in which the commit message is stored in.
"""
import os
import sys
import signal
import re
from collections import OrderedDict
import config
from message_store import run_cmd_output_lines, save
from pattern.en import Sentence, parse, mood
from pattern.en import INDICATIVE, IMPERATIVE, CONDITIONAL, SUBJUNCTIVE
# If the commit is to fail, due to an incorrect formated commit message,
# the information of why is stored here.
FAIL_REASON = []
# If any fixups was done on to the message, information of what fixups are stored here.
FIXUPS = []
# Warnings are shown in the end to the user. Warnings are not cause for a failure
# and are not fixable either.
WARNINGS = []
COMMIT_MSG_PATH = sys.argv[1]
C_END = "\033[0m"
C_RED = "\033[91m"
C_BLUE = "\033[96m"
C_GREEN = "\033[92m"
C_YELLOW = "\033[93m"
C_UNDERLINE = "\033[4m"
def underline(text):
"""
Returns the given text, but will be shown as underlined if print to the console.
"""
return C_UNDERLINE + text.replace(C_END, C_END + C_UNDERLINE) + C_END
def red(text):
"""
Returns the given text, but will be shown as red if print to the console.
"""
return C_RED + text.replace(C_END, C_END + C_RED) + C_END
def blue(text):
"""
Returns the given text, but will be shown as blue if print to the console.
"""
return C_BLUE + text.replace(C_END, C_END + C_BLUE) + C_END
def green(text):
"""
Returns the given text, but will be shown as green if print to the console.
"""
return C_GREEN + text.replace(C_END, C_END + C_GREEN ) + C_END
def yellow(text):
"""
Returns the given text, but will be shown as yellow if print to the console.
"""
return C_YELLOW + text.replace(C_END, C_END + C_YELLOW) + C_END
def indent(text):
"""
Returns the given text, but every line is indented with one tab.
"""
return "\t" + text.replace("\n", "\n\t")
def mood_color_c(_mood):
"""
Returns the color prefix corresponding to the given mood.
"""
if _mood == IMPERATIVE:
return C_GREEN
elif _mood == INDICATIVE:
return C_BLUE
elif _mood == SUBJUNCTIVE:
return C_RED
elif _mood == CONDITIONAL:
return C_YELLOW
def mood_color(_mood):
"""
Returns the color function corresponding to the given mood.
"""
if _mood == IMPERATIVE:
return green
elif _mood == INDICATIVE:
return blue
elif _mood == SUBJUNCTIVE:
return red
elif _mood == CONDITIONAL:
return yellow
def wait_for_input_matching(prompt, regex="[yn].*"):
"""
Prompts the user with the given prompt, until a input is
given that matches the given regex.
"""
sys.stdin = open('/dev/tty')
answer = raw_input(prompt).lower()
while not re.match(regex, answer):
answer = raw_input(prompt).lower()
return answer
def assert_subject_length(subject):
"""
Asserts that the subject line is shorter than config.SUBJECT_LINE_LENGTH_HARD_LIMIT.
Warns if above config.SUBJECT_LINE_LENGTH_WARNING.
"""
if len(subject) > config.SUBJECT_LINE_LENGTH_HARD_LIMIT:
FAIL_REASON.append(
"* Too long line (" + red(str(len(subject))) + ")! The subject line may not be " +
"longer than " + red(str(config.SUBJECT_LINE_LENGTH_HARD_LIMIT)) + " characters.")
elif len(subject) > config.SUBJECT_LINE_LENGTH_WARNING:
WARNINGS.append(
"* Long subject line (" + blue(str(len(subject))) + "). The preferable length of the " +
"subject line is below " + green(str(config.SUBJECT_LINE_LENGTH_WARNING)) + ", " +
"hard limit at " + red(str(config.SUBJECT_LINE_LENGTH_HARD_LIMIT)) + ".")
def assert_body_line_length(lines):
"""
Asserts that the body lines are shorter than config.BODY_LINE_LENGTH_HARD_LIMIT characters.
Warns if above config.BODY_LINE_LENGTH_WARNING.
"""
warned = False
for line in lines[1:]:
if len(line) > config.BODY_LINE_LENGTH_HARD_LIMIT:
FAIL_REASON.append(
"* Too long line (" + red(str(len(line))) + ")! No line of the body may contain " +
"more than " + green(str(config.BODY_LINE_LENGTH_HARD_LIMIT)) + " characters. ")
break
elif len(line) > config.BODY_LINE_LENGTH_WARNING and not warned:
warned = True # Only warn once.
WARNINGS.append(
"* Long line (" + blue(str(len(line))) + "). Line lengths above " +
green(str(config.BODY_LINE_LENGTH_WARNING)) +
" are discouraged, however the hard limit is " +
red(str(config.BODY_LINE_LENGTH_HARD_LIMIT)) + ".")
def ensure_subject_line_non_empty(lines):
"""
Ensures that the subject line is non empty.
If it is empty and there exist a next line, it is removed and a fixup message is added.
If it is empty and there does not exist a following line,
a fail reason is added and the commit will be aborted
"""
while len(lines) > 0 and re.match(r"^\s*$", lines[0]):
if len(lines) > 1:
lines.pop(0)
FIXUPS.append("* Removed the first line, it was all white space.")
else:
FAIL_REASON.append("* The commit message may not be empty.")
if re.match(r"^\s+.+", lines[0]):
lines[0] = re.sub(r"^\s", "", lines[0])
FIXUPS.append("* Removed leading white space in subject line.")
def ensure_capitalization(subject):
"""
Returns a correctly capitalized version of input subject line.
"""
if subject[0].islower():
FIXUPS.append("* Capitalized the subject line.")
return subject[0].upper() + subject[1:]
else:
return subject
def ensure_punctuation(subject):
"""
Returns a subject line without trailing punctuations.
"""
while subject[-1] == '.' or subject[-1] == '!' or subject[-1] == '?':
FIXUPS.append(
"* Removed '" + subject[-1] +
"' from the end of the subject line, no need for punctuations in the subject line.")
subject = subject[0:-1]
return subject
def ensure_subject_body_seperate(lines):
"""
Ensures that the subject line is seperated by a blank line.
The subject and the body needs to be seperated in order for
git to properly dicern them in the logs. Also i looks better.
"""
if len(lines) > 1 and not re.compile(r"^\s*$").match(lines[1]):
lines.insert(1, "")
FIXUPS.append("* Inserted an empty line between the subject and the body.")
def get_wordlist():
"""
If no wordlist is found one will be created.
"""
if not os.path.exists(os.path.expanduser(config.SPELL_CHECK_PERSONAL_WORDLIST)):
print "No person wordlist found, creating one "\
"at '" + config.SPELL_CHECK_PERSONAL_WORDLIST + "'"
wordlist = open(os.path.expanduser(config.SPELL_CHECK_PERSONAL_WORDLIST), 'a+')
wordlist.write("personal_ws-1.1 en 0\n")
else:
wordlist = open(os.path.expanduser(config.SPELL_CHECK_PERSONAL_WORDLIST), 'a')
return wordlist
def do_language_checks(message):
"""
Perform a spell check and a mood check and prompt the user
with the result if the result is negative.
"""
bad_mood_subject_line = False
bad_mood_sentence = False
bad_spelling_found = False
# Run spell checking, if enabled and available.
aspell = run_cmd_output_lines("which aspell")[0]
if config.DO_SPELL_CHECK and aspell == "":
print "No `aspell` found, install it to enable spell check."
if config.DO_SPELL_CHECK and not aspell == "":
aspell_cmd = "echo '" + message.replace("'", "'\\''") + "' | " + aspell + " list"
misspelled_words = set(run_cmd_output_lines(aspell_cmd)[:-1])
for word in misspelled_words:
message = re.sub(r"\b" + word + r"\b", underline(word), message)
bad_spelling_found = True
# Continue with mood check if enabled
if config.DO_MOOD_CHECK:
mood_by_sentence = OrderedDict()
sentenceable_message = message
# The subject line might not contain any punctuation
# But should allways be treated as a sentence.
subject = message.split("\n", 1)[0]
if len(subject) > 0 and (subject[-1] != "." or subject[-1] != "!" or subject[-1] != "?"):
sentenceable_message = message.replace(subject + "\n", subject + ".\n", 1)
# Split on punctuation and start of bullet point
sentences = re.split(r"[.?!]|^\s?[-*]\s?", sentenceable_message)
for sentence in sentences:
if re.match(r"^\s*$", sentence):
continue
sent = Sentence(parse(sentence, lemmata=True))
mood_by_sentence[sentence] = mood(sent)
if sentences[0] in mood_by_sentence and mood_by_sentence[sentences[0]] != IMPERATIVE:
bad_mood_subject_line = True
complete_message = ""
for sent, sent_mood in mood_by_sentence.iteritems():
end = message.find(sent) + len(sent)
complete_message += message[0:end].replace(sent, mood_color(sent_mood)(sent))
message = message[end:]
if sent_mood == SUBJUNCTIVE:
bad_mood_sentence = True
message = complete_message
# Prompt the user with any errors found.
if bad_mood_subject_line or bad_mood_sentence or bad_spelling_found:
count = 1
prompt = ""
if bad_mood_subject_line:
prompt += str(count) + ". "
prompt += "The mood of the subject line should be "
prompt += mood_color(IMPERATIVE)(IMPERATIVE)
prompt += " not "
prompt += mood_color(mood_by_sentence[sentences[0]])(mood_by_sentence[sentences[0]])
prompt += ".\n"
count += 1
if bad_mood_sentence:
prompt += str(count) + ". "
prompt += "No sentence in the body of the message should be "
prompt += mood_color(SUBJUNCTIVE)(SUBJUNCTIVE)
prompt += ".\n"
count += 1
if bad_spelling_found:
prompt += str(count) + ". "
prompt += underline("Underlnied")
prompt += " some questionable spellings"
prompt += " in the message.\n"
count += 1
print ""
print prompt
print ""
print indent(message)
print ""
print mood_color(IMPERATIVE)(IMPERATIVE + " (command)")
print mood_color(INDICATIVE)(INDICATIVE + " (fact/belief)")
print mood_color(CONDITIONAL)(CONDITIONAL + " (conjecture)")
print mood_color(SUBJUNCTIVE)(SUBJUNCTIVE + " (opinion/wish)")
print ""
if bad_spelling_found:
print "To extend your dictionary enter " + green("add") + "."
answer = wait_for_input_matching("Would you like to continue anyway? ", regex="[yna].*")
else:
answer = wait_for_input_matching("Would you like to continue anyway? ")
if answer.startswith('n'):
save(ORIGINAL_MESSAGE)
sys.exit(1) # Aborts the commit.
elif answer.startswith('a'):
wordlist = get_wordlist()
for word in misspelled_words:
add = wait_for_input_matching("Add " + green(word) + "? ")
if add.startswith('y'):
wordlist.write(word + "\n")
print "Added " + green(word)
print ""
wordlist.close()
answer = wait_for_input_matching("Would you like to continue now? ")
if answer.startswith('n'):
save(ORIGINAL_MESSAGE)
sys.exit(1) # Aborts the commit.
def signal_handler(signal, frame):
"""
Saves the commit message and kills the program with a
non zero exit code, which in turn will abort the commit.
"""
save(ORIGINAL_MESSAGE)
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
MESSAGE_FILE = open(COMMIT_MSG_PATH, 'r')
ORIGINAL_MESSAGE = MESSAGE_FILE.read()
MESSAGE_FILE.close()
MESSAGE = re.sub(r"^#(?:\n|.)*", "", ORIGINAL_MESSAGE, flags=re.M)
LINES = MESSAGE.splitlines()
# If the commit message is empty git itself will dissmiss it.
if not LINES:
sys.exit(0)
# The subject is the first line of the commit message
ensure_subject_line_non_empty(LINES)
LINES[0] = ensure_capitalization(LINES[0])
LINES[0] = ensure_punctuation(LINES[0])
assert_subject_length(LINES[0])
# Check the body of the message
if len(LINES) > 1:
assert_body_line_length(LINES)
ensure_subject_body_seperate(LINES)
# Done with the asserts!
# Did we fail? We want to fail as early as possible.
if FAIL_REASON:
# Save the message to be used in the next try.
save(ORIGINAL_MESSAGE)
print "COMMIT ABORTED!"
print "\n".join(FAIL_REASON)
print ""
print "Please do try again, or add '--no-verify' to skip"
sys.exit(1) # Aborts the commit
# Spell check and check for bad moods.
do_language_checks(MESSAGE)
# Did we do any fixups to the message?
if FIXUPS and config.DO_MESSAGE_FIXUP:
NEW_MESSAGE = "\n".join(LINES)
MSG_FILE = open(COMMIT_MSG_PATH, 'w')
MSG_FILE.write(NEW_MESSAGE)
MSG_FILE.close()
print ""
print "Did some fixups on the message for you:"
print "\n".join(FIXUPS)
print ""
if WARNINGS and config.PRINT_WARNINGS:
print "Friendly reminders:"
print "\n".join(WARNINGS)
print ""