~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2005-07-21 12:01:14 UTC
  • Revision ID: mbp@sourcefrog.net-20050721120114-100fff1ee8c35385
- fix up self reference in msvc lock object

Show diffs side-by-side

added added

removed removed

Lines of Context:
122
122
    return revs
123
123
 
124
124
 
125
 
def get_merge_type(typestring):
126
 
    """Attempt to find the merge class/factory associated with a string."""
127
 
    from merge import merge_types
128
 
    try:
129
 
        return merge_types[typestring][0]
130
 
    except KeyError:
131
 
        templ = '%s%%7s: %%s' % (' '*12)
132
 
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
133
 
        type_list = '\n'.join(lines)
134
 
        msg = "No known merge type %s. Supported types are:\n%s" %\
135
 
            (typestring, type_list)
136
 
        raise BzrCommandError(msg)
137
 
    
138
 
 
139
125
 
140
126
def _get_cmd_dict(plugins_override=True):
141
127
    d = {}
214
200
        assert isinstance(arguments, dict)
215
201
        cmdargs = options.copy()
216
202
        cmdargs.update(arguments)
217
 
        if self.__doc__ == Command.__doc__:
218
 
            from warnings import warn
219
 
            warn("No help message set for %r" % self)
 
203
        assert self.__doc__ != Command.__doc__, \
 
204
               ("No help message set for %r" % self)
220
205
        self.status = self.run(**cmdargs)
221
 
        if self.status is None:
222
 
            self.status = 0
223
206
 
224
207
    
225
208
    def run(self):
426
409
    whether already versioned or not, are searched for files or
427
410
    subdirectories that are neither versioned or ignored, and these
428
411
    are added.  This search proceeds recursively into versioned
429
 
    directories.  If no names are given '.' is assumed.
 
412
    directories.
430
413
 
431
 
    Therefore simply saying 'bzr add' will version all files that
 
414
    Therefore simply saying 'bzr add .' will version all files that
432
415
    are currently unknown.
433
416
 
434
417
    TODO: Perhaps adding a file whose directly is not versioned should
435
418
    recursively add that parent, rather than giving an error?
436
419
    """
437
 
    takes_args = ['file*']
 
420
    takes_args = ['file+']
438
421
    takes_options = ['verbose', 'no-recurse']
439
422
    
440
423
    def run(self, file_list, verbose=False, no_recurse=False):
1271
1254
 
1272
1255
 
1273
1256
 
1274
 
class cmd_scan_cache(Command):
 
1257
class cmd_update_hashes(Command):
1275
1258
    hidden = True
1276
1259
    def run(self):
1277
1260
        from bzrlib.hashcache import HashCache
1278
 
        import os
1279
1261
 
1280
1262
        c = HashCache('.')
1281
1263
        c.read()
1282
 
        c.scan()
 
1264
        for name in c._cache:
 
1265
            c.get_sha1(name)
1283
1266
            
1284
 
        print '%6d stats' % c.stat_count
1285
 
        print '%6d in hashcache' % len(c._cache)
1286
 
        print '%6d files removed from cache' % c.removed_count
1287
 
        print '%6d hashes updated' % c.update_count
1288
 
        print '%6d files changed too recently to cache' % c.danger_count
 
1267
        
1289
1268
 
1290
 
        if c.needs_write:
1291
 
            c.write()
1292
 
            
1293
1269
 
1294
1270
 
1295
1271
class cmd_upgrade(Command):
1320
1296
class cmd_selftest(Command):
1321
1297
    """Run internal test suite"""
1322
1298
    hidden = True
1323
 
    takes_options = ['verbose']
1324
 
    def run(self, verbose=False):
 
1299
    def run(self):
1325
1300
        from bzrlib.selftest import selftest
1326
 
        return int(not selftest(verbose=verbose))
 
1301
        return int(not selftest())
1327
1302
 
1328
1303
 
1329
1304
class cmd_version(Command):
1407
1382
    --force is given.
1408
1383
    """
1409
1384
    takes_args = ['other_spec', 'base_spec?']
1410
 
    takes_options = ['force', 'merge-type']
 
1385
    takes_options = ['force']
1411
1386
 
1412
 
    def run(self, other_spec, base_spec=None, force=False, merge_type=None):
 
1387
    def run(self, other_spec, base_spec=None, force=False):
1413
1388
        from bzrlib.merge import merge
1414
 
        from bzrlib.merge_core import ApplyMerge3
1415
 
        if merge_type is None:
1416
 
            merge_type = ApplyMerge3
1417
1389
        merge(parse_spec(other_spec), parse_spec(base_spec),
1418
 
              check_clean=(not force), merge_type=merge_type)
 
1390
              check_clean=(not force))
 
1391
 
1419
1392
 
1420
1393
 
1421
1394
class cmd_revert(Command):
 
1395
    """Restore selected files from a previous revision.
 
1396
    """
 
1397
    takes_args = ['file+']
 
1398
    def run(self, file_list):
 
1399
        from bzrlib.branch import find_branch
 
1400
        
 
1401
        if not file_list:
 
1402
            file_list = ['.']
 
1403
            
 
1404
        b = find_branch(file_list[0])
 
1405
 
 
1406
        b.revert([b.relpath(f) for f in file_list])
 
1407
 
 
1408
 
 
1409
class cmd_merge_revert(Command):
1422
1410
    """Reverse all changes since the last commit.
1423
1411
 
1424
 
    Only versioned files are affected.  Specify filenames to revert only 
1425
 
    those files.  By default, any files that are changed will be backed up
1426
 
    first.  Backup files have a '~' appended to their name.
 
1412
    Only versioned files are affected.
 
1413
 
 
1414
    TODO: Store backups of any files that will be reverted, so
 
1415
          that the revert can be undone.          
1427
1416
    """
1428
 
    takes_options = ['revision', 'no-backup']
1429
 
    takes_args = ['file*']
1430
 
    aliases = ['merge-revert']
 
1417
    takes_options = ['revision']
1431
1418
 
1432
 
    def run(self, revision=None, no_backup=False, file_list=None):
 
1419
    def run(self, revision=None):
1433
1420
        from bzrlib.merge import merge
1434
 
        if file_list is not None:
1435
 
            if len(file_list) == 0:
1436
 
                raise BzrCommandError("No files specified")
1437
1421
        if revision is None:
1438
1422
            revision = [-1]
1439
1423
        elif len(revision) != 1:
1440
 
            raise BzrCommandError('bzr revert --revision takes exactly 1 argument')
 
1424
            raise BzrCommandError('bzr merge-revert --revision takes exactly 1 argument')
1441
1425
        merge(('.', revision[0]), parse_spec('.'),
1442
1426
              check_clean=False,
1443
 
              ignore_zero=True,
1444
 
              backup_files=not no_backup,
1445
 
              file_list=file_list)
 
1427
              ignore_zero=True)
1446
1428
 
1447
1429
 
1448
1430
class cmd_assert_fail(Command):
1507
1489
    'update':                 None,
1508
1490
    'long':                   None,
1509
1491
    'root':                   str,
1510
 
    'no-backup':              None,
1511
 
    'merge-type':             get_merge_type,
1512
1492
    }
1513
1493
 
1514
1494
SHORT_OPTIONS = {
1684
1664
                    This is also a non-master option.
1685
1665
        --help      Run help and exit, also a non-master option (I think that should stay, though)
1686
1666
 
1687
 
    >>> argv, opts = _parse_master_args(['--test'])
 
1667
    >>> argv, opts = _parse_master_args(['bzr', '--test'])
1688
1668
    Traceback (most recent call last):
1689
1669
    ...
1690
1670
    BzrCommandError: Invalid master option: 'test'
1691
 
    >>> argv, opts = _parse_master_args(['--version', 'command'])
 
1671
    >>> argv, opts = _parse_master_args(['bzr', '--version', 'command'])
1692
1672
    >>> print argv
1693
1673
    ['command']
1694
1674
    >>> print opts['version']
1695
1675
    True
1696
 
    >>> argv, opts = _parse_master_args(['--profile', 'command', '--more-options'])
 
1676
    >>> argv, opts = _parse_master_args(['bzr', '--profile', 'command', '--more-options'])
1697
1677
    >>> print argv
1698
1678
    ['command', '--more-options']
1699
1679
    >>> print opts['profile']
1700
1680
    True
1701
 
    >>> argv, opts = _parse_master_args(['--no-plugins', 'command'])
 
1681
    >>> argv, opts = _parse_master_args(['bzr', '--no-plugins', 'command'])
1702
1682
    >>> print argv
1703
1683
    ['command']
1704
1684
    >>> print opts['no-plugins']
1705
1685
    True
1706
1686
    >>> print opts['profile']
1707
1687
    False
1708
 
    >>> argv, opts = _parse_master_args(['command', '--profile'])
 
1688
    >>> argv, opts = _parse_master_args(['bzr', 'command', '--profile'])
1709
1689
    >>> print argv
1710
1690
    ['command', '--profile']
1711
1691
    >>> print opts['profile']
1718
1698
        'help':False
1719
1699
    }
1720
1700
 
 
1701
    # This is the point where we could hook into argv[0] to determine
 
1702
    # what front-end is supposed to be run
 
1703
    # For now, we are just ignoring it.
 
1704
    cmd_name = argv.pop(0)
1721
1705
    for arg in argv[:]:
1722
1706
        if arg[:2] != '--': # at the first non-option, we return the rest
1723
1707
            break
1737
1721
 
1738
1722
    This is similar to main(), but without all the trappings for
1739
1723
    logging and error handling.  
1740
 
    
1741
 
    argv
1742
 
       The command-line arguments, without the program name.
1743
 
    
1744
 
    Returns a command status or raises an exception.
1745
1724
    """
1746
1725
    argv = [a.decode(bzrlib.user_encoding) for a in argv]
1747
 
 
1748
 
    # some options like --builtin and --no-plugins have special effects
1749
 
    argv, master_opts = _parse_master_args(argv)
1750
 
    if not master_opts['no-plugins']:
1751
 
        from bzrlib.plugin import load_plugins
1752
 
        load_plugins()
1753
 
 
1754
 
    args, opts = parse_args(argv)
1755
 
 
1756
 
    if master_opts.get('help') or 'help' in opts:
1757
 
        from bzrlib.help import help
1758
 
        if argv:
1759
 
            help(argv[0])
1760
 
        else:
1761
 
            help()
1762
 
        return 0            
1763
 
        
1764
 
    if 'version' in opts:
1765
 
        show_version()
1766
 
        return 0
1767
 
    
1768
 
    if args and args[0] == 'builtin':
1769
 
        include_plugins=False
1770
 
        args = args[1:]
1771
1726
    
1772
1727
    try:
 
1728
        # some options like --builtin and --no-plugins have special effects
 
1729
        argv, master_opts = _parse_master_args(argv)
 
1730
        if not master_opts['no-plugins']:
 
1731
            from bzrlib.plugin import load_plugins
 
1732
            load_plugins()
 
1733
 
 
1734
        args, opts = parse_args(argv)
 
1735
 
 
1736
        if master_opts['help']:
 
1737
            from bzrlib.help import help
 
1738
            if argv:
 
1739
                help(argv[0])
 
1740
            else:
 
1741
                help()
 
1742
            return 0            
 
1743
            
 
1744
        if 'help' in opts:
 
1745
            from bzrlib.help import help
 
1746
            if args:
 
1747
                help(args[0])
 
1748
            else:
 
1749
                help()
 
1750
            return 0
 
1751
        elif 'version' in opts:
 
1752
            show_version()
 
1753
            return 0
 
1754
        elif args and args[0] == 'builtin':
 
1755
            include_plugins=False
 
1756
            args = args[1:]
1773
1757
        cmd = str(args.pop(0))
1774
1758
    except IndexError:
1775
 
        print >>sys.stderr, "please try 'bzr help' for help"
 
1759
        import help
 
1760
        help.help()
1776
1761
        return 1
 
1762
          
1777
1763
 
1778
1764
    plugins_override = not (master_opts['builtin'])
1779
1765
    canonical_cmd, cmd_class = get_cmd_class(cmd, plugins_override=plugins_override)
1844
1830
    try:
1845
1831
        try:
1846
1832
            try:
1847
 
                return run_bzr(argv[1:])
 
1833
                return run_bzr(argv)
1848
1834
            finally:
1849
1835
                # do this here inside the exception wrappers to catch EPIPE
1850
1836
                sys.stdout.flush()