~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Aaron Bentley
  • Date: 2006-07-01 22:25:01 UTC
  • mfrom: (408.1.1 bzrtools)
  • Revision ID: aaron.bentley@utoronto.ca-20060701222501-a5dfbdeacbccd2da
Update colordiff to use wrapped versions of diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
"""\
3
3
Various useful plugins for working with bzr.
4
4
"""
5
 
import bzrlib.commands
6
 
import push
 
5
import rspush
7
6
from errors import CommandError
8
7
from patchsource import BzrPatchSource
9
8
from shelf import Shelf
 
9
from switch import cmd_switch
10
10
import sys
11
11
import os.path
12
 
from bzrlib.option import Option
 
12
 
 
13
from bzrlib import DEFAULT_IGNORE
 
14
import bzrlib.builtins
13
15
import bzrlib.branch
 
16
import bzrlib.commands
14
17
from bzrlib.errors import BzrCommandError
 
18
from bzrlib.help import command_usage
 
19
from bzrlib.option import Option
15
20
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), 
16
 
                                                 "external")))
17
 
from bzrlib import DEFAULT_IGNORE
 
21
                                                 "external")))
18
22
 
19
23
 
20
24
DEFAULT_IGNORE.append('./.shelf')
21
25
DEFAULT_IGNORE.append('./.bzr-shelf*')
22
26
 
23
27
 
24
 
Option.OPTIONS['ignored'] = Option('ignored',
25
 
        help='delete all ignored files.')
26
 
Option.OPTIONS['detritus'] = Option('detritus',
27
 
        help='delete conflict files merge backups, and failed selftest dirs.' +
28
 
              '(*.THIS, *.BASE, *.OTHER, *~, *.tmp')
29
 
Option.OPTIONS['dry-run'] = Option('dry-run',
30
 
        help='show files to delete instead of deleting them.')
31
 
 
32
28
class cmd_clean_tree(bzrlib.commands.Command):
33
29
    """Remove unwanted files from working tree.  <BZRTOOLS>
34
30
    Normally, ignored files are left alone.
35
31
    """
36
 
    takes_options = ['ignored', 'detritus', 'dry-run']
 
32
    takes_options = [Option('ignored', help='delete all ignored files.'), 
 
33
                     Option('detritus', help='delete conflict files merge'
 
34
                            ' backups, and failed selftest dirs.  (*.THIS, '
 
35
                            '*.BASE, *.OTHER, *~, *.tmp)'), 
 
36
                     Option('dry-run', help='show files to delete instead of'
 
37
                            ' deleting them.')]
37
38
    def run(self, ignored=False, detritus=False, dry_run=False):
38
39
        from clean_tree import clean_tree
39
40
        clean_tree('.', ignored=ignored, detritus=detritus, dry_run=dry_run)
40
41
 
41
 
Option.OPTIONS['merge-branch'] = Option('merge-branch',type=str)
42
 
 
43
42
class cmd_graph_ancestry(bzrlib.commands.Command):
44
43
    """Produce ancestry graphs using dot.  <BZRTOOLS>
45
44
    
105
104
 
106
105
strip_help="""Strip the smallest prefix containing num leading slashes  from \
107
106
each file name found in the patch file."""
108
 
Option.OPTIONS['strip'] = Option('strip', type=int, help=strip_help)
109
107
Option.OPTIONS['bzrdiff'] = Option('bzrdiff',type=None,
110
108
                                help="""Handle extra bzr tags""")
111
109
class cmd_patch(bzrlib.commands.Command):
112
110
    """Apply a named patch to the current tree.  <BZRTOOLS>
113
111
    """
114
112
    takes_args = ['filename?']
115
 
    takes_options = ['strip','bzrdiff']
 
113
    takes_options = [Option('strip', type=int, help=strip_help)]
116
114
    def run(self, filename=None, strip=-1, bzrdiff=0):
117
115
        from patch import patch
118
116
        from bzrlib.workingtree import WorkingTree
119
117
        wt = WorkingTree.open_containing('.')[0]
120
118
        if strip == -1:
121
119
            if bzrdiff: strip = 0
122
 
            else:       strip = 1
 
120
            else:       strip = 0
123
121
 
124
122
        return patch(wt, filename, strip, legacy= not bzrdiff)
125
123
 
138
136
    By default shelve asks you what you want to shelve, press '?' at the
139
137
    prompt to get help. To shelve everything run shelve --all.
140
138
 
141
 
    You can put multiple items on the shelf, each time you run unshelve the
142
 
    most recently shelved changes will be reinstated.
143
 
 
144
139
    If filenames are specified, only the changes to those files will be
145
140
    shelved, other files will be left untouched.
146
141
 
147
142
    If a revision is specified, changes since that revision will be shelved.
 
143
 
 
144
    You can put multiple items on the shelf. Normally each time you run
 
145
    unshelve the most recently shelved changes will be reinstated. However,
 
146
    you can also unshelve changes in a different order by explicitly
 
147
    specifiying which changes to unshelve. This works best when the changes
 
148
    don't depend on each other.
148
149
    """
149
150
 
150
151
    takes_args = ['file*']
164
165
        s.shelve(source, all, message)
165
166
        return 0
166
167
 
 
168
 
 
169
# The following classes are only used as subcommands for 'shelf', they're
 
170
# not to be registered directly with bzr.
 
171
 
 
172
class cmd_shelf_list(bzrlib.commands.Command):
 
173
    """List the patches on the current shelf."""
 
174
    aliases = ['list', 'ls']
 
175
    def run(self):
 
176
        self.shelf.list()
 
177
 
 
178
 
 
179
class cmd_shelf_delete(bzrlib.commands.Command):
 
180
    """Delete the patch from the current shelf."""
 
181
    aliases = ['delete', 'del']
 
182
    takes_args = ['patch']
 
183
    def run(self, patch):
 
184
        self.shelf.delete(patch)
 
185
 
 
186
 
 
187
class cmd_shelf_switch(bzrlib.commands.Command):
 
188
    """Switch to the other shelf, create it if necessary."""
 
189
    aliases = ['switch']
 
190
    takes_args = ['othershelf']
 
191
    def run(self, othershelf):
 
192
        s = Shelf(self.shelf.base, othershelf)
 
193
        s.make_default()
 
194
 
 
195
 
 
196
class cmd_shelf_show(bzrlib.commands.Command):
 
197
    """Show the contents of the specified or topmost patch."""
 
198
    aliases = ['show', 'cat', 'display']
 
199
    takes_args = ['patch?']
 
200
    def run(self, patch=None):
 
201
        self.shelf.display(patch)
 
202
 
 
203
 
 
204
class cmd_shelf_upgrade(bzrlib.commands.Command):
 
205
    """Upgrade old format shelves."""
 
206
    aliases = ['upgrade']
 
207
    def run(self):
 
208
        self.shelf.upgrade()
 
209
 
 
210
 
167
211
class cmd_shelf(bzrlib.commands.Command):
168
 
    """Perform various operations on your shelved patches. See also shelve.
169
 
 
170
 
    Subcommands:
171
 
        list   (ls)           List the patches on the current shelf.
172
 
        delete (del) <patch>  Delete a patch from the current shelf.
173
 
        switch       <shelf>  Switch to the named shelf, create it if necessary.
174
 
        show         <patch>  Show the contents of the specified patch.
175
 
        upgrade               Upgrade old format shelves.
176
 
    """
 
212
    """Perform various operations on your shelved patches. See also shelve."""
177
213
    takes_args = ['subcommand', 'args*']
178
214
 
 
215
    subcommands = [cmd_shelf_list, cmd_shelf_delete, cmd_shelf_switch,
 
216
        cmd_shelf_show, cmd_shelf_upgrade]
 
217
 
179
218
    def run(self, subcommand, args_list):
180
219
        import sys
181
220
 
 
221
        cmd = self._get_cmd_object(subcommand)
182
222
        source = BzrPatchSource()
183
223
        s = Shelf(source.base)
184
 
 
185
 
        if subcommand == 'ls' or subcommand == 'list':
186
 
            self.__check_no_args(args_list, "shelf list takes no arguments!")
187
 
            s.list()
188
 
        elif subcommand == 'delete' or subcommand == 'del':
189
 
            self.__check_one_arg(args_list, "shelf delete takes one argument!")
190
 
            s.delete(args_list[0])
191
 
        elif subcommand == 'switch':
192
 
            self.__check_one_arg(args_list, "shelf switch takes one argument!")
193
 
            s = Shelf(source.base, args_list[0])
194
 
            s.make_default()
195
 
        elif subcommand == 'show':
196
 
            self.__check_one_arg(args_list, "shelf show takes one argument!")
197
 
            s.display(args_list[0])
198
 
        elif subcommand == 'upgrade':
199
 
            self.__check_no_args(args_list, "shelf upgrade takes no arguments!")
200
 
            s.upgrade()
201
 
        else:
202
 
            print subcommand, args_list
203
 
            print >>sys.stderr, "Unknown shelf subcommand '%s'" % subcommand
204
 
 
205
 
    def __check_one_arg(self, args, msg):
206
 
        if args is None or len(args) != 1:
207
 
            raise CommandError(msg)
208
 
 
209
 
    def __check_no_args(self, args, msg):
210
 
        if args is not None:
211
 
            raise CommandError(msg)
 
224
        cmd.shelf = s
 
225
        return cmd.run_argv_aliases(args_list)
 
226
 
 
227
    def _get_cmd_object(self, cmd_name):
 
228
        for cmd_class in self.subcommands:
 
229
            for alias in cmd_class.aliases:
 
230
                if alias == cmd_name:
 
231
                    return cmd_class()
 
232
        raise CommandError("Unknown shelf subcommand '%s'" % cmd_name)
 
233
 
 
234
    def help(self):
 
235
        text = ["%s\n\nSubcommands:\n" % self.__doc__]
 
236
 
 
237
        for cmd_class in self.subcommands:
 
238
            text.extend(self.sub_help(cmd_class) + ['\n'])
 
239
 
 
240
        return ''.join(text)
 
241
 
 
242
    def sub_help(self, cmd_class):
 
243
        text = []
 
244
        cmd_obj = cmd_class()
 
245
        indent = 2 * ' '
 
246
 
 
247
        usage = command_usage(cmd_obj)
 
248
        usage = usage.replace('bzr shelf-', '')
 
249
        text.append('%s%s\n' % (indent, usage))
 
250
 
 
251
        text.append('%s%s\n' % (2 * indent, cmd_class.__doc__))
 
252
 
 
253
        # Somewhat copied from bzrlib.help.help_on_command_options
 
254
        option_help = []
 
255
        for option_name, option in sorted(cmd_obj.options().items()):
 
256
            if option_name == 'help':
 
257
                continue
 
258
            option_help.append('%s--%s' % (3 * indent, option_name))
 
259
            if option.type is not None:
 
260
                option_help.append(' %s' % option.argname.upper())
 
261
            if option.short_name():
 
262
                option_help.append(', -%s' % option.short_name())
 
263
            option_help.append('%s%s\n' % (2 * indent, option.help))
 
264
 
 
265
        if len(option_help) > 0:
 
266
            text.append('%soptions:\n' % (2 * indent))
 
267
            text.extend(option_help)
 
268
 
 
269
        return text
 
270
 
212
271
 
213
272
 
214
273
class cmd_unshelve(bzrlib.commands.Command):
215
 
    """Restore the most recently shelved changes to current tree.  <BZRTOOLS>
 
274
    """Restore shelved changes.  <BZRTOOLS>
 
275
 
 
276
    By default the most recently shelved changes are restored. However if you
 
277
    specify a patch by name those changes will be restored instead.
 
278
 
216
279
    See 'shelve' for more information.
217
280
    """
218
281
    takes_options = [
219
282
            Option('all', help='Unshelve all changes without prompting'),
220
283
            Option('force', help='Force unshelving even if errors occur'),
221
284
    ]
222
 
    def run(self, all=False, force=False):
 
285
    takes_args = ['patch?']
 
286
    def run(self, patch=None, all=False, force=False):
223
287
        source = BzrPatchSource()
224
288
        s = Shelf(source.base)
225
 
        s.unshelve(source, all, force)
 
289
        s.unshelve(source, patch, all, force)
226
290
        return 0
227
291
 
228
292
 
269
333
    This command will remove a checkout without losing data.  That means
270
334
    it only removes checkouts, and only if they have no uncommitted changes.
271
335
    """
 
336
    takes_options = [Option("branch", help="Remove associtated branch from"
 
337
                                           " repository")]
272
338
    takes_args = ["checkout"]
273
 
    def run(self, checkout):
 
339
    def run(self, checkout, branch=False):
274
340
        from zap import zap
275
 
        return zap(checkout)
 
341
        return zap(checkout, remove_branch=branch)
276
342
 
277
343
 
278
344
class cmd_cbranch(bzrlib.commands.Command):
294
360
    standalone branches will be produced.  Standalone branches will also
295
361
    be produced if the source branch is in 0.7 format (or earlier).
296
362
    """
297
 
    takes_args = ["source", "target"]
298
 
    def run(self, source, target):
 
363
    takes_options = [Option("lightweight", 
 
364
                            help="Create a lightweight checkout")]
 
365
    takes_args = ["source", "target?"]
 
366
    def run(self, source, target=None, lightweight=False):
299
367
        from cbranch import cbranch
300
 
        return cbranch(source, target)
 
368
        return cbranch(source, target, lightweight=lightweight)
 
369
 
301
370
 
302
371
class cmd_branches(bzrlib.commands.Command):
303
372
    """Scan a location for branches <BZRTOOLS>"""
347
416
                print e
348
417
 
349
418
 
 
419
class cmd_branch_mark(bzrlib.commands.Command):
 
420
    """
 
421
    Add, view or list branch markers <EXPERIMENTAL>
 
422
 
 
423
    To add a mark, do 'bzr branch-mark MARK'.
 
424
    To list marks, do 'bzr branch-mark' (this lists all marks for the branch's
 
425
    repository).
 
426
    To delete a mark, do 'bzr branch-mark --delete MARK'
 
427
 
 
428
    These marks can be used to track a branch's status.
 
429
    """
 
430
    takes_args = ['mark?', 'branch?']
 
431
    takes_options = [Option('delete', help='Delete this mark')]
 
432
    def run(self, mark=None, branch=None, delete=False):
 
433
        from branch_mark import branch_mark
 
434
        branch_mark(mark, branch, delete)
 
435
 
 
436
class cmd_import(bzrlib.commands.Command):
 
437
    """Import sources from a tarball <BZRTOOLS>
 
438
    
 
439
    This command will import a tarball into a bzr tree, replacing any versioned
 
440
    files already present.  If a directory is specified, it is used as the
 
441
    target.  If the directory does not exist, or is not versioned, it is
 
442
    created.
 
443
 
 
444
    Tarballs may be gzip or bzip2 compressed.  This is autodetected.
 
445
 
 
446
    If the tarball has a single root directory, that directory is stripped
 
447
    when extracting the tarball.
 
448
    """
 
449
    
 
450
    takes_args = ['source', 'tree?']
 
451
    def run(self, source, tree=None):
 
452
        from upstream_import import do_import
 
453
        do_import(source, tree)
 
454
 
 
455
class cmd_shove(bzrlib.commands.Command):
 
456
    """Apply uncommitted changes to another tree <BZRTOOLS>
 
457
    
 
458
    This is useful when you start to make changes in one tree, then realize
 
459
    they should really be done in a different tree.
 
460
 
 
461
    Shove is implemented using merge, so:
 
462
     - All changes, including renames and adds, will be applied.
 
463
     - No changes that have already been applied will be applied.
 
464
     - If the target is significantly different from the source, conflicts may
 
465
       be produced.
 
466
    """
 
467
 
 
468
    takes_args = ['target', 'source?']
 
469
    def run(self, target, source='.'):
 
470
        from shove import do_shove
 
471
        do_shove(source, target)
 
472
 
 
473
class cmd_cdiff(bzrlib.commands.Command):
 
474
    """A color version of bzr's diff <BZRTOOLS>"""
 
475
    takes_args = bzrlib.builtins.cmd_diff.takes_args
 
476
    takes_options = bzrlib.builtins.cmd_diff.takes_options
 
477
    def run(*args, **kwargs):
 
478
        from colordiff import colordiff
 
479
        colordiff(*args, **kwargs)
 
480
 
350
481
commands = [cmd_shelve, cmd_unshelve, cmd_shelf, cmd_clean_tree,
351
482
            cmd_graph_ancestry, cmd_fetch_ghosts, cmd_patch, cmd_shell,
352
483
            cmd_branch_history, cmd_zap, cmd_cbranch, cmd_branches, 
353
 
            cmd_multi_pull]
354
 
 
355
 
 
356
 
command_decorators = []
357
 
 
358
 
 
359
 
import bzrlib.builtins
360
 
if not hasattr(bzrlib.builtins, "cmd_push"):
361
 
    commands.append(push.cmd_push)
362
 
else:
363
 
    command_decorators.append(push.cmd_push)
 
484
            cmd_multi_pull, cmd_switch, cmd_branch_mark, cmd_import, cmd_shove,
 
485
            cmd_cdiff]
 
486
 
 
487
 
 
488
commands.append(rspush.cmd_rspush)
364
489
 
365
490
from errors import NoPyBaz
366
491
try:
393
518
if hasattr(bzrlib.commands, 'register_command'):
394
519
    for command in commands:
395
520
        bzrlib.commands.register_command(command)
396
 
    for command in command_decorators:
397
 
        command._original_command = bzrlib.commands.register_command(
398
 
            command, True)
399
521
 
400
522
 
401
523
def test_suite():
402
 
    import baz_import
403
524
    from bzrlib.tests.TestUtil import TestLoader
404
525
    import tests
405
526
    from doctest import DocTestSuite, ELLIPSIS
406
527
    from unittest import TestSuite
407
 
    import clean_tree
 
528
    import tests.clean_tree
 
529
    import upstream_import
408
530
    import zap
409
531
    import tests.blackbox
410
532
    import tests.shelf_tests
411
533
    result = TestSuite()
412
534
    result.addTest(DocTestSuite(bzrtools, optionflags=ELLIPSIS))
413
 
    result.addTest(clean_tree.test_suite())
414
 
    result.addTest(DocTestSuite(baz_import))
 
535
    result.addTest(tests.clean_tree.test_suite())
 
536
    try:
 
537
        import baz_import
 
538
        result.addTest(DocTestSuite(baz_import))
 
539
    except NoPyBaz:
 
540
        pass
415
541
    result.addTest(tests.test_suite())
416
542
    result.addTest(TestLoader().loadTestsFromModule(tests.shelf_tests))
417
543
    result.addTest(tests.blackbox.test_suite())
 
544
    result.addTest(upstream_import.test_suite())
418
545
    result.addTest(zap.test_suite())
419
546
    return result