Rev 4: Add some tests. 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: 4
revision-id: jelmer at samba.org-20070704130530-d79ro9s5ndtbif0p
parent: jelmer at samba.org-20070627000448-v16xyg6ua67ojfm1
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: bzr-rebase
timestamp: Wed 2007-07-04 15:05:30 +0200
message:
  Add some tests.
modified:
  README                         readme-20070626220000-c62864tuxlldx6uc-1
  __init__.py                    __init__.py-20070626215909-fi0s39bkwxn4gcto-1
  rebase.py                      rebase.py-20070626221123-ellanmf93nw8z9r1-1
  test_rebase.py                 test_rebase.py-20070626221123-ellanmf93nw8z9r1-2
=== modified file 'README'
--- a/README	2007-06-26 22:49:45 +0000
+++ b/README	2007-07-04 13:05:30 +0000
@@ -12,13 +12,28 @@
 The rebase-state file contains the following information:
  * last-revision-info when the rebase was started
  * map of revisions that need to be replaced. In simple situations, 
-   this would just be one revision. In more complex situations 
-   (upgrading from one bzr-svn mapping version to another), it 
-   could be a longer list.
-
-   The map is from old to new revids.
- * map of revisions that need to be rewritten. In simple situations, 
-   this would be the set of revisions between the upstream head and the 
-   local working tree head.
-
-   The map is from old to (planned) new revids.
+   this would contain the revision on top of which the rebase is taking 
+   place and the revisions that are only on the branch that is being 
+   rebased.
+
+   The map is from old revid to a tupole with new revid and new parents.
+
+   For example, in the following scenario:
+
+   A -> B -> C -> D main
+        \ -> E -> F next
+
+   Where next would be rebased on top of main, the replace map would look 
+   something like this:
+
+	E -> (E', [D])
+	F -> (F', [E'])
+
+   Corner cases would be situations like this:
+
+    A -> B -> C -> D main
+	     |\-> G -\
+         \ -> E ---> F next
+
+    Would G get rebased on top of D rather than B?
+

=== modified file '__init__.py'
--- a/__init__.py	2007-06-27 00:04:48 +0000
+++ b/__init__.py	2007-07-04 13:05:30 +0000
@@ -15,7 +15,7 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 from bzrlib.branch import Branch
-from bzrlib.commands import Command, Option, display_command
+from bzrlib.commands import Command, Option, display_command, register_command
 from bzrlib.errors import BzrCommandError
 from bzrlib.workingtree import WorkingTree
 
@@ -42,25 +42,24 @@
             # Pull required revisions
             wt.branch.repository.fetch(upstream.repository, 
                                        upstream.last_revision())
-            if onto is not None:
-                wt.branch.repository.fetch(upstream.repository, onto)
-
             if onto is None:
                 onto = upstream.last_revision()
 
+            wt.branch.repository.fetch(upstream.repository, onto)
+
             # Create plan
-            (start_rev, replace_map, rewrite_map) = generate_simple_plan(
-                    wt.branch, upstream, onto)
+            replace_map = generate_simple_plan(
+                    wt.branch, upstream.last_revision(), onto)
 
             # Write plan file
-            write_rebase_plan(wt, replace_map, rewrite_map)
+            write_rebase_plan(wt, replace_map)
 
             # Set last-revision back to start revision
-            wt.set_last_revision(start_rev)
+            wt.set_last_revision(onto)
 
             # Start executing plan
             try:
-                rebase(wt, replace_map, rewrite_map)
+                rebase(wt, replace_map)
             except Conflict:
                 raise BzrCommandError("A conflict occurred applying a patch. Resolve the conflict and run 'bzr rebase-continue' or run 'bzr rebase-abort'.")
             # Remove plan file
@@ -100,11 +99,11 @@
             if len(wt.conflicts()) != 0:
                 raise BzrCommandError("There are still conflicts present")
             # Read plan file
-            (replace_map, rewrite_map) = read_rebase_plan(wt)[1:2]
+            replace_map = read_rebase_plan(wt)[1]
 
             try:
                 # Start executing plan from current Branch.last_revision()
-                rebase(wt, replace_map, rewrite_map)
+                rebase(wt, replace_map)
             except Conflict:
                 raise BzrCommandError("A conflict occurred applying a patch. Resolve the conflict and run 'bzr rebase-continue' or run 'bzr rebase-abort'.")
             # Remove plan file  
@@ -113,6 +112,10 @@
             wt.unlock()
 
 
+register_command(cmd_rebase)
+register_command(cmd_rebase_abort)
+register_command(cmd_rebase_continue)
+
 def test_suite():
     from unittest import TestSuite
     from bzrlib.tests import TestUtil

=== modified file 'rebase.py'
--- a/rebase.py	2007-06-27 00:04:48 +0000
+++ b/rebase.py	2007-07-04 13:05:30 +0000
@@ -14,7 +14,12 @@
 # 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.errors import UnknownFormatError
+from bzrlib.generate_ids import gen_revision_id
+from bzrlib.trace import mutter
+
 REBASE_PLAN_FILENAME = 'rebase-plan'
+REBASE_PLAN_VERSION = 1
 
 def rebase_plan_exists(wt):
     """Check whether there is a rebase plan present.
@@ -29,24 +34,22 @@
     """Read a rebase plan file.
 
     :param wt: Working Tree for which to write the plan.
-    :return: Tuple with last revision info, replace map and rewrite map.
+    :return: Tuple with last revision info and replace map.
     """
     text = wt._control_files.get(REBASE_PLAN_FILENAME).read()
     if text == '':
         raise BzrError("No rebase plan exists")
-    return parse_rebase_plan(text)
-
-
-def write_rebase_plan(wt, replace_map, rewrite_map):
+    return unmarshall_rebase_plan(text)
+
+
+def write_rebase_plan(wt, replace_map):
     """Write a rebase plan file.
 
     :param wt: Working Tree for which to write the plan.
-    :param replace_map: Replace map (old revid -> new revid)
-    :param rewrite_map: Rewrite map (old revid -> new revid)
+    :param replace_map: Replace map (old revid -> (new revid, new parents))
     """
     wt._control_files.put(REBASE_PLAN_FILENAME, 
-            generate_rebase_plan(wt.last_revision_info(), replace_map, 
-                                 rewrite_map))
+            marshall_rebase_plan(wt.last_revision_info(), replace_map))
 
 
 def remove_rebase_plan(wt):
@@ -57,34 +60,58 @@
     wt._control_files.put(REBASE_PLAN_FILENAME, '')
 
 
-def generate_rebase_plan(last_rev_info, replace_map, rewrite_map):
+def marshall_rebase_plan(last_rev_info, replace_map):
     """Marshall a rebase plan.
 
     :param last_rev_info: Last revision info tuple.
-    :param replace_map: Replace map (old revid -> new revid)
-    :param rewrite_map: Rewrite map (old revid -> new revid)
+    :param replace_map: Replace map (old revid -> (new revid, new parents))
     :return: string
     """
-    # TODO
-
-
-def parse_rebase_plan(text):
+    ret = "# Bazaar rebase plan %d\n" % REBASE_PLAN_VERSION
+    ret += "%d %s\n" % last_rev_info
+    for oldrev in replace_map:
+        (newrev, newparents) = replace_map[oldrev]
+        ret += "%s %s" % (oldrev, newrev) + \
+            "".join([" %s" % p for p in newparents]) + "\n"
+    return ret
+
+
+def unmarshall_rebase_plan(text):
     """Unmarshall a rebase plan.
 
     :param text: Text to parse
-    :return: Tuple with last revision info, replace map and rewrite map.
+    :return: Tuple with last revision info, replace map.
     """
-    # TODO
-
-
-def generate_simple_plan(subject_branch, upstream_branch, onto):
+    lines = text.split('\n')
+    # Make sure header is there
+    if lines[0] != "# Bazaar rebase plan %d" % REBASE_PLAN_VERSION:
+        raise UnknownFormatError(lines[0])
+
+    pts = lines[1].split(" ", 1)
+    last_revision_info = (int(pts[0]), pts[1])
+    replace_map = {}
+    for l in lines[2:]:
+        if l == "":
+            # Skip empty lines
+            continue
+        pts = l.split(" ")
+        replace_map[pts[0]] = (pts[1], pts[2:])
+    return (last_revision_info, replace_map)
+
+def generate_simple_plan(subject_branch, start_revid, onto_revid):
     """Create a simple rebase plan that replays history based 
     on one revision being mapped.
 
     :param subject_branch: Branch that will be changed
-    :param upstream_branch: Branch 
-    :param onto: Revision on top of which subject will be replayed 
+    :param start_revid: Revision at which to start replaying
+    :param onto_revid: Revision on top of which to replay
 
-    :return: tuple with last_revision_info, replace map and rewrite map
+    :return: replace map
     """
+    replace_map = {}
+
+    need_rewrite = []
+
+    for revid in need_rewrite:
+        replace_map[revid] = gen_revision_id()
     # TODO

=== modified file 'test_rebase.py'
--- a/test_rebase.py	2007-06-27 00:04:48 +0000
+++ b/test_rebase.py	2007-07-04 13:05:30 +0000
@@ -14,7 +14,31 @@
 # 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.errors import UnknownFormatError
 from bzrlib.tests import TestCase
 
+from rebase import marshall_rebase_plan, unmarshall_rebase_plan
+
 class RebasePlanReadWriterTests(TestCase):
-    pass
+    def test_simple_marshall_rebase_plan(self):
+        self.assertEqualDiff(
+"""# Bazaar rebase plan 1
+1 bla
+oldrev newrev newparent1 newparent2
+""", marshall_rebase_plan((1, "bla"), 
+                          {"oldrev": ("newrev", ["newparent1", "newparent2"])}))
+
+    def test_simple_unmarshall_rebase_plan(self):
+        self.assertEquals(((1, "bla"), 
+                          {"oldrev": ("newrev", ["newparent1", "newparent2"])}),
+                         unmarshall_rebase_plan("""# Bazaar rebase plan 1
+1 bla
+oldrev newrev newparent1 newparent2
+"""))
+
+    def test_unmarshall_rebase_plan_formatunknown(self):
+        self.assertRaises(UnknownFormatError,
+                         unmarshall_rebase_plan, """# Bazaar rebase plan x
+1 bla
+oldrev newrev newparent1 newparent2
+""")




More information about the bazaar-commits mailing list