changeset 8690:c5b4f662109f

convert: add --sourcesort option for source specific sort Only supported by Mercurial source for now.
author Patrick Mezard <pmezard@gmail.com>
date Mon, 01 Jun 2009 17:12:39 +0200
parents 9bc95f8eb018
children a0a541d6fed6
files hgext/convert/__init__.py hgext/convert/common.py hgext/convert/convcmd.py hgext/convert/hg.py tests/test-convert-datesort tests/test-convert-datesort.out tests/test-convert.out
diffstat 7 files changed, 64 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/convert/__init__.py	Mon Jun 01 17:12:38 2009 +0200
+++ b/hgext/convert/__init__.py	Mon Jun 01 17:12:39 2009 +0200
@@ -247,7 +247,8 @@
           ('s', 'source-type', '', _('source repository type')),
           ('', 'splicemap', '', _('splice synthesized history into place')),
           ('', 'branchmap', '', _('change branch names while converting')),
-          ('', 'datesort', None, _('try to sort changesets by date'))],
+          ('', 'datesort', None, _('try to sort changesets by date')),
+          ('', 'sourcesort', None, _('preserve source changesets order'))],
          _('hg convert [OPTION]... SOURCE [DEST [REVMAP]]')),
     "debugsvnlog":
         (debugsvnlog,
--- a/hgext/convert/common.py	Mon Jun 01 17:12:38 2009 +0200
+++ b/hgext/convert/common.py	Mon Jun 01 17:12:39 2009 +0200
@@ -38,7 +38,7 @@
 
 class commit(object):
     def __init__(self, author, date, desc, parents, branch=None, rev=None,
-                 extra={}):
+                 extra={}, sortkey=None):
         self.author = author or 'unknown'
         self.date = date or '0 0'
         self.desc = desc
@@ -46,6 +46,7 @@
         self.branch = branch
         self.rev = rev
         self.extra = extra
+        self.sortkey = sortkey
 
 class converter_source(object):
     """Conversion source interface"""
--- a/hgext/convert/convcmd.py	Mon Jun 01 17:12:38 2009 +0200
+++ b/hgext/convert/convcmd.py	Mon Jun 01 17:12:39 2009 +0200
@@ -169,6 +169,13 @@
                 return next
             return picknext
 
+        def makesourcesorter():
+            """Source specific sort."""
+            keyfn = lambda n: self.commitcache[n].sortkey
+            def picknext(nodes):
+                return sorted(nodes, key=keyfn)[0]
+            return picknext
+
         def makedatesorter():
             """Sort revisions by date."""
             dates = {}
@@ -186,6 +193,8 @@
             picknext = makebranchsorter()
         elif sortmode == 'datesort':
             picknext = makedatesorter()
+        elif sortmode == 'sourcesort':
+            picknext = makesourcesorter()
         else:
             raise util.Abort(_('unknown sort mode: %s') % sortmode)
 
@@ -362,9 +371,11 @@
             shutil.rmtree(path, True)
         raise
 
-    sortmode = 'branchsort'
-    if opts.get('datesort'):
-        sortmode = 'datesort'
+    sortmodes = ('datesort', 'sourcesort')
+    sortmode = [m for m in sortmodes if opts.get(m)]
+    if len(sortmode) > 1:
+        raise util.Abort(_('more than one sort mode specified'))
+    sortmode = sortmode and sortmode[0] or 'branchsort'
 
     fmap = opts.get('filemap')
     if fmap:
--- a/hgext/convert/hg.py	Mon Jun 01 17:12:38 2009 +0200
+++ b/hgext/convert/hg.py	Mon Jun 01 17:12:39 2009 +0200
@@ -302,7 +302,8 @@
             crev = None
         return commit(author=ctx.user(), date=util.datestr(ctx.date()),
                       desc=ctx.description(), rev=crev, parents=parents,
-                      branch=ctx.branch(), extra=ctx.extra())
+                      branch=ctx.branch(), extra=ctx.extra(),
+                      sortkey=ctx.rev())
 
     def gettags(self):
         tags = [t for t in self.repo.tagslist() if t[0] != 'tip']
--- a/tests/test-convert-datesort	Mon Jun 01 17:12:38 2009 +0200
+++ b/tests/test-convert-datesort	Mon Jun 01 17:12:39 2009 +0200
@@ -20,10 +20,10 @@
 hg up -C 0
 hg branch branchb
 echo b >> b
-hg ci -Am b0 -d '5 0'
+hg ci -Am b0 -d '6 0'
 hg up -C brancha
 echo a >> a
-hg ci -m a4 -d '6 0'
+hg ci -m a4 -d '5 0'
 echo a >> a
 hg ci -m a5 -d '7 0'
 echo a >> a
@@ -34,7 +34,12 @@
 cd ..
 
 echo % convert with datesort
-hg convert --datesort t t2
+hg convert --datesort t t-datesort
 echo % graph converted repo
-hg -R t2 glog --template '{rev} "{desc}"\n'
+hg -R t-datesort glog --template '{rev} "{desc}"\n'
 
+echo % convert with datesort
+hg convert --sourcesort t t-sourcesort
+echo % graph converted repo
+hg -R t-sourcesort glog --template '{rev} "{desc}"\n'
+
--- a/tests/test-convert-datesort.out	Mon Jun 01 17:12:38 2009 +0200
+++ b/tests/test-convert-datesort.out	Mon Jun 01 17:12:39 2009 +0200
@@ -7,7 +7,40 @@
 1 files updated, 0 files merged, 1 files removed, 0 files unresolved
 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
 % convert with datesort
-initializing destination t2 repository
+initializing destination t-datesort repository
+scanning source...
+sorting...
+converting...
+8 a0
+7 a1
+6 a2
+5 a3
+4 a4
+3 b0
+2 a5
+1 a6
+0 b1
+% graph converted repo
+o  8 "b1"
+|
+| o  7 "a6"
+| |
+| o  6 "a5"
+| |
+o |  5 "b0"
+| |
+| o  4 "a4"
+| |
+| o  3 "a3"
+| |
+| o  2 "a2"
+| |
+| o  1 "a1"
+|/
+o  0 "a0"
+
+% convert with datesort
+initializing destination t-sourcesort repository
 scanning source...
 sorting...
 converting...
--- a/tests/test-convert.out	Mon Jun 01 17:12:38 2009 +0200
+++ b/tests/test-convert.out	Mon Jun 01 17:12:39 2009 +0200
@@ -210,6 +210,7 @@
     --splicemap    splice synthesized history into place
     --branchmap    change branch names while converting
     --datesort     try to sort changesets by date
+    --sourcesort   preserve source changesets order
 
 use "hg -v help convert" to show global options
 adding a