From 8379a348350a0e6749ce8f458da9af207a132ac3 Mon Sep 17 00:00:00 2001
From: Jimmy Berry <jimmy@boombatower.com>
Date: Tue, 27 Sep 2011 02:10:37 -0500
Subject: Issue #493074 by boombatower: Back-link to the commit as a comment
 on the related issue.

---
 versioncontrol_project.module |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/versioncontrol_project.module b/versioncontrol_project.module
index dc8d11e..375c8de 100644
--- a/versioncontrol_project.module
+++ b/versioncontrol_project.module
@@ -633,3 +633,40 @@ function versioncontrol_project_commits_page($display_type, $node) {
   }
   return versioncontrol_render_commitlog_view($view_name, 'default', array($node->nid), '', $feed_url);
 }
+
+/**
+ * Implementation of hook_versioncontrol_operation().
+ */
+function versioncontrol_project_versioncontrol_operation($op, $operation, $operation_items) {
+  // Look for a commit operation being inserted that has an issue reference in
+  // the commit message to a valid project issue node.
+  if ($op == 'insert' && $operation['type'] == VERSIONCONTROL_OPERATION_COMMIT &&
+    preg_match('/#(\d+)/', $operation['message'], $match) && ($node = node_load($match[0])) && $node->type == 'project_issue') {
+
+    // Build project_issue followup changes and t() parameters for comment.
+    $changes = array(
+      'nid' => $node->nid,
+    );
+    $comment = array(
+      '!revision' => $operation['revision'],
+      '@message' => $operation['message'],
+    );
+
+    // If the Drupal user is known then format the author as a link and post the
+    // comment as the detected user. Otherwise format the author as the VCS
+    // username without a link and post as the auto-followup user.
+    if ($operation['uid']) {
+      $comment['!author'] = l($operation['username'], 'user/' . $operation['uid']);
+      $changes['comment'] = t('Commit !revision by !author: "@message"', $comment);
+
+      $changes['uid'] = $operation['uid'];
+      project_issue_add_followup($changes);
+    }
+    else {
+      $comment['!author'] = $operation['username'];
+      $changes['comment'] = t('Commit !revision by !author: "@message"', $comment);
+
+      project_issue_add_auto_followup($changes);
+    }
+  }
+}
-- 
1.7.6.3

