Source

hg-patches / convert_marks.patch

Full commit
# HG changeset patch
# User Paul Molodowitch <pm@stanfordalumni.org>

convert: preserve local tags + bookmarks

diff -r f9f2d3679153 hgext/convert/convcmd.py
--- a/hgext/convert/convcmd.py	Thu May 20 17:26:17 2010 -0700
+++ b/hgext/convert/convcmd.py	Thu May 20 17:28:30 2010 -0700
@@ -333,13 +333,22 @@
                 self.ui.note(_("source: %s\n") % recode(c))
                 self.copy(c)
 
-            tags = self.source.gettags()
-            ctags = {}
-            for k in tags:
-                v = tags[k]
-                if self.map.get(v, SKIPREV) != SKIPREV:
-                    ctags[k] = self.map[v]
-
+            def convertrevs(oldrevs):
+                """
+                Given a dictionary mapping keys to revisions in the old repo,
+                returns a dictionary mapping those same keys to their mapped
+                revision in the new repo.
+                
+                Keys whose revisions do not exist in the new repo will be
+                filtered from the returned dict.
+                """
+                convRevDict = {}
+                for k, v in oldrevs.iteritems():
+                    if self.map.get(v, SKIPREV) != SKIPREV:
+                        convRevDict[k] = self.map[v]
+                return convRevDict
+                
+            ctags = convertrevs(self.source.gettags())
             if c and ctags:
                 nrev, tagsparent = self.dest.puttags(ctags)
                 if nrev and tagsparent:
@@ -349,6 +358,14 @@
                                    if e[1] == tagsparent]
                     if tagsparents:
                         self.map[tagsparents[0][0]] = nrev
+                        
+            if hasattr(self.source, 'getlocaltags') and hasattr(self.dest, 'putlocaltags'):
+                clocaltags = convertrevs(self.source.getlocaltags())
+                self.dest.putlocaltags(clocaltags)
+
+            if hasattr(self.source, 'getbookmarks') and hasattr(self.dest, 'putbookmarks'):
+                cmarks = convertrevs(self.source.getbookmarks())
+                self.dest.putbookmarks(cmarks)
 
             self.writeauthormap()
         finally:
diff -r f9f2d3679153 hgext/convert/filemap.py
--- a/hgext/convert/filemap.py	Thu May 20 17:26:17 2010 -0700
+++ b/hgext/convert/filemap.py	Thu May 20 17:28:30 2010 -0700
@@ -128,6 +128,11 @@
         self.origparents = {}
         self.children = {}
         self.seenchildren = {}
+        
+        if hasattr(self.base, 'getlocaltags'):
+            self.getlocaltags = self._getlocaltags
+        if hasattr(self.base, 'getbookmarks'):
+            self.getbookmarks = self._getbookmarks
 
     def before(self):
         self.base.before()
@@ -351,6 +356,12 @@
 
     def gettags(self):
         return self.base.gettags()
+    
+    def _getlocaltags(self):
+        return self.base.getlocaltags()
+
+    def _getbookmarks(self):
+        return self.base.getbookmarks()
 
     def hasnativeorder(self):
         return self.base.hasnativeorder()
diff -r f9f2d3679153 hgext/convert/hg.py
--- a/hgext/convert/hg.py	Thu May 20 17:26:17 2010 -0700
+++ b/hgext/convert/hg.py	Thu May 20 17:28:30 2010 -0700
@@ -21,7 +21,7 @@
 import os, time, cStringIO
 from mercurial.i18n import _
 from mercurial.node import bin, hex, nullid
-from mercurial import hg, util, context, error
+from mercurial import hg, util, context, error, commands
 
 from common import NoRepo, commit, converter_source, converter_sink
 
@@ -206,6 +206,23 @@
                              extra)
         self.repo.commitctx(ctx)
         return hex(self.repo.changelog.tip()), hex(tagparent)
+    
+    def putlocaltags(self, newLocals):
+        self.ui.status(_("updating localtags\n"))
+        for tag, rev in newLocals.iteritems():
+            try:
+                commands.tag(self.repo.ui, self.repo, tag, local=True, rev=rev)
+            except util.Abort, inst:
+                self.repo.ui.warn(str(inst))         
+        
+    def putbookmarks(self, newMarks):
+        from hgext import bookmarks
+        self.ui.status(_("updating bookmarks\n"))
+        for mark, rev in newMarks.iteritems():
+            try:
+                bookmarks.bookmark(self.repo.ui, self.repo, mark=mark, rev=rev)
+            except util.Abort, inst:
+                self.repo.ui.warn(str(inst))         
 
     def setfilemapmode(self, active):
         self.filemapmode = active
@@ -333,7 +350,19 @@
     def gettags(self):
         tags = [t for t in self.repo.tagslist() if t[0] != 'tip']
         return dict([(name, hex(node)) for name, node in tags
-                     if self.keep(node)])
+                     if self.keep(node) and self.repo.tagtype(name) == 'global'])
+        
+    def getlocaltags(self):
+        localtags = [t for t in self.repo.tagslist() if t[0] != 'tip']
+        return dict([(name, hex(node)) for name, node in localtags
+                     if self.keep(node) and self.repo.tagtype(name) == 'local'])        
+
+    def getbookmarks(self):
+        marks = {}
+        if hasattr(self.repo, '_bookmarks'):
+            for name, rev in self.repo._bookmarks.iteritems():
+                marks[name] = hex(rev)
+        return marks
 
     def getchangedfiles(self, rev, i):
         ctx = self.changectx(rev)