Rev 5: Import change_revision_parent from bzr-svn. in file:///data/jelmer/bzr-rebase/trunk/
Jelmer Vernooij
jelmer at samba.org
Thu Jul 12 09:22:36 BST 2007
At file:///data/jelmer/bzr-rebase/trunk/
------------------------------------------------------------
revno: 5
revision-id: jelmer at samba.org-20070704142405-9diewhvola010xec
parent: jelmer at samba.org-20070704130530-d79ro9s5ndtbif0p
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: bzr-rebase
timestamp: Wed 2007-07-04 16:24:05 +0200
message:
Import change_revision_parent from bzr-svn.
modified:
rebase.py rebase.py-20070626221123-ellanmf93nw8z9r1-1
test_rebase.py test_rebase.py-20070626221123-ellanmf93nw8z9r1-2
=== modified file 'rebase.py'
--- a/rebase.py 2007-07-04 13:05:30 +0000
+++ b/rebase.py 2007-07-04 14:24:05 +0000
@@ -14,9 +14,11 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+from bzrlib.config import Config
from bzrlib.errors import UnknownFormatError
from bzrlib.generate_ids import gen_revision_id
from bzrlib.trace import mutter
+import bzrlib.ui as ui
REBASE_PLAN_FILENAME = 'rebase-plan'
REBASE_PLAN_VERSION = 1
@@ -115,3 +117,98 @@
for revid in need_rewrite:
replace_map[revid] = gen_revision_id()
# TODO
+
+def rebase(wt, replace_map):
+ """Rebase a working tree according to the specified map.
+
+ :param wt: Working tree to rebase
+ :param replace_map: Dictionary with revisions to (optionally) rewrite
+ """
+ #TODO
+
+
+# Change the parent of a revision
+def change_revision_parent(repository, oldrevid, newrevid, new_parents):
+ """Create a copy of a revision with different parents.
+
+ :param repository: Repository in which the revision is present.
+ :param oldrevid: Revision id of the revision to copy.
+ :param newrevid: Revision id of the revision to create.
+ :param new_parents: Revision ids of the new parent revisions.
+ """
+ assert isinstance(new_parents, list)
+ mutter('creating copy %r of %r with new parents %r' % (newrevid, oldrevid, new_parents))
+ oldrev = repository.get_revision(oldrevid)
+
+ builder = repository.get_commit_builder(branch=None, parents=new_parents,
+ config=Config(),
+ committer=oldrev.committer,
+ timestamp=oldrev.timestamp,
+ timezone=oldrev.timezone,
+ revprops=oldrev.properties,
+ revision_id=newrevid)
+
+ # Check what new_ie.file_id should be
+ # use old and new parent inventories to generate new_id map
+ old_parents = oldrev.parent_ids
+ new_id = {}
+ for (oldp, newp) in zip(old_parents, new_parents):
+ oldinv = repository.get_revision_inventory(oldp)
+ newinv = repository.get_revision_inventory(newp)
+ for path, ie in oldinv.iter_entries():
+ if newinv.has_filename(path):
+ new_id[ie.file_id] = newinv.path2id(path)
+
+ i = 0
+ class MapTree:
+ def __init__(self, oldtree, map):
+ self.oldtree = oldtree
+ self.map = map
+
+ def old_id(self, file_id):
+ for x in self.map:
+ if self.map[x] == file_id:
+ return x
+ return file_id
+
+ def get_file_sha1(self, file_id, path=None):
+ return self.oldtree.get_file_sha1(file_id=self.old_id(file_id),
+ path=path)
+
+ def get_file(self, file_id):
+ return self.oldtree.get_file(self.old_id(file_id=file_id))
+
+ def is_executable(self, file_id, path=None):
+ return self.oldtree.is_executable(self.old_id(file_id=file_id),
+ path=path)
+
+ oldtree = MapTree(repository.revision_tree(oldrevid), new_id)
+ oldinv = repository.get_revision_inventory(oldrevid)
+ total = len(oldinv)
+ pb = ui.ui_factory.nested_progress_bar()
+ transact = repository.get_transaction()
+ try:
+ for path, ie in oldinv.iter_entries():
+ pb.update('upgrading file', i, total)
+ i += 1
+ new_ie = ie.copy()
+ if new_ie.revision == oldrevid:
+ new_ie.revision = None
+ def lookup(file_id):
+ try:
+ return new_id[file_id]
+ except KeyError:
+ return file_id
+
+ new_ie.file_id = lookup(new_ie.file_id)
+ new_ie.parent_id = lookup(new_ie.parent_id)
+ builder.record_entry_contents(new_ie,
+ map(repository.get_revision_inventory, new_parents),
+ path, oldtree)
+ finally:
+ pb.finished()
+
+ builder.finish_inventory()
+ return builder.commit(oldrev.message)
+
+
=== modified file 'test_rebase.py'
--- a/test_rebase.py 2007-07-04 13:05:30 +0000
+++ b/test_rebase.py 2007-07-04 14:24:05 +0000
@@ -15,9 +15,11 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from bzrlib.errors import UnknownFormatError
-from bzrlib.tests import TestCase
-
-from rebase import marshall_rebase_plan, unmarshall_rebase_plan
+from bzrlib.tests import TestCase, TestCaseWithTransport
+
+from rebase import (marshall_rebase_plan, unmarshall_rebase_plan,
+ change_revision_parent)
+
class RebasePlanReadWriterTests(TestCase):
def test_simple_marshall_rebase_plan(self):
@@ -42,3 +44,24 @@
1 bla
oldrev newrev newparent1 newparent2
""")
+
+
+class ConversionTests(TestCaseWithTransport):
+ def test_simple(self):
+ wt = self.make_branch_and_tree('.')
+ b = wt.branch
+ file('hello', 'w').write('hello world')
+ wt.add('hello')
+ wt.commit(message='add hello', rev_id="bla")
+ file('hello', 'w').write('world')
+ wt.commit(message='change hello', rev_id="bloe")
+ wt.set_last_revision("bla")
+ b.set_revision_history(["bla"])
+ file('hello', 'w').write('world')
+ wt.commit(message='change hello', rev_id="bla2")
+
+ newrev = change_revision_parent(wt.branch.repository, "bla2", "bla4",
+ ["bloe"])
+ self.assertEqual("bla4", newrev)
+ self.assertTrue(wt.branch.repository.has_revision(newrev))
+ self.assertEqual(["bloe"], wt.branch.repository.revision_parents(newrev))
More information about the bazaar-commits
mailing list