Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
Forked from keyo/hgfogbugz.py
Last active August 29, 2015 14:15
Show Gist options
  • Save LSTANCZYK/452ada7a7bdac1034ef6 to your computer and use it in GitHub Desktop.
Save LSTANCZYK/452ada7a7bdac1034ef6 to your computer and use it in GitHub Desktop.
"""
HG Fogbugz Mercurial Extension
Prevents committing bad BugIDs
Add to .hgrc or mercurial.ini
[extensions]
fogbugz = C:\Users\User\hg_extensions\hgfogbugz.py
"""
import re
import mercurial, sys, os
_branch_regex = re.compile('(feature|bug|case|bugid|fogbugz)_(?P<bugid>\d+)')
_commit_regex = re.compile(r'\b(?P<case>(review|case|bug[zs]?(\s| )*(id)?:?)s?(\s| )*([#:; ]| )+)((([ ,:;#]|and)*)(?P<bugid>\d+))+',re.I)
def pretxncommithook(ui, repo, **kwargs):
"""
Checks a single commit message for adherence to commit message rules.
To use add the following to your project .hg/hgrc for each
project you want to check, or to your user hgrc/mercurial.ini to apply to all projects.
[hooks]
pretxncommit.hgfogbugz = python:hgfogbugz.pretxncommithook
"""
commit_message = repo['tip'].description()
branch = repo['tip'].branch()
commit_match = _commit_regex.match(commit_message)
branch_match = _branch_regex.match(branch)
if commit_match and branch_match and (commit_match.group('bugid') != branch_match.group('bugid')):
ui.warn('Commit bugid does not match with branch name bugid.\n')
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment