/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
0.437.5 by James Westby
Add some help text to the rebase command.
25
    Rebasing is the process of taking a branch and modifying the history so
26
    that it appears to start from a different point. This can be useful
27
    to clean up the history before submitting your changes. The tree at the
28
    end of the process will be the same as if you had merged the other branch,
29
    but the history will be different.
30
31
    The command takes the location of another branch on to which the branch in
32
    the current working directory will be rebased. If a branch is not specified
33
    then the parent branch is used, and this is usually the desired result.
34
35
    The first step identifies the revisions that are in the current branch that
36
    are not in the parent branch. The current branch is then set to be at the
37
    same revision as the target branch, and each revision is replayed on top
38
    of the branch. At the end of the process it will appear as though your
39
    current branch was branched off the current last revision of the target.
40
41
    Each revision that is replayed may cause conflicts in the tree. If this
42
    happens the command will stop and allow you to fix them up. Resolve the
43
    commits as you would for a merge, and then run 'bzr resolve' to marked
44
    them as resolved. Once you have resolved all the conflicts you should
45
    run 'bzr rebase-continue' to continue the rebase operation.
46
47
    If conflicts are encountered and you decide that you do not wish to continue
48
    you can run 'bzr rebase-abort'.
49
50
    The '--onto' option allows you to specify a different revision in the
51
    target branch to start at when replaying the revisions. This means that
52
    you can change the point at which the current branch will appear to be
53
    branched from when the operation completes.
0.436.1 by Jelmer Vernooij
Add framework for git-rebase plugin.
54
    """
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
55
    takes_args = ['upstream_location?']
0.436.19 by Jelmer Vernooij
- Add blackbox tests
56
    takes_options = ['revision', 'merge-type', 'verbose',
0.437.1 by James Westby
--onto needs to take a parameter. It was a boolean flag before.
57
                     Option('onto', help='Different revision to replay onto',
58
                            type=str)]
0.436.1 by Jelmer Vernooij
Add framework for git-rebase plugin.
59
    
60
    @display_command
0.436.16 by Jelmer Vernooij
Some more work on maptree.
61
    def run(self, upstream_location=None, onto=None, revision=None, 
0.436.19 by Jelmer Vernooij
- Add blackbox tests
62
            merge_type=None, verbose=False):
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
63
        from bzrlib.branch import Branch
64
        from bzrlib.revisionspec import RevisionSpec
65
        from bzrlib.workingtree import WorkingTree
0.436.16 by Jelmer Vernooij
Some more work on maptree.
66
        from rebase import (generate_simple_plan, rebase, rebase_plan_exists, 
67
                            read_rebase_plan, remove_rebase_plan, 
0.437.4 by James Westby
Import rebase_todo as it is needed for --verbose.
68
                            workingtree_replay, write_rebase_plan,
69
                            rebase_todo)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
70
        wt = WorkingTree.open('.')
71
        wt.lock_write()
72
        if upstream_location is None:
73
            upstream_location = wt.branch.get_parent()
0.436.3 by Jelmer Vernooij
Fill in commands.
74
        upstream = Branch.open(upstream_location)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
75
        upstream_repository = upstream.repository
76
        upstream_revision = upstream.last_revision()
0.436.3 by Jelmer Vernooij
Fill in commands.
77
        try:
78
            # Abort if there already is a plan file
79
            if rebase_plan_exists(wt):
80
                raise BzrCommandError("A rebase operation was interrupted. Continue using 'bzr rebase-continue' or abort using 'bzr rebase-abort'")
81
82
            # Pull required revisions
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
83
            wt.branch.repository.fetch(upstream_repository, 
84
                                       upstream_revision)
0.436.3 by Jelmer Vernooij
Fill in commands.
85
            if onto is None:
86
                onto = upstream.last_revision()
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
87
            else:
0.437.2 by James Westby
Lookup the onto revision in the upstream branch.
88
                rev_spec = RevisionSpec.from_string(onto)
89
                onto = rev_spec.in_history(upstream).rev_id
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
90
91
            wt.branch.repository.fetch(upstream_repository, onto)
92
93
            revhistory = wt.branch.revision_history()
94
            revhistory.reverse()
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
95
            common_revid = None
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
96
            for revid in revhistory:
97
                if revid in upstream.revision_history():
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
98
                    common_revid = revid
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
99
                    break
100
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
101
            if common_revid is None:
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
102
                raise UnrelatedBranches()
0.436.4 by Jelmer Vernooij
Add some tests.
103
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
104
            if common_revid == upstream.last_revision():
105
                raise BzrCommandError("Already rebased on %s" % upstream)
106
0.436.16 by Jelmer Vernooij
Some more work on maptree.
107
            start_revid = wt.branch.get_rev_id(
108
                    wt.branch.revision_id_to_revno(common_revid)+1)
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
109
0.436.3 by Jelmer Vernooij
Fill in commands.
110
            # Create plan
0.436.4 by Jelmer Vernooij
Add some tests.
111
            replace_map = generate_simple_plan(
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
112
                    wt.branch.repository, 
113
                    wt.branch.revision_history(), start_revid, onto)
0.436.3 by Jelmer Vernooij
Fill in commands.
114
115
            # Write plan file
0.436.4 by Jelmer Vernooij
Add some tests.
116
            write_rebase_plan(wt, replace_map)
0.436.3 by Jelmer Vernooij
Fill in commands.
117
0.436.19 by Jelmer Vernooij
- Add blackbox tests
118
            if verbose:
119
                for revid in rebase_todo(wt.branch.repository, replace_map):
120
                    info("%s -> %s" % (revid, replace_map[revid][0]))
121
0.436.3 by Jelmer Vernooij
Fill in commands.
122
            # Start executing plan
123
            try:
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
124
                rebase(wt.branch.repository, replace_map, workingtree_replay(wt, merge_type=merge_type))
0.436.8 by Jelmer Vernooij
Couple more minor fixes.
125
            except ConflictsInTree:
126
                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.
127
            # Remove plan file
128
            remove_rebase_plan(wt)
129
        finally:
130
            wt.unlock()
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
131
0.436.12 by Jelmer Vernooij
Give sane error when branch is already rebased.
132
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
133
class cmd_rebase_abort(Command):
134
    """Abort an interrupted rebase
135
136
    """
137
    
138
    @display_command
139
    def run(self):
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
140
        from rebase import read_rebase_plan, remove_rebase_plan, complete_revert
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
141
        from bzrlib.workingtree import WorkingTree
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
142
        wt = WorkingTree.open('.')
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
143
        wt.lock_write()
0.436.3 by Jelmer Vernooij
Fill in commands.
144
        try:
145
            # Read plan file and set last revision
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
146
            try:
147
                last_rev_info = read_rebase_plan(wt)[0]
148
            except NoSuchFile:
149
                raise BzrCommandError("No rebase to abort")
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
150
            complete_revert(wt, [last_rev_info[1]])
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
151
            remove_rebase_plan(wt)
0.436.3 by Jelmer Vernooij
Fill in commands.
152
        finally:
153
            wt.unlock()
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
154
155
156
class cmd_rebase_continue(Command):
157
    """Continue an interrupted rebase after resolving conflicts
158
159
    """
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
160
    takes_options = ['merge-type']
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
161
    
162
    @display_command
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
163
    def run(self, merge_type=None):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
164
        from rebase import (commit_rebase, rebase, rebase_plan_exists, 
165
                            read_rebase_plan, read_active_rebase_revid, 
166
                            remove_rebase_plan, workingtree_replay)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
167
        from bzrlib.workingtree import WorkingTree
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
168
        wt = WorkingTree.open('.')
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
169
        wt.lock_write()
0.436.3 by Jelmer Vernooij
Fill in commands.
170
        try:
171
            # Abort if there are any conflicts
172
            if len(wt.conflicts()) != 0:
0.437.3 by James Westby
Provide a hint in the conflicts present message to rebase-continue.
173
                raise BzrCommandError("There are still conflicts present. "
174
                                      "Resolve the conflicts and then run "
175
                                      "'bzr resolve' and try again.")
0.436.3 by Jelmer Vernooij
Fill in commands.
176
            # Read plan file
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
177
            try:
178
                replace_map = read_rebase_plan(wt)[1]
179
            except NoSuchFile:
180
                raise BzrCommandError("No rebase to continue")
181
            oldrevid = read_active_rebase_revid(wt)
182
            if oldrevid is not None:
183
                oldrev = wt.branch.repository.get_revision(oldrevid)
184
                commit_rebase(wt, oldrev, replace_map[oldrevid][0])
0.436.3 by Jelmer Vernooij
Fill in commands.
185
            try:
186
                # Start executing plan from current Branch.last_revision()
0.436.16 by Jelmer Vernooij
Some more work on maptree.
187
                rebase(wt.branch.repository, replace_map, 
188
                        workingtree_replay(wt, merge_type=merge_type))
0.436.8 by Jelmer Vernooij
Couple more minor fixes.
189
            except ConflictsInTree:
190
                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.
191
            # Remove plan file  
192
            remove_rebase_plan(wt)
193
        finally:
194
            wt.unlock()
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
195
196
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
197
class cmd_rebase_todo(Command):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
198
    """Print list of revisions that still need to be replayed as part of the 
199
    current rebase operation.
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
200
201
    """
202
    
203
    def run(self):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
204
        from rebase import (rebase_todo, read_rebase_plan, 
205
                            read_active_rebase_revid)
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
206
        from bzrlib.workingtree import WorkingTree
207
        wt = WorkingTree.open('.')
208
        wt.lock_read()
209
        try:
210
            try:
211
                replace_map = read_rebase_plan(wt)[1]
212
            except NoSuchFile:
0.436.16 by Jelmer Vernooij
Some more work on maptree.
213
                raise BzrCommandError("No rebase in progress")
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
214
            currentrevid = read_active_rebase_revid(wt)
215
            if currentrevid is not None:
216
                info("Currently replaying: %s" % currentrevid)
217
            for revid in rebase_todo(wt.branch.repository, replace_map):
218
                info("%s -> %s" % (revid, replace_map[revid][0]))
219
        finally:
220
            wt.unlock()
221
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
222
0.436.4 by Jelmer Vernooij
Add some tests.
223
register_command(cmd_rebase)
224
register_command(cmd_rebase_abort)
225
register_command(cmd_rebase_continue)
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
226
register_command(cmd_rebase_todo)
0.436.4 by Jelmer Vernooij
Add some tests.
227
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
228
def test_suite():
229
    from unittest import TestSuite
230
    from bzrlib.tests import TestUtil
231
232
    loader = TestUtil.TestLoader()
233
    suite = TestSuite()
0.436.19 by Jelmer Vernooij
- Add blackbox tests
234
    testmod_names = ['test_blackbox', 'test_rebase', 'test_maptree']
0.436.16 by Jelmer Vernooij
Some more work on maptree.
235
    suite.addTest(loader.loadTestsFromModuleNames(
236
                              ["%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.
237
238
    return suite
0.436.1 by Jelmer Vernooij
Add framework for git-rebase plugin.
239