Patchwork D7732: convert: refactor authormap into separate function for outside use

login
register
mail settings
Submitter phabricator
Date Jan. 5, 2020, 1:57 p.m.
Message ID <0a64919cae31d1388418a181df38059b@localhost.localdomain>
Download mbox | patch
Permalink /patch/44146/
State Not Applicable
Headers show

Comments

phabricator - Jan. 5, 2020, 1:57 p.m.
joerg.sonnenberger updated this revision to Diff 19035.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7732?vs=18948&id=19035

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7732/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7732

AFFECTED FILES
  hgext/convert/convcmd.py

CHANGE DETAILS




To: joerg.sonnenberger, #hg-reviewers
Cc: pulkit, mercurial-devel

Patch

diff --git a/hgext/convert/convcmd.py b/hgext/convert/convcmd.py
--- a/hgext/convert/convcmd.py
+++ b/hgext/convert/convcmd.py
@@ -55,6 +55,32 @@ 
 
 orig_encoding = b'ascii'
 
+def readauthormap(ui, authorfile, authors={}):
+    with open(authorfile, b'rb') as afile:
+        for line in afile:
+
+            line = line.strip()
+            if not line or line.startswith(b'#'):
+                continue
+
+            try:
+                srcauthor, dstauthor = line.split(b'=', 1)
+            except ValueError:
+                msg = _(b'ignoring bad line in author map file %s: %s\n')
+                ui.warn(msg % (authorfile, line.rstrip()))
+                continue
+
+            srcauthor = srcauthor.strip()
+            dstauthor = dstauthor.strip()
+            if authors.get(srcauthor) in (None, dstauthor):
+                msg = _(b'mapping author %s to %s\n')
+                ui.debug(msg % (srcauthor, dstauthor))
+                authors[srcauthor] = dstauthor
+                continue
+
+            m = _(b'overriding mapping for author %s, was %s, will be %s\n')
+            ui.status(m % (srcauthor, authors[srcauthor], dstauthor))
+    return authors
 
 def recode(s):
     if isinstance(s, pycompat.unicode):
@@ -448,32 +474,7 @@ 
             ofile.close()
 
     def readauthormap(self, authorfile):
-        afile = open(authorfile, b'rb')
-        for line in afile:
-
-            line = line.strip()
-            if not line or line.startswith(b'#'):
-                continue
-
-            try:
-                srcauthor, dstauthor = line.split(b'=', 1)
-            except ValueError:
-                msg = _(b'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 self.authors.get(srcauthor) in (None, dstauthor):
-                msg = _(b'mapping author %s to %s\n')
-                self.ui.debug(msg % (srcauthor, dstauthor))
-                self.authors[srcauthor] = dstauthor
-                continue
-
-            m = _(b'overriding mapping for author %s, was %s, will be %s\n')
-            self.ui.status(m % (srcauthor, self.authors[srcauthor], dstauthor))
-
-        afile.close()
+        self.authors = readauthormap(self.ui, authorfile, self.authors or {})
 
     def cachecommit(self, rev):
         commit = self.source.getcommit(rev)