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