[PATCH 3 of 4] Add user names mapping

Edouard Gomez ed.gomez at free.fr
Tue Feb 6 21:14:08 UTC 2007


# HG changeset patch
# User Edouard Gomez <ed.gomez at free.fr>
# Date 1170718579 -3600
# Node ID 5d510b4b3e8cc06a13b505dcde4f93e56a7c4914
# Parent  d9d0d9f3db675624e4f4308a0d732592eec703f7
Add user names mapping

Allows mapping usernames to new ones during conversion process.
 - Use -A option for first import
 - Then at the end of the conversion process and if the destination
   repo supports authorfile attribute, author map content is copied
   to the file pointed by the authorfile call.
 - On incremental conversions w/o any -A option specified, the
   destination authorfile, if any, gets read automatically.

EG: This allows mapping unix system usernames used in CVS accounts
    to a more typical "Firstname Lastname <address at server.org>" pair.

diff -r d9d0d9f3db67 -r 5d510b4b3e8c contrib/convert-repo
--- a/contrib/convert-repo	Tue Feb 06 15:46:50 2007 +0100
+++ b/contrib/convert-repo	Tue Feb 06 00:36:19 2007 +0100
@@ -381,6 +381,9 @@ class convert_mercurial:
     def mapfile(self):
         return os.path.join(self.path, ".hg", "shamap")
 
+    def authorsfile(self):
+        return os.path.join(self.path, ".hg", "authorsmap")
+
     def getheads(self):
         h = self.repo.changelog.heads()
         return [ hg.hex(x) for x in h ]
@@ -475,6 +478,7 @@ class convert:
         self.commitcache = {}
         self.mapfile = mapfile
         self.mapfilefd = None
+        self.authors = {}
 
         self.map = {}
         try:
@@ -485,6 +489,23 @@ class convert:
             origmapfile.close()
         except IOError:
             pass
+
+        authorsfile = None
+        if 'authors' in opts:
+            authorsfile = opts.get('authors')
+        elif hasattr(dest, 'authorsfile'):
+            authorsfile = dest.authorsfile()
+
+        try:
+            afile = open(authorsfile, 'rb')
+            for line in afile:
+                try:
+                    self.authors[line.split('=')[0].strip()] = line.split('=')[1].strip()
+                except IndexError:
+                    pass
+            afile.close()
+        except IOError:
+           pass
 
     def walktree(self, heads):
         visit = heads
@@ -568,6 +589,13 @@ class convert:
         self.mapfilefd.write("%s %s\n" % (src, dst))
         self.mapfilefd.flush()
 
+    def writeauthorsmap(self):
+        if len(self.authors) > 0 and hasattr(self.dest, 'authorsfile'):
+           ofile = open(self.dest.authorsfile(), 'w+')
+           for author in self.authors:
+               ofile.write("%s=%s\n" % (author, self.authors[author]))
+           ofile.close()
+
     def copy(self, rev):
         c = self.commitcache[rev]
         files = self.source.getchanges(rev)
@@ -602,6 +630,9 @@ class convert:
             if "\n" in desc:
                 desc = desc.splitlines()[0]
             status("%d %s\n" % (num, desc))
+            author = self.commitcache[c].author
+            if author in self.authors:
+                self.commitcache[c].author = self.authors[author]
             self.copy(c)
 
         tags = self.source.gettags()
@@ -617,6 +648,8 @@ class convert:
             # one so we don't end up with extra tag heads
             if nrev:
                 self.mapentry(c, nrev)
+
+        self.writeauthorsmap()
 
     def cleanup(self):
        if self.mapfilefd:
@@ -650,6 +683,7 @@ def command(src, dest=None, mapfile=None
         c.cleanup()  
 
 options = [('q', 'quiet', None, 'suppress output'),
+           ('A', 'authors', '', 'User name mapping file'),
            ('', 'datesort', None, 'try to sort changesets by date')]
 opts = {}
 args = fancyopts.fancyopts(sys.argv[1:], options, opts)



More information about the Mercurial-devel mailing list