/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Jelmer Vernooij
  • Date: 2012-01-02 14:27:58 UTC
  • mfrom: (6410 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6441.
  • Revision ID: jelmer@samba.org-20120102142758-hl7xrn7m5hjhecdv
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""builtin bzr commands"""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
import os
20
22
 
21
23
import bzrlib.bzrdir
23
25
from bzrlib.lazy_import import lazy_import
24
26
lazy_import(globals(), """
25
27
import cStringIO
 
28
import errno
26
29
import sys
27
30
import time
28
31
 
558
561
    _see_also = ['info']
559
562
    takes_args = ['location?']
560
563
    takes_options = [
561
 
        Option('tree', help='Show revno of working tree'),
 
564
        Option('tree', help='Show revno of working tree.'),
562
565
        'revision',
563
566
        ]
564
567
 
606
609
        custom_help('directory',
607
610
            help='Branch to examine, '
608
611
                 'rather than the one containing the working directory.'),
609
 
        Option('tree', help='Show revno of working tree'),
 
612
        Option('tree', help='Show revno of working tree.'),
610
613
        ]
611
614
 
612
615
    @display_command
753
756
    """
754
757
 
755
758
    takes_args = ['dir+']
 
759
    takes_options = [
 
760
        Option(
 
761
            'parents',
 
762
            help='No error if existing, make parent directories as needed.',
 
763
            short_name='p'
 
764
            )
 
765
        ]
756
766
    encoding_type = 'replace'
757
767
 
758
 
    def run(self, dir_list):
759
 
        for d in dir_list:
760
 
            wt, dd = WorkingTree.open_containing(d)
761
 
            base = os.path.dirname(dd)
762
 
            id = wt.path2id(base)
763
 
            if id != None:
764
 
                os.mkdir(d)
765
 
                wt.add([dd])
766
 
                if not is_quiet():
767
 
                    self.outf.write(gettext('added %s\n') % d)
 
768
    @classmethod
 
769
    def add_file_with_parents(cls, wt, relpath):
 
770
        if wt.path2id(relpath) is not None:
 
771
            return
 
772
        cls.add_file_with_parents(wt, osutils.dirname(relpath))
 
773
        wt.add([relpath])
 
774
 
 
775
    @classmethod
 
776
    def add_file_single(cls, wt, relpath):
 
777
        wt.add([relpath])
 
778
 
 
779
    def run(self, dir_list, parents=False):
 
780
        if parents:
 
781
            add_file = self.add_file_with_parents
 
782
        else:
 
783
            add_file = self.add_file_single
 
784
        for dir in dir_list:
 
785
            wt, relpath = WorkingTree.open_containing(dir)
 
786
            if parents:
 
787
                try:
 
788
                    os.makedirs(dir)
 
789
                except OSError, e:
 
790
                    if e.errno != errno.EEXIST:
 
791
                        raise
768
792
            else:
769
 
                raise errors.NotVersionedError(path=base)
 
793
                os.mkdir(dir)
 
794
            add_file(wt, relpath)
 
795
            if not is_quiet():
 
796
                self.outf.write(gettext('added %s\n') % dir)
770
797
 
771
798
 
772
799
class cmd_relpath(Command):
1224
1251
        if location is None:
1225
1252
            stored_loc = br_from.get_push_location()
1226
1253
            if stored_loc is None:
1227
 
                raise errors.BzrCommandError(gettext(
1228
 
                    "No push location known or specified."))
 
1254
                parent_loc = br_from.get_parent()
 
1255
                if parent_loc:
 
1256
                    raise errors.BzrCommandError(gettext(
 
1257
                        "No push location known or specified. To push to the "
 
1258
                        "parent branch (at %s), use 'bzr push :parent'." %
 
1259
                        urlutils.unescape_for_display(parent_loc,
 
1260
                            self.outf.encoding)))
 
1261
                else:
 
1262
                    raise errors.BzrCommandError(gettext(
 
1263
                        "No push location known or specified."))
1229
1264
            else:
1230
1265
                display_url = urlutils.unescape_for_display(stored_loc,
1231
1266
                        self.outf.encoding)
1312
1347
            # RBC 20060209
1313
1348
            revision_id = br_from.last_revision()
1314
1349
        if to_location is None:
1315
 
            to_location = urlutils.derive_to_location(from_location)
 
1350
            to_location = getattr(br_from, "name", None)
 
1351
            if to_location is None:
 
1352
                to_location = urlutils.derive_to_location(from_location)
1316
1353
        to_transport = transport.get_transport(to_location)
1317
1354
        try:
1318
1355
            to_transport.mkdir('.')
1319
1356
        except errors.FileExists:
1320
 
            if not use_existing_dir:
1321
 
                raise errors.BzrCommandError(gettext('Target directory "%s" '
1322
 
                    'already exists.') % to_location)
1323
 
            else:
1324
 
                try:
1325
 
                    to_dir = controldir.ControlDir.open_from_transport(
1326
 
                        to_transport)
1327
 
                except errors.NotBranchError:
 
1357
            try:
 
1358
                to_dir = controldir.ControlDir.open_from_transport(
 
1359
                    to_transport)
 
1360
            except errors.NotBranchError:
 
1361
                if not use_existing_dir:
 
1362
                    raise errors.BzrCommandError(gettext('Target directory "%s" '
 
1363
                        'already exists.') % to_location)
 
1364
                else:
1328
1365
                    to_dir = None
 
1366
            else:
 
1367
                try:
 
1368
                    to_dir.open_branch()
 
1369
                except errors.NotBranchError:
 
1370
                    pass
1329
1371
                else:
1330
 
                    try:
1331
 
                        to_dir.open_branch()
1332
 
                    except errors.NotBranchError:
1333
 
                        pass
1334
 
                    else:
1335
 
                        raise errors.AlreadyBranchError(to_location)
 
1372
                    raise errors.AlreadyBranchError(to_location)
1336
1373
        except errors.NoSuchFile:
1337
1374
            raise errors.BzrCommandError(gettext('Parent of "%s" does not exist.')
1338
1375
                                         % to_location)
1348
1385
                                            force_new_repo=standalone,
1349
1386
                                            create_tree_if_local=not no_tree,
1350
1387
                                            source_branch=br_from)
1351
 
                branch = to_dir.open_branch()
 
1388
                branch = to_dir.open_branch(
 
1389
                    possible_transports=[
 
1390
                        br_from.bzrdir.root_transport, to_transport])
1352
1391
            except errors.NoSuchRevision:
1353
1392
                to_transport.delete_tree('.')
1354
1393
                msg = gettext("The branch {0} has no revision {1}.").format(
1404
1443
                    self.outf.encoding).rstrip("/"))
1405
1444
        else:
1406
1445
            dir = controldir.ControlDir.open_containing(location)[0]
1407
 
            for branch in dir.list_branches():
1408
 
                if branch.name is None:
1409
 
                    self.outf.write(gettext(" (default)\n"))
 
1446
            try:
 
1447
                active_branch = dir.open_branch(name=None)
 
1448
            except errors.NotBranchError:
 
1449
                active_branch = None
 
1450
            branches = dir.get_branches()
 
1451
            names = {}
 
1452
            for name, branch in branches.iteritems():
 
1453
                if name is None:
 
1454
                    continue
 
1455
                active = (active_branch is not None and
 
1456
                          active_branch.base == branch.base)
 
1457
                names[name] = active
 
1458
            # Only mention the current branch explicitly if it's not
 
1459
            # one of the colocated branches
 
1460
            if not any(names.values()) and active_branch is not None:
 
1461
                self.outf.write("* %s\n" % gettext("(default)"))
 
1462
            for name in sorted(names.keys()):
 
1463
                active = names[name]
 
1464
                if active:
 
1465
                    prefix = "*"
1410
1466
                else:
1411
 
                    self.outf.write(" %s\n" % branch.name.encode(
1412
 
                        self.outf.encoding))
 
1467
                    prefix = " "
 
1468
                self.outf.write("%s %s\n" % (
 
1469
                    prefix, name.encode(self.outf.encoding)))
1413
1470
 
1414
1471
 
1415
1472
class cmd_checkout(Command):
2029
2086
            location = '.'
2030
2087
 
2031
2088
        to_transport = transport.get_transport(location)
2032
 
        to_transport.ensure_base()
2033
2089
 
2034
 
        newdir = format.initialize_on_transport(to_transport)
2035
 
        repo = newdir.create_repository(shared=True)
2036
 
        repo.set_make_working_trees(not no_trees)
 
2090
        (repo, newdir, require_stacking, repository_policy) = (
 
2091
            format.initialize_on_transport_ex(to_transport,
 
2092
            create_prefix=True, make_working_trees=not no_trees,
 
2093
            shared_repo=True, force_new_repo=True,
 
2094
            use_existing_dir=True,
 
2095
            repo_format_name=format.repository_format.get_format_string()))
2037
2096
        if not is_quiet():
2038
2097
            from bzrlib.info import show_bzrdir_info
2039
 
            show_bzrdir_info(repo.bzrdir, verbose=0, outfile=self.outf)
 
2098
            show_bzrdir_info(newdir, verbose=0, outfile=self.outf)
2040
2099
 
2041
2100
 
2042
2101
class cmd_diff(Command):
2512
2571
                   help='Do not report commits with more than one parent.'),
2513
2572
            Option('exclude-common-ancestry',
2514
2573
                   help='Display only the revisions that are not part'
2515
 
                   ' of both ancestries (require -rX..Y)'
 
2574
                   ' of both ancestries (require -rX..Y).'
2516
2575
                   ),
2517
2576
            Option('signatures',
2518
 
                   help='Show digital signature validity'),
 
2577
                   help='Show digital signature validity.'),
2519
2578
            ListOption('match',
2520
2579
                short_name='m',
2521
2580
                help='Show revisions whose properties match this '
3144
3203
        Option('per-file-timestamps',
3145
3204
               help='Set modification time of files to that of the last '
3146
3205
                    'revision in which it was changed.'),
 
3206
        Option('uncommitted',
 
3207
               help='Export the working tree contents rather than that of the '
 
3208
                    'last revision.'),
3147
3209
        ]
3148
3210
    def run(self, dest, branch_or_subdir=None, revision=None, format=None,
3149
 
        root=None, filters=False, per_file_timestamps=False, directory=u'.'):
 
3211
        root=None, filters=False, per_file_timestamps=False, uncommitted=False,
 
3212
        directory=u'.'):
3150
3213
        from bzrlib.export import export
3151
3214
 
3152
3215
        if branch_or_subdir is None:
3153
 
            tree = WorkingTree.open_containing(directory)[0]
3154
 
            b = tree.branch
3155
 
            subdir = None
 
3216
            branch_or_subdir = directory
 
3217
 
 
3218
        (tree, b, subdir) = controldir.ControlDir.open_containing_tree_or_branch(
 
3219
            branch_or_subdir)
 
3220
        if tree is not None:
 
3221
            self.add_cleanup(tree.lock_read().unlock)
 
3222
 
 
3223
        if uncommitted:
 
3224
            if tree is None:
 
3225
                raise errors.BzrCommandError(
 
3226
                    gettext("--uncommitted requires a working tree"))
 
3227
            export_tree = tree
3156
3228
        else:
3157
 
            b, subdir = Branch.open_containing(branch_or_subdir)
3158
 
            tree = None
3159
 
 
3160
 
        rev_tree = _get_one_revision_tree('export', revision, branch=b, tree=tree)
 
3229
            export_tree = _get_one_revision_tree('export', revision, branch=b, tree=tree)
3161
3230
        try:
3162
 
            export(rev_tree, dest, format, root, subdir, filtered=filters,
 
3231
            export(export_tree, dest, format, root, subdir, filtered=filters,
3163
3232
                   per_file_timestamps=per_file_timestamps)
3164
3233
        except errors.NoSuchExportFormat, e:
3165
 
            raise errors.BzrCommandError(gettext('Unsupported export format: %s') % e.format)
 
3234
            raise errors.BzrCommandError(
 
3235
                gettext('Unsupported export format: %s') % e.format)
3166
3236
 
3167
3237
 
3168
3238
class cmd_cat(Command):
3671
3741
            if directory is None:
3672
3742
                # use branch if we're inside one; otherwise global config
3673
3743
                try:
3674
 
                    c = Branch.open_containing(u'.')[0].get_config()
 
3744
                    c = Branch.open_containing(u'.')[0].get_config_stack()
3675
3745
                except errors.NotBranchError:
3676
 
                    c = _mod_config.GlobalConfig()
 
3746
                    c = _mod_config.GlobalStack()
3677
3747
            else:
3678
 
                c = Branch.open(directory).get_config()
 
3748
                c = Branch.open(directory).get_config_stack()
 
3749
            identity = c.get('email')
3679
3750
            if email:
3680
 
                self.outf.write(c.user_email() + '\n')
 
3751
                self.outf.write(_mod_config.extract_email_address(identity)
 
3752
                                + '\n')
3681
3753
            else:
3682
 
                self.outf.write(c.username() + '\n')
 
3754
                self.outf.write(identity + '\n')
3683
3755
            return
3684
3756
 
3685
3757
        if email:
3696
3768
        # use global config unless --branch given
3697
3769
        if branch:
3698
3770
            if directory is None:
3699
 
                c = Branch.open_containing(u'.')[0].get_config()
 
3771
                c = Branch.open_containing(u'.')[0].get_config_stack()
3700
3772
            else:
3701
 
                c = Branch.open(directory).get_config()
 
3773
                c = Branch.open(directory).get_config_stack()
3702
3774
        else:
3703
 
            c = _mod_config.GlobalConfig()
3704
 
        c.set_user_option('email', name)
 
3775
            c = _mod_config.GlobalStack()
 
3776
        c.set('email', name)
3705
3777
 
3706
3778
 
3707
3779
class cmd_nick(Command):
4651
4723
 
4652
4724
    @display_command
4653
4725
    def run(self, context=None):
4654
 
        import shellcomplete
 
4726
        from bzrlib import shellcomplete
4655
4727
        shellcomplete.shellcomplete(context)
4656
4728
 
4657
4729
 
5015
5087
 
5016
5088
    def _run(self, b, revision_id_list, revision):
5017
5089
        import bzrlib.gpg as gpg
5018
 
        gpg_strategy = gpg.GPGStrategy(b.get_config())
 
5090
        gpg_strategy = gpg.GPGStrategy(b.get_config_stack())
5019
5091
        if revision_id_list is not None:
5020
5092
            b.repository.start_write_group()
5021
5093
            try:
5537
5609
                self.outf.writelines(directive.to_lines())
5538
5610
        else:
5539
5611
            message = directive.to_email(mail_to, branch, sign)
5540
 
            s = SMTPConnection(branch.get_config())
 
5612
            s = SMTPConnection(branch.get_config_stack())
5541
5613
            s.send_email(message)
5542
5614
 
5543
5615
 
6068
6140
            if '/' not in to_location and '\\' not in to_location:
6069
6141
                # This path is meant to be relative to the existing branch
6070
6142
                this_url = self._get_branch_location(control_dir)
6071
 
                to_location = urlutils.join(this_url, '..', to_location)
 
6143
                # Perhaps the target control dir supports colocated branches?
 
6144
                try:
 
6145
                    root = controldir.ControlDir.open(this_url,
 
6146
                        possible_transports=[control_dir.user_transport])
 
6147
                except errors.NotBranchError:
 
6148
                    colocated = False
 
6149
                else:
 
6150
                    colocated = root._format.colocated_branches
 
6151
                if colocated:
 
6152
                    to_location = urlutils.join_segment_parameters(this_url,
 
6153
                        {"branch": urlutils.escape(to_location)})
 
6154
                else:
 
6155
                    to_location = urlutils.join(
 
6156
                        this_url, '..', urlutils.escape(to_location))
6072
6157
            to_branch = branch.bzrdir.sprout(to_location,
6073
6158
                                 possible_transports=[branch.bzrdir.root_transport],
6074
6159
                                 source_branch=branch).open_branch()
6075
6160
        else:
 
6161
            # Perhaps it's a colocated branch?
6076
6162
            try:
6077
 
                to_branch = Branch.open(to_location)
6078
 
            except errors.NotBranchError:
6079
 
                this_url = self._get_branch_location(control_dir)
6080
 
                to_branch = Branch.open(
6081
 
                    urlutils.join(this_url, '..', to_location))
 
6163
                to_branch = control_dir.open_branch(to_location)
 
6164
            except (errors.NotBranchError, errors.NoColocatedBranchSupport):
 
6165
                try:
 
6166
                    to_branch = Branch.open(to_location)
 
6167
                except errors.NotBranchError:
 
6168
                    this_url = self._get_branch_location(control_dir)
 
6169
                    to_branch = Branch.open(
 
6170
                        urlutils.join(
 
6171
                            this_url, '..', urlutils.escape(to_location)))
6082
6172
        if revision is not None:
6083
6173
            revision = revision.as_revision_id(to_branch)
6084
6174
        switch.switch(control_dir, to_branch, force, revision_id=revision)
6517
6607
    takes_options = [Option('plugin', 
6518
6608
                            help='Export help text from named command '\
6519
6609
                                 '(defaults to all built in commands).',
6520
 
                            type=str)]
 
6610
                            type=str),
 
6611
                     Option('include-duplicates',
 
6612
                            help='Output multiple copies of the same msgid '
 
6613
                                 'string if it appears more than once.'),
 
6614
                            ]
6521
6615
 
6522
 
    def run(self, plugin=None):
 
6616
    def run(self, plugin=None, include_duplicates=False):
6523
6617
        from bzrlib.export_pot import export_pot
6524
 
        export_pot(self.outf, plugin)
 
6618
        export_pot(self.outf, plugin, include_duplicates)
6525
6619
 
6526
6620
 
6527
6621
def _register_lazy_builtins():