From 6448daf3921ce53b482e74db5bc2b17e8c43100f Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Fri, 21 Jan 2011 19:33:16 -0500
Subject: [PATCH] Add new gitweb-rewrite webviewer plugin for pathinfo gitweb feature.

---
 ...ioncontrolRepositoryUrlHandlerGitwebRewrite.inc |   55 ++++++++++++++++++++
 .../webviewer_url_handlers/gitweb-rewrite.inc      |   28 ++++++++++
 2 files changed, 83 insertions(+), 0 deletions(-)
 create mode 100644 includes/plugins/webviewer_url_handlers/VersioncontrolRepositoryUrlHandlerGitwebRewrite.inc
 create mode 100644 includes/plugins/webviewer_url_handlers/gitweb-rewrite.inc

diff --git includes/plugins/webviewer_url_handlers/VersioncontrolRepositoryUrlHandlerGitwebRewrite.inc includes/plugins/webviewer_url_handlers/VersioncontrolRepositoryUrlHandlerGitwebRewrite.inc
new file mode 100644
index 0000000..626104d
--- /dev/null
+++ includes/plugins/webviewer_url_handlers/VersioncontrolRepositoryUrlHandlerGitwebRewrite.inc
@@ -0,0 +1,55 @@
+<?php
+// $Id$
+/**
+ * @file
+ * Repository Url handler class for gitweb with pathinfo feature enabled.
+ */
+
+/**
+ * Support pathinfo gitweb feature.
+ *
+ * All this overwriten methods avoid using blob hashes and use just
+ * commit hashes. See plugin file for details in URLs.
+ */
+class VersioncontrolRepositoryUrlHandlerGitwebRewrite extends VersioncontrolRepositoryUrlHandler {
+
+  public function getItemLogViewUrl(&$item) {
+    $placeholders = array(
+      '%repo_name' => $this->repository->name,
+      '%path'      => $item->path,
+      '%revision'  => $item->revision,
+    );
+
+    if ($item->isFile()) {
+      return strtr($this->getTemplateUrl('file_log_view'), $placeholders);
+    }
+    else { // directory
+      return strtr($this->getTemplateUrl('directory_log_view'), $placeholders);
+    }
+  }
+
+  public function getItemViewUrl(&$item) {
+    $placeholders = array(
+      '%repo_name' => $this->repository->name,
+      '%path'      => $item->path,
+      '%revision'  => $item->revision,
+    );
+
+    $view_url = $item->isFile()
+      ? $this->getTemplateUrl('file_view')
+      : $this->getTemplateUrl('directory_view');
+
+    return strtr($view_url, $placeholders);
+  }
+
+  public function getDiffUrl(&$file_item_new, $file_item_old) {
+    $placeholders = array(
+      '%repo_name'    => $this->repository->name,
+      '%path'         => $file_item_new->path,
+      '%old_revision' => $file_item_new->revision,
+      '%new_revision' => $file_item_old->revision,
+    );
+
+    return strtr($this->getTemplateUrl('diff'), $placeholders);
+  }
+}
diff --git includes/plugins/webviewer_url_handlers/gitweb-rewrite.inc includes/plugins/webviewer_url_handlers/gitweb-rewrite.inc
new file mode 100644
index 0000000..8057851
--- /dev/null
+++ includes/plugins/webviewer_url_handlers/gitweb-rewrite.inc
@@ -0,0 +1,28 @@
+<?php
+// $Id$
+/**
+ * @file
+ * This plugin provides support for gitweb, when gitweb configuration
+ * contains "$feature{'pathinfo'}{'default'} = [1];".
+ */
+
+$plugin = array(
+  'vcs' => 'git',
+  'title' => t('Gitweb with pathinfo feature enabled URL autogenerator'),
+  'url_templates' => array(
+    // Revisions refer commit hashes for this plugin to support gitweb pathinfo feature.
+    'repository_view' => '%base_url/%repo_name.git',
+    'commit_view' => '%base_url/%repo_name.git/commit/%revision',
+    'file_log_view' => '%base_url/%repo_name.git/history/%revision:%path',
+    'directory_log_view' => '%base_url/%repo_name.git/history/%revision:%path',
+    'file_view' => '%base_url/%repo_name.git/blob/%revision:%path',
+    'directory_view' => '%base_url/%repo_name.git/tree/%revision:%path',
+    'diff' => '%base_url/%repo_name.git/blobdiff/%old_revision..%new_revision:%path',
+  ),
+  'handler' => array(
+    'class' => 'VersioncontrolRepositoryUrlHandlerGitwebRewrite',
+    'file' => 'VersioncontrolRepositoryUrlHandlerGitwebRewrite.inc',
+    'path' => drupal_get_path('module', 'versioncontrol_git') . '/includes/plugins/webviewer_url_handlers',
+    'parent' => 'none',
+  ),
+);
-- 
1.7.2.3

