convert: hg: optionally create branches as clones
authorBrendan Cully <brendan@kublai.com>
Wed, 15 Aug 2007 13:21:23 -0700
changeset 5172 6b4c332f241b
parent 5171 f53d97d651f4
child 5173 7e05bdeee7de
child 5181 090ef15e1a03
convert: hg: optionally create branches as clones If convert.hg.clonebranches is set, branches will be created as clones of their parent revisions. All clones will be subdirectories of the destination path.
hgext/convert/__init__.py
hgext/convert/common.py
hgext/convert/hg.py
--- a/hgext/convert/__init__.py
+++ b/hgext/convert/__init__.py
@@ -194,6 +194,12 @@ class convert(object):
         filenames = []
 
         files, copies = self.source.getchanges(rev)
+        parents = [self.map[r] for r in commit.parents]
+        if commit.parents:
+            pbranch = self.commitcache[commit.parents[0]].branch
+        else:
+            pbranch = None
+        self.dest.setbranch(commit.branch, pbranch, parents)
         for f, v in files:
             newf = self.mapfile(f)
             if not newf:
@@ -213,7 +219,6 @@ class convert(object):
                             # Merely marks that a copy happened.
                             self.dest.copyfile(copyf, newf)
 
-        parents = [self.map[r] for r in commit.parents]
         newnode = self.dest.putcommit(filenames, parents, commit)
         self.mapentry(rev, newnode)
 
--- a/hgext/convert/common.py
+++ b/hgext/convert/common.py
@@ -134,3 +134,10 @@ class converter_sink(object):
         tags: {tagname: sink_rev_id, ...}"""
         raise NotImplementedError()
 
+    def setbranch(self, branch, pbranch, parents):
+        """Set the current branch name. Called before the first putfile
+        on the branch.
+        branch: branch name for subsequent commits
+        pbranch: branch name of parent commit
+        parents: destination revisions of parent"""
+        pass
--- a/hgext/convert/hg.py
+++ b/hgext/convert/hg.py
@@ -18,13 +18,15 @@ class mercurial_sink(converter_sink):
     def __init__(self, ui, path):
         self.path = path
         self.ui = ui
+        self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True)
+        self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False)
+        self.lastbranch = None
         try:
             self.repo = hg.repository(self.ui, path)
         except:
             raise NoRepo("could not open hg repo %s as sink" % path)
         self.lock = None
         self.wlock = None
-        self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True)
 
     def before(self):
         self.wlock = self.repo.wlock()
@@ -59,6 +61,30 @@ class mercurial_sink(converter_sink):
         except:
             pass
 
+    def setbranch(self, branch, pbranch, parents):
+        if (not self.clonebranches) or (branch == self.lastbranch):
+            return
+
+        self.lastbranch = branch
+        self.after()
+        if not branch:
+            branch = 'default'
+        if not pbranch:
+            pbranch = 'default'
+
+        branchpath = os.path.join(self.path, branch)
+        try:
+            self.repo = hg.repository(self.ui, branchpath)
+        except:
+            if not parents:
+                self.repo = hg.repository(self.ui, branchpath, create=True)
+            else:
+                self.ui.note(_('cloning branch %s to %s\n') % (pbranch, branch))
+                hg.clone(self.ui, os.path.join(self.path, pbranch),
+                         branchpath, rev=parents, update=False,
+                         stream=True)
+                self.repo = hg.repository(self.ui, branchpath)
+
     def putcommit(self, files, parents, commit):
         if not files:
             return hex(self.repo.changelog.tip())