~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
2
 
 
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
24
import sys
25
25
 
26
26
import bzrlib
 
27
from bzrlib import (
 
28
    branch,
 
29
    bundle,
 
30
    bzrdir,
 
31
    config,
 
32
    errors,
 
33
    ignores,
 
34
    log,
 
35
    osutils,
 
36
    repository,
 
37
    transport,
 
38
    ui,
 
39
    urlutils,
 
40
    )
27
41
from bzrlib.branch import Branch, BranchReferenceFormat
28
 
from bzrlib import (bundle, branch, bzrdir, errors, osutils, ui, config,
29
 
    repository, log)
30
42
from bzrlib.bundle import read_bundle_from_url
31
43
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
32
44
from bzrlib.conflicts import ConflictList
42
54
from bzrlib.revisionspec import RevisionSpec
43
55
from bzrlib.trace import mutter, note, log_error, warning, is_quiet, info
44
56
from bzrlib.transport.local import LocalTransport
45
 
import bzrlib.urlutils as urlutils
46
57
from bzrlib.workingtree import WorkingTree
47
58
 
48
59
 
516
527
            create_prefix=False, verbose=False):
517
528
        # FIXME: Way too big!  Put this into a function called from the
518
529
        # command.
519
 
        from bzrlib.transport import get_transport
520
530
        
521
531
        br_from = Branch.open_containing('.')[0]
522
532
        stored_loc = br_from.get_push_location()
529
539
                self.outf.write("Using saved location: %s\n" % display_url)
530
540
                location = stored_loc
531
541
 
532
 
        transport = get_transport(location)
533
 
        location_url = transport.base
 
542
        to_transport = transport.get_transport(location)
 
543
        location_url = to_transport.base
534
544
 
535
545
        old_rh = []
536
546
        try:
538
548
            br_to = dir_to.open_branch()
539
549
        except NotBranchError:
540
550
            # create a branch.
541
 
            transport = transport.clone('..')
 
551
            to_transport = to_transport.clone('..')
542
552
            if not create_prefix:
543
553
                try:
544
 
                    relurl = transport.relpath(location_url)
 
554
                    relurl = to_transport.relpath(location_url)
545
555
                    mutter('creating directory %s => %s', location_url, relurl)
546
 
                    transport.mkdir(relurl)
 
556
                    to_transport.mkdir(relurl)
547
557
                except NoSuchFile:
548
558
                    raise BzrCommandError("Parent directory of %s "
549
559
                                          "does not exist." % location)
550
560
            else:
551
 
                current = transport.base
552
 
                needed = [(transport, transport.relpath(location_url))]
 
561
                current = to_transport.base
 
562
                needed = [(to_transport, to_transport.relpath(location_url))]
553
563
                while needed:
554
564
                    try:
555
 
                        transport, relpath = needed[-1]
556
 
                        transport.mkdir(relpath)
 
565
                        to_transport, relpath = needed[-1]
 
566
                        to_transport.mkdir(relpath)
557
567
                        needed.pop()
558
568
                    except NoSuchFile:
559
 
                        new_transport = transport.clone('..')
 
569
                        new_transport = to_transport.clone('..')
560
570
                        needed.append((new_transport,
561
 
                                       new_transport.relpath(transport.base)))
562
 
                        if new_transport.base == transport.base:
 
571
                                       new_transport.relpath(to_transport.base)))
 
572
                        if new_transport.base == to_transport.base:
563
573
                            raise BzrCommandError("Could not create "
564
574
                                                  "path prefix.")
565
575
            dir_to = br_from.bzrdir.clone(location_url,
618
628
    aliases = ['get', 'clone']
619
629
 
620
630
    def run(self, from_location, to_location=None, revision=None, basis=None):
621
 
        from bzrlib.transport import get_transport
622
631
        if revision is None:
623
632
            revision = [None]
624
633
        elif len(revision) > 1:
651
660
            else:
652
661
                name = os.path.basename(to_location) + '\n'
653
662
 
654
 
            to_transport = get_transport(to_location)
 
663
            to_transport = transport.get_transport(to_location)
655
664
            try:
656
665
                to_transport.mkdir('.')
657
666
            except errors.FileExists:
753
762
        old_format = bzrdir.BzrDirFormat.get_default_format()
754
763
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
755
764
        try:
756
 
            if lightweight:
757
 
                checkout = bzrdir.BzrDirMetaFormat1().initialize(to_location)
758
 
                branch.BranchReferenceFormat().initialize(checkout, source)
759
 
            else:
760
 
                checkout_branch =  bzrdir.BzrDir.create_branch_convenience(
761
 
                    to_location, force_new_tree=False)
762
 
                checkout = checkout_branch.bzrdir
763
 
                checkout_branch.bind(source)
764
 
                if revision_id is not None:
765
 
                    rh = checkout_branch.revision_history()
766
 
                    checkout_branch.set_revision_history(rh[:rh.index(revision_id) + 1])
767
 
            checkout.create_workingtree(revision_id)
 
765
            bzrdir.BzrDir.create_checkout_convenience(to_location, source,
 
766
                                                      revision_id, lightweight)
768
767
        finally:
769
768
            bzrdir.BzrDirFormat.set_default_format(old_format)
770
769
 
800
799
    'bzr revert' instead of 'bzr commit' after the update.
801
800
    """
802
801
    takes_args = ['dir?']
 
802
    aliases = ['up']
803
803
 
804
804
    def run(self, dir='.'):
805
805
        tree = WorkingTree.open_containing(dir)[0]
806
806
        tree.lock_write()
 
807
        existing_pending_merges = tree.pending_merges()
807
808
        try:
808
809
            last_rev = tree.last_revision() 
809
810
            if last_rev == tree.branch.last_revision():
816
817
            conflicts = tree.update()
817
818
            revno = tree.branch.revision_id_to_revno(tree.last_revision())
818
819
            note('Updated to revision %d.' % (revno,))
 
820
            if tree.pending_merges() != existing_pending_merges:
 
821
                note('Your local commits will now show as pending merges with '
 
822
                     "'bzr status', and can be committed with 'bzr commit'.")
819
823
            if conflicts != 0:
820
824
                return 1
821
825
            else:
866
870
                raise BzrCommandError('Specify one or more files to remove, or'
867
871
                                      ' use --new.')
868
872
        else:
869
 
            from bzrlib.delta import compare_trees
870
 
            added = [compare_trees(tree.basis_tree(), tree,
871
 
                                   specific_files=file_list).added]
872
 
            file_list = sorted([f[0] for f in added[0]], reverse=True)
 
873
            added = tree.changes_from(tree.basis_tree(),
 
874
                specific_files=file_list).added
 
875
            file_list = sorted([f[0] for f in added], reverse=True)
873
876
            if len(file_list) == 0:
874
877
                raise BzrCommandError('No matching files.')
875
878
        tree.remove(file_list, verbose=verbose, to_file=self.outf)
1016
1019
            format = get_format_type('default')
1017
1020
        if location is None:
1018
1021
            location = u'.'
1019
 
        else:
1020
 
            # The path has to exist to initialize a
1021
 
            # branch inside of it.
1022
 
            # Just using os.mkdir, since I don't
1023
 
            # believe that we want to create a bunch of
1024
 
            # locations if the user supplies an extended path
1025
 
            if not os.path.exists(location):
1026
 
                os.mkdir(location)
 
1022
 
 
1023
        to_transport = transport.get_transport(location)
 
1024
 
 
1025
        # The path has to exist to initialize a
 
1026
        # branch inside of it.
 
1027
        # Just using os.mkdir, since I don't
 
1028
        # believe that we want to create a bunch of
 
1029
        # locations if the user supplies an extended path
 
1030
        # TODO: create-prefix
 
1031
        try:
 
1032
            to_transport.mkdir('.')
 
1033
        except errors.FileExists:
 
1034
            pass
 
1035
                    
1027
1036
        try:
1028
1037
            existing_bzrdir = bzrdir.BzrDir.open(location)
1029
1038
        except NotBranchError:
1031
1040
            bzrdir.BzrDir.create_branch_convenience(location, format=format)
1032
1041
        else:
1033
1042
            if existing_bzrdir.has_branch():
1034
 
                if existing_bzrdir.has_workingtree():
1035
 
                    raise errors.AlreadyBranchError(location)
1036
 
                else:
1037
 
                    raise errors.BranchExistsWithoutWorkingTree(location)
 
1043
                if (isinstance(to_transport, LocalTransport)
 
1044
                    and not existing_bzrdir.has_workingtree()):
 
1045
                        raise errors.BranchExistsWithoutWorkingTree(location)
 
1046
                raise errors.AlreadyBranchError(location)
1038
1047
            else:
1039
1048
                existing_bzrdir.create_branch()
1040
1049
                existing_bzrdir.create_workingtree()
1066
1075
                             ' a working tree')]
1067
1076
    aliases = ["init-repo"]
1068
1077
    def run(self, location, format=None, trees=False):
1069
 
        from bzrlib.transport import get_transport
1070
1078
        if format is None:
1071
1079
            format = get_format_type('default')
1072
 
        transport = get_transport(location)
1073
 
        if not transport.has('.'):
1074
 
            transport.mkdir('')
1075
 
        newdir = format.initialize_on_transport(transport)
 
1080
 
 
1081
        if location is None:
 
1082
            location = '.'
 
1083
 
 
1084
        to_transport = transport.get_transport(location)
 
1085
        try:
 
1086
            to_transport.mkdir('.')
 
1087
        except errors.FileExists:
 
1088
            pass
 
1089
 
 
1090
        newdir = format.initialize_on_transport(to_transport)
1076
1091
        repo = newdir.create_repository(shared=True)
1077
1092
        repo.set_make_working_trees(trees)
1078
1093
 
1149
1164
                raise BzrCommandError("Files are in different branches")
1150
1165
            file_list = None
1151
1166
        except NotBranchError:
1152
 
            # Don't raise an error when bzr diff is called from
1153
 
            # outside a working tree.
1154
 
            tree1, tree2 = None, None
 
1167
            if (revision is not None and len(revision) == 2
 
1168
                and not revision[0].needs_branch()
 
1169
                and not revision[1].needs_branch()):
 
1170
                # If both revision specs include a branch, we can
 
1171
                # diff them without needing a local working tree
 
1172
                tree1, tree2 = None, None
 
1173
            else:
 
1174
                raise
1155
1175
        if revision is not None:
1156
1176
            if tree2 is not None:
1157
1177
                raise BzrCommandError("Can't specify -r with two branches")
1205
1225
    hidden = True
1206
1226
    @display_command
1207
1227
    def run(self):
1208
 
        from bzrlib.delta import compare_trees
1209
 
 
1210
1228
        tree = WorkingTree.open_containing(u'.')[0]
1211
 
        td = compare_trees(tree.basis_tree(), tree)
1212
 
 
 
1229
        td = tree.changes_from(tree.basis_tree())
1213
1230
        for path, id, kind, text_modified, meta_modified in td.modified:
1214
1231
            self.outf.write(path + '\n')
1215
1232
 
1225
1242
        for file_id in inv:
1226
1243
            if file_id in basis_inv:
1227
1244
                continue
 
1245
            if inv.is_root(file_id) and len(basis_inv) == 0:
 
1246
                continue
1228
1247
            path = inv.id2path(file_id)
1229
1248
            if not os.access(osutils.abspath(path), os.F_OK):
1230
1249
                continue
1480
1499
        from bzrlib.atomicfile import AtomicFile
1481
1500
        if old_default_rules is not None:
1482
1501
            # dump the rules and exit
1483
 
            for pattern in bzrlib.DEFAULT_IGNORE:
 
1502
            for pattern in ignores.OLD_DEFAULTS:
1484
1503
                print pattern
1485
1504
            return
1486
1505
        if name_pattern is None:
1732
1751
            raise BzrCommandError("Commit refused because there are unknown "
1733
1752
                                  "files in the working tree.")
1734
1753
        except errors.BoundBranchOutOfDate, e:
1735
 
            raise BzrCommandError(str(e)
1736
 
                                  + ' Either unbind, update, or'
1737
 
                                    ' pass --local to commit.')
1738
 
 
 
1754
            raise BzrCommandError(str(e) + "\n"
 
1755
                'To commit to master branch, run update and then commit.\n'
 
1756
                'You can also pass --local to commit to continue working '
 
1757
                'disconnected.')
1739
1758
 
1740
1759
class cmd_check(Command):
1741
1760
    """Validate consistency of branch history.
1976
1995
 
1977
1996
def show_version():
1978
1997
    import bzrlib
1979
 
    print "bzr (bazaar-ng) %s" % bzrlib.__version__
 
1998
    print "Bazaar (bzr) %s" % bzrlib.__version__
1980
1999
    # is bzrlib itself in a branch?
1981
2000
    branch = _get_bzr_branch()
1982
2001
    if branch:
2008
2027
 
2009
2028
class cmd_version(Command):
2010
2029
    """Show version of bzr."""
 
2030
 
2011
2031
    @display_command
2012
2032
    def run(self):
2013
2033
        show_version()
2014
2034
 
 
2035
 
2015
2036
class cmd_rocks(Command):
2016
2037
    """Statement of optimism."""
 
2038
 
2017
2039
    hidden = True
 
2040
 
2018
2041
    @display_command
2019
2042
    def run(self):
2020
2043
        print "it sure does!"
2021
2044
 
2022
2045
 
2023
2046
class cmd_find_merge_base(Command):
2024
 
    """Find and print a base revision for merging two branches.
2025
 
    """
 
2047
    """Find and print a base revision for merging two branches."""
2026
2048
    # TODO: Options to specify revisions on either side, as if
2027
2049
    #       merging only part of the history.
2028
2050
    takes_args = ['branch', 'other']
2395
2417
        if other_branch is None:
2396
2418
            other_branch = parent
2397
2419
            if other_branch is None:
2398
 
                raise BzrCommandError("No missing location known or specified.")
 
2420
                raise BzrCommandError("No peer location known or specified.")
2399
2421
            print "Using last location: " + local_branch.get_parent()
2400
2422
        remote_branch = Branch.open(other_branch)
2401
2423
        if remote_branch.base == local_branch.base: