/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
1
# Copyright (C) 2007 by Jelmer Vernooij
2
# 
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0.436.1 by Jelmer Vernooij
Add framework for git-rebase plugin.
16
0.436.4 by Jelmer Vernooij
Add some tests.
17
from bzrlib.commands import Command, Option, display_command, register_command
0.436.16 by Jelmer Vernooij
Some more work on maptree.
18
from bzrlib.errors import (BzrCommandError, ConflictsInTree, NoSuchFile, 
19
                           UnrelatedBranches)
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
20
from bzrlib.trace import info
0.436.1 by Jelmer Vernooij
Add framework for git-rebase plugin.
21
22
class cmd_rebase(Command):
23
    """Re-base a branch.
24
25
    """
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
26
    takes_args = ['upstream_location?']
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
27
    takes_options = ['revision', 'merge-type', 
28
                     Option('onto', help='Different revision to replay onto')]
0.436.1 by Jelmer Vernooij
Add framework for git-rebase plugin.
29
    
30
    @display_command
0.436.16 by Jelmer Vernooij
Some more work on maptree.
31
    def run(self, upstream_location=None, onto=None, revision=None, 
32
            merge_type=None):
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
33
        from bzrlib.branch import Branch
34
        from bzrlib.revisionspec import RevisionSpec
35
        from bzrlib.workingtree import WorkingTree
0.436.16 by Jelmer Vernooij
Some more work on maptree.
36
        from rebase import (generate_simple_plan, rebase, rebase_plan_exists, 
37
                            read_rebase_plan, remove_rebase_plan, 
38
                            workingtree_replay, write_rebase_plan)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
39
        wt = WorkingTree.open('.')
40
        wt.lock_write()
41
        if upstream_location is None:
42
            upstream_location = wt.branch.get_parent()
0.436.3 by Jelmer Vernooij
Fill in commands.
43
        upstream = Branch.open(upstream_location)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
44
        upstream_repository = upstream.repository
45
        upstream_revision = upstream.last_revision()
0.436.3 by Jelmer Vernooij
Fill in commands.
46
        try:
47
            # Abort if there already is a plan file
48
            if rebase_plan_exists(wt):
49
                raise BzrCommandError("A rebase operation was interrupted. Continue using 'bzr rebase-continue' or abort using 'bzr rebase-abort'")
50
51
            # Pull required revisions
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
52
            wt.branch.repository.fetch(upstream_repository, 
53
                                       upstream_revision)
0.436.3 by Jelmer Vernooij
Fill in commands.
54
            if onto is None:
55
                onto = upstream.last_revision()
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
56
            else:
57
                onto = RevisionSpec.from_string(onto)
58
59
            wt.branch.repository.fetch(upstream_repository, onto)
60
61
            revhistory = wt.branch.revision_history()
62
            revhistory.reverse()
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
63
            common_revid = None
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
64
            for revid in revhistory:
65
                if revid in upstream.revision_history():
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
66
                    common_revid = revid
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
67
                    break
68
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
69
            if common_revid is None:
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
70
                raise UnrelatedBranches()
0.436.4 by Jelmer Vernooij
Add some tests.
71
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
72
            if common_revid == upstream.last_revision():
73
                raise BzrCommandError("Already rebased on %s" % upstream)
74
0.436.16 by Jelmer Vernooij
Some more work on maptree.
75
            start_revid = wt.branch.get_rev_id(
76
                    wt.branch.revision_id_to_revno(common_revid)+1)
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
77
0.436.3 by Jelmer Vernooij
Fill in commands.
78
            # Create plan
0.436.4 by Jelmer Vernooij
Add some tests.
79
            replace_map = generate_simple_plan(
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
80
                    wt.branch.repository, 
81
                    wt.branch.revision_history(), start_revid, onto)
0.436.3 by Jelmer Vernooij
Fill in commands.
82
83
            # Write plan file
0.436.4 by Jelmer Vernooij
Add some tests.
84
            write_rebase_plan(wt, replace_map)
0.436.3 by Jelmer Vernooij
Fill in commands.
85
86
            # Start executing plan
87
            try:
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
88
                rebase(wt.branch.repository, replace_map, workingtree_replay(wt, merge_type=merge_type))
0.436.8 by Jelmer Vernooij
Couple more minor fixes.
89
            except ConflictsInTree:
90
                raise BzrCommandError("A conflict occurred replaying a commit. Resolve the conflict and run 'bzr rebase-continue' or run 'bzr rebase-abort'.")
0.436.3 by Jelmer Vernooij
Fill in commands.
91
            # Remove plan file
92
            remove_rebase_plan(wt)
93
        finally:
94
            wt.unlock()
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
95
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
96
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
97
class cmd_rebase_abort(Command):
98
    """Abort an interrupted rebase
99
100
    """
101
    
102
    @display_command
103
    def run(self):
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
104
        from rebase import read_rebase_plan, remove_rebase_plan, complete_revert
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
105
        from bzrlib.workingtree import WorkingTree
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
106
        wt = WorkingTree.open('.')
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
107
        wt.lock_write()
0.436.3 by Jelmer Vernooij
Fill in commands.
108
        try:
109
            # Read plan file and set last revision
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
110
            try:
111
                last_rev_info = read_rebase_plan(wt)[0]
112
            except NoSuchFile:
113
                raise BzrCommandError("No rebase to abort")
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
114
            complete_revert(wt, [last_rev_info[1]])
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
115
            remove_rebase_plan(wt)
0.436.3 by Jelmer Vernooij
Fill in commands.
116
        finally:
117
            wt.unlock()
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
118
119
120
class cmd_rebase_continue(Command):
121
    """Continue an interrupted rebase after resolving conflicts
122
123
    """
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
124
    takes_options = ['merge-type']
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
125
    
126
    @display_command
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
127
    def run(self, merge_type=None):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
128
        from rebase import (commit_rebase, rebase, rebase_plan_exists, 
129
                            read_rebase_plan, read_active_rebase_revid, 
130
                            remove_rebase_plan, workingtree_replay)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
131
        from bzrlib.workingtree import WorkingTree
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
132
        wt = WorkingTree.open('.')
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
133
        wt.lock_write()
0.436.3 by Jelmer Vernooij
Fill in commands.
134
        try:
135
            # Abort if there are any conflicts
136
            if len(wt.conflicts()) != 0:
137
                raise BzrCommandError("There are still conflicts present")
138
            # Read plan file
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
139
            try:
140
                replace_map = read_rebase_plan(wt)[1]
141
            except NoSuchFile:
142
                raise BzrCommandError("No rebase to continue")
143
            oldrevid = read_active_rebase_revid(wt)
144
            if oldrevid is not None:
145
                oldrev = wt.branch.repository.get_revision(oldrevid)
146
                commit_rebase(wt, oldrev, replace_map[oldrevid][0])
0.436.3 by Jelmer Vernooij
Fill in commands.
147
            try:
148
                # Start executing plan from current Branch.last_revision()
0.436.16 by Jelmer Vernooij
Some more work on maptree.
149
                rebase(wt.branch.repository, replace_map, 
150
                        workingtree_replay(wt, merge_type=merge_type))
0.436.8 by Jelmer Vernooij
Couple more minor fixes.
151
            except ConflictsInTree:
152
                raise BzrCommandError("A conflict occurred replaying a commit. Resolve the conflict and run 'bzr rebase-continue' or run 'bzr rebase-abort'.")
0.436.3 by Jelmer Vernooij
Fill in commands.
153
            # Remove plan file  
154
            remove_rebase_plan(wt)
155
        finally:
156
            wt.unlock()
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
157
158
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
159
class cmd_rebase_todo(Command):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
160
    """Print list of revisions that still need to be replayed as part of the 
161
    current rebase operation.
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
162
163
    """
164
    
165
    def run(self):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
166
        from rebase import (rebase_todo, read_rebase_plan, 
167
                            read_active_rebase_revid)
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
168
        from bzrlib.workingtree import WorkingTree
169
        wt = WorkingTree.open('.')
170
        wt.lock_read()
171
        try:
172
            try:
173
                replace_map = read_rebase_plan(wt)[1]
174
            except NoSuchFile:
0.436.16 by Jelmer Vernooij
Some more work on maptree.
175
                raise BzrCommandError("No rebase in progress")
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
176
            currentrevid = read_active_rebase_revid(wt)
177
            if currentrevid is not None:
178
                info("Currently replaying: %s" % currentrevid)
179
            for revid in rebase_todo(wt.branch.repository, replace_map):
180
                info("%s -> %s" % (revid, replace_map[revid][0]))
181
        finally:
182
            wt.unlock()
183
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
184
0.436.4 by Jelmer Vernooij
Add some tests.
185
register_command(cmd_rebase)
186
register_command(cmd_rebase_abort)
187
register_command(cmd_rebase_continue)
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
188
register_command(cmd_rebase_todo)
0.436.4 by Jelmer Vernooij
Add some tests.
189
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
190
def test_suite():
191
    from unittest import TestSuite
192
    from bzrlib.tests import TestUtil
193
194
    loader = TestUtil.TestLoader()
195
    suite = TestSuite()
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
196
    testmod_names = ['test_rebase', 'test_maptree']
0.436.16 by Jelmer Vernooij
Some more work on maptree.
197
    suite.addTest(loader.loadTestsFromModuleNames(
198
                              ["%s.%s" % (__name__, i) for i in testmod_names]))
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
199
200
    return suite
0.436.1 by Jelmer Vernooij
Add framework for git-rebase plugin.
201