~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Robert Collins
  • Date: 2006-08-01 04:54:43 UTC
  • mfrom: (1864.6.1 plugin-imports)
  • mto: This revision was merged to the branch mainline in revision 1901.
  • Revision ID: robertc@robertcollins.net-20060801045443-b99468f4250e01c4
Merge Johns bug 51810 fixup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
107
107
        format = bzrdir.BzrDirMetaFormat1()
108
108
        format.repository_format = repository.RepositoryFormatKnit1()
109
109
        return format
110
 
    if typestring == "experimental-knit2":
111
 
        format = bzrdir.BzrDirMetaFormat1()
112
 
        format.repository_format = repository.RepositoryFormatKnit2()
113
 
        return format
114
110
    msg = "Unknown bzr format %s. Current formats are: default, knit,\n" \
115
111
          "metaweave and weave" % typestring
116
112
    raise BzrCommandError(msg)
239
235
            revs.extend(revision)
240
236
        if revision_info_list is not None:
241
237
            for rev in revision_info_list:
242
 
                revs.append(RevisionSpec.from_string(rev))
 
238
                revs.append(RevisionSpec(rev))
243
239
        if len(revs) == 0:
244
240
            raise BzrCommandError('You must supply a revision identifier')
245
241
 
278
274
 
279
275
    --dry-run will show which files would be added, but not actually 
280
276
    add them.
281
 
 
282
 
    --file-ids-from will try to use the file ids from the supplied path.
283
 
    It looks up ids trying to find a matching parent directory with the
284
 
    same filename, and then by pure path.
285
277
    """
286
278
    takes_args = ['file*']
287
 
    takes_options = ['no-recurse', 'dry-run', 'verbose',
288
 
                     Option('file-ids-from', type=unicode,
289
 
                            help='Lookup file ids from here')]
 
279
    takes_options = ['no-recurse', 'dry-run', 'verbose']
290
280
    encoding_type = 'replace'
291
281
 
292
 
    def run(self, file_list, no_recurse=False, dry_run=False, verbose=False,
293
 
            file_ids_from=None):
 
282
    def run(self, file_list, no_recurse=False, dry_run=False, verbose=False):
294
283
        import bzrlib.add
295
284
 
296
 
        if file_ids_from is not None:
297
 
            try:
298
 
                base_tree, base_path = WorkingTree.open_containing(
299
 
                                            file_ids_from)
300
 
            except errors.NoWorkingTree:
301
 
                base_branch, base_path = branch.Branch.open_containing(
302
 
                                            file_ids_from)
303
 
                base_tree = base_branch.basis_tree()
304
 
 
305
 
            action = bzrlib.add.AddFromBaseAction(base_tree, base_path,
306
 
                          to_file=self.outf, should_print=(not is_quiet()))
307
 
        else:
308
 
            action = bzrlib.add.AddAction(to_file=self.outf,
309
 
                should_print=(not is_quiet()))
310
 
 
311
 
        added, ignored = bzrlib.add.smart_add(file_list, not no_recurse,
 
285
        action = bzrlib.add.AddAction(to_file=self.outf,
 
286
            should_print=(not is_quiet()))
 
287
 
 
288
        added, ignored = bzrlib.add.smart_add(file_list, not no_recurse, 
312
289
                                              action=action, save=not dry_run)
313
290
        if len(ignored) > 0:
314
291
            if verbose:
764
741
        # if the source and to_location are the same, 
765
742
        # and there is no working tree,
766
743
        # then reconstitute a branch
767
 
        if (osutils.abspath(to_location) ==
 
744
        if (osutils.abspath(to_location) == 
768
745
            osutils.abspath(branch_location)):
769
746
            try:
770
747
                source.bzrdir.open_workingtree()
785
762
        old_format = bzrdir.BzrDirFormat.get_default_format()
786
763
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
787
764
        try:
788
 
            source.create_checkout(to_location, revision_id, lightweight)
 
765
            if lightweight:
 
766
                checkout = bzrdir.BzrDirMetaFormat1().initialize(to_location)
 
767
                branch.BranchReferenceFormat().initialize(checkout, source)
 
768
            else:
 
769
                checkout_branch =  bzrdir.BzrDir.create_branch_convenience(
 
770
                    to_location, force_new_tree=False)
 
771
                checkout = checkout_branch.bzrdir
 
772
                checkout_branch.bind(source)
 
773
                if revision_id is not None:
 
774
                    rh = checkout_branch.revision_history()
 
775
                    checkout_branch.set_revision_history(rh[:rh.index(revision_id) + 1])
 
776
            checkout.create_workingtree(revision_id)
789
777
        finally:
790
778
            bzrdir.BzrDirFormat.set_default_format(old_format)
791
779
 
827
815
        tree = WorkingTree.open_containing(dir)[0]
828
816
        tree.lock_write()
829
817
        try:
830
 
            existing_pending_merges = tree.pending_merges()
831
 
            last_rev = tree.last_revision()
 
818
            last_rev = tree.last_revision() 
832
819
            if last_rev == tree.branch.last_revision():
833
820
                # may be up to date, check master too.
834
821
                master = tree.branch.get_master_branch()
839
826
            conflicts = tree.update()
840
827
            revno = tree.branch.revision_id_to_revno(tree.last_revision())
841
828
            note('Updated to revision %d.' % (revno,))
842
 
            if tree.pending_merges() != existing_pending_merges:
843
 
                note('Your local commits will now show as pending merges with '
844
 
                     "'bzr status', and can be committed with 'bzr commit'.")
845
829
            if conflicts != 0:
846
830
                return 1
847
831
            else:
915
899
    def run(self, filename):
916
900
        tree, relpath = WorkingTree.open_containing(filename)
917
901
        i = tree.inventory.path2id(relpath)
918
 
        if i is None:
 
902
        if i == None:
919
903
            raise BzrError("%r is not a versioned file" % filename)
920
904
        else:
921
905
            self.outf.write(i + '\n')
936
920
        tree, relpath = WorkingTree.open_containing(filename)
937
921
        inv = tree.inventory
938
922
        fid = inv.path2id(relpath)
939
 
        if fid is None:
 
923
        if fid == None:
940
924
            raise BzrError("%r is not a versioned file" % filename)
941
925
        for fip in inv.get_idpath(fid):
942
926
            self.outf.write(fip + '\n')
1000
984
            last_revision = wt.last_revision()
1001
985
 
1002
986
        revision_ids = b.repository.get_ancestry(last_revision)
1003
 
        assert revision_ids[0] is None
 
987
        assert revision_ids[0] == None
1004
988
        revision_ids.pop(0)
1005
989
        for revision_id in revision_ids:
1006
990
            self.outf.write(revision_id + '\n')
1350
1334
        else:
1351
1335
            # local dir only
1352
1336
            # FIXME ? log the current subdir only RBC 20060203 
1353
 
            if revision is not None \
1354
 
                    and len(revision) > 0 and revision[0].get_branch():
1355
 
                location = revision[0].get_branch()
1356
 
            else:
1357
 
                location = '.'
1358
 
            dir, relpath = bzrdir.BzrDir.open_containing(location)
 
1337
            dir, relpath = bzrdir.BzrDir.open_containing('.')
1359
1338
            b = dir.open_branch()
1360
1339
 
1361
1340
        if revision is None:
1364
1343
        elif len(revision) == 1:
1365
1344
            rev1 = rev2 = revision[0].in_history(b).revno
1366
1345
        elif len(revision) == 2:
1367
 
            if revision[1].get_branch() != revision[0].get_branch():
1368
 
                # b is taken from revision[0].get_branch(), and
1369
 
                # show_log will use its revision_history. Having
1370
 
                # different branches will lead to weird behaviors.
1371
 
                raise BzrCommandError(
1372
 
                    "Log doesn't accept two revisions in different branches.")
1373
1346
            if revision[0].spec is None:
1374
1347
                # missing begin-range means first revision
1375
1348
                rev1 = 1
1390
1363
        if rev1 > rev2:
1391
1364
            (rev2, rev1) = (rev1, rev2)
1392
1365
 
1393
 
        if (log_format is None):
 
1366
        if (log_format == None):
1394
1367
            default = b.get_config().log_format()
1395
1368
            log_format = get_log_format(long=long, short=short, line=line, 
1396
1369
                                        default=default)
1663
1636
 
1664
1637
        if tree is None:
1665
1638
            b, relpath = Branch.open_containing(filename)
1666
 
        if revision is not None and revision[0].get_branch() is not None:
1667
 
            b = Branch.open(revision[0].get_branch())
1668
1639
        if revision is None:
1669
1640
            revision_id = b.last_revision()
1670
1641
        else:
1784
1755
            raise BzrCommandError("Commit refused because there are unknown "
1785
1756
                                  "files in the working tree.")
1786
1757
        except errors.BoundBranchOutOfDate, e:
1787
 
            raise BzrCommandError(str(e) + "\n"
1788
 
                'To commit to master branch, run update and then commit.\n'
1789
 
                'You can also pass --local to commit to continue working '
1790
 
                'disconnected.')
 
1758
            raise BzrCommandError(str(e)
 
1759
                                  + ' Either unbind, update, or'
 
1760
                                    ' pass --local to commit.')
 
1761
 
1791
1762
 
1792
1763
class cmd_check(Command):
1793
1764
    """Validate consistency of branch history.
1969
1940
                     Option('lsprof-timed',
1970
1941
                            help='generate lsprof output for benchmarked'
1971
1942
                                 ' sections of code.'),
1972
 
                     Option('cache-dir', type=str,
1973
 
                            help='a directory to cache intermediate'
1974
 
                                 ' benchmark steps'),
1975
1943
                     ]
1976
1944
 
1977
1945
    def run(self, testspecs_list=None, verbose=None, one=False,
1978
1946
            keep_output=False, transport=None, benchmark=None,
1979
 
            lsprof_timed=None, cache_dir=None):
 
1947
            lsprof_timed=None):
1980
1948
        import bzrlib.ui
1981
1949
        from bzrlib.tests import selftest
1982
1950
        import bzrlib.benchmarks as benchmarks
1983
 
        from bzrlib.benchmarks import tree_creator
1984
 
 
1985
 
        if cache_dir is not None:
1986
 
            tree_creator.TreeCreator.CACHE_ROOT = osutils.abspath(cache_dir)
1987
1951
        # we don't want progress meters from the tests to go to the
1988
1952
        # real output; and we don't want log messages cluttering up
1989
1953
        # the real logs.
2002
1966
                test_suite_factory = benchmarks.test_suite
2003
1967
                if verbose is None:
2004
1968
                    verbose = True
2005
 
                benchfile = open(".perf_history", "at")
2006
1969
            else:
2007
1970
                test_suite_factory = None
2008
1971
                if verbose is None:
2009
1972
                    verbose = False
2010
 
                benchfile = None
2011
 
            try:
2012
 
                result = selftest(verbose=verbose, 
2013
 
                                  pattern=pattern,
2014
 
                                  stop_on_failure=one, 
2015
 
                                  keep_output=keep_output,
2016
 
                                  transport=transport,
2017
 
                                  test_suite_factory=test_suite_factory,
2018
 
                                  lsprof_timed=lsprof_timed,
2019
 
                                  bench_history=benchfile)
2020
 
            finally:
2021
 
                if benchfile is not None:
2022
 
                    benchfile.close()
 
1973
            result = selftest(verbose=verbose, 
 
1974
                              pattern=pattern,
 
1975
                              stop_on_failure=one, 
 
1976
                              keep_output=keep_output,
 
1977
                              transport=transport,
 
1978
                              test_suite_factory=test_suite_factory,
 
1979
                              lsprof_timed=lsprof_timed)
2023
1980
            if result:
2024
1981
                info('tests passed')
2025
1982
            else:
2029
1986
            ui.ui_factory = save_ui
2030
1987
 
2031
1988
 
 
1989
def _get_bzr_branch():
 
1990
    """If bzr is run from a branch, return Branch or None"""
 
1991
    from os.path import dirname
 
1992
    
 
1993
    try:
 
1994
        branch = Branch.open(dirname(osutils.abspath(dirname(__file__))))
 
1995
        return branch
 
1996
    except errors.BzrError:
 
1997
        return None
 
1998
    
 
1999
 
 
2000
def show_version():
 
2001
    import bzrlib
 
2002
    print "bzr (bazaar-ng) %s" % bzrlib.__version__
 
2003
    # is bzrlib itself in a branch?
 
2004
    branch = _get_bzr_branch()
 
2005
    if branch:
 
2006
        rh = branch.revision_history()
 
2007
        revno = len(rh)
 
2008
        print "  bzr checkout, revision %d" % (revno,)
 
2009
        print "  nick: %s" % (branch.nick,)
 
2010
        if rh:
 
2011
            print "  revid: %s" % (rh[-1],)
 
2012
    print "Using python interpreter:", sys.executable
 
2013
    import site
 
2014
    print "Using python standard library:", os.path.dirname(site.__file__)
 
2015
    print "Using bzrlib:",
 
2016
    if len(bzrlib.__path__) > 1:
 
2017
        # print repr, which is a good enough way of making it clear it's
 
2018
        # more than one element (eg ['/foo/bar', '/foo/bzr'])
 
2019
        print repr(bzrlib.__path__)
 
2020
    else:
 
2021
        print bzrlib.__path__[0]
 
2022
 
 
2023
    print
 
2024
    print bzrlib.__copyright__
 
2025
    print "http://bazaar-vcs.org/"
 
2026
    print
 
2027
    print "bzr comes with ABSOLUTELY NO WARRANTY.  bzr is free software, and"
 
2028
    print "you may use, modify and redistribute it under the terms of the GNU"
 
2029
    print "General Public License version 2 or later."
 
2030
 
 
2031
 
2032
2032
class cmd_version(Command):
2033
2033
    """Show version of bzr."""
2034
2034
 
2035
2035
    @display_command
2036
2036
    def run(self):
2037
 
        from bzrlib.version import show_version
2038
2037
        show_version()
2039
2038
 
2040
2039
 
2120
2119
    takes_args = ['branch?']
2121
2120
    takes_options = ['revision', 'force', 'merge-type', 'reprocess', 'remember',
2122
2121
                     Option('show-base', help="Show base revision text in "
2123
 
                            "conflicts"), 
2124
 
                     Option('uncommitted', help='Apply uncommitted changes'
2125
 
                            ' from a working copy, instead of branch changes')]
 
2122
                            "conflicts")]
2126
2123
 
2127
2124
    def help(self):
2128
2125
        from merge import merge_type_help
2130
2127
        return getdoc(self) + '\n' + merge_type_help() 
2131
2128
 
2132
2129
    def run(self, branch=None, revision=None, force=False, merge_type=None,
2133
 
            show_base=False, reprocess=False, remember=False, 
2134
 
            uncommitted=False):
 
2130
            show_base=False, reprocess=False, remember=False):
2135
2131
        if merge_type is None:
2136
2132
            merge_type = Merge3Merger
2137
2133
 
2150
2146
                else:
2151
2147
                    return 1
2152
2148
 
2153
 
        if revision is None \
2154
 
                or len(revision) < 1 or revision[0].needs_branch():
2155
 
            branch = self._get_remembered_parent(tree, branch, 'Merging from')
 
2149
        branch = self._get_remembered_parent(tree, branch, 'Merging from')
2156
2150
 
2157
2151
        if revision is None or len(revision) < 1:
2158
 
            if uncommitted:
2159
 
                base = [branch, -1]
2160
 
                other = [branch, None]
2161
 
            else:
2162
 
                base = [None, None]
2163
 
                other = [branch, -1]
 
2152
            base = [None, None]
 
2153
            other = [branch, -1]
2164
2154
            other_branch, path = Branch.open_containing(branch)
2165
2155
        else:
2166
 
            if uncommitted:
2167
 
                raise BzrCommandError('Cannot use --uncommitted and --revision'
2168
 
                                      ' at the same time.')
2169
 
            branch = revision[0].get_branch() or branch
2170
2156
            if len(revision) == 1:
2171
2157
                base = [None, None]
2172
2158
                other_branch, path = Branch.open_containing(branch)
2176
2162
                assert len(revision) == 2
2177
2163
                if None in revision:
2178
2164
                    raise BzrCommandError(
2179
 
                        "Merge doesn't permit empty revision specifier.")
2180
 
                base_branch, path = Branch.open_containing(branch)
2181
 
                branch1 = revision[1].get_branch() or branch
2182
 
                other_branch, path1 = Branch.open_containing(branch1)
2183
 
                if revision[0].get_branch() is not None:
2184
 
                    # then path was obtained from it, and is None.
2185
 
                    path = path1
 
2165
                        "Merge doesn't permit that revision specifier.")
 
2166
                other_branch, path = Branch.open_containing(branch)
2186
2167
 
2187
 
                base = [branch, revision[0].in_history(base_branch).revno]
2188
 
                other = [branch1, revision[1].in_history(other_branch).revno]
 
2168
                base = [branch, revision[0].in_history(other_branch).revno]
 
2169
                other = [branch, revision[1].in_history(other_branch).revno]
2189
2170
 
2190
2171
        if tree.branch.get_parent() is None or remember:
2191
2172
            tree.branch.set_parent(other_branch.base)
2273
2254
        tree, file_list = tree_files(file_list)
2274
2255
        tree.lock_write()
2275
2256
        try:
2276
 
            parents = tree.get_parent_ids()
2277
 
            if len(parents) != 2:
 
2257
            pending_merges = tree.pending_merges() 
 
2258
            if len(pending_merges) != 1:
2278
2259
                raise BzrCommandError("Sorry, remerge only works after normal"
2279
2260
                                      " merges.  Not cherrypicking or"
2280
2261
                                      " multi-merges.")
2281
2262
            repository = tree.branch.repository
2282
 
            base_revision = common_ancestor(parents[0],
2283
 
                                            parents[1], repository)
 
2263
            base_revision = common_ancestor(tree.branch.last_revision(), 
 
2264
                                            pending_merges[0], repository)
2284
2265
            base_tree = repository.revision_tree(base_revision)
2285
 
            other_tree = repository.revision_tree(parents[1])
 
2266
            other_tree = repository.revision_tree(pending_merges[0])
2286
2267
            interesting_ids = None
2287
2268
            new_conflicts = []
2288
2269
            conflicts = tree.conflicts()
2312
2293
                    pass
2313
2294
            conflicts = merge_inner(tree.branch, other_tree, base_tree,
2314
2295
                                    this_tree=tree,
2315
 
                                    interesting_ids=interesting_ids,
2316
 
                                    other_rev_id=parents[1],
2317
 
                                    merge_type=merge_type,
 
2296
                                    interesting_ids=interesting_ids, 
 
2297
                                    other_rev_id=pending_merges[0], 
 
2298
                                    merge_type=merge_type, 
2318
2299
                                    show_base=show_base,
2319
2300
                                    reprocess=reprocess)
2320
2301
        finally:
2450
2431
            remote_branch.lock_read()
2451
2432
            try:
2452
2433
                local_extra, remote_extra = find_unmerged(local_branch, remote_branch)
2453
 
                if (log_format is None):
 
2434
                if (log_format == None):
2454
2435
                    default = local_branch.get_config().log_format()
2455
2436
                    log_format = get_log_format(long=long, short=short, 
2456
2437
                                                line=line, default=default)
2504
2485
        import bzrlib.plugin
2505
2486
        from inspect import getdoc
2506
2487
        for name, plugin in bzrlib.plugin.all_plugins().items():
2507
 
            if getattr(plugin, '__path__', None) is not None:
 
2488
            if hasattr(plugin, '__path__'):
2508
2489
                print plugin.__path__[0]
2509
 
            elif getattr(plugin, '__file__', None) is not None:
 
2490
            elif hasattr(plugin, '__file__'):
2510
2491
                print plugin.__file__
2511
2492
            else:
2512
 
                print repr(plugin)
 
2493
                print `plugin`
2513
2494
                
2514
2495
            d = getdoc(plugin)
2515
2496
            if d:
2518
2499
 
2519
2500
class cmd_testament(Command):
2520
2501
    """Show testament (signing-form) of a revision."""
2521
 
    takes_options = ['revision', 
2522
 
                     Option('long', help='Produce long-format testament'), 
 
2502
    takes_options = ['revision', 'long', 
2523
2503
                     Option('strict', help='Produce a strict-format'
2524
2504
                            ' testament')]
2525
2505
    takes_args = ['branch?']
2767
2747
            pass
2768
2748
        
2769
2749
 
2770
 
class cmd_wait_until_signalled(Command):
2771
 
    """Test helper for test_start_and_stop_bzr_subprocess_send_signal.
2772
 
 
2773
 
    This just prints a line to signal when it is ready, then blocks on stdin.
2774
 
    """
2775
 
 
2776
 
    hidden = True
2777
 
 
2778
 
    def run(self):
2779
 
        sys.stdout.write("running\n")
2780
 
        sys.stdout.flush()
2781
 
        sys.stdin.readline()
2782
 
 
2783
 
 
2784
 
class cmd_serve(Command):
2785
 
    """Run the bzr server.
2786
 
    """
2787
 
    takes_options = [
2788
 
        Option('inet',
2789
 
               help='serve on stdin/out for use from inetd or sshd'),
2790
 
        Option('port',
2791
 
               help='listen for connections on nominated port of the form '
2792
 
                    '[hostname:]portnumber. Passing 0 as the port number will '
2793
 
                    'result in a dynamically allocated port.',
2794
 
               type=str),
2795
 
        Option('directory',
2796
 
               help='serve contents of directory',
2797
 
               type=unicode),
2798
 
        ]
2799
 
 
2800
 
    def run(self, port=None, inet=False, directory=None):
2801
 
        from bzrlib.transport import smart
2802
 
        from bzrlib.transport import get_transport
2803
 
        if directory is None:
2804
 
            directory = os.getcwd()
2805
 
        t = get_transport(directory)
2806
 
        if inet:
2807
 
            server = smart.SmartStreamServer(sys.stdin, sys.stdout, t)
2808
 
        elif port is not None:
2809
 
            if ':' in port:
2810
 
                host, port = port.split(':')
2811
 
            else:
2812
 
                host = '127.0.0.1'
2813
 
            server = smart.SmartTCPServer(t, host=host, port=int(port))
2814
 
            print 'listening on port: ', server.port
2815
 
            sys.stdout.flush()
2816
 
        else:
2817
 
            raise BzrCommandError("bzr serve requires one of --inet or --port")
2818
 
        server.serve()
2819
 
 
2820
2750
 
2821
2751
# command-line interpretation helper for merge-related commands
2822
2752
def merge(other_revision, base_revision,