Last active
January 31, 2018 15:54
-
-
Save yassu/d71fd7ac2a4233adaea171fc0e12d62c to your computer and use it in GitHub Desktop.
commit-msg file for prefix by coding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from sys import argv, stderr | |
from string import whitespace | |
from optparse import OptionParser | |
PREFIXES = ( | |
'feat', | |
'fix', | |
'docs', | |
'style', | |
'refactor', | |
'pref', | |
'test', | |
'chore') | |
PREFIX_USAGES = ( | |
'A new feature', | |
'A bug fix', | |
'Documentation only changes', | |
'Changes that do not affect the meaning of the code' | |
' (white-space, formatting, missing semi-colons, etc)', | |
'A code change that neither fixes a bug nor adds a feature', | |
'A code change that improves performance', | |
'Adding missing or correcting existing tests', | |
'Changes to the build process or auxiliary tools and libraries such as documentation generation' | |
) | |
def get_prefix(message): | |
return list(filter( | |
lambda s: not s.startswith("#") and s not in whitespace, | |
message.split('\n')))[0].split()[0].lower() | |
def get_prefix_list(): | |
return map(lambda s: s + ":", PREFIXES) | |
def get_parser(): | |
parser = OptionParser() | |
parser.add_option( | |
'--list', '-l', | |
action='store_true', | |
default=False, | |
dest='show_list', | |
help='show all prefixes') | |
return parser | |
def show_prefix_usage(): | |
for prefix, usage in zip(PREFIXES, PREFIX_USAGES): | |
print("{}: {}".format(prefix, usage)) | |
opts, args = get_parser().parse_args() | |
if opts.show_list: | |
show_prefix_usage() | |
else: | |
with open(argv[1]) as f: | |
message = f.read() | |
prefix = get_prefix(message) | |
if prefix not in get_prefix_list(): | |
stderr.write( | |
'There is no prefix.\n') | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment