~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

(jelmer) Reduce the number of connections made during "bzr branch
 --stacked". (Jelmer Vernooij)

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
 
 
21
19
import os
22
20
 
23
21
import bzrlib.bzrdir
25
23
from bzrlib.lazy_import import lazy_import
26
24
lazy_import(globals(), """
27
25
import cStringIO
28
 
import errno
29
26
import sys
30
27
import time
31
28
 
756
753
    """
757
754
 
758
755
    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
 
        ]
766
756
    encoding_type = 'replace'
767
757
 
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
 
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)
792
768
            else:
793
 
                os.mkdir(dir)
794
 
            add_file(wt, relpath)
795
 
            if not is_quiet():
796
 
                self.outf.write(gettext('added %s\n') % dir)
 
769
                raise errors.NotVersionedError(path=base)
797
770
 
798
771
 
799
772
class cmd_relpath(Command):
1251
1224
        if location is None:
1252
1225
            stored_loc = br_from.get_push_location()
1253
1226
            if stored_loc is None:
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."))
 
1227
                raise errors.BzrCommandError(gettext(
 
1228
                    "No push location known or specified."))
1264
1229
            else:
1265
1230
                display_url = urlutils.unescape_for_display(stored_loc,
1266
1231
                        self.outf.encoding)
1443
1408
                    self.outf.encoding).rstrip("/"))
1444
1409
        else:
1445
1410
            dir = controldir.ControlDir.open_containing(location)[0]
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 = "*"
 
1411
            for branch in dir.list_branches():
 
1412
                if branch.name is None:
 
1413
                    self.outf.write(gettext(" (default)\n"))
1466
1414
                else:
1467
 
                    prefix = " "
1468
 
                self.outf.write("%s %s\n" % (
1469
 
                    prefix, name.encode(self.outf.encoding)))
 
1415
                    self.outf.write(" %s\n" % branch.name.encode(
 
1416
                        self.outf.encoding))
1470
1417
 
1471
1418
 
1472
1419
class cmd_checkout(Command):
3201
3148
        Option('per-file-timestamps',
3202
3149
               help='Set modification time of files to that of the last '
3203
3150
                    'revision in which it was changed.'),
3204
 
        Option('uncommitted',
3205
 
               help='Export the working tree contents rather than that of the '
3206
 
                    'last revision.'),
3207
3151
        ]
3208
3152
    def run(self, dest, branch_or_subdir=None, revision=None, format=None,
3209
 
        root=None, filters=False, per_file_timestamps=False, uncommitted=False,
3210
 
        directory=u'.'):
 
3153
        root=None, filters=False, per_file_timestamps=False, directory=u'.'):
3211
3154
        from bzrlib.export import export
3212
3155
 
3213
3156
        if branch_or_subdir is None:
3214
 
            branch_or_subdir = directory
3215
 
 
3216
 
        (tree, b, subdir) = controldir.ControlDir.open_containing_tree_or_branch(
3217
 
            branch_or_subdir)
3218
 
        if tree is not None:
3219
 
            self.add_cleanup(tree.lock_read().unlock)
3220
 
 
3221
 
        if uncommitted:
3222
 
            if tree is None:
3223
 
                raise errors.BzrCommandError(
3224
 
                    gettext("--uncommitted requires a working tree"))
3225
 
            export_tree = tree
 
3157
            tree = WorkingTree.open_containing(directory)[0]
 
3158
            b = tree.branch
 
3159
            subdir = None
3226
3160
        else:
3227
 
            export_tree = _get_one_revision_tree('export', revision, branch=b, tree=tree)
 
3161
            b, subdir = Branch.open_containing(branch_or_subdir)
 
3162
            tree = None
 
3163
 
 
3164
        rev_tree = _get_one_revision_tree('export', revision, branch=b, tree=tree)
3228
3165
        try:
3229
 
            export(export_tree, dest, format, root, subdir, filtered=filters,
 
3166
            export(rev_tree, dest, format, root, subdir, filtered=filters,
3230
3167
                   per_file_timestamps=per_file_timestamps)
3231
3168
        except errors.NoSuchExportFormat, e:
3232
 
            raise errors.BzrCommandError(
3233
 
                gettext('Unsupported export format: %s') % e.format)
 
3169
            raise errors.BzrCommandError(gettext('Unsupported export format: %s') % e.format)
3234
3170
 
3235
3171
 
3236
3172
class cmd_cat(Command):
3739
3675
            if directory is None:
3740
3676
                # use branch if we're inside one; otherwise global config
3741
3677
                try:
3742
 
                    c = Branch.open_containing(u'.')[0].get_config_stack()
 
3678
                    c = Branch.open_containing(u'.')[0].get_config()
3743
3679
                except errors.NotBranchError:
3744
 
                    c = _mod_config.GlobalStack()
 
3680
                    c = _mod_config.GlobalConfig()
3745
3681
            else:
3746
 
                c = Branch.open(directory).get_config_stack()
3747
 
            identity = c.get('email')
 
3682
                c = Branch.open(directory).get_config()
3748
3683
            if email:
3749
 
                self.outf.write(_mod_config.extract_email_address(identity)
3750
 
                                + '\n')
 
3684
                self.outf.write(c.user_email() + '\n')
3751
3685
            else:
3752
 
                self.outf.write(identity + '\n')
 
3686
                self.outf.write(c.username() + '\n')
3753
3687
            return
3754
3688
 
3755
3689
        if email:
3766
3700
        # use global config unless --branch given
3767
3701
        if branch:
3768
3702
            if directory is None:
3769
 
                c = Branch.open_containing(u'.')[0].get_config_stack()
 
3703
                c = Branch.open_containing(u'.')[0].get_config()
3770
3704
            else:
3771
 
                c = Branch.open(directory).get_config_stack()
 
3705
                c = Branch.open(directory).get_config()
3772
3706
        else:
3773
 
            c = _mod_config.GlobalStack()
3774
 
        c.set('email', name)
 
3707
            c = _mod_config.GlobalConfig()
 
3708
        c.set_user_option('email', name)
3775
3709
 
3776
3710
 
3777
3711
class cmd_nick(Command):
5085
5019
 
5086
5020
    def _run(self, b, revision_id_list, revision):
5087
5021
        import bzrlib.gpg as gpg
5088
 
        gpg_strategy = gpg.GPGStrategy(b.get_config_stack())
 
5022
        gpg_strategy = gpg.GPGStrategy(b.get_config())
5089
5023
        if revision_id_list is not None:
5090
5024
            b.repository.start_write_group()
5091
5025
            try:
5607
5541
                self.outf.writelines(directive.to_lines())
5608
5542
        else:
5609
5543
            message = directive.to_email(mail_to, branch, sign)
5610
 
            s = SMTPConnection(branch.get_config_stack())
 
5544
            s = SMTPConnection(branch.get_config())
5611
5545
            s.send_email(message)
5612
5546
 
5613
5547
 
6605
6539
    takes_options = [Option('plugin', 
6606
6540
                            help='Export help text from named command '\
6607
6541
                                 '(defaults to all built in commands).',
6608
 
                            type=str),
6609
 
                     Option('include-duplicates',
6610
 
                            help='Output multiple copies of the same msgid '
6611
 
                                 'string if it appears more than once.'),
6612
 
                            ]
 
6542
                            type=str)]
6613
6543
 
6614
 
    def run(self, plugin=None, include_duplicates=False):
 
6544
    def run(self, plugin=None):
6615
6545
        from bzrlib.export_pot import export_pot
6616
 
        export_pot(self.outf, plugin, include_duplicates)
 
6546
        export_pot(self.outf, plugin)
6617
6547
 
6618
6548
 
6619
6549
def _register_lazy_builtins():