[PATCH 5 of 5] convert: add the --descmap option

Yury Sulsky yury.sulsky at gmail.com
Sat Nov 26 15:26:50 CST 2011


# HG changeset patch
# User Yury Sulsky <yury.sulsky at gmail.com>
# Date 1322340670 18000
# Branch stable
# Node ID 2d5e44d732e5207a683300f12207ef147b0e4e67
# Parent  74583e9f7f75bfa7b1a3c46ed3f81454e509a731
convert: add the --descmap option

diff -r 74583e9f7f75 -r 2d5e44d732e5 hgext/convert/__init__.py
--- a/hgext/convert/__init__.py	Sat Nov 26 05:11:03 2011 -0500
+++ b/hgext/convert/__init__.py	Sat Nov 26 15:51:10 2011 -0500
@@ -79,6 +79,23 @@
 
     Empty lines and lines starting with a ``#`` are ignored.
 
+    The descmap option allows you to rewrite commit messages in the
+    destination repository. The format uses indentation to distinguish
+    between revisions (specified on a line by themselves) and the
+    corresponding commit message::
+
+      revision
+
+        commit message line 1
+        commit message line 2 ...
+
+    If revision is the string ``DEFAULT``, all commits will have the
+    following description unless another one is specified in the
+    descmap file.
+
+    Lines starting with ``#'' are ignored and the commit messages
+    are stripped of whitespace at the beginning and at the end.
+
     The filemap is a file that allows filtering and remapping of files
     and directories. Each line can contain one of the following
     directives::
@@ -317,6 +334,8 @@
            _('import up to target revision REV'), _('REV')),
           ('A', 'authormap', '',
            _('remap usernames using this file'), _('FILE')),
+          ('', 'descmap', '',
+           _('remap commit messages using this file'), _('FILE')),
           ('', 'filemap', '',
            _('remap file names using contents of file'), _('FILE')),
           ('', 'splicemap', '',
diff -r 74583e9f7f75 -r 2d5e44d732e5 hgext/convert/common.py
--- a/hgext/convert/common.py	Sat Nov 26 05:11:03 2011 -0500
+++ b/hgext/convert/common.py	Sat Nov 26 15:51:10 2011 -0500
@@ -37,6 +37,7 @@
     pass
 
 SKIPREV = 'SKIP'
+DEFAULT = 'DEFAULT'
 
 class commit(object):
     def __init__(self, author, date, desc, parents, branch=None, rev=None,
diff -r 74583e9f7f75 -r 2d5e44d732e5 hgext/convert/convcmd.py
--- a/hgext/convert/convcmd.py	Sat Nov 26 05:11:03 2011 -0500
+++ b/hgext/convert/convcmd.py	Sat Nov 26 15:51:10 2011 -0500
@@ -5,7 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from common import NoRepo, MissingTool, SKIPREV, mapfile
+from common import NoRepo, MissingTool, SKIPREV, DEFAULT, mapfile
 from cvs import convert_cvs
 from darcs import darcs_source
 from git import convert_git
@@ -103,6 +103,7 @@
         self.commitcache = {}
         self.authors = {}
         self.authorfile = None
+        self.descmap = {}
 
         # Record converted revisions persistently: maps source revision
         # ID to target revision ID (both strings).  (This is how
@@ -118,6 +119,9 @@
             self.readauthormap(opts.get('authormap'))
             self.authorfile = self.dest.authorfile()
 
+        if opts.get('descmap'):
+            self.readdescmap(opts.get('descmap'))
+
         self.splicemap = mapfile(ui, opts.get('splicemap'))
         self.branchmap = mapfile(ui, opts.get('branchmap'))
 
@@ -291,10 +295,62 @@
 
         afile.close()
 
+    def readdescmap(self, descfile):
+        dmap  = self.descmap
+        dfile = open(descfile, 'r')
+        crev  = None
+
+        def indentation(line):
+            x = line.expandtabs()
+            return len(x) - len(x.lstrip())
+
+        def unindent(line, amt):
+            return (indentation(line) - amt)*' ' + line.lstrip()
+
+        def pushrev():
+            if crev is None: return
+            rev, lines = crev
+            indents = [indentation(line) for line in lines
+                       if line and not line.isspace()]
+            if indents:
+                amt = min(indents)
+                lines = [unindent(line, amt) for line in lines]
+            dmap[rev] = '\n'.join(lines).strip()
+
+        for line in dfile:
+
+            if line.startswith('#'):
+                continue
+
+            sline = line.strip()
+            isrev = line and not line[0].isspace()
+            if isrev:
+                pushrev()
+                rev = (sline==DEFAULT) and DEFAULT \
+                    or self.source.lookuprev(sline)
+                if rev is None:
+                    msg = _('Unknown revision in description map %s: %s')
+                    self.ui.warn(msg % (descfile, sline))
+                    crev = None
+                else:
+                    crev = (rev, [])
+            elif crev is not None:
+                crev[1].append(line)
+            elif sline:
+                msg = _('Ignoring indented line in description map %s: %s\n')
+                self.ui.warn(msg % (descfile, sline))
+
+        pushrev()
+        dfile.close()
+
     def cachecommit(self, rev):
         commit = self.source.getcommit(rev)
         commit.author = self.authors.get(commit.author, commit.author)
         commit.branch = self.branchmap.get(commit.branch, commit.branch)
+        if rev in self.descmap:
+            commit.desc = self.descmap[rev]
+        elif DEFAULT in self.descmap:
+            commit.desc = self.descmap[DEFAULT]
         self.commitcache[rev] = commit
         return commit
 
diff -r 74583e9f7f75 -r 2d5e44d732e5 tests/test-convert.t
--- a/tests/test-convert.t	Sat Nov 26 05:11:03 2011 -0500
+++ b/tests/test-convert.t	Sat Nov 26 15:51:10 2011 -0500
@@ -68,6 +68,22 @@
   
       Empty lines and lines starting with a "#" are ignored.
   
+      The descmap option allows you to rewrite commit messages in the
+      destination repository. The format uses indentation to distinguish between
+      revisions (specified on a line by themselves) and the corresponding commit
+      message:
+  
+        revision
+  
+          commit message line 1
+          commit message line 2 ...
+  
+      If revision is the string "DEFAULT", all commits will have the following
+      description unless another one is specified in the descmap file.
+  
+      Lines starting with "#'' are ignored and the commit messages are stripped
+      of whitespace at the beginning and at the end.
+  
       The filemap is a file that allows filtering and remapping of files and
       directories. Each line can contain one of the following directives:
   
@@ -269,6 +285,7 @@
    -d --dest-type TYPE   destination repository type
    -r --rev REV          import up to target revision REV
    -A --authormap FILE   remap usernames using this file
+      --descmap FILE     remap commit messages using this file
       --filemap FILE     remap file names using contents of file
       --splicemap FILE   splice synthesized history into place
       --branchmap FILE   change branch names while converting


More information about the Mercurial-devel mailing list