This is a follow-up from #2281551-14: Create input filter code for linking to git commits:

Furthermore, it would also be nice if we could turn the long URLs so they are displayed as the shorthand version as well (like GH does). So a comment that contains just the URL like: http://cgit.drupalcode.org/drupal/commit/?id=2da508579c6a30f8e51b1cbeaee...

Would be displayed as: drupal@2da50857

    $body = preg_replace_callback(
        '/https?:\/\/cgit\.drupalcode\.org\/([^/]*)\/commit\/.*(?:\?|&)id=([a-z0-9]+)/g',
        function ($matches) {
            return '<a href="' . $matches[0] . '" target="_blank">' . $matches[1] . '@' . substr($matches[2], 0, 8) . '</a>';
        },
        $body
    );

This would however need to be changed to utilize the callback introduced in that issue:

/**
 * Filter process callback for the Cgit absolute URL filter.
 */
function drupalorg_cgit_revision_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  $text = preg_replace_callback('/https?:\/\/cgit\.drupalcode\.org\/([^/]*)\/commit\/.*(?:\?|&)id=([a-z0-9]+)/g', 'versioncontrol_project_git_revision_process_callback', $text);
  return $text;
}

Question

Would it be beneficial to add a filter for the previous gitweb URLs? I know that there are redirects already in place, however I think it would at least help to present the URLs in the shorthand version while at the same time also converting them into the proper/current URLs (if one decides to copy/paste the link).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

markhalliwell’s picture

Title: Create input filter to convert absolute Cgit URLs into shorthand versions » Create input filter to convert absolute repository URLs into shorthand

Changing title to reflect that this issue doesn't have to be limited to the current Cgit URLs, if we indeed decide to also introduce the previous Gitweb URLs as well.

markhalliwell’s picture

The regex for old Gitweb URLs:

  // Replace old Gitweb URLs.
  $text = preg_replace_callback('/https?:\/\/(?:www\.)?drupalcode\.org\/project/([^/]+)\.git\/commit\/([a-z0-9]+)/g', 'versioncontrol_project_git_revision_process_callback', $text);
markhalliwell’s picture

Here is the patch. It is really CNR, but I am postponing this issue until the related issue is committed, which this patch is dependent on.

markhalliwell’s picture

Status: Active » Postponed
markhalliwell’s picture

I forgot that preg_replace_callback could take an array. Here is an updated patch.

markhalliwell’s picture

drumm’s picture

This looks like it would filter URLs already part of links, like <a href="http://cgit.drupalcode.org/drupal/commit/?id=2da508579c6a30f8e51b1cbeaeea45f88cce4034">my commit</a>. This part of the processing can likely be borrowed from core's URL filter.

markhalliwell’s picture

Ah, good point. I'll update the patch when I can.

YesCT’s picture