/brz/remove-bazaar

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