|
| 1 | +#!/usr/bin/env python |
| 2 | +import re, sys, os |
| 3 | + |
| 4 | +examples = """fix: navbar not responsive on mobile |
| 5 | +test: prepared test cases for user authentication |
| 6 | +chore: moved to semantic versioning |
| 7 | +feat(auth): added social login using twitter |
| 8 | +""" |
| 9 | + |
| 10 | +types = """build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm) |
| 11 | +ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs) |
| 12 | +docs: Documentation only changes |
| 13 | +feat: A new feature |
| 14 | +fix: A bug fix |
| 15 | +perf: A code change that improves performance |
| 16 | +refactor: A code change that neither fixes a bug nor adds a feature |
| 17 | +style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) |
| 18 | +test: Adding missing tests or correcting existing tests |
| 19 | +""" |
| 20 | + |
| 21 | +def main(): |
| 22 | + emojies = {"new": ':tada:', "build" : ':construction:' , "ci" : ':green_heart', "docs": ':books:', |
| 23 | + "feature" : ':sparkles:' ,"fix" : ':bug:', "perf": ':racehorse:', "refactor" : ':hammer:' |
| 24 | + , "style" : 1, "test": ':heavy_check_mark:', "chore" : ':arrow_up:' , "revert" : ':rewind:'} |
| 25 | + pattern = r'(new|build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert)(\([\w\-]+\))?:\s.*' |
| 26 | + filename = sys.argv[1] |
| 27 | + ss = open(filename, 'r').read() |
| 28 | + m = re.match(pattern, ss) |
| 29 | + if m == None: |
| 30 | + print("\nCOMMIT FAILED!") |
| 31 | + print("\nPlease enter commit message in the conventional format and try to commit again. Examples:") |
| 32 | + print("\n" + examples) |
| 33 | + print("\nCHECK COMMIT CONVENTIONS BELOW!\n" + types) |
| 34 | + print("\nCheck the Conventional Commits at https://www.conventionalcommits.org/\n" ) |
| 35 | + sys.exit(1) |
| 36 | + else: |
| 37 | + commitPrefix = ss.split(':')[0] |
| 38 | + commitPostfix = ss.split(':')[1] |
| 39 | + if '(' in commitPrefix: |
| 40 | + commitEmojie = emojies.get(commitPrefix.split('(')[0]) |
| 41 | + newCommit = open(filename, 'w') |
| 42 | + newCommit.write(commitPrefix + ": " + commitEmojie + commitPostfix) |
| 43 | + else: |
| 44 | + commitEmojie = emojies.get(commitPrefix) |
| 45 | + newCommit = open(filename, 'w') |
| 46 | + newCommit.write(commitPrefix + ": " + commitEmojie + commitPostfix) |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + main() |
0 commit comments