Skip to content

Instantly share code, notes, and snippets.

@danjac
Created May 24, 2018 18:16
Show Gist options
  • Save danjac/f4c66938ede9f2cd60b4a43e2e1dbed6 to your computer and use it in GitHub Desktop.
Save danjac/f4c66938ede9f2cd60b4a43e2e1dbed6 to your computer and use it in GitHub Desktop.
import re
CASE_RE = r'^Case-(?P<case_id>\d+)'
MERGE_RE = r'^(merge|merged) (?P<branch_name>[\w-]+)'
def prefix_commit_message(repo, **kwargs):
commitctx = repo.commitctx
"""
Checks before commit when committing to default
"""
if (repo[None].branch() == 'default' and
raw_input('You are about to commit to the default '
'branch. Are you sure you wish to proceed? '
'([Y]/N) ') != 'y'):
# hg expects a truthy value on failure, for some reason
return True
def rewrite_ctx(ctx, error):
branch_name = ctx.branch()
if branch_name == 'default':
result = re.match(MERGE_RE, ctx._text)
if result:
merged = result.groups()[1]
title = raw_input("Description: ")
ctx._text = "%s: %s" % (merged, title)
else:
old_text = ctx._text
old_text_lower = old_text.lower()
if old_text_lower == 'mergeup':
old_text = 'Merged upstream changes'
elif old_text_lower == 'close':
old_text = 'Request implemented; case closed'
result = re.match(CASE_RE, branch_name)
if result:
branch_name = "Case %s" % result.groups()[0]
ctx._text = "%s: %s" % (branch_name, old_text)
return commitctx(ctx, error)
repo.commitctx = rewrite_ctx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment