Created
May 26, 2020 23:10
-
-
Save Ambro17/fc56167d95783f6b91385006c6f05db5 to your computer and use it in GitHub Desktop.
Auto Git Emoji
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/python3.6 | |
""" | |
Loosely based on gitmoji https://gitmoji.carloscuesta.me/ | |
WIP: π§ | |
Meant for work in progress. Incomplete or still an idea | |
NEW: β¨ | |
New functionality that is working. Meant as a checkpoint | |
FIX: π§ | |
Minor code changes/ Refactors that are steps before a NEW feature | |
BUG: π | |
Bugfix, it explais itself | |
ART: π¨ | |
Cosmetic changes based on linting. (Docstrings, typos, bad variable names, etc) | |
TDD: βοΈ | |
Test related changes. Create/Delete/Update tests | |
merge: π | |
Merge related changes | |
docs: π | |
Add documentation | |
Any combination of this three structures will work, case insensitive | |
* Opening -> [ ( { : | |
* Content -> wip new fix bug art tdd | |
* Closing -> ] ) } : | |
""" | |
import sys | |
import re | |
# Will match things like [wip: too, but anyway ... | |
# who would want to see that instead of a beautiful emoji? | |
full_regex = re.compile( | |
r'([{|:|\[|\(](wip|new|fix|bug|art|tdd|doc|merge)[}|:|\]|\)])', | |
re.IGNORECASE, | |
) | |
replacement_icons = { | |
'wip': 'π§', | |
'new': 'β¨', | |
'fix': 'π§', | |
'bug': 'π', | |
'art': 'π¨', | |
'tdd': 'βοΈ', | |
'merge': 'π', | |
'doc': 'π', | |
} | |
def insert_emojis(commit_file): | |
with open(commit_file, 'r') as f: | |
commit_message = f.read() | |
for full_match, emoji_str in set(full_regex.findall(commit_message)): | |
commit_message = commit_message.replace( | |
full_match, | |
replacement_icons[emoji_str.lower()], | |
) | |
with open(commit_file, 'w') as f: | |
f.write(commit_message) | |
def main(): | |
commit_file = sys.argv[1] | |
with open(commit_file, 'r') as f: | |
backup = f.read() | |
try: | |
insert_emojis(commit_file) | |
except Exception: | |
print("Could not place emojis in commit message") | |
with open(commit_file, 'w') as f: | |
f.write(backup) | |
finally: | |
sys.exit(0) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To setup in your git run
git config --global core.hooksPath /path/to/your/centralized/hooks
and inside that folder create this file with
commit-msg
as the name and add execution permissions (chmod +x commit-msg
).You can then check everything went ok with
git config --list
and you should see the new property value