/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: Aaron Bentley
  • Date: 2006-06-21 13:37:30 UTC
  • mfrom: (1799 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1802.
  • Revision ID: abentley@panoramicfeedback.com-20060621133730-6f6f965f4d2e3743
Merge bzr.dev

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
25
import bzrlib
27
 
import bzrlib.branch
28
 
from bzrlib.branch import Branch
29
 
import bzrlib.bzrdir as bzrdir
 
26
from bzrlib.branch import Branch, BranchReferenceFormat
 
27
from bzrlib import (bundle, branch, bzrdir, errors, osutils, ui, config,
 
28
    repository, log)
30
29
from bzrlib.bundle import read_bundle_from_url
31
30
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
32
31
from bzrlib.commands import Command, display_command
33
 
import bzrlib.errors as errors
34
32
from bzrlib.errors import (BzrError, BzrCheckError, BzrCommandError, 
35
33
                           NotBranchError, DivergedBranches, NotConflicted,
36
34
                           NoSuchFile, NoWorkingTree, FileInWrongBranch,
37
35
                           NotVersionedError, NotABundle)
38
 
from bzrlib.log import show_one_log
39
36
from bzrlib.merge import Merge3Merger
40
37
from bzrlib.option import Option
41
 
import bzrlib.osutils
42
38
from bzrlib.progress import DummyProgress, ProgressPhase
43
39
from bzrlib.revision import common_ancestor
44
40
from bzrlib.revisionspec import RevisionSpec
45
 
import bzrlib.trace
46
 
from bzrlib.trace import mutter, note, log_error, warning, is_quiet
 
41
from bzrlib.trace import mutter, note, log_error, warning, is_quiet, info
47
42
from bzrlib.transport.local import LocalTransport
48
 
import bzrlib.ui
49
43
import bzrlib.urlutils as urlutils
50
44
from bzrlib.workingtree import WorkingTree
51
45
 
94
88
        return bzrdir.BzrDirMetaFormat1()
95
89
    if typestring == "metaweave":
96
90
        format = bzrdir.BzrDirMetaFormat1()
97
 
        format.repository_format = bzrlib.repository.RepositoryFormat7()
 
91
        format.repository_format = repository.RepositoryFormat7()
98
92
        return format
99
93
    if typestring == "knit":
100
94
        format = bzrdir.BzrDirMetaFormat1()
101
 
        format.repository_format = bzrlib.repository.RepositoryFormatKnit1()
 
95
        format.repository_format = repository.RepositoryFormatKnit1()
102
96
        return format
103
97
    msg = "Unknown bzr format %s. Current formats are: default, knit,\n" \
104
98
          "metaweave and weave" % typestring
415
409
 
416
410
    If there is no default location set, the first pull will set it.  After
417
411
    that, you can omit the location to use the default.  To change the
418
 
    default, use --remember.
 
412
    default, use --remember. The value will only be saved if the remote
 
413
    location can be accessed.
419
414
    """
420
415
 
421
416
    takes_options = ['remember', 'overwrite', 'revision', 'verbose']
434
429
        reader = None
435
430
        if location is not None:
436
431
            try:
437
 
                reader = read_bundle_from_url(location)
 
432
                reader = bundle.read_bundle_from_url(location)
438
433
            except NotABundle:
439
434
                pass # Continue on considering this url a Branch
440
435
 
505
500
 
506
501
    If there is no default push location set, the first push will set it.
507
502
    After that, you can omit the location to use the default.  To change the
508
 
    default, use --remember.
 
503
    default, use --remember. The value will only be saved if the remote
 
504
    location can be accessed.
509
505
    """
510
506
 
511
507
    takes_options = ['remember', 'overwrite', 'verbose',
529
525
            else:
530
526
                display_url = urlutils.unescape_for_display(stored_loc,
531
527
                        self.outf.encoding)
532
 
                self.outf.write("Using saved location: %s" % display_url)
 
528
                self.outf.write("Using saved location: %s\n" % display_url)
533
529
                location = stored_loc
534
530
 
535
531
        transport = get_transport(location)
536
532
        location_url = transport.base
537
 
        if br_from.get_push_location() is None or remember:
538
 
            br_from.set_push_location(location_url)
539
533
 
540
534
        old_rh = []
541
535
        try:
542
 
            dir_to = bzrlib.bzrdir.BzrDir.open(location_url)
 
536
            dir_to = bzrdir.BzrDir.open(location_url)
543
537
            br_to = dir_to.open_branch()
544
538
        except NotBranchError:
545
539
            # create a branch.
571
565
                revision_id=br_from.last_revision())
572
566
            br_to = dir_to.open_branch()
573
567
            count = len(br_to.revision_history())
 
568
            # We successfully created the target, remember it
 
569
            if br_from.get_push_location() is None or remember:
 
570
                br_from.set_push_location(br_to.base)
574
571
        else:
 
572
            # We were able to connect to the remote location, so remember it
 
573
            # we don't need to successfully push because of possible divergence.
 
574
            if br_from.get_push_location() is None or remember:
 
575
                br_from.set_push_location(br_to.base)
575
576
            old_rh = br_to.revision_history()
576
577
            try:
577
578
                try:
617
618
 
618
619
    def run(self, from_location, to_location=None, revision=None, basis=None):
619
620
        from bzrlib.transport import get_transport
620
 
        from bzrlib.osutils import rmtree
621
621
        if revision is None:
622
622
            revision = [None]
623
623
        elif len(revision) > 1:
653
653
            to_transport = get_transport(to_location)
654
654
            try:
655
655
                to_transport.mkdir('.')
656
 
            except bzrlib.errors.FileExists:
 
656
            except errors.FileExists:
657
657
                raise BzrCommandError('Target directory "%s" already'
658
658
                                      ' exists.' % to_location)
659
 
            except bzrlib.errors.NoSuchFile:
 
659
            except errors.NoSuchFile:
660
660
                raise BzrCommandError('Parent of "%s" does not exist.' %
661
661
                                      to_location)
662
662
            try:
664
664
                dir = br_from.bzrdir.sprout(to_transport.base,
665
665
                        revision_id, basis_dir)
666
666
                branch = dir.open_branch()
667
 
            except bzrlib.errors.NoSuchRevision:
 
667
            except errors.NoSuchRevision:
668
668
                to_transport.delete_tree('.')
669
669
                msg = "The branch %s has no revision %s." % (from_location, revision[0])
670
670
                raise BzrCommandError(msg)
671
 
            except bzrlib.errors.UnlistableBranch:
672
 
                rmtree(to_location)
 
671
            except errors.UnlistableBranch:
 
672
                osutils.rmtree(to_location)
673
673
                msg = "The branch %s cannot be used as a --basis" % (basis,)
674
674
                raise BzrCommandError(msg)
675
675
            if name:
718
718
            raise BzrCommandError(
719
719
                'bzr checkout --revision takes exactly 1 revision value')
720
720
        if branch_location is None:
721
 
            branch_location = bzrlib.osutils.getcwd()
 
721
            branch_location = osutils.getcwd()
722
722
            to_location = branch_location
723
723
        source = Branch.open(branch_location)
724
724
        if len(revision) == 1 and revision[0] is not None:
730
730
        # if the source and to_location are the same, 
731
731
        # and there is no working tree,
732
732
        # then reconstitute a branch
733
 
        if (bzrlib.osutils.abspath(to_location) == 
734
 
            bzrlib.osutils.abspath(branch_location)):
 
733
        if (osutils.abspath(to_location) == 
 
734
            osutils.abspath(branch_location)):
735
735
            try:
736
736
                source.bzrdir.open_workingtree()
737
737
            except errors.NoWorkingTree:
748
748
                                      to_location)
749
749
            else:
750
750
                raise
751
 
        old_format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
752
 
        bzrlib.bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
751
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
752
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
753
753
        try:
754
754
            if lightweight:
755
755
                checkout = bzrdir.BzrDirMetaFormat1().initialize(to_location)
756
 
                bzrlib.branch.BranchReferenceFormat().initialize(checkout, source)
 
756
                branch.BranchReferenceFormat().initialize(checkout, source)
757
757
            else:
758
 
                checkout_branch =  bzrlib.bzrdir.BzrDir.create_branch_convenience(
 
758
                checkout_branch =  bzrdir.BzrDir.create_branch_convenience(
759
759
                    to_location, force_new_tree=False)
760
760
                checkout = checkout_branch.bzrdir
761
761
                checkout_branch.bind(source)
764
764
                    checkout_branch.set_revision_history(rh[:rh.index(revision_id) + 1])
765
765
            checkout.create_workingtree(revision_id)
766
766
        finally:
767
 
            bzrlib.bzrdir.BzrDirFormat.set_default_format(old_format)
 
767
            bzrdir.BzrDirFormat.set_default_format(old_format)
768
768
 
769
769
 
770
770
class cmd_renames(Command):
777
777
 
778
778
    @display_command
779
779
    def run(self, dir=u'.'):
 
780
        from bzrlib.tree import find_renames
780
781
        tree = WorkingTree.open_containing(dir)[0]
781
782
        old_inv = tree.basis_tree().inventory
782
783
        new_inv = tree.read_working_inventory()
783
 
 
784
 
        renames = list(bzrlib.tree.find_renames(old_inv, new_inv))
 
784
        renames = list(find_renames(old_inv, new_inv))
785
785
        renames.sort()
786
786
        for old_name, new_name in renames:
787
787
            self.outf.write("%s => %s\n" % (old_name, new_name))
935
935
 
936
936
    def run(self, branch="."):
937
937
        from bzrlib.reconcile import reconcile
938
 
        dir = bzrlib.bzrdir.BzrDir.open(branch)
 
938
        dir = bzrdir.BzrDir.open(branch)
939
939
        reconcile(dir)
940
940
 
941
941
 
1008
1008
                            type=get_format_type),
1009
1009
                     ]
1010
1010
    def run(self, location=None, format=None):
1011
 
        from bzrlib.branch import Branch
1012
1011
        if format is None:
1013
1012
            format = get_format_type('default')
1014
1013
        if location is None:
1213
1212
            if file_id in basis_inv:
1214
1213
                continue
1215
1214
            path = inv.id2path(file_id)
1216
 
            if not os.access(bzrlib.osutils.abspath(path), os.F_OK):
 
1215
            if not os.access(osutils.abspath(path), os.F_OK):
1217
1216
                continue
1218
1217
            self.outf.write(path + '\n')
1219
1218
 
1328
1327
            (rev2, rev1) = (rev1, rev2)
1329
1328
 
1330
1329
        if (log_format == None):
1331
 
            default = bzrlib.config.BranchConfig(b).log_format()
 
1330
            default = config.BranchConfig(b).log_format()
1332
1331
            log_format = get_log_format(long=long, short=short, line=line, default=default)
1333
1332
        lf = log_formatter(log_format,
1334
1333
                           show_ids=show_ids,
1371
1370
        b = tree.branch
1372
1371
        inv = tree.read_working_inventory()
1373
1372
        file_id = inv.path2id(relpath)
1374
 
        for revno, revision_id, what in bzrlib.log.find_touching_revisions(b, file_id):
 
1373
        for revno, revision_id, what in log.find_touching_revisions(b, file_id):
1375
1374
            self.outf.write("%6d %s\n" % (revno, what))
1376
1375
 
1377
1376
 
1433
1432
    """List unknown files."""
1434
1433
    @display_command
1435
1434
    def run(self):
1436
 
        from bzrlib.osutils import quotefn
1437
1435
        for f in WorkingTree.open_containing(u'.')[0].unknowns():
1438
 
            self.outf.write(quotefn(f) + '\n')
 
1436
            self.outf.write(osutils.quotefn(f) + '\n')
1439
1437
 
1440
1438
 
1441
1439
class cmd_ignore(Command):
1605
1603
    hidden = True    
1606
1604
    @display_command
1607
1605
    def run(self):
1608
 
        print bzrlib.osutils.local_time_offset()
 
1606
        print osutils.local_time_offset()
1609
1607
 
1610
1608
 
1611
1609
 
1689
1687
            message = codecs.open(file, 'rt', bzrlib.user_encoding).read()
1690
1688
 
1691
1689
        if message == "":
1692
 
                raise BzrCommandError("empty commit message specified")
 
1690
            raise BzrCommandError("empty commit message specified")
1693
1691
        
1694
1692
        if verbose:
1695
1693
            reporter = ReportCommitToLog()
1788
1786
    def run(self, email=False):
1789
1787
        try:
1790
1788
            b = WorkingTree.open_containing(u'.')[0].branch
1791
 
            config = bzrlib.config.BranchConfig(b)
 
1789
            c = config.BranchConfig(b)
1792
1790
        except NotBranchError:
1793
 
            config = bzrlib.config.GlobalConfig()
1794
 
        
 
1791
            c = config.GlobalConfig()
1795
1792
        if email:
1796
 
            print config.user_email()
 
1793
            print c.user_email()
1797
1794
        else:
1798
 
            print config.username()
 
1795
            print c.username()
1799
1796
 
1800
1797
 
1801
1798
class cmd_nick(Command):
1881
1878
        # we don't want progress meters from the tests to go to the
1882
1879
        # real output; and we don't want log messages cluttering up
1883
1880
        # the real logs.
1884
 
        save_ui = bzrlib.ui.ui_factory
1885
 
        print '%10s: %s' % ('bzr', bzrlib.osutils.realpath(sys.argv[0]))
 
1881
        save_ui = ui.ui_factory
 
1882
        print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
1886
1883
        print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
1887
1884
        print
1888
 
        bzrlib.trace.info('running tests...')
 
1885
        info('running tests...')
1889
1886
        try:
1890
 
            bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
1887
            ui.ui_factory = ui.SilentUIFactory()
1891
1888
            if testspecs_list is not None:
1892
1889
                pattern = '|'.join(testspecs_list)
1893
1890
            else:
1908
1905
                              test_suite_factory=test_suite_factory,
1909
1906
                              lsprof_timed=lsprof_timed)
1910
1907
            if result:
1911
 
                bzrlib.trace.info('tests passed')
 
1908
                info('tests passed')
1912
1909
            else:
1913
 
                bzrlib.trace.info('tests failed')
 
1910
                info('tests failed')
1914
1911
            return int(not result)
1915
1912
        finally:
1916
 
            bzrlib.ui.ui_factory = save_ui
 
1913
            ui.ui_factory = save_ui
1917
1914
 
1918
1915
 
1919
1916
def _get_bzr_branch():
1920
1917
    """If bzr is run from a branch, return Branch or None"""
1921
 
    import bzrlib.errors
1922
 
    from bzrlib.branch import Branch
1923
 
    from bzrlib.osutils import abspath
1924
1918
    from os.path import dirname
1925
1919
    
1926
1920
    try:
1927
 
        branch = Branch.open(dirname(abspath(dirname(__file__))))
 
1921
        branch = Branch.open(dirname(osutils.abspath(dirname(__file__))))
1928
1922
        return branch
1929
 
    except bzrlib.errors.BzrError:
 
1923
    except errors.BzrError:
1930
1924
        return None
1931
1925
    
1932
1926
 
1933
1927
def show_version():
 
1928
    import bzrlib
1934
1929
    print "bzr (bazaar-ng) %s" % bzrlib.__version__
1935
1930
    # is bzrlib itself in a branch?
1936
1931
    branch = _get_bzr_branch()
2002
1997
        base_rev_id = common_ancestor(last1, last2, source)
2003
1998
 
2004
1999
        print 'merge base is revision %s' % base_rev_id
2005
 
        
2006
 
        return
2007
 
 
2008
 
        if base_revno is None:
2009
 
            raise bzrlib.errors.UnrelatedBranches()
2010
 
 
2011
 
        print ' r%-6d in %s' % (base_revno, branch)
2012
 
 
2013
 
        other_revno = branch2.revision_id_to_revno(base_revid)
2014
 
        
2015
 
        print ' r%-6d in %s' % (other_revno, other)
2016
 
 
2017
2000
 
2018
2001
 
2019
2002
class cmd_merge(Command):
2020
2003
    """Perform a three-way merge.
2021
2004
    
2022
 
    The branch is the branch you will merge from.  By default, it will
2023
 
    merge the latest revision.  If you specify a revision, that
2024
 
    revision will be merged.  If you specify two revisions, the first
2025
 
    will be used as a BASE, and the second one as OTHER.  Revision
2026
 
    numbers are always relative to the specified branch.
 
2005
    The branch is the branch you will merge from.  By default, it will merge
 
2006
    the latest revision.  If you specify a revision, that revision will be
 
2007
    merged.  If you specify two revisions, the first will be used as a BASE,
 
2008
    and the second one as OTHER.  Revision numbers are always relative to the
 
2009
    specified branch.
2027
2010
 
2028
2011
    By default, bzr will try to merge in all new work from the other
2029
2012
    branch, automatically determining an appropriate base.  If this
2038
2021
 
2039
2022
    If there is no default branch set, the first merge will set it. After
2040
2023
    that, you can omit the branch to use the default.  To change the
2041
 
    default, use --remember.
 
2024
    default, use --remember. The value will only be saved if the remote
 
2025
    location can be accessed.
2042
2026
 
2043
2027
    Examples:
2044
2028
 
2075
2059
 
2076
2060
        if branch is not None:
2077
2061
            try:
2078
 
                reader = read_bundle_from_url(branch)
 
2062
                reader = bundle.read_bundle_from_url(branch)
2079
2063
            except NotABundle:
2080
2064
                pass # Continue on considering this url a Branch
2081
2065
            else:
2115
2099
            interesting_files = [path]
2116
2100
        else:
2117
2101
            interesting_files = None
2118
 
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
2102
        pb = ui.ui_factory.nested_progress_bar()
2119
2103
        try:
2120
2104
            try:
2121
2105
                conflict_count = merge(other, base, check_clean=(not force),
2129
2113
                return 1
2130
2114
            else:
2131
2115
                return 0
2132
 
        except bzrlib.errors.AmbiguousBase, e:
 
2116
        except errors.AmbiguousBase, e:
2133
2117
            m = ("sorry, bzr can't determine the right merge base yet\n"
2134
2118
                 "candidates are:\n  "
2135
2119
                 + "\n  ".join(e.bases)
2268
2252
            raise BzrCommandError('bzr revert --revision takes exactly 1 argument')
2269
2253
        else:
2270
2254
            rev_id = revision[0].in_history(tree.branch).rev_id
2271
 
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
2255
        pb = ui.ui_factory.nested_progress_bar()
2272
2256
        try:
2273
2257
            tree.revert(file_list, 
2274
2258
                        tree.branch.repository.revision_tree(rev_id),
2322
2306
    takes_args = ['from_branch', 'to_branch']
2323
2307
    def run(self, from_branch, to_branch):
2324
2308
        from bzrlib.fetch import Fetcher
2325
 
        from bzrlib.branch import Branch
2326
2309
        from_b = Branch.open(from_branch)
2327
2310
        to_b = Branch.open(to_branch)
2328
2311
        Fetcher(to_b, from_b)
2351
2334
            show_ids=False, verbose=False):
2352
2335
        from bzrlib.missing import find_unmerged, iter_log_data
2353
2336
        from bzrlib.log import log_formatter
2354
 
        local_branch = bzrlib.branch.Branch.open_containing(u".")[0]
 
2337
        local_branch = Branch.open_containing(u".")[0]
2355
2338
        parent = local_branch.get_parent()
2356
2339
        if other_branch is None:
2357
2340
            other_branch = parent
2358
2341
            if other_branch is None:
2359
2342
                raise BzrCommandError("No missing location known or specified.")
2360
2343
            print "Using last location: " + local_branch.get_parent()
2361
 
        remote_branch = bzrlib.branch.Branch.open(other_branch)
 
2344
        remote_branch = Branch.open(other_branch)
2362
2345
        if remote_branch.base == local_branch.base:
2363
2346
            remote_branch = local_branch
2364
2347
        local_branch.lock_read()
2367
2350
            try:
2368
2351
                local_extra, remote_extra = find_unmerged(local_branch, remote_branch)
2369
2352
                if (log_format == None):
2370
 
                    default = bzrlib.config.BranchConfig(local_branch).log_format()
 
2353
                    default = config.BranchConfig(local_branch).log_format()
2371
2354
                    log_format = get_log_format(long=long, short=short, line=line, default=default)
2372
2355
                lf = log_formatter(log_format, sys.stdout,
2373
2356
                                   show_ids=show_ids,
2432
2415
 
2433
2416
class cmd_testament(Command):
2434
2417
    """Show testament (signing-form) of a revision."""
2435
 
    takes_options = ['revision', 'long']
 
2418
    takes_options = ['revision', 'long', 
 
2419
                     Option('strict', help='Produce a strict testament')]
2436
2420
    takes_args = ['branch?']
2437
2421
    @display_command
2438
 
    def run(self, branch=u'.', revision=None, long=False):
2439
 
        from bzrlib.testament import Testament
 
2422
    def run(self, branch=u'.', revision=None, long=False, strict=False):
 
2423
        from bzrlib.testament import Testament, StrictTestament
 
2424
        if strict is True:
 
2425
            testament_class = StrictTestament
 
2426
        else:
 
2427
            testament_class = Testament
2440
2428
        b = WorkingTree.open_containing(branch)[0].branch
2441
2429
        b.lock_read()
2442
2430
        try:
2444
2432
                rev_id = b.last_revision()
2445
2433
            else:
2446
2434
                rev_id = revision[0].in_history(b).rev_id
2447
 
            t = Testament.from_revision(b.repository, rev_id)
 
2435
            t = testament_class.from_revision(b.repository, rev_id)
2448
2436
            if long:
2449
2437
                sys.stdout.writelines(t.as_text_lines())
2450
2438
            else:
2502
2490
    takes_options = ['revision']
2503
2491
    
2504
2492
    def run(self, revision_id_list=None, revision=None):
2505
 
        import bzrlib.config as config
2506
2493
        import bzrlib.gpg as gpg
2507
2494
        if revision_id_list is not None and revision is not None:
2508
2495
            raise BzrCommandError('You can only supply one of revision_id or --revision')
2570
2557
            raise BzrCommandError('Local branch is not bound')
2571
2558
 
2572
2559
 
2573
 
class cmd_uncommit(bzrlib.commands.Command):
 
2560
class cmd_uncommit(Command):
2574
2561
    """Remove the last committed revision.
2575
2562
 
2576
2563
    --verbose will print out what is being removed.
2594
2581
    def run(self, location=None, 
2595
2582
            dry_run=False, verbose=False,
2596
2583
            revision=None, force=False):
2597
 
        from bzrlib.branch import Branch
2598
2584
        from bzrlib.log import log_formatter
2599
2585
        import sys
2600
2586
        from bzrlib.uncommit import uncommit