Patchwork [2,of,5,perfarce] add support for using an authormap as a fallback from P4 user names

login
register
mail settings
Submitter Dan Villiom Podlaski Christiansen
Date Nov. 29, 2014, 2:52 p.m.
Message ID <a6699b8868ea9892c34b.1417272729@dookie.local>
Download mbox | patch
Permalink /patch/6889/
State Not Applicable
Headers show

Comments

Dan Villiom Podlaski Christiansen - Nov. 29, 2014, 2:52 p.m.
# HG changeset patch
# User Dan Villiom Podlaski Christiansen  <danchr@gmail.com>
# Date 1417271360 -3600
#      Sat Nov 29 15:29:20 2014 +0100
# Node ID a6699b8868ea9892c34b15e9df9cbe3525c981b9
# Parent  f84a43b809a9c59516b2e9c732352c78fafddd96
add support for using an authormap as a fallback from P4 user names

Patch

diff --git a/perfarce.py b/perfarce.py
--- a/perfarce.py
+++ b/perfarce.py
@@ -217,6 +217,7 @@  class p4client(object):
         self.keep = ui.configbool('perfarce', 'keep', True)
         self.lowercasepaths = ui.configbool('perfarce', 'lowercasepaths', False)
         self.ignorecase = ui.configbool('perfarce', 'ignorecase', False)
+        self.authormap = self.readauthormap()
 
         # caches
         self.clientspec = {}
@@ -276,6 +277,42 @@  class p4client(object):
             if self.root.endswith('/'):
                 self.root = self.root[:-1]
 
+    def readauthormap(self):
+        authorfile = self.ui.configpath('perfarce', 'authormap')
+        authormap = {}
+
+        if not authorfile or not os.path.isfile(authorfile):
+            return authormap
+
+        afile = open(authorfile, 'r')
+        for line in afile:
+
+            line = line.strip()
+            if not line or line.startswith('#'):
+                continue
+
+            try:
+                srcauthor, dstauthor = line.split('=', 1)
+            except ValueError:
+                msg = _('ignoring bad line in author map file %s: %s\n')
+                self.ui.warn(msg % (authorfile, line.rstrip()))
+                continue
+
+            srcauthor = srcauthor.strip()
+            dstauthor = dstauthor.strip()
+            if authormap.get(srcauthor) in (None, dstauthor):
+                msg = _('mapping author %s to %s\n')
+                self.ui.debug(msg % (srcauthor, dstauthor))
+                authormap[srcauthor] = dstauthor
+                continue
+
+            m = _('overriding mapping for author %s, was %s, will be %s\n')
+            self.ui.status(m % (srcauthor, authormap[srcauthor], dstauthor))
+
+        afile.close()
+
+        return authormap
+
     @propertycache
     def filemapper(self):
         if self.ui.config('perfarce', 'filemap'):
@@ -613,15 +650,19 @@  class p4client(object):
             finally:
                 os.chdir(old)
 
-        else:
-            d = self.runone('user -o %s' % util.shellquote(user), abort=False)
-            if 'Update' in d:
-                try:
-                    r = '%s <%s>' % (d['FullName'], d['Email'])
-                    self.usercache[(user, None)] = r
-                    return r
-                except Exception:
-                    pass
+        d = self.runone('user -o %s' % util.shellquote(user), abort=False)
+        if 'Update' in d:
+            try:
+                r = '%s <%s>' % (d['FullName'], d['Email'])
+                self.usercache[(user, None)] = r
+                return r
+            except Exception:
+                pass
+
+        r = self.authormap.get(user)
+        if r:
+            self.usercache[(user, None)] = r
+            return r
 
         return user