Rev 17: Move maptree code to separate files. in file:///data/jelmer/bzr-rebase/trunk/
Jelmer Vernooij
jelmer at samba.org
Thu Jul 12 09:22:42 BST 2007
At file:///data/jelmer/bzr-rebase/trunk/
------------------------------------------------------------
revno: 17
revision-id: jelmer at samba.org-20070709163514-l1pqhcthz06lawak
parent: jelmer at samba.org-20070709163106-2lvmxf46qy473ua2
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: trunk
timestamp: Mon 2007-07-09 17:35:14 +0100
message:
Move maptree code to separate files.
added:
maptree.py maptree.py-20070709163407-quin1nc7pd9bp2mo-1
test_maptree.py test_maptree.py-20070709163406-wv3n8o12iygpxf4l-1
modified:
__init__.py __init__.py-20070626215909-fi0s39bkwxn4gcto-1
rebase.py rebase.py-20070626221123-ellanmf93nw8z9r1-1
test_rebase.py test_rebase.py-20070626221123-ellanmf93nw8z9r1-2
=== added file 'maptree.py'
--- a/maptree.py 1970-01-01 00:00:00 +0000
+++ b/maptree.py 2007-07-09 16:35:14 +0000
@@ -0,0 +1,107 @@
+# Copyright (C) 2006-2007 by Jelmer Vernooij
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""Map Tree."""
+
+from bzrlib.config import Config
+from bzrlib.errors import BzrError, NoSuchFile, UnknownFormatError
+from bzrlib.generate_ids import gen_revision_id
+from bzrlib import osutils
+from bzrlib.revision import NULL_REVISION
+from bzrlib.trace import mutter
+import bzrlib.ui as ui
+
+
+def map_file_ids(repository, old_parents, new_parents):
+ ret = {}
+ 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):
+ ret[ie.file_id] = newinv.path2id(path)
+ return ret
+
+
+class MapInventory:
+ def __init__(self, oldinv, maptree):
+ self.oldinv = oldinv
+ self.maptree = maptree
+
+ def map_ie(self, ie):
+ """Fix the references to old file ids in an inventory entry.
+
+ :param ie: Inventory entry to map
+ :return: New inventory entry
+ """
+ new_ie = ie.copy()
+ new_ie.file_id = self.maptree.new_id(new_ie.file_id)
+ new_ie.parent_id = self.maptree.new_id(new_ie.parent_id)
+ return new_ie
+
+ def __len__(self):
+ return len(self.oldinv)
+
+ def iter_entries(self):
+ for path, ie in self.oldinv.iter_entries():
+ yield path, self.map_ie(ie)
+
+
+class MapTree:
+ """Wrapper around a tree that translates file ids.
+ """
+ # TODO: Inventory
+ def __init__(self, oldtree, fileid_map):
+ """Create a new MapTree.
+
+ :param oldtree: Old tree to map to.
+ :param fileid_map: Map with old -> new file ids.
+ """
+ self.oldtree = oldtree
+ self.map = fileid_map
+ self.inventory = MapInventory(self.oldtree.inventory, self)
+
+ def old_id(self, file_id):
+ """Look up the original file id of a file.
+
+ :param file_id: New file id
+ :return: Old file id if mapped, otherwise new file id
+ """
+ for x in self.map:
+ if self.map[x] == file_id:
+ return x
+ return file_id
+
+ def new_id(self, file_id):
+ """Look up the new file id of a file.
+
+ :param file_id: Old file id
+ :return: New file id
+ """
+ try:
+ return self.map[file_id]
+ except KeyError:
+ 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)
=== added file 'test_maptree.py'
--- a/test_maptree.py 1970-01-01 00:00:00 +0000
+++ b/test_maptree.py 2007-07-09 16:35:14 +0000
@@ -0,0 +1,34 @@
+# Copyright (C) 2006-2007 by Jelmer Vernooij
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""Tests for the maptree code."""
+
+from bzrlib.tests import TestCase, TestCaseWithTransport
+from bzrlib.treebuilder import TreeBuilder
+
+from maptree import MapTree
+
+
+class MapTreeTests(TestCaseWithTransport):
+ def setUp(self):
+ super(MapTreeTests, self).setUp()
+
+ def test_empty_map(self):
+ tree = self.make_branch_and_memory_tree('branch')
+ builder = TreeBuilder()
+ builder.start_tree(tree)
+ builder.build(['foo'])
+ builder.finish_tree()
+ m = MapTree(tree, {})
=== modified file '__init__.py'
--- a/__init__.py 2007-07-09 16:31:06 +0000
+++ b/__init__.py 2007-07-09 16:35:14 +0000
@@ -193,8 +193,7 @@
loader = TestUtil.TestLoader()
suite = TestSuite()
- testmod_names = [
- 'test_rebase']
+ testmod_names = ['test_rebase', 'test_maptree']
suite.addTest(loader.loadTestsFromModuleNames(
["%s.%s" % (__name__, i) for i in testmod_names]))
=== modified file 'rebase.py'
--- a/rebase.py 2007-07-09 16:31:06 +0000
+++ b/rebase.py 2007-07-09 16:35:14 +0000
@@ -23,6 +23,8 @@
from bzrlib.trace import mutter
import bzrlib.ui as ui
+from maptree import MapTree, map_file_ids
+
REBASE_PLAN_FILENAME = 'rebase-plan'
REBASE_CURRENT_REVID_FILENAME = 'rebase-current'
REBASE_PLAN_VERSION = 1
@@ -257,90 +259,6 @@
assert all(map(repository.has_revision,
[replace_map[r][0] for r in replace_map]))
-
-
-def map_file_ids(repository, old_parents, new_parents):
- ret = {}
- 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):
- ret[ie.file_id] = newinv.path2id(path)
- return ret
-
-
-class MapInventory:
- def __init__(self, oldinv, maptree):
- self.oldinv = oldinv
- self.maptree = maptree
-
- def map_ie(self, ie):
- """Fix the references to old file ids in an inventory entry.
-
- :param ie: Inventory entry to map
- :return: New inventory entry
- """
- new_ie = ie.copy()
- new_ie.file_id = self.maptree.new_id(new_ie.file_id)
- new_ie.parent_id = self.maptree.new_id(new_ie.parent_id)
- return new_ie
-
- def __len__(self):
- return len(self.oldinv)
-
- def iter_entries(self):
- for path, ie in self.oldinv.iter_entries():
- yield path, self.map_ie(ie)
-
-
-class MapTree:
- """Wrapper around a tree that translates file ids.
- """
- # TODO: Inventory
- def __init__(self, oldtree, fileid_map):
- """Create a new MapTree.
-
- :param oldtree: Old tree to map to.
- :param fileid_map: Map with old -> new file ids.
- """
- self.oldtree = oldtree
- self.map = fileid_map
- self.inventory = MapInventory(self.oldtree.inventory, self)
-
- def old_id(self, file_id):
- """Look up the original file id of a file.
-
- :param file_id: New file id
- :return: Old file id if mapped, otherwise new file id
- """
- for x in self.map:
- if self.map[x] == file_id:
- return x
- return file_id
-
- def new_id(self, file_id):
- """Look up the new file id of a file.
-
- :param file_id: Old file id
- :return: New file id
- """
- try:
- return self.map[file_id]
- except KeyError:
- 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)
-
def replay_snapshot(repository, oldrevid, newrevid, new_parents):
"""Replay a commit by simply commiting the same snapshot with different parents.
=== modified file 'test_rebase.py'
--- a/test_rebase.py 2007-07-09 16:31:06 +0000
+++ b/test_rebase.py 2007-07-09 16:35:14 +0000
@@ -18,7 +18,6 @@
from bzrlib.errors import UnknownFormatError, NoSuchFile
from bzrlib.revision import NULL_REVISION
from bzrlib.tests import TestCase, TestCaseWithTransport
-from bzrlib.treebuilder import TreeBuilder
from rebase import (marshall_rebase_plan, unmarshall_rebase_plan,
replay_snapshot, generate_simple_plan,
@@ -233,16 +232,3 @@
wt = self.make_branch_and_tree('.')
write_active_rebase_revid(wt, None)
self.assertIs(None, read_active_rebase_revid(wt))
-
-
-class MapTreeTests(TestCaseWithTransport):
- def setUp(self):
- super(MapTreeTests, self).setUp()
-
- def test_empty_map(self):
- tree = self.make_branch_and_memory_tree('branch')
- builder = TreeBuilder()
- builder.start_tree(tree)
- builder.build(['foo'])
- builder.finish_tree()
- m = MapTree(tree, {})
More information about the bazaar-commits
mailing list