diff --git a/versioncontrol_project_git/versioncontrol_project_git.module b/versioncontrol_project_git/versioncontrol_project_git.module index 2449714..251cfcc 100644 --- a/versioncontrol_project_git/versioncontrol_project_git.module +++ b/versioncontrol_project_git/versioncontrol_project_git.module @@ -121,3 +121,68 @@ 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() { + $return = array(); + + $return['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 $return; +} + +/** + * 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 8-40. For example: drupal@2569242a, drupal@2569242afd5eec297e7d72065f4e3dd2586d0fd8"); + } + else { + return t('Referenced git commits (ex. drupal@2569242a) 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) { + $text = preg_replace_callback('/([a-z0-9_]{2,})@([a-z0-9]{8,40})/', 'versioncontrol_project_git_revision_process_callback', $text); + + return $text; +} + +/** + * preg_replace_callback replacement callback for git revision links. + * + * @see versioncontrol_project_git_revision_process + */ +function versioncontrol_project_git_revision_process_callback($matches) { + $machine_name = $matches[1]; + $revision = $matches[2]; + + try { + // Try and fetch a project for the machine name, then from that fetch a + // rep and then from that the URL handler, and finally a URL. + if (($nid = project_get_nid_from_machinename($machine_name)) && ($repo = versioncontrol_project_repository_load($nid)) && ($repo instanceof VersioncontrolRepository) && ($url_handler = $repo->getUrlHandler()) && ($url_handler instanceof VersioncontrolWebviewerUrlHandlerInterface)) { + $url = $url_handler->getCommitViewUrl($revision); + } + } + catch (Exception $e) { + // We ignore any exceptions, as we just don't form a URL. + } + + if (isset($url)) { + return l($matches[0], $url); + } + else { + return $matches[0]; + } +}