Rev 3: Fill in commands. in file:///home/jelmer/bzr/bzr-rebase/
Jelmer Vernooij
jelmer at samba.org
Wed Jul 4 01:01:46 BST 2007
At file:///home/jelmer/bzr/bzr-rebase/
------------------------------------------------------------
revno: 3
revision-id: jelmer at samba.org-20070627000448-v16xyg6ua67ojfm1
parent: jelmer at samba.org-20070626224945-gpt2exzzuak611qa
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: bzr-rebase
timestamp: Wed 2007-06-27 02:04:48 +0200
message:
Fill in commands.
modified:
__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 '__init__.py'
--- a/__init__.py 2007-06-26 22:49:45 +0000
+++ b/__init__.py 2007-06-27 00:04:48 +0000
@@ -14,29 +14,59 @@
# 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.branch import Branch
from bzrlib.commands import Command, Option, display_command
+from bzrlib.errors import BzrCommandError
from bzrlib.workingtree import WorkingTree
class cmd_rebase(Command):
"""Re-base a branch.
"""
- takes_args = ['upstream']
+ takes_args = ['upstream_location']
takes_options = [Option('onto', help='Different revision to replay onto')]
@display_command
- def run(self, upstream, onto=None):
+ def run(self, upstream_location, onto=None):
+ from rebase import (generate_simple_plan, rebase,
+ rebase_plan_exists, write_rebase_plan,
+ read_rebase_plan)
+ upstream = Branch.open(upstream_location)
wt = WorkingTree.open('.')
- # TODO: Abort if there are any pending changes
- # TODO: Abort if there are any conflicts
- # TODO: Pull required revisions
- # TODO: Write plan file
- # TODO: Start executing plan
- # If any conflicts occur:
- # TODO: Apply failed merge to the working tree
- # TODO: Inform user about rebase-continue and rebase-abort
- # TODO: Abort
- # TODO: Remove plan file
+ wt.write_lock()
+ try:
+ # Abort if there already is a plan file
+ if rebase_plan_exists(wt):
+ raise BzrCommandError("A rebase operation was interrupted. Continue using 'bzr rebase-continue' or abort using 'bzr rebase-abort'")
+
+ # 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()
+
+ # Create plan
+ (start_rev, replace_map, rewrite_map) = generate_simple_plan(
+ wt.branch, upstream, onto)
+
+ # Write plan file
+ write_rebase_plan(wt, replace_map, rewrite_map)
+
+ # Set last-revision back to start revision
+ wt.set_last_revision(start_rev)
+
+ # Start executing plan
+ try:
+ rebase(wt, replace_map, rewrite_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
+ remove_rebase_plan(wt)
+ finally:
+ wt.unlock()
class cmd_rebase_abort(Command):
"""Abort an interrupted rebase
@@ -45,9 +75,14 @@
@display_command
def run(self):
+ from rebase import read_rebase_plan
wt = WorkingTree.open('.')
- # TODO: Read plan file
- # TODO: Set last revision
+ wt.write_lock()
+ try:
+ # Read plan file and set last revision
+ wt.set_last_revision_info(read_rebase_plan(wt)[0])
+ finally:
+ wt.unlock()
class cmd_rebase_continue(Command):
@@ -57,15 +92,25 @@
@display_command
def run(self):
+ from rebase import read_rebase_plan, rebase_plan_exists
wt = WorkingTree.open('.')
- # TODO: Read plan file
- # TODO: Abort if there are any conflicts
- # TODO: Start executing plan from current Branch.last_revision()
- # If conflict appears:
- # TODO: Apply failed merge to the working tree
- # TODO: Inform user about rebase-continue and rebase-abort
- # TODO: Abort
- # TODO: Remove plan file
+ wt.write_lock()
+ try:
+ # Abort if there are any conflicts
+ 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]
+
+ try:
+ # Start executing plan from current Branch.last_revision()
+ rebase(wt, replace_map, rewrite_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
+ remove_rebase_plan(wt)
+ finally:
+ wt.unlock()
def test_suite():
=== modified file 'rebase.py'
--- a/rebase.py 2007-06-26 22:49:45 +0000
+++ b/rebase.py 2007-06-27 00:04:48 +0000
@@ -13,3 +13,78 @@
# 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
+
+REBASE_PLAN_FILENAME = 'rebase-plan'
+
+def rebase_plan_exists(wt):
+ """Check whether there is a rebase plan present.
+
+ :param wt: Working tree for which to check.
+ :return: boolean
+ """
+ return wt._control_files.get(REBASE_PLAN_FILENAME).read() != ''
+
+
+def read_rebase_plan(wt):
+ """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.
+ """
+ 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):
+ """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)
+ """
+ wt._control_files.put(REBASE_PLAN_FILENAME,
+ generate_rebase_plan(wt.last_revision_info(), replace_map,
+ rewrite_map))
+
+
+def remove_rebase_plan(wt):
+ """Remove a rebase plan file.
+
+ :param wt: Working Tree for which to remove the plan.
+ """
+ wt._control_files.put(REBASE_PLAN_FILENAME, '')
+
+
+def generate_rebase_plan(last_rev_info, replace_map, rewrite_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)
+ :return: string
+ """
+ # TODO
+
+
+def parse_rebase_plan(text):
+ """Unmarshall a rebase plan.
+
+ :param text: Text to parse
+ :return: Tuple with last revision info, replace map and rewrite map.
+ """
+ # TODO
+
+
+def generate_simple_plan(subject_branch, upstream_branch, onto):
+ """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
+
+ :return: tuple with last_revision_info, replace map and rewrite map
+ """
+ # TODO
=== modified file 'test_rebase.py'
--- a/test_rebase.py 2007-06-26 22:49:45 +0000
+++ b/test_rebase.py 2007-06-27 00:04:48 +0000
@@ -14,3 +14,7 @@
# 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.tests import TestCase
+
+class RebasePlanReadWriterTests(TestCase):
+ pass
More information about the bazaar-commits
mailing list