~xnox/bzr-pipeline/bump-bzr

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
423
424
425
426
427
# Copyright (C) 2009-2011 Aaron Bentley
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA


from bzrlib import (
    builtins,
    errors,
    merge,
    revision as _mod_revision, trace,
    ui,
)
from bzrlib.branch import Branch
from bzrlib.bzrdir import BzrDir
from bzrlib.commands import Command
from bzrlib.option import Option, RegistryOption
from bzrlib.osutils import pathjoin
from bzrlib.switch import switch
from bzrlib.workingtree import WorkingTree
from bzrlib.plugins.pipeline.pipeline import (
    dwim_pipe,
    is_light_checkout,
    MergeConfig,
    NoSuchPipe,
    PipeManager,
    PipeStorage,
    tree_to_pipeline,
)


def require_light_checkout(tree):
    if not is_light_checkout(tree):
        raise errors.BzrCommandError('Directory is not a lightweight'
                                     ' checkout.')


class PipeCommand(Command):

    def _get_manager(self, location, after, before):
        tree, manager = self._get_checkout_manager(location, allow_tree=True)
        if after is not None:
            if before is not None:
                raise errors.BzrCommandError('Cannot specify --before and'
                                             ' --after.')
            manager = PipeManager(manager.storage.find_pipe(after), tree)
        if before is not None:
            manager = PipeManager(manager.storage.find_pipe(before), tree)
        return tree, manager

    def _get_location_manager(self, location='.'):
        branch = Branch.open_containing(location)[0]
        return PipeManager(branch, None)

    @staticmethod
    def _get_tree_and_branch(location, tree_optional):
        if tree_optional:
            tree, branch = BzrDir.open_containing_tree_or_branch(location)[:2]
        else:
            tree = WorkingTree.open_containing(location)[0]
            branch = tree.branch
        return tree, branch

    @classmethod
    def _get_checkout_manager(klass, location='.', checkout_optional=False,
                              pipe=None, allow_tree=False):
        try:
            tree, branch = klass._get_tree_and_branch('.', checkout_optional)
            branch = PipeStorage(branch).find_pipe(location)
        except (errors.NotBranchError, NoSuchPipe):
            tree, branch = klass._get_tree_and_branch(location,
                                                      checkout_optional)
        if tree is not None and not allow_tree:
            require_light_checkout(tree)
        manager = PipeManager(branch, tree)
        if pipe is not None:
            manager = PipeManager(manager.storage.find_pipe(pipe), tree)
        return tree, manager

    def _get_revision_id(self, branch, revision, manager, insert_before):
        if revision is None:
            if not insert_before:
                return None
            prev_revision_id = manager.get_prev_revision_id()
            if prev_revision_id is not None:
                return prev_revision_id
            else:
                return branch.last_revision()
        if len(revision) > 1:
            raise errors.BzrCommandError('Only one revision may be supplied.')
        return revision[0].as_revision_id(branch)


class cmd_add_pipe(PipeCommand):
    """Add a pipe to the pipeline.

    By default, the pipe is added after the current active pipe, at the
    current revision.

    This command can be used to start a new pipeline.
    """

    takes_args = ['pipe', 'neighbour?']
    takes_options = [RegistryOption.from_kwargs('position',
                     'Position to insert the new pipe at.',
                     after='Insert after the selected pipe.',
                     before='Insert before the selected pipe.',
                     value_switches=True, enum_switch=False),
                     Option('interactive', short_name='i',
                             help='Interactively decide which changes to place'
                             ' in the new pipe.'),
                     Option('no-switch', help="Do not switch to new pipe."),
                     Option('directory', short_name='d', type=unicode,
                            help='Directory of the pipe to add to, rather than'
                            ' the one for the working directory.'),
                     'revision',]

    def run(self, pipe, neighbour=None, revision=None, interactive=False,
            no_switch=False, directory=None, position='after'):
        tree, manager = self._get_checkout_manager(directory, pipe=neighbour,
            allow_tree=True)
        if not is_light_checkout(tree):
            raise errors.BzrCommandError('add-pipe should be run in a '
                'lightweight checkout.  See bzr help pipeline for details.')
        insert_before = (position == 'before')
        tree.branch.lock_read()
        try:
            revision_id = self._get_revision_id(tree.branch, revision,
                                                manager, insert_before)
        finally:
            tree.branch.unlock()
        try:
            new_br = Branch.open(pipe)
        except errors.NotBranchError:
            new_br = manager.storage.insert_pipe(pipe, revision_id,
                                                 before=insert_before)
        else:
            manager.storage.insert_branch(new_br, insert_before)
        if revision_id is None and no_switch:
            PipeManager(new_br, tree).store_uncommitted(interactive)
        if no_switch:
            trace.note('Created pipe "%s".' % pipe)
        if not no_switch:
            switch(tree.bzrdir, new_br)
            trace.note('Created and switched to pipe "%s".' % pipe)


class cmd_rename_pipe(PipeCommand):
    """Rename a pipe to a different name.

    This will rename the branch directory and update the pipeline metadata.
    It is not connected to the branch nick.
    """

    takes_args = ['new_name']

    def run(self, new_name):
        tree, manager = self._get_checkout_manager('.')
        manager.rename_pipe(new_name)

class cmd_merge(builtins.cmd_merge):
    #Support merge --uncommitted PIPE
    __doc__ = builtins.cmd_merge.__doc__

    def get_merger_from_uncommitted(self, tree, location, pb):
        try:
            pipe = PipeManager(dwim_pipe(PipeManager(tree.branch, tree),
                                         location), tree)
        except NoSuchPipe:
            return builtins.cmd_merge.get_merger_from_uncommitted(
                self, tree, location, pb)
        merger = pipe.make_uncommitted_merger(self)
        if merger is None:
            # Have to return a merger of some kind, so we don't try another
            # code path.
            merger = merge.Merger.from_revision_ids(None, tree,
                _mod_revision.NULL_REVISION, _mod_revision.NULL_REVISION)
        return merger


class cmd_reconfigure_pipeline(PipeCommand):
    """Reconfigure a tree with branch into a lightweight checkout of a pipe.

    The pipeline will be stored in a new "branches" subdirectory of .bzr.
    This is compatible with the bzr-colo plugin.

    This is suitable if you have a standalone tree, but if you have a
    shared repository with its own organization scheme already, it's probably
    better to just create a lightweight checkout.
    """

    def run(self):
        tree = WorkingTree.open_containing('.')[0]
        tree_to_pipeline(tree)


class cmd_remove_pipe(PipeCommand):
    """Remove a pipe from the pipeline.

    By default, the current pipe is removed, but a pipe may be specified as
    the first parameter.  By default, only the association of the pipe with
    its pipeline is removed, but if --branch is specified, the branch is
    also deleted.
    """

    takes_args = ['pipe?']
    takes_options = [Option('branch', help="Remove pipe's branch.")]

    def run(self, pipe=None, branch=False):
        tree, manager = self._get_checkout_manager(location=pipe,
                                                   checkout_optional=True,
                                                   allow_tree=True)
        target_branch = manager.get_nearest_pipe()
        if target_branch is None:
            raise errors.BzrCommandError('Branch is not connected to a'
                                         ' pipeline.')
        manager.remove_pipe(target_branch, delete_branch=branch)


class cmd_switch_pipe(PipeCommand):
    """Switch from one pipe to another.

    Any uncommitted changes are stored.  Any stored changes in the target
    pipe are restored.
    """
    
    aliases = ['swp']

    takes_args = ['pipe']
    takes_options = [
        Option('directory', type=unicode, short_name='d',
               help='Directory of the checkout to switch, rather than the'
                    ' current directory.')]

    def run(self, pipe, directory=None):
        checkout, manager = self._get_checkout_manager(directory)
        old = checkout.branch.nick
        target = dwim_pipe(manager, pipe)
        manager.switch_to_pipe(target)
        trace.note('Switched from "%s" to "%s".' % (old, target.nick))


class cmd_store(PipeCommand):

    """Store uncommitted changes in the pipe."""

    hidden = True

    def run(self):
        checkout, manager = self._get_checkout_manager('.')
        manager.store_uncommitted()


class cmd_show_pipeline(PipeCommand):
    """Show the current pipeline.

    All pipes are listed with the beginning of the pipeline at the top and the
    end of the pipeline at the bottom. These indicators are used::

      * - The current pipe.
      U - A pipe holding uncommitted changes.

    Uncommitted changes are automatically restored by the 'switch-pipe'
    command.
    """

    takes_args = ['location?']
    aliases = ['pipes']
    _see_also = ['nick']

    def run(self, location='.'):
        manager = self._get_location_manager(location)
        for pipe in manager.list_pipes():
            if pipe is manager.storage.branch:
                selected = '*'
            else:
                selected = ' '
            if PipeStorage(pipe).has_stored_changes():
                uncommitted = 'U'
            else:
                uncommitted = ' '
            self.outf.write('%s%s %s\n' % (selected, uncommitted, pipe.nick))


class cmd_pump(PipeCommand):
    """From this pipe onward, merge all pipes into their next pipe and commit.

    If the merge is successful, the changes are automatically committed, and
    the process repeats for the next pipe.  Eventually, the last pipe will
    have all the changes from all of the affected pipes.  On success, the
    checkout's initial state is restored.

    If the merge produces conflicts, the process aborts and no commit is
    performed.  You should resolve the conflicts, commit, and re-run pump.

    --from may be a pipe, in which case the pumping starts at the specified
    pipe, or it may be a branch, in which case the branch will be pumped into
    the first pipe.
    """

    takes_options = [
        Option('directory', short_name='d', type=unicode,
               help='Directory in the pipeline to pump from.'),
        Option('from-submit', help="Start from the first pipe's submit"
               " branch."),
        Option('from',
               help="Pump start.  May be a pipe or a non-pipe.",
               type=unicode),
        Option('show-base', help='Show base revision text in conflicts.'),
        'reprocess', 'merge-type',
    ]

    def run(self, directory=None, from_submit=False, show_base=False,
            reprocess=False, merge_type=None, **kwargs):
        """Run the command.  kwargs are needed to accept --from."""
        tree, manager = self._get_checkout_manager(directory)
        unexpected = set(kwargs.keys()).difference(set(['from']))
        if len(unexpected) != 0:
            raise TypeError("run() got an unexpected keyword argument '%s'" %
                            list(unexpected)[0])
        from_ = kwargs.get('from')
        from_branch = None
        if from_submit or from_:
            first_pipe = manager.get_first_pipe()
            manager = PipeManager(first_pipe, tree)
        if from_submit:
            from_ = first_pipe.get_submit_branch()
        if from_ is not None:
            try:
                from_pipe = dwim_pipe(manager, from_)
                manager = PipeManager(from_pipe, tree)
            except NoSuchPipe:
                from_branch = Branch.open(from_)
        else:
            from_branch = None

        merge_config = MergeConfig(merge_type, show_base, reprocess)
        if not manager.pipeline_merge(from_branch, merge_config):
            trace.note('Please resolve conflicts, commit, and re-run pump.')


class cmd_pipe_patches(PipeCommand):
    """Export the pipeline as a collection of patches, one per pipe.

    The patch name begins with a sequence number, and ends with the pipe
    name.
    """
    takes_args = ['patch_location?']
    takes_options = [Option('directory', short_name='d', type=unicode,
               help='Directory of the pipeline.'),
    ]
    def run(self, patch_location='.', directory=None):
        checkout, manager = self._get_checkout_manager(directory,
                                                       checkout_optional=True,
                                                       allow_tree=True)
        for num, pipe in enumerate(manager.list_pipes()):
            pipe.lock_read()
            try:
                patch = PipeManager(pipe, checkout).get_patch()
            finally:
                pipe.unlock()
            if patch is None:
                continue
            filename = pathjoin(patch_location,
                                '%.2d-%s.patch' % (num, pipe.nick))
            my_file = open(filename, 'wb')
            try:
                my_file.write(patch)
            finally:
                my_file.close()


class cmd_sync_pipeline(PipeCommand):
    """Synchronise the contents of this pipeline with another copy.

    The first argument is the location of one of the pipes in the remote
    pipeline.  It defaults to the push location.  If it does not exist, the
    whole remote pipeline will be created.  If any remote pipes are missing,
    they will be created.

    The pipelines are then synchronized by pulling and pushing between
    pipes, depending on which is newer.

    If pipes have diverged, the process will abort.  You should then merge the
    remote pipe into the local pipe and re-run sync-pipeline.
    """

    takes_args = ['location?']

    def run(self, location=None):
        checkout, manager = self._get_checkout_manager(checkout_optional=True,
                                                       allow_tree=True)
        remote = None
        if location is None:
            branchless_location = None
            for pipe in manager.list_pipes():
                location = pipe.get_push_location()
                if location is not None:
                    try:
                        remote = Branch.open(location)
                    except errors.NotBranchError:
                        if branchless_location is None:
                            branchless_location = location
                        continue
                    manager = PipeManager(pipe, checkout)
                    break
            else:
                if branchless_location is not None:
                    location = branchless_location
                else:
                    raise errors.BzrCommandError(
                        'No location specified and none remembered.')
        try:
            manager.sync_pipeline(location, remote)
        finally:
            ui.ui_factory.clear_term()