~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Martin Pool
  • Date: 2006-06-20 03:57:11 UTC
  • mto: This revision was merged to the branch mainline in revision 1798.
  • Revision ID: mbp@sourcefrog.net-20060620035711-400bb6b6bc6ff95b
Add pyflakes makefile target; fix many warnings

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import codecs
21
21
import errno
22
22
import os
23
 
from shutil import rmtree
24
23
import sys
25
24
 
26
 
import bzrlib
27
 
import bzrlib.branch
28
 
from bzrlib.branch import Branch
29
 
import bzrlib.bzrdir as bzrdir
 
25
from bzrlib.branch import Branch, BranchReferenceFormat
 
26
from bzrlib import (branch, bzrdir, errors, osutils, ui, config, user_encoding,
 
27
    repository, log)
30
28
from bzrlib.bundle.read_bundle import BundleReader
31
29
from bzrlib.bundle.apply_bundle import merge_bundle
32
30
from bzrlib.commands import Command, display_command
33
 
import bzrlib.errors as errors
34
31
from bzrlib.errors import (BzrError, BzrCheckError, BzrCommandError, 
35
32
                           NotBranchError, DivergedBranches, NotConflicted,
36
33
                           NoSuchFile, NoWorkingTree, FileInWrongBranch,
37
34
                           NotVersionedError, NotABundle)
38
 
from bzrlib.log import show_one_log
39
35
from bzrlib.merge import Merge3Merger
40
36
from bzrlib.option import Option
41
 
import bzrlib.osutils
42
37
from bzrlib.progress import DummyProgress, ProgressPhase
43
38
from bzrlib.revision import common_ancestor
44
39
from bzrlib.revisionspec import RevisionSpec
45
 
import bzrlib.trace
46
 
from bzrlib.trace import mutter, note, log_error, warning, is_quiet
 
40
from bzrlib.trace import mutter, note, log_error, warning, is_quiet, info
47
41
from bzrlib.transport.local import LocalTransport
48
 
import bzrlib.ui
49
42
import bzrlib.urlutils as urlutils
50
43
from bzrlib.workingtree import WorkingTree
51
44
 
94
87
        return bzrdir.BzrDirMetaFormat1()
95
88
    if typestring == "metaweave":
96
89
        format = bzrdir.BzrDirMetaFormat1()
97
 
        format.repository_format = bzrlib.repository.RepositoryFormat7()
 
90
        format.repository_format = repository.RepositoryFormat7()
98
91
        return format
99
92
    if typestring == "knit":
100
93
        format = bzrdir.BzrDirMetaFormat1()
101
 
        format.repository_format = bzrlib.repository.RepositoryFormatKnit1()
 
94
        format.repository_format = repository.RepositoryFormatKnit1()
102
95
        return format
103
96
    msg = "Unknown bzr format %s. Current formats are: default, knit,\n" \
104
97
          "metaweave and weave" % typestring
528
521
 
529
522
        old_rh = []
530
523
        try:
531
 
            dir_to = bzrlib.bzrdir.BzrDir.open(location_url)
 
524
            dir_to = bzrdir.BzrDir.open(location_url)
532
525
            br_to = dir_to.open_branch()
533
526
        except NotBranchError:
534
527
            # create a branch.
606
599
 
607
600
    def run(self, from_location, to_location=None, revision=None, basis=None):
608
601
        from bzrlib.transport import get_transport
609
 
        from bzrlib.osutils import rmtree
610
602
        if revision is None:
611
603
            revision = [None]
612
604
        elif len(revision) > 1:
642
634
            to_transport = get_transport(to_location)
643
635
            try:
644
636
                to_transport.mkdir('.')
645
 
            except bzrlib.errors.FileExists:
 
637
            except errors.FileExists:
646
638
                raise BzrCommandError('Target directory "%s" already'
647
639
                                      ' exists.' % to_location)
648
 
            except bzrlib.errors.NoSuchFile:
 
640
            except errors.NoSuchFile:
649
641
                raise BzrCommandError('Parent of "%s" does not exist.' %
650
642
                                      to_location)
651
643
            try:
653
645
                dir = br_from.bzrdir.sprout(to_transport.base,
654
646
                        revision_id, basis_dir)
655
647
                branch = dir.open_branch()
656
 
            except bzrlib.errors.NoSuchRevision:
 
648
            except errors.NoSuchRevision:
657
649
                to_transport.delete_tree('.')
658
650
                msg = "The branch %s has no revision %s." % (from_location, revision[0])
659
651
                raise BzrCommandError(msg)
660
 
            except bzrlib.errors.UnlistableBranch:
661
 
                rmtree(to_location)
 
652
            except errors.UnlistableBranch:
 
653
                osutils.rmtree(to_location)
662
654
                msg = "The branch %s cannot be used as a --basis" % (basis,)
663
655
                raise BzrCommandError(msg)
664
656
            if name:
707
699
            raise BzrCommandError(
708
700
                'bzr checkout --revision takes exactly 1 revision value')
709
701
        if branch_location is None:
710
 
            branch_location = bzrlib.osutils.getcwd()
 
702
            branch_location = osutils.getcwd()
711
703
            to_location = branch_location
712
704
        source = Branch.open(branch_location)
713
705
        if len(revision) == 1 and revision[0] is not None:
719
711
        # if the source and to_location are the same, 
720
712
        # and there is no working tree,
721
713
        # then reconstitute a branch
722
 
        if (bzrlib.osutils.abspath(to_location) == 
723
 
            bzrlib.osutils.abspath(branch_location)):
 
714
        if (osutils.abspath(to_location) == 
 
715
            osutils.abspath(branch_location)):
724
716
            try:
725
717
                source.bzrdir.open_workingtree()
726
718
            except errors.NoWorkingTree:
737
729
                                      to_location)
738
730
            else:
739
731
                raise
740
 
        old_format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
741
 
        bzrlib.bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
732
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
733
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
742
734
        try:
743
735
            if lightweight:
744
736
                checkout = bzrdir.BzrDirMetaFormat1().initialize(to_location)
745
 
                bzrlib.branch.BranchReferenceFormat().initialize(checkout, source)
 
737
                branch.BranchReferenceFormat().initialize(checkout, source)
746
738
            else:
747
 
                checkout_branch =  bzrlib.bzrdir.BzrDir.create_branch_convenience(
 
739
                checkout_branch =  bzrdir.BzrDir.create_branch_convenience(
748
740
                    to_location, force_new_tree=False)
749
741
                checkout = checkout_branch.bzrdir
750
742
                checkout_branch.bind(source)
753
745
                    checkout_branch.set_revision_history(rh[:rh.index(revision_id) + 1])
754
746
            checkout.create_workingtree(revision_id)
755
747
        finally:
756
 
            bzrlib.bzrdir.BzrDirFormat.set_default_format(old_format)
 
748
            bzrdir.BzrDirFormat.set_default_format(old_format)
757
749
 
758
750
 
759
751
class cmd_renames(Command):
766
758
 
767
759
    @display_command
768
760
    def run(self, dir=u'.'):
 
761
        from bzrlib.tree import find_renames
769
762
        tree = WorkingTree.open_containing(dir)[0]
770
763
        old_inv = tree.basis_tree().inventory
771
764
        new_inv = tree.read_working_inventory()
772
 
 
773
 
        renames = list(bzrlib.tree.find_renames(old_inv, new_inv))
 
765
        renames = list(find_renames(old_inv, new_inv))
774
766
        renames.sort()
775
767
        for old_name, new_name in renames:
776
768
            self.outf.write("%s => %s\n" % (old_name, new_name))
924
916
 
925
917
    def run(self, branch="."):
926
918
        from bzrlib.reconcile import reconcile
927
 
        dir = bzrlib.bzrdir.BzrDir.open(branch)
 
919
        dir = bzrdir.BzrDir.open(branch)
928
920
        reconcile(dir)
929
921
 
930
922
 
987
979
                            type=get_format_type),
988
980
                     ]
989
981
    def run(self, location=None, format=None):
990
 
        from bzrlib.branch import Branch
991
982
        if format is None:
992
983
            format = get_format_type('default')
993
984
        if location is None:
1192
1183
            if file_id in basis_inv:
1193
1184
                continue
1194
1185
            path = inv.id2path(file_id)
1195
 
            if not os.access(bzrlib.osutils.abspath(path), os.F_OK):
 
1186
            if not os.access(osutils.abspath(path), os.F_OK):
1196
1187
                continue
1197
1188
            self.outf.write(path + '\n')
1198
1189
 
1307
1298
            (rev2, rev1) = (rev1, rev2)
1308
1299
 
1309
1300
        if (log_format == None):
1310
 
            default = bzrlib.config.BranchConfig(b).log_format()
 
1301
            default = config.BranchConfig(b).log_format()
1311
1302
            log_format = get_log_format(long=long, short=short, line=line, default=default)
1312
1303
        lf = log_formatter(log_format,
1313
1304
                           show_ids=show_ids,
1350
1341
        b = tree.branch
1351
1342
        inv = tree.read_working_inventory()
1352
1343
        file_id = inv.path2id(relpath)
1353
 
        for revno, revision_id, what in bzrlib.log.find_touching_revisions(b, file_id):
 
1344
        for revno, revision_id, what in log.find_touching_revisions(b, file_id):
1354
1345
            self.outf.write("%6d %s\n" % (revno, what))
1355
1346
 
1356
1347
 
1412
1403
    """List unknown files."""
1413
1404
    @display_command
1414
1405
    def run(self):
1415
 
        from bzrlib.osutils import quotefn
 
1406
        from osutils import quotefn
1416
1407
        for f in WorkingTree.open_containing(u'.')[0].unknowns():
1417
1408
            self.outf.write(quotefn(f) + '\n')
1418
1409
 
1584
1575
    hidden = True    
1585
1576
    @display_command
1586
1577
    def run(self):
1587
 
        print bzrlib.osutils.local_time_offset()
 
1578
        print osutils.local_time_offset()
1588
1579
 
1589
1580
 
1590
1581
 
1665
1656
            raise BzrCommandError("please specify either --message or --file")
1666
1657
        
1667
1658
        if file:
1668
 
            message = codecs.open(file, 'rt', bzrlib.user_encoding).read()
 
1659
            message = codecs.open(file, 'rt', user_encoding).read()
1669
1660
 
1670
1661
        if message == "":
1671
 
                raise BzrCommandError("empty commit message specified")
 
1662
            raise BzrCommandError("empty commit message specified")
1672
1663
        
1673
1664
        if verbose:
1674
1665
            reporter = ReportCommitToLog()
1767
1758
    def run(self, email=False):
1768
1759
        try:
1769
1760
            b = WorkingTree.open_containing(u'.')[0].branch
1770
 
            config = bzrlib.config.BranchConfig(b)
 
1761
            c = config.BranchConfig(b)
1771
1762
        except NotBranchError:
1772
 
            config = bzrlib.config.GlobalConfig()
1773
 
        
 
1763
            c = config.GlobalConfig()
1774
1764
        if email:
1775
 
            print config.user_email()
 
1765
            print c.user_email()
1776
1766
        else:
1777
 
            print config.username()
 
1767
            print c.username()
1778
1768
 
1779
1769
 
1780
1770
class cmd_nick(Command):
1860
1850
        # we don't want progress meters from the tests to go to the
1861
1851
        # real output; and we don't want log messages cluttering up
1862
1852
        # the real logs.
1863
 
        save_ui = bzrlib.ui.ui_factory
1864
 
        print '%10s: %s' % ('bzr', bzrlib.osutils.realpath(sys.argv[0]))
 
1853
        save_ui = ui.ui_factory
 
1854
        print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
1865
1855
        print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
1866
1856
        print
1867
 
        bzrlib.trace.info('running tests...')
 
1857
        info('running tests...')
1868
1858
        try:
1869
 
            bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
1859
            ui.ui_factory = ui.SilentUIFactory()
1870
1860
            if testspecs_list is not None:
1871
1861
                pattern = '|'.join(testspecs_list)
1872
1862
            else:
1887
1877
                              test_suite_factory=test_suite_factory,
1888
1878
                              lsprof_timed=lsprof_timed)
1889
1879
            if result:
1890
 
                bzrlib.trace.info('tests passed')
 
1880
                info('tests passed')
1891
1881
            else:
1892
 
                bzrlib.trace.info('tests failed')
 
1882
                info('tests failed')
1893
1883
            return int(not result)
1894
1884
        finally:
1895
 
            bzrlib.ui.ui_factory = save_ui
 
1885
            ui.ui_factory = save_ui
1896
1886
 
1897
1887
 
1898
1888
def _get_bzr_branch():
1899
1889
    """If bzr is run from a branch, return Branch or None"""
1900
 
    import bzrlib.errors
1901
 
    from bzrlib.branch import Branch
1902
 
    from bzrlib.osutils import abspath
1903
1890
    from os.path import dirname
1904
1891
    
1905
1892
    try:
1906
 
        branch = Branch.open(dirname(abspath(dirname(__file__))))
 
1893
        branch = Branch.open(dirname(osutils.abspath(dirname(__file__))))
1907
1894
        return branch
1908
 
    except bzrlib.errors.BzrError:
 
1895
    except errors.BzrError:
1909
1896
        return None
1910
1897
    
1911
1898
 
1912
1899
def show_version():
 
1900
    import bzrlib
1913
1901
    print "bzr (bazaar-ng) %s" % bzrlib.__version__
1914
1902
    # is bzrlib itself in a branch?
1915
1903
    branch = _get_bzr_branch()
1981
1969
        base_rev_id = common_ancestor(last1, last2, source)
1982
1970
 
1983
1971
        print 'merge base is revision %s' % base_rev_id
1984
 
        
1985
 
        return
1986
 
 
1987
 
        if base_revno is None:
1988
 
            raise bzrlib.errors.UnrelatedBranches()
1989
 
 
1990
 
        print ' r%-6d in %s' % (base_revno, branch)
1991
 
 
1992
 
        other_revno = branch2.revision_id_to_revno(base_revid)
1993
 
        
1994
 
        print ' r%-6d in %s' % (other_revno, other)
1995
 
 
1996
1972
 
1997
1973
 
1998
1974
class cmd_merge(Command):
1999
1975
    """Perform a three-way merge.
2000
1976
    
2001
 
    The branch is the branch you will merge from.  By default, it will
2002
 
    merge the latest revision.  If you specify a revision, that
2003
 
    revision will be merged.  If you specify two revisions, the first
2004
 
    will be used as a BASE, and the second one as OTHER.  Revision
2005
 
    numbers are always relative to the specified branch.
 
1977
    The branch is the branch you will merge from.  By default, it will merge
 
1978
    the latest revision.  If you specify a revision, that revision will be
 
1979
    merged.  If you specify two revisions, the first will be used as a BASE,
 
1980
    and the second one as OTHER.  Revision numbers are always relative to the
 
1981
    specified branch.
2006
1982
 
2007
1983
    By default, bzr will try to merge in all new work from the other
2008
1984
    branch, automatically determining an appropriate base.  If this
2100
2076
            interesting_files = [path]
2101
2077
        else:
2102
2078
            interesting_files = None
2103
 
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
2079
        pb = ui.ui_factory.nested_progress_bar()
2104
2080
        try:
2105
2081
            try:
2106
2082
                conflict_count = merge(other, base, check_clean=(not force),
2114
2090
                return 1
2115
2091
            else:
2116
2092
                return 0
2117
 
        except bzrlib.errors.AmbiguousBase, e:
 
2093
        except errors.AmbiguousBase, e:
2118
2094
            m = ("sorry, bzr can't determine the right merge base yet\n"
2119
2095
                 "candidates are:\n  "
2120
2096
                 + "\n  ".join(e.bases)
2253
2229
            raise BzrCommandError('bzr revert --revision takes exactly 1 argument')
2254
2230
        else:
2255
2231
            rev_id = revision[0].in_history(tree.branch).rev_id
2256
 
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
2232
        pb = ui.ui_factory.nested_progress_bar()
2257
2233
        try:
2258
2234
            tree.revert(file_list, 
2259
2235
                        tree.branch.repository.revision_tree(rev_id),
2307
2283
    takes_args = ['from_branch', 'to_branch']
2308
2284
    def run(self, from_branch, to_branch):
2309
2285
        from bzrlib.fetch import Fetcher
2310
 
        from bzrlib.branch import Branch
2311
2286
        from_b = Branch.open(from_branch)
2312
2287
        to_b = Branch.open(to_branch)
2313
2288
        Fetcher(to_b, from_b)
2336
2311
            show_ids=False, verbose=False):
2337
2312
        from bzrlib.missing import find_unmerged, iter_log_data
2338
2313
        from bzrlib.log import log_formatter
2339
 
        local_branch = bzrlib.branch.Branch.open_containing(u".")[0]
 
2314
        local_branch = Branch.open_containing(u".")[0]
2340
2315
        parent = local_branch.get_parent()
2341
2316
        if other_branch is None:
2342
2317
            other_branch = parent
2343
2318
            if other_branch is None:
2344
2319
                raise BzrCommandError("No missing location known or specified.")
2345
2320
            print "Using last location: " + local_branch.get_parent()
2346
 
        remote_branch = bzrlib.branch.Branch.open(other_branch)
 
2321
        remote_branch = Branch.open(other_branch)
2347
2322
        if remote_branch.base == local_branch.base:
2348
2323
            remote_branch = local_branch
2349
2324
        local_branch.lock_read()
2352
2327
            try:
2353
2328
                local_extra, remote_extra = find_unmerged(local_branch, remote_branch)
2354
2329
                if (log_format == None):
2355
 
                    default = bzrlib.config.BranchConfig(local_branch).log_format()
 
2330
                    default = config.BranchConfig(local_branch).log_format()
2356
2331
                    log_format = get_log_format(long=long, short=short, line=line, default=default)
2357
2332
                lf = log_formatter(log_format, sys.stdout,
2358
2333
                                   show_ids=show_ids,
2487
2462
    takes_options = ['revision']
2488
2463
    
2489
2464
    def run(self, revision_id_list=None, revision=None):
2490
 
        import bzrlib.config as config
2491
2465
        import bzrlib.gpg as gpg
2492
2466
        if revision_id_list is not None and revision is not None:
2493
2467
            raise BzrCommandError('You can only supply one of revision_id or --revision')
2555
2529
            raise BzrCommandError('Local branch is not bound')
2556
2530
 
2557
2531
 
2558
 
class cmd_uncommit(bzrlib.commands.Command):
 
2532
class cmd_uncommit(Command):
2559
2533
    """Remove the last committed revision.
2560
2534
 
2561
2535
    --verbose will print out what is being removed.
2579
2553
    def run(self, location=None, 
2580
2554
            dry_run=False, verbose=False,
2581
2555
            revision=None, force=False):
2582
 
        from bzrlib.branch import Branch
2583
2556
        from bzrlib.log import log_formatter
2584
2557
        import sys
2585
2558
        from bzrlib.uncommit import uncommit