Bug linking support
Bug linking support

This allows specification of a regex and url to find bug references in
commit messages, and allows them to be linked to an external bug
tracker. Currently this is just a very basic regex find and replace,
there's no advanced logic such as finding patterns that span lines, or
other preprocessing.
Currently this also only display in places where the full message text
is shown, and is not a link - the commit page, the commitdiff page, and
the log page. In the various other places where commit messages may be
shown (shortlog, headers, summary, etc), the message text is already a
link to the commit itself. Putting a link inside a link is weird
usability wise since that's not the way the user is expecting things to
behave.

Enhancement request #43

--- a/include/git/Project.class.php
+++ b/include/git/Project.class.php
@@ -152,6 +152,24 @@
 	protected $pushUrl = null;
 
 	/**
+	 * bugUrl
+	 *
+	 * Stores the bug url internally
+	 *
+	 * @access protected
+	 */
+	protected $bugUrl = null;
+
+	/**
+	 * bugPattern
+	 *
+	 * Stores the bug pattern internally
+	 *
+	 * @access protected
+	 */
+	protected $bugPattern = null;
+
+	/**
 	 * commitCache
 	 *
 	 * Caches fetched commit objects in case of
@@ -458,6 +476,64 @@
 	public function SetPushUrl($pUrl)
 	{
 		$this->pushUrl = $pUrl;
+	}
+
+	/**
+	 * GetBugUrl
+	 *
+	 * Gets the bug URL for this repository, if specified
+	 *
+	 * @access public
+	 * @return string bug url
+	 */
+	public function GetBugUrl()
+	{
+		if ($this->bugUrl != null)
+			return $this->bugUrl;
+
+		return GitPHP_Config::GetInstance()->GetValue('bugurl', '');
+	}
+
+	/**
+	 * SetBugUrl
+	 *
+	 * Overrides the bug URL for this repository
+	 *
+	 * @access public
+	 * @param string $bUrl bug url
+	 */
+	public function SetBugUrl($bUrl)
+	{
+		$this->bugUrl = $bUrl;
+	}
+
+	/**
+	 * GetBugPattern
+	 *
+	 * Gets the bug pattern for this repository, if specified
+	 *
+	 * @access public
+	 * @return string bug pattern
+	 */
+	public function GetBugPattern()
+	{
+		if ($this->bugPattern != null)
+			return $this->bugPattern;
+
+		return GitPHP_Config::GetInstance()->GetValue('bugpattern', '');
+	}
+
+	/**
+	 * SetBugPattern
+	 *
+	 * Overrides the bug pattern for this repository
+	 *
+	 * @access public
+	 * @param string $bPat bug pattern
+	 */
+	public function SetBugPattern($bPat)
+	{
+		$this->bugPattern = $bPat;
 	}
 
 	/**

--- /dev/null
+++ b/include/smartyplugins/modifier.buglink.php
@@ -1,1 +1,21 @@
+<?php
+/**
+ * Buglink
+ *
+ * Modifier to turn bug references into links
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2010 Christopher Han
+ * @package GitPHP
+ */
 
+function smarty_modifier_buglink($text, $pattern = null, $link = null)
+{
+	if (empty($text) || empty($pattern) || empty($link))
+		return $text;
+
+	$fullLink = '<a href="' . $link . '">${0}</a>';
+
+	return preg_replace($pattern, $fullLink, $text);
+}
+

--- a/templates/commit.tpl
+++ b/templates/commit.tpl
@@ -62,8 +62,10 @@
    </table>
  </div>
  <div class="page_body">
+   {assign var=bugpattern value=$project->GetBugPattern()}
+   {assign var=bugurl value=$project->GetBugUrl()}
    {foreach from=$commit->GetComment() item=line}
-     {$line|escape}<br />
+     {$line|escape|buglink:$bugpattern:$bugurl}<br />
    {/foreach}
  </div>
  <div class="list_head">

--- a/templates/commitdiff.tpl
+++ b/templates/commitdiff.tpl
@@ -20,8 +20,10 @@
  {include file='title.tpl' titlecommit=$commit}
  
  <div class="page_body">
+   {assign var=bugpattern value=$project->GetBugPattern()}
+   {assign var=bugurl value=$project->GetBugUrl()}
    {foreach from=$commit->GetComment() item=line}
-     {$line|escape}<br />
+     {$line|escape|buglink:$bugpattern:$bugurl}<br />
    {/foreach}
    <br />
    {* Diff each file changed *}

--- a/templates/log.tpl
+++ b/templates/log.tpl
@@ -67,8 +67,10 @@
      <em>{$rev->GetAuthorName()} [{$rev->GetAuthorEpoch()|date_format:"%a, %d %b %Y %H:%M:%S %z"}]</em><br />
    </div>
    <div class="log_body">
+     {assign var=bugpattern value=$project->GetBugPattern()}
+     {assign var=bugurl value=$project->GetBugUrl()}
      {foreach from=$rev->GetComment() item=line}
-       {$line|escape}<br />
+       {$line|escape|buglink:$bugpattern:$bugurl}<br />
      {/foreach}
      {if count($rev->GetComment()) > 0}
        <br />

comments