Automatically Link to Your Bug Database from TortoiseHg

One of my favorite features in TortoiseHg is the so-called "Issue Regex."

This feature lets you include bug tracking database ID numbers in your commit messages.  TortoiseHg will convert these numbers into clickable URLs after you make the commit.  The feature uses regular expressions to extract the ID numbers and replacement expressions to build the URLs, so it is very flexible.

The upshot?  The text FB 1234 becomes a hyperlink that links to https://grandjean.fogbugz.com/?1234.

Configuring the Feature

In TortoiseHg Workbench, go to File > Settings > Issue Tracking.

  • Set Issue Regex to a regular expression with one or more capture groups
  • Set Issue Link to a URL that references the capture group(s) from above

Building the Issue Regex

In the example above, I use the following Issue Regex:

(?i)FB\s{0,1}(\d+)|(?i)(W\d+)

Let's break this down.

The pipe character "|" represents an either/or operation.  So we can divide the regex into two distinct regexes:

(?i)FB\s{0,1}(\d+)
(?i)(W\d+)

The "(?i)" prefix is a modifier that means the regex should be considered case-insensitive.

If we remove that modifier prefix, the rest of the regex is easier to digest:

FB\s{0,1}(\d+)
(W\d+)

The top regex matches a string with:

  • FB: the literal characters "FB" (short for FogBugz, the bug-tracking database we use)
  • \s{0,1}: zero or one whitespace characters
  • (\d+): a capture group of one or more numeric digits (this is capture group 1)

The bottom regex matches a string with:

  • (W\d+): a capture group with a literal character "W" followed by one or more digits with no spaces in between (this is capture group 2)

The top regex extracts FogBugz case numbers, while the second one extracts references to FogBugz wiki articles (we stopped using the FogBugz wiki when we migrated to DokuWiki).

This part is pretty easy, assuming your bug tracker uses id-based URLs.

Here's our default Issue Link:

https://grandjean.fogbugz.com/?{1}{2}

Thus, the auto-generated URL consists of:

  • the literal characters: https://grandjean.fogbugz.com/?
  • {1}: the first capture group from above
  • {2}: the second capture group from above

Since the original regex had a pipe character (i.e., either-or operator) separating two different regexes, only one of the two capturing groups will ever match.

FogBugz cases can be looked up by simply passing the case number as the sole URL query parameter after your customer subdomain.  FogBugz wiki article URLs are similar, except that you need to include the literal "W" in the query parameter.

Regex Troubleshooting

If you are having trouble getting your regular expression to work, I highly recommend regex101.com for debugging purposes.  I wrote an in-depth explainer for that website here: RegEx101.com.

Reader Submissions

If you have set up Issue Tracking for a different bug-tracking database than FogBugz, please leave a comment below with your Issue Regex and Issue Link for the benefit of others.

External references

TortoiseHg
TortoiseHg Official Website.

Referenced articles

RegEx101.com
Writing and reading regular expressions is like speaking a foreign language. Think of regex101.com like Google Translate for regexes.

Image by Денис Марчук from Pixabay