LSTANCZYK
2/12/2015 - 1:57 AM

hgfogbugz.py


"""
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