~spiv/bzr-loom/bzr-2.4-compat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Loom, a plugin for bzr to assist in developing focused patches.
# Copyright (C) 2006, 2008 Canonical Limited.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
# 

"""Loom commands."""

from bzrlib import bzrdir, directory_service, workingtree
import bzrlib.commands
import bzrlib.branch
from bzrlib import errors
from bzrlib.lazy_import import lazy_import
import bzrlib.merge
from bzrlib.option import Option
import bzrlib.revision
import bzrlib.trace
import bzrlib.transport

import formats

lazy_import(globals(), """
import branch
from tree import LoomTreeDecorator
""")


class cmd_loomify(bzrlib.commands.Command):
    """Add a loom to this branch.

    This creates a loom in your branch, which will alter the behaviour of
    bzr for a number of commands to manage a group of patches being evolved
    in parallel.

    You must have a branch nickname explicitly set to use this command, as the
    branch nickname becomes the 'base thread' of the loom.  You can specify
    the branch nick with the --base option.
    """

    takes_args = ['location?']
    takes_options = [Option('base', type=str,
                            help='The name to use for the base thread.')]

    def run(self, location='.', base=None):
        (target, path) = bzrlib.branch.Branch.open_containing(location)
        target.lock_write()
        try:
            if base is not None:
                target.nick = base
            elif not target.get_config().has_explicit_nickname():
                raise errors.BzrCommandError(
                    'You must specify --base or have a branch nickname set to'
                    ' loomify a branch')
            branch.loomify(target)
            loom = target.bzrdir.open_branch()
        finally:
            target.unlock()
        # requires a new lock as its a new instance, XXX: teach bzrdir about
        # format changes ?
        loom.new_thread(loom.nick)


class cmd_combine_thread(bzrlib.commands.Command):
    __doc__ = """Combine the current thread with the thread below it.
    
    This will currently refuse to operate on the last thread, but in the future
    will just turn the loom into a normal branch again.
    
    Use combine-thread to remove a thread which has been merged into upstream.

    In precise terms this will:
     * Remove the entry from the loom for the current thread.
     * Change threads to the thread below.
    """

    takes_options = [
        Option('force', help='Combine even if work in the thread is not '
            'integrated up or down the loom.'),
        ]

    def run(self, force=False):
        (tree, path) = workingtree.WorkingTree.open_containing('.')
        branch.require_loom_branch(tree.branch)
        self.add_cleanup(tree.lock_write().unlock)
        current_thread = tree.branch.nick
        state = tree.branch.get_loom_state()
        if not force:
            # Check for unmerged work.
            # XXX: Layering issue whom should be caring for the check, not the
            # command thats for sure.
            threads = state.get_threads()
            current_index = state.thread_index(current_thread)
            rev_below = None
            rev_current = threads[current_index][1]
            rev_above = None
            if current_index:
                # There is a thread below
                rev_below = threads[current_index - 1][1]
            if current_index < len(threads) - 1:
                rev_above = threads[current_index + 1][1]
            graph = tree.branch.repository.get_graph()
            candidates = [rev for rev in
                (rev_below, rev_current, rev_above) if rev]
            heads = graph.heads(candidates)
            # If current is not a head, its trivially merged, or
            # if current is == rev_below, its also merged, or
            # if there is only one thread its merged (well its not unmerged).
            if (rev_current == rev_below or rev_current not in heads or
                (rev_below is None and rev_above is None)):
                merged = True
            else:
                merged = False
            if not merged:
                raise errors.BzrCommandError("Thread '%s' has unmerged work"
                    ". Use --force to combine anyway." % current_thread)
        new_thread = state.get_new_thread_after_deleting(current_thread)
        if new_thread is None:
            raise branch.CannotCombineOnLastThread
        bzrlib.trace.note("Combining thread '%s' into '%s'",
            current_thread, new_thread)
        LoomTreeDecorator(tree).down_thread(new_thread)
        tree.branch.remove_thread(current_thread)


class cmd_create_thread(bzrlib.commands.Command):
    """Add a thread to this loom.

    This creates a new thread in this loom and moves the branch onto that
    thread.

    The thread-name must be a valid branch 'nickname', and must not be the name
    of an existing thread in your loom.

    The new thread is created immediately after the current thread.
    """

    takes_args = ['thread']

    def run(self, thread):
        (loom, path) = bzrlib.branch.Branch.open_containing('.')
        branch.create_thread(loom, thread)


class cmd_show_loom(bzrlib.commands.Command):
    """Show the threads in this loom.

    Output the threads in this loom with the newest thread at the top and
    the base thread at the bottom. A => marker indicates the thread that
    'commit' will commit to.
    """

    takes_args = ['location?']

    def run(self, location='.'):
        (loom, path) = bzrlib.branch.Branch.open_containing(location)
        branch.require_loom_branch(loom)
        loom.lock_read()
        try:
            threads = loom.get_loom_state().get_threads()
            nick = loom.nick
            for thread, revid, parents in reversed(threads):
                if thread == nick:
                    symbol = '=>'
                else:
                    symbol = '  '
                print "%s%s" % (symbol, thread)
        finally:
            loom.unlock()


class cmd_status(bzrlib.builtins.cmd_status):
    """Show status of a loom."""

    _original_command = None

    def run(self, show_ids=False, file_list=None, revision=None, short=False,
            versioned=False, no_pending=False, verbose=None):
        if file_list is None:
            path = '.'
        else:
            path = file_list[0]
        (loom, _) = bzrlib.branch.Branch.open_containing(path)
        formats.require_loom_branch(loom)
        loom.lock_read()
        try:
            print 'Current thread: %s' % loom.nick
        finally:
            loom.unlock()

    def run_argv_aliases(self, argv, alias_argv=None):
        """Decorate bzr's cmd_status to show status for looms as well."""
        self._original_command().run_argv_aliases(argv, alias_argv)
        try:
            super(cmd_status, self).run_argv_aliases(list(argv), alias_argv)
        except formats.NotALoom:
            pass


class cmd_switch(bzrlib.builtins.cmd_switch):
    """Set the branch of a checkout and update.
 
    For looms, this is equivalent to 'down-thread' when to_location is the name
    of a thread in the loom.
    For lightweight checkouts, this changes the branch being referenced.
    For heavyweight checkouts, this checks that there are no local commits
    versus the current bound branch, then it makes the local branch a mirror
    of the new location and binds to it.
    
    In both cases, the working tree is updated and uncommitted changes
    are merged. The user can commit or revert these as they desire.

    Pending merges need to be committed or reverted before using switch.
    """

    _original_command = None

    def _get_thread_name(self, loom, to_location):
        """Return the name of the thread pointed to by 'to_location'.

        Most of the time this will be the name of the thread, but if
        'to_location' is 'bottom:' it will be the name of the bottom thread.
        If 'to_location' is 'top:', then it'll be the name of the top thread.
        """
        aliases = {'bottom:': 0, 'top:': -1}
        if to_location in aliases:
            threads = loom.get_loom_state().get_threads()
            thread = threads[aliases[to_location]]
            return thread[0]
        return to_location

    def run(self, to_location=None, force=False, create_branch=False,
            revision=None, directory=None):
        # The top of this is cribbed from bzr; because bzr isn't factored out
        # enough.
        if directory is None:
            directory = u'.'
        control_dir, path = bzrdir.BzrDir.open_containing(directory)
        if to_location is None:
            if revision is None:
                raise errors.BzrCommandError(
                    'You must supply either a revision or a location')
            to_location = '.'
        try:
            from_branch = control_dir.open_branch()
        except errors.NotBranchError:
            from_branch = None
        if create_branch:
            if from_branch is None:
                raise errors.BzrCommandError(
                    'cannot create branch without source branch')
            to_location = directory_service.directories.dereference(
                to_location)
        if from_branch is not None:
            # Note: reopens.
            (tree, path) = workingtree.WorkingTree.open_containing(directory)
            tree = LoomTreeDecorator(tree)
            try:
                if create_branch:
                    return branch.create_thread(tree.branch, to_location)
                thread_name = self._get_thread_name(tree.branch, to_location)
                return tree.down_thread(thread_name)
            except (AttributeError, branch.NoSuchThread, branch.NotALoom):
                # When there is no thread its probably an external branch
                # that we have been given.
                raise errors.MustUseDecorated
        else:
            # switching to a relocated branch
            raise errors.MustUseDecorated

    def run_argv_aliases(self, argv, alias_argv=None):
        """Parse command line and run.
        
        If the command requests it, run the decorated version.
        """
        try:
            super(cmd_switch, self).run_argv_aliases(list(argv), alias_argv)
        except (errors.MustUseDecorated, errors.BzrOptionError):
            if self._original_command is None:
                raise
            self._original_command().run_argv_aliases(argv, alias_argv)


class cmd_record(bzrlib.commands.Command):
    """Record the current last-revision of this tree into the current thread."""

    takes_args = ['message']

    def run(self, message):
        (abranch, path) = bzrlib.branch.Branch.open_containing('.')
        branch.require_loom_branch(abranch)
        abranch.record_loom(message)
        print "Loom recorded."


class cmd_revert_loom(bzrlib.commands.Command):
    """Revert part or all of a loom.
    
    This will update the current loom to be the same as the basis when --all
    is supplied. If no parameters or options are supplied then nothing will
    happen. If a thread is named, then only that thread is reverted to its
    state in the last committed loom.
    """

    takes_args = ['thread?']
    takes_options = [Option('all',
                        help='Revert all threads.'),
                     ]

    def run(self, thread=None, all=None):
        if thread is None and all is None:
            bzrlib.trace.note('Please see revert-loom -h.')
            return
        (tree, path) = workingtree.WorkingTree.open_containing('.')
        branch.require_loom_branch(tree.branch)
        tree = LoomTreeDecorator(tree)
        if all:
            tree.revert_loom()
            bzrlib.trace.note('All threads reverted.')
        else:
            tree.revert_loom(thread)
            bzrlib.trace.note("thread '%s' reverted.", thread)


class cmd_down_thread(bzrlib.commands.Command):
    """Move the branch down a thread in the loom.

    This removes the changes introduced by the current thread from the branch
    and sets the branch to be the next thread down.

    Down-thread refuses to operate if there are uncommitted changes, since
    this is typically a mistake.  Switch can be used for this purpose, instead.
    """

    takes_args = ['thread?']
    aliases = ['down']
    _see_also = ['switch', 'up-thread']

    def run(self, thread=None):
        (wt, path) = workingtree.WorkingTree.open_containing('.')
        branch.require_loom_branch(wt.branch)
        tree = LoomTreeDecorator(wt)
        tree.lock_write()
        try:
            basis = wt.basis_tree()
            basis.lock_read()
            try:
                for change in wt.iter_changes(basis):
                    raise errors.BzrCommandError(
                        'Working tree has uncommitted changes.')
            finally:
                basis.unlock()
            return tree.down_thread(thread)
        finally:
            tree.unlock()


class cmd_up_thread(bzrlib.commands.Command):
    """Move the branch up to the top thread in the loom.

    This merges the changes done in this thread but not incorporated into
    the next thread up into the next thread up and switches your tree to be
    that thread.  Unless there are conflicts, or --manual is specified, it
    will then commit and repeat the process.
    """

    takes_args = ['thread?']

    takes_options = ['merge-type', Option('auto',
        help='Deprecated - now the default.'),
        Option('manual', help='Perform commit manually.'),
        ]

    _see_also = ['down-thread', 'switch']

    def run(self, merge_type=None, manual=False, thread=None, auto=None):
        (tree, path) = workingtree.WorkingTree.open_containing('.')
        branch.require_loom_branch(tree.branch)
        tree = LoomTreeDecorator(tree)
        if manual:
            if thread is not None:
                raise errors.BzrCommandError('Specifying a thread does not'
                                             ' work with --manual.')
            return tree.up_thread(merge_type)
        else:
            return tree.up_many(merge_type, thread)


class cmd_export_loom(bzrlib.commands.Command):
    """Export loom threads as a full-fledged branches.

    LOCATION specifies the location to export the threads under.  If it does
    not exist, it will be created.

    In any of the standard config files, "export_loom_root" may be set to
    provide a default location that will be used if no location is supplied.
    """

    takes_args = ['location?']
    _see_also = ['configuration']

    def run(self, location=None):
        root_transport = None
        loom = bzrlib.branch.Branch.open_containing('.')[0]
        if location is None:
            location = loom.get_config().get_user_option('export_loom_root')
        if location is None:
            raise errors.BzrCommandError('No export root known or specified.')
        root_transport = bzrlib.transport.get_transport(location,
            possible_transports=[loom.bzrdir.root_transport])
        root_transport.ensure_base()
        loom.export_threads(root_transport)