Skip to content

Instantly share code, notes, and snippets.

@lgvital
Last active October 28, 2019 21:38
Show Gist options
  • Save lgvital/b994261cfcf1d6e34ea2ec1a02b365de to your computer and use it in GitHub Desktop.
Save lgvital/b994261cfcf1d6e34ea2ec1a02b365de to your computer and use it in GitHub Desktop.
hook to find clubhouse story in branch name and add to commit
#!/usr/bin/env python
"""
Adds clubhouse story # to your commit message.
1. Replace CLUBHOUSE_URL and TASK_REPLACE_STR
2. Copy this file to $GITREPOSITORY/.git/hooks/prepare-commit-msg
3. Mark hook as executable. e.g. chmod -x .git/hooks/prepare-commit-msg
"""
import sys, os, re
from subprocess import check_output
CLUBHOUSE_URL = 'https://app.clubhouse.io/crobot/story/' # replace with your clubhouse URL
TASK_REPLACE_STR = '<TASK_URL>' # replace with your task string in your commit template
# Collect the parameters
commit_msg_filepath = sys.argv[1]
if len(sys.argv) > 2:
commit_type = sys.argv[2]
else:
commit_type = ''
if len(sys.argv) > 3:
commit_hash = sys.argv[3]
else:
commit_hash = ''
print("prepare-commit-msg: File: %s\nType: %s\nHash: %s" % (commit_msg_filepath, commit_type, commit_hash))
# Figure out which branch we're on
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
print "prepare-commit-msg: On branch '%s'" % branch
# Populate the commit message with the prepare #, if there is one
if branch.startswith('story/'):
print "prepare-commit-msg: found story # branch."
result = re.match('story/([0-9]*)', branch)
story_number = result.group(1)
with open(commit_msg_filepath, 'r+') as f:
content = f.read()
f.seek(0, 0)
commit_msg = content.replace(TASK_REPLACE_STR, "%s%s" % (CLUBHOUSE_URL, story_number))
# if you wanted to instead append instead of find and replace:
# commit_msg = "%s %s%s" % (content, clubhouse_url, story_number)
f.write(commit_msg)
# f.write("%s %s%s" % (content, clubhouse_url, story_number)) # optional - find and replace instead of appending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment