From 7af070a4b7aeb3f091dc1f9f293a0491f21c2d68 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Fri, 3 Dec 2010 05:17:18 -0500
Subject: [PATCH] feature #498086: Implement a user_mapping_method() that only use only the email address.

---
 .../VersioncontrolUserMapperGitMailOnly.class.php  |   35 ++++++++++++++++++++
 .../plugins/user_mapping_methods/git_mailonly.inc  |   10 ++++++
 versioncontrol_git.log.inc                         |    2 +-
 versioncontrol_git.module                          |    9 +++++
 4 files changed, 55 insertions(+), 1 deletions(-)
 create mode 100644 includes/plugins/user_mapping_methods/VersioncontrolUserMapperGitMailOnly.class.php
 create mode 100644 includes/plugins/user_mapping_methods/git_mailonly.inc

diff --git includes/plugins/user_mapping_methods/VersioncontrolUserMapperGitMailOnly.class.php includes/plugins/user_mapping_methods/VersioncontrolUserMapperGitMailOnly.class.php
new file mode 100644
index 0000000..2683215
--- /dev/null
+++ includes/plugins/user_mapping_methods/VersioncontrolUserMapperGitMailOnly.class.php
@@ -0,0 +1,35 @@
+<?php
+// $Id$
+
+/**
+ * Plugin to map using only email assuming a usual git user formatting.
+ *
+ * Assumes $commit->author and $commit->committer have the format:
+ *   user.name <user.email>
+ */
+class VersioncontrolUserMapperGitMailOnly implements VersioncontrolUserMapperInterface {
+  public function mapAuthor(VersioncontrolOperation $commit) {
+    return $this->map($commit->author);
+  }
+
+  public function mapCommitter(VersioncontrolOperation $commit) {
+    return $this->map($commit->committer);
+  }
+
+  public function map($person) {
+    if ($email = $this->extractMail($person)) {
+      $uid = db_result(db_query("SELECT uid FROM {users} WHERE mail = '%s'", $email));
+    }
+    return empty($uid) ? FALSE : $uid;
+  }
+
+  public function extractMail($person) {
+    preg_match('/(?P<email>[[:alnum:]\.]+@[[:alnum:]\.]+)/', $person, $matches);
+    if (isset($matches['email'])) {
+      if (valid_email_address($matches['email'])) {
+        return $matches['email'];
+      }
+    }
+    return FALSE;
+  }
+}
diff --git includes/plugins/user_mapping_methods/git_mailonly.inc includes/plugins/user_mapping_methods/git_mailonly.inc
new file mode 100644
index 0000000..c8c5530
--- /dev/null
+++ includes/plugins/user_mapping_methods/git_mailonly.inc
@@ -0,0 +1,10 @@
+<?php
+// $Id$
+
+$plugin = array(
+  'title' => t('Map using only email assuming a usual git user formatting'),
+  'mapper' => array(
+    'class' => 'VersioncontrolUserMapperGitMailOnly',
+    'file' => drupal_get_path('module', 'versioncontrol_git') . '/includes/plugins/user_mapping_methods/VersioncontrolUserMapperGitMailOnly.class.php',
+  ),
+);
diff --git versioncontrol_git.log.inc versioncontrol_git.log.inc
index e07607c..103c78f 100644
--- versioncontrol_git.log.inc
+++ versioncontrol_git.log.inc
@@ -335,7 +335,7 @@ function _versioncontrol_git_log_parse_and_insert_commit(VersioncontrolRepositor
   $op = new VersioncontrolGitOperation($repository->getBackend());
   $op->build($op_data);
   $op->labels = _versioncontrol_git_log_get_branches_of_commit($revision, $branch_label_list);
-  $op->insert();
+  $op->insert(array('map users' => TRUE));
 
   $item_action = $merge ? VERSIONCONTROL_ACTION_MERGED : VERSIONCONTROL_ACTION_MODIFIED;
   // build the data array to be used as default values for the item revision
diff --git versioncontrol_git.module versioncontrol_git.module
index 69307cc..6606ec7 100644
--- versioncontrol_git.module
+++ versioncontrol_git.module
@@ -109,3 +109,12 @@ function _versioncontrol_git_get_branch_intersect($repository, $item1, $item2) {
   }
   return array_pop($branches1); // We don't know any keys in $branches, so we use array_pop here. Also it'll return NULL if needed
 }
+
+/**
+ * Implementation of ctools hook_ctools_plugin_directory().
+ */
+function versioncontrol_git_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'versioncontrol') {
+    return "includes/plugins/$plugin";
+  }
+}
-- 
1.7.2.3

