From f5f88d603e31d53fd52e08093266bed6da7aaef7 Mon Sep 17 00:00:00 2001
From: Mark Carver <mark.carver@me.com>
Date: Thu, 18 Sep 2014 15:12:00 -0500
Subject: Issue #2281551 by Mark Carver, Steven Jones: Create input filter code
 for linking to git commits

---
 .../versioncontrol_project_git.module              | 87 ++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/versioncontrol_project_git/versioncontrol_project_git.module b/versioncontrol_project_git/versioncontrol_project_git.module
index 2449714..39d34e2 100644
--- a/versioncontrol_project_git/versioncontrol_project_git.module
+++ b/versioncontrol_project_git/versioncontrol_project_git.module
@@ -121,3 +121,90 @@ function versioncontrol_project_git_views_api() {
     'path' => drupal_get_path('module', 'versioncontrol_project_git') . '/views',
   );
 }
+
+/**
+ * Implements hook_filter_info().
+ */
+function versioncontrol_project_git_filter_info() {
+  $filters['versioncontrol_project_git'] = array(
+    'title' => t('Versioncontrol project - Git revision'),
+    'description' => t('Replaces git revision strings with links to the appropriate commit viewer.'),
+    'process callback' => 'versioncontrol_project_git_revision_filter_process',
+    'tips callback' => 'versioncontrol_project_git_revision_filter_tips',
+  );
+  return $filters;
+}
+
+/**
+ * Filter tips callback for the Git revision filter.
+ */
+function versioncontrol_project_git_revision_filter_tips($filter, $format, $long = FALSE) {
+  if ($long) {
+    return t('References to git commits in the form of project_short_name@commit_hash turn into links automatically. You may reference sandboxes by referencing their numeric short name, and you may use any length of commit hash from 7-40. For example: drupal@2569242, drupal@2569242afd5eec297e7d72065f4e3dd2586d0fd8');
+  }
+  else {
+    return t('Referenced git commits (ex. drupal@2569242) turn into links automatically.');
+  }
+}
+
+/**
+ * Filter process callback for the Git revision filter.
+ */
+function versioncontrol_project_git_revision_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
+  return preg_replace_callback('/([a-z0-9_]{2,})@([a-z0-9]{7,40})/', 'versioncontrol_project_git_revision_process_callback', $text);
+}
+
+/**
+ * A preg_replace_callback for generating git revision links.
+ *
+ * @param array $matches
+ *   The grouped matches from the regex that invoked this callback. This
+ *   callback assumes that $matches[1] is the project machine name and
+ *   $matches[2] is the commit revision.
+ *
+ * @return string
+ *   A link to the repository with a specific revision. The link will use the
+ *   shorthand display text of "project@revision" where "project" is the
+ *   machine name of the project and "revision" is the commit revision using
+ *   the "short" format. If this callback could not successfully generate a
+ *   link, the original text will be returned.
+ *
+ * @see versioncontrol_project_git_revision_filter_process()
+ * @see VersioncontrolBackend::formatRevisionIdentifier()
+ */
+function versioncontrol_project_git_revision_process_callback($matches) {
+  $original_text = $matches[0];
+  $machine_name = $matches[1];
+  $revision = $matches[2];
+
+  // Get the node ID for the project machine name.
+  $nid = project_get_nid_from_machinename($machine_name);
+  if (!$nid) {
+    return $original_text;
+  }
+
+  /* @var VersioncontrolGitRepository $repository */
+  $repository = versioncontrol_project_repository_load($nid);
+  if (!($repository instanceof VersioncontrolGitRepository)) {
+    return $original_text;
+  }
+
+  /* @var VersioncontrolRepositoryUrlHandler $url_handler */
+  $url_handler = $repository->getUrlHandler();
+  if (!($url_handler instanceof VersioncontrolRepositoryUrlHandler)) {
+    return $original_text;
+  }
+
+  // Get the URL for the revision.
+  $url = $url_handler->getCommitViewUrl($revision);
+  if (empty($url)) {
+    return $original_text;
+  }
+
+  // Generate a link to the revision.
+  return l($machine_name . '@' . $repository->getBackend()->formatRevisionIdentifier($revision, 'short'), $url, array(
+    'attributes' => array(
+      'target' => '_blank',
+    ),
+  ));
+}
--
2.0.1

