~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to command_classes.py

  • Committer: Aaron Bentley
  • Date: 2009-02-10 18:07:22 UTC
  • Revision ID: aaron@aaronbentley.com-20090210180722-minlp55nx4uebl63
Tags: release-1.12.0
Update for 1.12.0 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
"""\
3
 
Various useful plugins for working with bzr.
4
 
"""
5
 
import rspush
 
1
# Copyright (C) 2005, 2006, 2007 Aaron Bentley <aaron@aaronbentley.com>
 
2
# Copyright (C) 2005, 2006 Canonical Limited.
 
3
# Copyright (C) 2006 Michael Ellerman.
 
4
#
 
5
#    This program is free software; you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation; either version 2 of the License, or
 
8
#    (at your option) any later version.
 
9
#
 
10
#    This program is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
#
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with this program; if not, write to the Free Software
 
17
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
import bzrlib
 
20
 
 
21
from bzrlib.lazy_import import lazy_import
 
22
lazy_import(globals(), """
 
23
from bzrlib import help, urlutils
 
24
import shelf
 
25
""")
 
26
 
 
27
from command import BzrToolsCommand
6
28
from errors import CommandError
7
29
from patchsource import BzrPatchSource
8
 
from shelf import Shelf
9
 
from switch import cmd_switch
10
30
import sys
11
31
import os.path
12
32
 
13
 
from bzrlib import DEFAULT_IGNORE
14
33
import bzrlib.builtins
15
 
import bzrlib.branch
16
34
import bzrlib.commands
 
35
from bzrlib.branch import Branch
 
36
from bzrlib.bzrdir import BzrDir
17
37
from bzrlib.commands import get_cmd_object
18
38
from bzrlib.errors import BzrCommandError
19
 
from bzrlib.help import command_usage
20
 
from bzrlib.option import Option
21
 
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), 
22
 
                                                 "external")))
23
 
 
24
 
 
25
 
DEFAULT_IGNORE.append('./.shelf')
26
 
DEFAULT_IGNORE.append('./.bzr-shelf*')
27
 
 
28
 
 
29
 
class cmd_clean_tree(bzrlib.commands.Command):
 
39
import bzrlib.ignores
 
40
from bzrlib.trace import note
 
41
from bzrlib.option import Option, RegistryOption
 
42
 
 
43
from command import BzrToolsCommand
 
44
 
 
45
 
 
46
class cmd_clean_tree(BzrToolsCommand):
30
47
    """Remove unwanted files from working tree.
31
 
    Normally, ignored files are left alone.
 
48
 
 
49
    By default, only unknown files, not ignored files, are deleted.  Versioned
 
50
    files are never deleted.
 
51
 
 
52
    Another class is 'detritus', which includes files emitted by bzr during
 
53
    normal operations and selftests.  (The value of these files decreases with
 
54
    time.)
 
55
 
 
56
    If no options are specified, unknown files are deleted.  Otherwise, option
 
57
    flags are respected, and may be combined.
 
58
 
 
59
    To check what clean-tree will do, use --dry-run.
32
60
    """
33
 
    takes_options = [Option('ignored', help='delete all ignored files.'), 
34
 
                     Option('detritus', help='delete conflict files merge'
35
 
                            ' backups, and failed selftest dirs.  (*.THIS, '
36
 
                            '*.BASE, *.OTHER, *~, *.tmp)'), 
37
 
                     Option('unknown', help='delete files unknown to bzr.  (default)'),
38
 
                     Option('dry-run', help='show files to delete instead of'
39
 
                            ' deleting them.')]
40
 
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False):
 
61
    takes_options = [Option('ignored', help='Delete all ignored files.'),
 
62
                     Option('detritus', help='Delete conflict files, merge'
 
63
                            ' backups, and failed selftest dirs.'),
 
64
                     Option('unknown',
 
65
                            help='Delete files unknown to bzr (default).'),
 
66
                     Option('dry-run', help='Show files to delete instead of'
 
67
                            ' deleting them.'),
 
68
                     Option('force', help='Do not prompt before deleting.')]
 
69
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False,
 
70
            force=False):
41
71
        from clean_tree import clean_tree
42
72
        if not (unknown or ignored or detritus):
43
73
            unknown = True
44
 
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus, dry_run=dry_run)
45
 
 
46
 
class cmd_graph_ancestry(bzrlib.commands.Command):
 
74
        if dry_run:
 
75
            force = True
 
76
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus, 
 
77
                   dry_run=dry_run, no_prompt=force)
 
78
 
 
79
 
 
80
class cmd_graph_ancestry(BzrToolsCommand):
47
81
    """Produce ancestry graphs using dot.
48
82
    
49
83
    Output format is detected according to file extension.  Some of the more
55
89
    with the last 5 characters of their revision identifier are used instead.
56
90
 
57
91
    The value starting with d is "(maximum) distance from the null revision".
58
 
    
 
92
 
59
93
    If --merge-branch is specified, the two branches are compared and a merge
60
94
    base is selected.
61
 
    
 
95
 
62
96
    Legend:
63
97
    white    normal revision
64
98
    yellow   THIS  history
78
112
    If available, rsvg is used to antialias PNG and JPEG output, but this can
79
113
    be disabled with --no-antialias.
80
114
    """
81
 
    takes_args = ['branch', 'file']
82
 
    takes_options = [Option('no-collapse', help="Do not skip simple nodes"), 
 
115
    takes_args = ['file', 'merge_branch?']
 
116
    takes_options = [Option('no-collapse', help="Do not skip simple nodes."),
83
117
                     Option('no-antialias',
84
 
                     help="Do not use rsvg to produce antialiased output"), 
85
 
                     Option('merge-branch', type=str, 
86
 
                     help="Use this branch to calcuate a merge base"), 
87
 
                     Option('cluster', help="Use clustered output.")]
88
 
    def run(self, branch, file, no_collapse=False, no_antialias=False,
89
 
        merge_branch=None, cluster=False):
 
118
                     help="Do not use rsvg to produce antialiased output."),
 
119
                     Option('merge-branch', type=str,
 
120
                     help="Use this branch to calcuate a merge base."),
 
121
                     Option('cluster', help="Use clustered output."),
 
122
                     Option('max-distance',
 
123
                            help="Show no nodes farther than this.", type=int),
 
124
                     Option('directory',
 
125
                            help='Source branch to use (default is current'
 
126
                            ' directory).',
 
127
                            short_name='d',
 
128
                            type=unicode),
 
129
                    ]
 
130
    def run(self, file, merge_branch=None, no_collapse=False,
 
131
            no_antialias=False, cluster=False, max_distance=100,
 
132
            directory='.'):
 
133
        if max_distance == -1:
 
134
            max_distance = None
90
135
        import graph
91
136
        if cluster:
92
137
            ranking = "cluster"
93
138
        else:
94
139
            ranking = "forced"
95
 
        graph.write_ancestry_file(branch, file, not no_collapse, 
96
 
                                  not no_antialias, merge_branch, ranking)
97
 
 
98
 
class cmd_fetch_ghosts(bzrlib.commands.Command):
 
140
        graph.write_ancestry_file(directory, file, not no_collapse,
 
141
                                  not no_antialias, merge_branch, ranking,
 
142
                                  max_distance=max_distance)
 
143
 
 
144
 
 
145
class cmd_fetch_ghosts(BzrToolsCommand):
99
146
    """Attempt to retrieve ghosts from another branch.
100
147
    If the other branch is not supplied, the last-pulled branch is used.
101
148
    """
102
149
    aliases = ['fetch-missing']
103
150
    takes_args = ['branch?']
104
 
    takes_options = [Option('no-fix')]
 
151
    takes_options = [Option('no-fix', help="Skip additional synchonization.")]
105
152
    def run(self, branch=None, no_fix=False):
106
153
        from fetch_ghosts import fetch_ghosts
107
154
        fetch_ghosts(branch, no_fix)
108
155
 
109
156
strip_help="""Strip the smallest prefix containing num leading slashes  from \
110
157
each file name found in the patch file."""
111
 
Option.OPTIONS['bzrdiff'] = Option('bzrdiff',type=None,
112
 
                                help="""Handle extra bzr tags""")
113
 
class cmd_patch(bzrlib.commands.Command):
 
158
 
 
159
 
 
160
class cmd_patch(BzrToolsCommand):
114
161
    """Apply a named patch to the current tree.
115
162
    """
116
163
    takes_args = ['filename?']
117
 
    takes_options = [Option('strip', type=int, help=strip_help)]
118
 
    def run(self, filename=None, strip=-1, bzrdiff=0):
 
164
    takes_options = [Option('strip', type=int, help=strip_help),
 
165
                     Option('silent', help='Suppress chatter.')]
 
166
    def run(self, filename=None, strip=None, silent=False):
119
167
        from patch import patch
120
168
        from bzrlib.workingtree import WorkingTree
121
169
        wt = WorkingTree.open_containing('.')[0]
122
 
        if strip == -1:
123
 
            if bzrdiff: strip = 0
124
 
            else:       strip = 0
125
 
 
126
 
        return patch(wt, filename, strip, legacy= not bzrdiff)
127
 
 
128
 
class cmd_shelve(bzrlib.commands.Command):
 
170
        if strip is None:
 
171
            strip = 0
 
172
        return patch(wt, filename, strip, silent)
 
173
 
 
174
 
 
175
class cmd_shelve1(BzrToolsCommand):
129
176
    """Temporarily set aside some changes from the current tree.
130
177
 
131
178
    Shelve allows you to temporarily put changes you've made "on the shelf",
132
179
    ie. out of the way, until a later time when you can bring them back from
133
 
    the shelf with the 'unshelve' command.
 
180
    the shelf with the 'unshelve1' command.
134
181
 
135
182
    Shelve is intended to help separate several sets of text changes that have
136
183
    been inappropriately mingled.  If you just want to get rid of all changes
137
184
    (text and otherwise) and you don't need to restore them later, use revert.
138
 
    If you want to shelve all text changes at once, use shelve --all.
 
185
    If you want to shelve all text changes at once, use shelve1 --all.
139
186
 
140
 
    By default shelve asks you what you want to shelve, press '?' at the
141
 
    prompt to get help. To shelve everything run shelve --all.
 
187
    By default shelve1 asks you what you want to shelve, press '?' at the
 
188
    prompt to get help. To shelve everything run shelve1 --all.
142
189
 
143
190
    If filenames are specified, only the changes to those files will be
144
191
    shelved, other files will be left untouched.
146
193
    If a revision is specified, changes since that revision will be shelved.
147
194
 
148
195
    You can put multiple items on the shelf. Normally each time you run
149
 
    unshelve the most recently shelved changes will be reinstated. However,
 
196
    unshelve1 the most recently shelved changes will be reinstated. However,
150
197
    you can also unshelve changes in a different order by explicitly
151
 
    specifiying which changes to unshelve. This works best when the changes
 
198
    specifiying which changes to unshelve1. This works best when the changes
152
199
    don't depend on each other.
 
200
 
 
201
    While you have patches on the shelf you can view and manipulate them with
 
202
    the 'shelf1' command. Run 'bzr shelf1 -h' for more info.
153
203
    """
154
204
 
155
205
    takes_args = ['file*']
156
 
    takes_options = ['message', 'revision',
157
 
            Option('all', help='Shelve all changes without prompting')]
 
206
    takes_options = [Option('message',
 
207
            help='A message to associate with the shelved changes.',
 
208
            short_name='m', type=unicode),
 
209
            'revision',
 
210
            Option('all', help='Shelve all changes without prompting.'),
 
211
            Option('no-color', help='Never display changes in color.')]
158
212
 
159
 
    def run(self, all=False, file_list=None, message=None, revision=None):
 
213
    def run(self, all=False, file_list=None, message=None, revision=None,
 
214
            no_color=False):
160
215
        if revision is not None and revision:
161
216
            if len(revision) == 1:
162
217
                revision = revision[0]
165
220
                                  "parameter.")
166
221
 
167
222
        source = BzrPatchSource(revision, file_list)
168
 
        s = Shelf(source.base)
169
 
        s.shelve(source, all, message)
 
223
        s = shelf.Shelf(source.base)
 
224
        s.shelve(source, all, message, no_color)
170
225
        return 0
171
226
 
172
227
 
173
 
# The following classes are only used as subcommands for 'shelf', they're
 
228
# The following classes are only used as subcommands for 'shelf1', they're
174
229
# not to be registered directly with bzr.
175
230
 
176
231
class cmd_shelf_list(bzrlib.commands.Command):
193
248
    aliases = ['switch']
194
249
    takes_args = ['othershelf']
195
250
    def run(self, othershelf):
196
 
        s = Shelf(self.shelf.base, othershelf)
 
251
        s = shelf.Shelf(self.shelf.base, othershelf)
197
252
        s.make_default()
198
253
 
199
254
 
212
267
        self.shelf.upgrade()
213
268
 
214
269
 
215
 
class cmd_shelf(bzrlib.commands.Command):
216
 
    """Perform various operations on your shelved patches. See also shelve."""
 
270
class cmd_shelf1(BzrToolsCommand):
 
271
    """Perform various operations on your shelved patches. See also shelve1."""
217
272
    takes_args = ['subcommand', 'args*']
218
273
 
219
274
    subcommands = [cmd_shelf_list, cmd_shelf_delete, cmd_shelf_switch,
222
277
    def run(self, subcommand, args_list):
223
278
        import sys
224
279
 
 
280
        if args_list is None:
 
281
            args_list = []
225
282
        cmd = self._get_cmd_object(subcommand)
226
283
        source = BzrPatchSource()
227
 
        s = Shelf(source.base)
 
284
        s = shelf.Shelf(source.base)
228
285
        cmd.shelf = s
 
286
 
 
287
        if args_list is None:
 
288
            args_list = []
229
289
        return cmd.run_argv_aliases(args_list)
230
290
 
231
291
    def _get_cmd_object(self, cmd_name):
248
308
        cmd_obj = cmd_class()
249
309
        indent = 2 * ' '
250
310
 
251
 
        usage = command_usage(cmd_obj)
 
311
        usage = cmd_obj._usage()
252
312
        usage = usage.replace('bzr shelf-', '')
253
313
        text.append('%s%s\n' % (indent, usage))
254
314
 
273
333
        return text
274
334
 
275
335
 
276
 
 
277
 
class cmd_unshelve(bzrlib.commands.Command):
 
336
class cmd_unshelve1(BzrToolsCommand):
278
337
    """Restore shelved changes.
279
338
 
280
339
    By default the most recently shelved changes are restored. However if you
281
340
    specify a patch by name those changes will be restored instead.
282
341
 
283
 
    See 'shelve' for more information.
 
342
    See 'shelve1' for more information.
284
343
    """
285
344
    takes_options = [
286
 
            Option('all', help='Unshelve all changes without prompting'),
287
 
            Option('force', help='Force unshelving even if errors occur'),
288
 
    ]
 
345
            Option('all', help='Unshelve all changes without prompting.'),
 
346
            Option('force', help='Force unshelving even if errors occur.'),
 
347
            Option('no-color', help='Never display changes in color.')
 
348
        ]
289
349
    takes_args = ['patch?']
290
 
    def run(self, patch=None, all=False, force=False):
 
350
    def run(self, patch=None, all=False, force=False, no_color=False):
291
351
        source = BzrPatchSource()
292
 
        s = Shelf(source.base)
293
 
        s.unshelve(source, patch, all, force)
 
352
        s = shelf.Shelf(source.base)
 
353
        s.unshelve(source, patch, all, force, no_color)
294
354
        return 0
295
355
 
296
356
 
297
 
class cmd_shell(bzrlib.commands.Command):
 
357
class cmd_shell(BzrToolsCommand):
298
358
    """Begin an interactive shell tailored for bzr.
299
359
    Bzr commands can be used without typing bzr first, and will be run natively
300
360
    when possible.  Tab completion is tailored for bzr.  The shell prompt shows
316
376
        import shell
317
377
        return shell.run_shell()
318
378
 
319
 
class cmd_branch_history(bzrlib.commands.Command):
 
379
 
 
380
class cmd_branch_history(BzrToolsCommand):
320
381
    """\
321
382
    Display the development history of a branch.
322
383
 
326
387
    """
327
388
    takes_args = ["branch?"]
328
389
    def run(self, branch=None):
329
 
        from branchhistory import branch_history 
 
390
        from branchhistory import branch_history
330
391
        return branch_history(branch)
331
392
 
332
393
 
333
 
class cmd_zap(bzrlib.commands.Command):
 
394
class cmd_zap(BzrToolsCommand):
334
395
    """\
335
396
    Remove a lightweight checkout, if it can be done safely.
336
397
 
341
402
    If --branch is specified, the branch will be deleted too, but only if the
342
403
    the branch has no new commits (relative to its parent).
343
404
    """
344
 
    takes_options = [Option("branch", help="Remove associtated branch from"
345
 
                                           " repository")]
 
405
    takes_options = [Option("branch", help="Remove associated branch from"
 
406
                                           " repository."),
 
407
                     Option('force', help='Delete tree even if contents are'
 
408
                     ' modified.')]
346
409
    takes_args = ["checkout"]
347
 
    def run(self, checkout, branch=False):
 
410
    def run(self, checkout, branch=False, force=False):
348
411
        from zap import zap
349
 
        return zap(checkout, remove_branch=branch)
350
 
 
351
 
 
352
 
class cmd_cbranch(bzrlib.commands.Command):
 
412
        return zap(checkout, remove_branch=branch, allow_modified=force)
 
413
 
 
414
 
 
415
class cmd_cbranch(BzrToolsCommand):
353
416
    """
354
417
    Create a new checkout, associated with a new repository branch.
355
 
    
356
 
    When you cbranch, bzr looks up the repository associated with your current
357
 
    directory in branches.conf.  It creates a new branch in that repository
358
 
    with the same name and relative path as the checkout you request.
359
 
 
360
 
    The branches.conf parameter is "cbranch_root".  So if you want 
361
 
    cbranch operations in /home/jrandom/bigproject to produce branches in 
362
 
    /home/jrandom/bigproject/repository, you'd add this:
363
 
 
364
 
    [/home/jrandom/bigproject]
365
 
    cbranch_root = /home/jrandom/bigproject/repository
366
 
 
367
 
    Note that if "/home/jrandom/bigproject/repository" isn't a repository,
368
 
    standalone branches will be produced.  Standalone branches will also
369
 
    be produced if the source branch is in 0.7 format (or earlier).
 
418
 
 
419
    When you cbranch, bzr looks up a target location in locations.conf, and
 
420
    creates the branch there.
 
421
 
 
422
    In your locations.conf, add the following lines:
 
423
    [/working_directory_root]
 
424
    cbranch_target = /branch_root
 
425
    cbranch_target:policy = appendpath
 
426
 
 
427
    This will mean that if you run "bzr cbranch foo/bar foo/baz" in the
 
428
    working directory root, the branch will be created in
 
429
    "/branch_root/foo/baz"
 
430
 
 
431
    NOTE: cbranch also supports "cbranch_root", but that behaviour is
 
432
    deprecated.
370
433
    """
371
 
    takes_options = [Option("lightweight", 
372
 
                            help="Create a lightweight checkout")]
 
434
    takes_options = [Option("lightweight",
 
435
                            help="Create a lightweight checkout."), 'revision',
 
436
                     Option('files-from', type=unicode,
 
437
                            help='Accelerate checkout using files from this'
 
438
                                 ' tree.'),
 
439
                     Option('hardlink',
 
440
                            help='Hard-link files from source/files-from tree'
 
441
                            ' where posible.')]
373
442
    takes_args = ["source", "target?"]
374
 
    def run(self, source, target=None, lightweight=False):
 
443
    def run(self, source, target=None, lightweight=False, revision=None,
 
444
            files_from=None, hardlink=False):
375
445
        from cbranch import cbranch
376
 
        return cbranch(source, target, lightweight=lightweight)
377
 
 
378
 
 
379
 
class cmd_branches(bzrlib.commands.Command):
 
446
        return cbranch(source, target, lightweight=lightweight,
 
447
                       revision=revision, files_from=files_from,
 
448
                       hardlink=hardlink)
 
449
 
 
450
 
 
451
class cmd_branches(BzrToolsCommand):
380
452
    """Scan a location for branches"""
381
453
    takes_args = ["location?"]
382
454
    def run(self, location=None):
383
455
        from branches import branches
384
456
        return branches(location)
385
457
 
 
458
class cmd_trees(BzrToolsCommand):
 
459
    """Scan a location for trees"""
 
460
    takes_args = ['location?']
 
461
    def run(self, location='.'):
 
462
        from bzrlib.workingtree import WorkingTree
 
463
        from bzrlib.transport import get_transport
 
464
        t = get_transport(location)
 
465
        for tree in WorkingTree.find_trees(location):
 
466
            self.outf.write('%s\n' % t.relpath(
 
467
                tree.bzrdir.root_transport.base))
386
468
 
387
 
class cmd_multi_pull(bzrlib.commands.Command):
 
469
class cmd_multi_pull(BzrToolsCommand):
388
470
    """Pull all the branches under a location, e.g. a repository.
389
 
    
 
471
 
390
472
    Both branches present in the directory and the branches of checkouts are
391
473
    pulled.
392
474
    """
393
475
    takes_args = ["location?"]
394
476
    def run(self, location=None):
395
 
        from bzrlib.branch import Branch
396
477
        from bzrlib.transport import get_transport
397
478
        from bzrtools import iter_branch_tree
398
479
        if location is None:
399
480
            location = '.'
400
481
        t = get_transport(location)
 
482
        possible_transports = []
401
483
        if not t.listable():
402
484
            print "Can't list this type of location."
403
485
            return 3
419
501
                relpath = base
420
502
            print "Pulling %s from %s" % (relpath, parent)
421
503
            try:
422
 
                pullable.pull(Branch.open(parent))
 
504
                branch_t = get_transport(parent, possible_transports)
 
505
                pullable.pull(Branch.open_from_transport(branch_t))
423
506
            except Exception, e:
424
507
                print e
425
508
 
426
509
 
427
 
class cmd_branch_mark(bzrlib.commands.Command):
428
 
    """
429
 
    Add, view or list branch markers <EXPERIMENTAL>
430
 
 
431
 
    To add a mark, do 'bzr branch-mark MARK'.
432
 
    To list marks, do 'bzr branch-mark' (this lists all marks for the branch's
433
 
    repository).
434
 
    To delete a mark, do 'bzr branch-mark --delete MARK'
435
 
 
436
 
    These marks can be used to track a branch's status.
437
 
    """
438
 
    takes_args = ['mark?', 'branch?']
439
 
    takes_options = [Option('delete', help='Delete this mark')]
440
 
    def run(self, mark=None, branch=None, delete=False):
441
 
        from branch_mark import branch_mark
442
 
        branch_mark(mark, branch, delete)
443
 
 
444
 
class cmd_import(bzrlib.commands.Command):
445
 
    """Import sources from a tarball
446
 
    
447
 
    This command will import a tarball into a bzr tree, replacing any versioned
448
 
    files already present.  If a directory is specified, it is used as the
449
 
    target.  If the directory does not exist, or is not versioned, it is
450
 
    created.
 
510
 
 
511
class cmd_import(BzrToolsCommand):
 
512
    """Import sources from a directory, tarball or zip file
 
513
 
 
514
    This command will import a directory, tarball or zip file into a bzr
 
515
    tree, replacing any versioned files already present.  If a directory is
 
516
    specified, it is used as the target.  If the directory does not exist, or
 
517
    is not versioned, it is created.
451
518
 
452
519
    Tarballs may be gzip or bzip2 compressed.  This is autodetected.
453
520
 
454
 
    If the tarball has a single root directory, that directory is stripped
455
 
    when extracting the tarball.
 
521
    If the tarball or zip has a single root directory, that directory is
 
522
    stripped when extracting the tarball.  This is not done for directories.
456
523
    """
457
 
    
 
524
 
458
525
    takes_args = ['source', 'tree?']
459
526
    def run(self, source, tree=None):
460
527
        from upstream_import import do_import
461
528
        do_import(source, tree)
462
529
 
463
 
class cmd_shove(bzrlib.commands.Command):
464
 
    """Apply uncommitted changes to another tree
465
 
    
466
 
    This is useful when you start to make changes in one tree, then realize
467
 
    they should really be done in a different tree.
468
 
 
469
 
    Shove is implemented using merge, so:
470
 
     - All changes, including renames and adds, will be applied.
471
 
     - No changes that have already been applied will be applied.
472
 
     - If the target is significantly different from the source, conflicts may
473
 
       be produced.
474
 
    """
475
 
 
476
 
    takes_args = ['target', 'source?']
477
 
    def run(self, target, source='.'):
478
 
        from shove import do_shove
479
 
        do_shove(source, target)
480
 
 
481
 
class cmd_cdiff(bzrlib.commands.Command):
 
530
 
 
531
class cmd_cdiff(BzrToolsCommand):
482
532
    """A color version of bzr's diff"""
483
533
    takes_args = property(lambda x: get_cmd_object('diff').takes_args)
484
 
    takes_options = property(lambda x: get_cmd_object('diff').takes_options)
485
 
    def run(*args, **kwargs):
 
534
    takes_options = list(get_cmd_object('diff').takes_options) + [
 
535
        RegistryOption.from_kwargs('color',
 
536
            'Color mode to use.',
 
537
            title='Color Mode', value_switches=False, enum_switch=True,
 
538
            never='Never colorize output.',
 
539
            auto='Only colorize output if terminal supports it and STDOUT is a'
 
540
            ' TTY.',
 
541
            always='Always colorize output (default).'),
 
542
        Option('check-style',
 
543
            help='Warn if trailing whitespace or spurious changes have been'
 
544
                 ' added.')]
 
545
 
 
546
    def run(self, color='always', check_style=False, *args, **kwargs):
486
547
        from colordiff import colordiff
487
 
        colordiff(*args, **kwargs)
488
 
 
489
 
commands = [cmd_shelve, cmd_unshelve, cmd_shelf, cmd_clean_tree,
490
 
            cmd_graph_ancestry, cmd_fetch_ghosts, cmd_patch, cmd_shell,
491
 
            cmd_branch_history, cmd_zap, cmd_cbranch, cmd_branches, 
492
 
            cmd_multi_pull, cmd_switch, cmd_branch_mark, cmd_import, cmd_shove,
493
 
            cmd_cdiff]
494
 
 
495
 
 
496
 
commands.append(rspush.cmd_rspush)
497
 
 
498
 
from errors import NoPyBaz
499
 
try:
500
 
    import baz_import
501
 
    commands.append(baz_import.cmd_baz_import_branch)
502
 
    commands.append(baz_import.cmd_baz_import)
503
 
 
504
 
except NoPyBaz:
505
 
    class cmd_baz_import_branch(bzrlib.commands.Command):
506
 
        """Disabled. (Requires PyBaz)"""
507
 
        takes_args = ['to_location?', 'from_branch?', 'reuse_history*']
508
 
        takes_options = ['verbose', Option('max-count', type=int)]
509
 
        def run(self, to_location=None, from_branch=None, fast=False, 
510
 
                max_count=None, verbose=False, dry_run=False,
511
 
                reuse_history_list=[]):
512
 
            print "This command is disabled.  Please install PyBaz."
513
 
 
514
 
 
515
 
    class cmd_baz_import(bzrlib.commands.Command):
516
 
        """Disabled. (Requires PyBaz)"""
517
 
        takes_args = ['to_root_dir?', 'from_archive?', 'reuse_history*']
518
 
        takes_options = ['verbose', Option('prefixes', type=str,
519
 
                         help="Prefixes of branches to import")]
520
 
        def run(self, to_root_dir=None, from_archive=None, verbose=False,
521
 
                reuse_history_list=[], prefixes=None):
522
 
                print "This command is disabled.  Please install PyBaz."
523
 
    commands.extend((cmd_baz_import_branch, cmd_baz_import))
524
 
 
525
 
 
526
 
if hasattr(bzrlib.commands, 'register_command'):
527
 
    for command in commands:
528
 
        bzrlib.commands.register_command(command)
529
 
 
530
 
 
531
 
def test_suite():
532
 
    from bzrlib.tests.TestUtil import TestLoader
533
 
    import tests
534
 
    from doctest import DocTestSuite, ELLIPSIS
535
 
    from unittest import TestSuite
536
 
    import tests.clean_tree
537
 
    import upstream_import
538
 
    import zap
539
 
    import tests.blackbox
540
 
    import tests.shelf_tests
541
 
    result = TestSuite()
542
 
    result.addTest(DocTestSuite(bzrtools, optionflags=ELLIPSIS))
543
 
    result.addTest(tests.clean_tree.test_suite())
544
 
    try:
545
 
        import baz_import
546
 
        result.addTest(DocTestSuite(baz_import))
547
 
    except NoPyBaz:
548
 
        pass
549
 
    result.addTest(tests.test_suite())
550
 
    result.addTest(TestLoader().loadTestsFromModule(tests.shelf_tests))
551
 
    result.addTest(tests.blackbox.test_suite())
552
 
    result.addTest(upstream_import.test_suite())
553
 
    result.addTest(zap.test_suite())
554
 
    return result
 
548
        colordiff(color, check_style, *args, **kwargs)
 
549
 
 
550
 
 
551
class cmd_rspush(BzrToolsCommand):
 
552
    """Upload this branch to another location using rsync.
 
553
 
 
554
    If no location is specified, the last-used location will be used.  To
 
555
    prevent dirty trees from being uploaded, rspush will error out if there are
 
556
    unknown files or local changes.  It will also error out if the upstream
 
557
    directory is non-empty and not an earlier version of the branch.
 
558
    """
 
559
    takes_args = ['location?']
 
560
    takes_options = [Option('overwrite', help='Ignore differences between'
 
561
                            ' branches and overwrite unconditionally.'),
 
562
                     Option('no-tree', help='Do not push the working tree,'
 
563
                            ' just the .bzr.')]
 
564
 
 
565
    def run(self, location=None, overwrite=False, no_tree=False):
 
566
        from bzrlib import workingtree
 
567
        import bzrtools
 
568
        cur_branch = workingtree.WorkingTree.open_containing(".")[0]
 
569
        bzrtools.rspush(cur_branch, location, overwrite=overwrite,
 
570
                      working_tree=not no_tree)
 
571
 
 
572
 
 
573
class cmd_link_tree(BzrToolsCommand):
 
574
    """Hardlink matching files to another tree.
 
575
 
 
576
    Only files with identical content and execute bit will be linked.
 
577
    """
 
578
    takes_args = ['location']
 
579
 
 
580
    def run(self, location):
 
581
        from bzrlib import workingtree
 
582
        from bzrlib.plugins.bzrtools.link_tree import link_tree
 
583
        target_tree = workingtree.WorkingTree.open_containing(".")[0]
 
584
        source_tree = workingtree.WorkingTree.open(location)
 
585
        target_tree.lock_write()
 
586
        try:
 
587
            source_tree.lock_read()
 
588
            try:
 
589
                link_tree(target_tree, source_tree)
 
590
            finally:
 
591
                source_tree.unlock()
 
592
        finally:
 
593
            target_tree.unlock()