~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Martin
  • Date: 2010-05-16 15:18:43 UTC
  • mfrom: (5235 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5239.
  • Revision ID: gzlist@googlemail.com-20100516151843-lu53u7caehm3ie3i
Merge bzr.dev to resolve conflicts in NEWS and _chk_map_pyx

Show diffs side-by-side

added added

removed removed

Lines of Context:
232
232
    return view_info
233
233
 
234
234
 
 
235
def _open_directory_or_containing_tree_or_branch(filename, directory):
 
236
    """Open the tree or branch containing the specified file, unless
 
237
    the --directory option is used to specify a different branch."""
 
238
    if directory is not None:
 
239
        return (None, Branch.open(directory), filename)
 
240
    return bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
241
 
 
242
 
235
243
# TODO: Make sure no commands unconditionally use the working directory as a
236
244
# branch.  If a filename argument is used, the first of them should be used to
237
245
# specify the branch.  (Perhaps this can be factored out into some kind of
340
348
 
341
349
    hidden = True
342
350
    takes_args = ['revision_id?']
343
 
    takes_options = ['revision']
 
351
    takes_options = ['directory', 'revision']
344
352
    # cat-revision is more for frontends so should be exact
345
353
    encoding = 'strict'
346
354
 
353
361
        self.outf.write(revtext.decode('utf-8'))
354
362
 
355
363
    @display_command
356
 
    def run(self, revision_id=None, revision=None):
 
364
    def run(self, revision_id=None, revision=None, directory=u'.'):
357
365
        if revision_id is not None and revision is not None:
358
366
            raise errors.BzrCommandError('You can only supply one of'
359
367
                                         ' revision_id or --revision')
360
368
        if revision_id is None and revision is None:
361
369
            raise errors.BzrCommandError('You must supply either'
362
370
                                         ' --revision or a revision_id')
363
 
        b = WorkingTree.open_containing(u'.')[0].branch
 
371
        b = WorkingTree.open_containing(directory)[0].branch
364
372
 
365
373
        revisions = b.repository.revisions
366
374
        if revisions is None:
528
536
        if tree:
529
537
            try:
530
538
                wt = WorkingTree.open_containing(location)[0]
531
 
                wt.lock_read()
 
539
                self.add_cleanup(wt.lock_read().unlock)
532
540
            except (errors.NoWorkingTree, errors.NotLocalUrl):
533
541
                raise errors.NoWorkingTree(location)
534
 
            self.add_cleanup(wt.unlock)
535
542
            revid = wt.last_revision()
536
543
            try:
537
544
                revno_t = wt.branch.revision_id_to_dotted_revno(revid)
540
547
            revno = ".".join(str(n) for n in revno_t)
541
548
        else:
542
549
            b = Branch.open_containing(location)[0]
543
 
            b.lock_read()
544
 
            self.add_cleanup(b.unlock)
 
550
            self.add_cleanup(b.lock_read().unlock)
545
551
            revno = b.revno()
546
552
        self.cleanup_now()
547
553
        self.outf.write(str(revno) + '\n')
554
560
    takes_args = ['revision_info*']
555
561
    takes_options = [
556
562
        'revision',
557
 
        Option('directory',
 
563
        custom_help('directory',
558
564
            help='Branch to examine, '
559
 
                 'rather than the one containing the working directory.',
560
 
            short_name='d',
561
 
            type=unicode,
562
 
            ),
 
565
                 'rather than the one containing the working directory.'),
563
566
        Option('tree', help='Show revno of working tree'),
564
567
        ]
565
568
 
570
573
        try:
571
574
            wt = WorkingTree.open_containing(directory)[0]
572
575
            b = wt.branch
573
 
            wt.lock_read()
574
 
            self.add_cleanup(wt.unlock)
 
576
            self.add_cleanup(wt.lock_read().unlock)
575
577
        except (errors.NoWorkingTree, errors.NotLocalUrl):
576
578
            wt = None
577
579
            b = Branch.open_containing(directory)[0]
578
 
            b.lock_read()
579
 
            self.add_cleanup(b.unlock)
 
580
            self.add_cleanup(b.lock_read().unlock)
580
581
        revision_ids = []
581
582
        if revision is not None:
582
583
            revision_ids.extend(rev.as_revision_id(b) for rev in revision)
681
682
                should_print=(not is_quiet()))
682
683
 
683
684
        if base_tree:
684
 
            base_tree.lock_read()
685
 
            self.add_cleanup(base_tree.unlock)
 
685
            self.add_cleanup(base_tree.lock_read().unlock)
686
686
        tree, file_list = tree_files_for_add(file_list)
687
687
        added, ignored = tree.smart_add(file_list, not
688
688
            no_recurse, action=action, save=not dry_run)
760
760
 
761
761
        revision = _get_one_revision('inventory', revision)
762
762
        work_tree, file_list = tree_files(file_list)
763
 
        work_tree.lock_read()
764
 
        self.add_cleanup(work_tree.unlock)
 
763
        self.add_cleanup(work_tree.lock_read().unlock)
765
764
        if revision is not None:
766
765
            tree = revision.as_tree(work_tree.branch)
767
766
 
768
767
            extra_trees = [work_tree]
769
 
            tree.lock_read()
770
 
            self.add_cleanup(tree.unlock)
 
768
            self.add_cleanup(tree.lock_read().unlock)
771
769
        else:
772
770
            tree = work_tree
773
771
            extra_trees = []
833
831
        if len(names_list) < 2:
834
832
            raise errors.BzrCommandError("missing file argument")
835
833
        tree, rel_names = tree_files(names_list, canonicalize=False)
836
 
        tree.lock_tree_write()
837
 
        self.add_cleanup(tree.unlock)
 
834
        self.add_cleanup(tree.lock_tree_write().unlock)
838
835
        self._run(tree, names_list, rel_names, after)
839
836
 
840
837
    def run_auto(self, names_list, after, dry_run):
845
842
            raise errors.BzrCommandError('--after cannot be specified with'
846
843
                                         ' --auto.')
847
844
        work_tree, file_list = tree_files(names_list, default_branch='.')
848
 
        work_tree.lock_tree_write()
849
 
        self.add_cleanup(work_tree.unlock)
 
845
        self.add_cleanup(work_tree.lock_tree_write().unlock)
850
846
        rename_map.RenameMap.guess_renames(work_tree, dry_run)
851
847
 
852
848
    def _run(self, tree, names_list, rel_names, after):
960
956
    takes_options = ['remember', 'overwrite', 'revision',
961
957
        custom_help('verbose',
962
958
            help='Show logs of pulled revisions.'),
963
 
        Option('directory',
 
959
        custom_help('directory',
964
960
            help='Branch to pull into, '
965
 
                 'rather than the one containing the working directory.',
966
 
            short_name='d',
967
 
            type=unicode,
968
 
            ),
 
961
                 'rather than the one containing the working directory.'),
969
962
        Option('local',
970
963
            help="Perform a local pull in a bound "
971
964
                 "branch.  Local pulls are not applied to "
986
979
        try:
987
980
            tree_to = WorkingTree.open_containing(directory)[0]
988
981
            branch_to = tree_to.branch
989
 
            tree_to.lock_write()
990
 
            self.add_cleanup(tree_to.unlock)
 
982
            self.add_cleanup(tree_to.lock_write().unlock)
991
983
        except errors.NoWorkingTree:
992
984
            tree_to = None
993
985
            branch_to = Branch.open_containing(directory)[0]
994
 
            branch_to.lock_write()
995
 
            self.add_cleanup(branch_to.unlock)
 
986
            self.add_cleanup(branch_to.lock_write().unlock)
996
987
 
997
988
        if local and not branch_to.get_bound_location():
998
989
            raise errors.LocalRequiresBoundBranch()
1029
1020
        else:
1030
1021
            branch_from = Branch.open(location,
1031
1022
                possible_transports=possible_transports)
1032
 
            branch_from.lock_read()
1033
 
            self.add_cleanup(branch_from.unlock)
 
1023
            self.add_cleanup(branch_from.lock_read().unlock)
1034
1024
 
1035
1025
            if branch_to.get_parent() is None or remember:
1036
1026
                branch_to.set_parent(branch_from.base)
1088
1078
        Option('create-prefix',
1089
1079
               help='Create the path leading up to the branch '
1090
1080
                    'if it does not already exist.'),
1091
 
        Option('directory',
 
1081
        custom_help('directory',
1092
1082
            help='Branch to push from, '
1093
 
                 'rather than the one containing the working directory.',
1094
 
            short_name='d',
1095
 
            type=unicode,
1096
 
            ),
 
1083
                 'rather than the one containing the working directory.'),
1097
1084
        Option('use-existing-dir',
1098
1085
               help='By default push will fail if the target'
1099
1086
                    ' directory exists, but does not already'
1219
1206
        accelerator_tree, br_from = bzrdir.BzrDir.open_tree_or_branch(
1220
1207
            from_location)
1221
1208
        revision = _get_one_revision('branch', revision)
1222
 
        br_from.lock_read()
1223
 
        self.add_cleanup(br_from.unlock)
 
1209
        self.add_cleanup(br_from.lock_read().unlock)
1224
1210
        if revision is not None:
1225
1211
            revision_id = revision.as_revision_id(br_from)
1226
1212
        else:
1350
1336
            except errors.NoWorkingTree:
1351
1337
                source.bzrdir.create_workingtree(revision_id)
1352
1338
                return
 
1339
 
 
1340
        if not lightweight:
 
1341
            message = ('Copying history to "%s". '
 
1342
                'To checkout without local history use --lightweight.' % to_location)
 
1343
            ui.ui_factory.show_message(message)
1353
1344
        source.create_checkout(to_location, revision_id, lightweight,
1354
1345
                               accelerator_tree, hardlink)
1355
1346
 
1366
1357
    @display_command
1367
1358
    def run(self, dir=u'.'):
1368
1359
        tree = WorkingTree.open_containing(dir)[0]
1369
 
        tree.lock_read()
1370
 
        self.add_cleanup(tree.unlock)
 
1360
        self.add_cleanup(tree.lock_read().unlock)
1371
1361
        new_inv = tree.inventory
1372
1362
        old_tree = tree.basis_tree()
1373
 
        old_tree.lock_read()
1374
 
        self.add_cleanup(old_tree.unlock)
 
1363
        self.add_cleanup(old_tree.lock_read().unlock)
1375
1364
        old_inv = old_tree.inventory
1376
1365
        renames = []
1377
1366
        iterator = tree.iter_changes(old_tree, include_unchanged=True)
1415
1404
        master = branch.get_master_branch(
1416
1405
            possible_transports=possible_transports)
1417
1406
        if master is not None:
1418
 
            tree.lock_write()
1419
1407
            branch_location = master.base
 
1408
            tree.lock_write()
1420
1409
        else:
 
1410
            branch_location = tree.branch.base
1421
1411
            tree.lock_tree_write()
1422
 
            branch_location = tree.branch.base
1423
1412
        self.add_cleanup(tree.unlock)
1424
1413
        # get rid of the final '/' and be ready for display
1425
1414
        branch_location = urlutils.unescape_for_display(
1545
1534
        if file_list is not None:
1546
1535
            file_list = [f for f in file_list]
1547
1536
 
1548
 
        tree.lock_write()
1549
 
        self.add_cleanup(tree.unlock)
 
1537
        self.add_cleanup(tree.lock_write().unlock)
1550
1538
        # Heuristics should probably all move into tree.remove_smart or
1551
1539
        # some such?
1552
1540
        if new:
2003
1991
    # level of effort but possibly much less IO.  (Or possibly not,
2004
1992
    # if the directories are very large...)
2005
1993
    _see_also = ['status', 'ls']
2006
 
    takes_options = ['show-ids']
 
1994
    takes_options = ['directory', 'show-ids']
2007
1995
 
2008
1996
    @display_command
2009
 
    def run(self, show_ids=False):
2010
 
        tree = WorkingTree.open_containing(u'.')[0]
2011
 
        tree.lock_read()
2012
 
        self.add_cleanup(tree.unlock)
 
1997
    def run(self, show_ids=False, directory=u'.'):
 
1998
        tree = WorkingTree.open_containing(directory)[0]
 
1999
        self.add_cleanup(tree.lock_read().unlock)
2013
2000
        old = tree.basis_tree()
2014
 
        old.lock_read()
2015
 
        self.add_cleanup(old.unlock)
 
2001
        self.add_cleanup(old.lock_read().unlock)
2016
2002
        for path, ie in old.inventory.iter_entries():
2017
2003
            if not tree.has_id(ie.file_id):
2018
2004
                self.outf.write(path)
2028
2014
 
2029
2015
    hidden = True
2030
2016
    _see_also = ['status', 'ls']
2031
 
    takes_options = [
2032
 
            Option('null',
2033
 
                   help='Write an ascii NUL (\\0) separator '
2034
 
                   'between files rather than a newline.')
2035
 
            ]
 
2017
    takes_options = ['directory', 'null']
2036
2018
 
2037
2019
    @display_command
2038
 
    def run(self, null=False):
2039
 
        tree = WorkingTree.open_containing(u'.')[0]
 
2020
    def run(self, null=False, directory=u'.'):
 
2021
        tree = WorkingTree.open_containing(directory)[0]
2040
2022
        td = tree.changes_from(tree.basis_tree())
2041
2023
        for path, id, kind, text_modified, meta_modified in td.modified:
2042
2024
            if null:
2051
2033
 
2052
2034
    hidden = True
2053
2035
    _see_also = ['status', 'ls']
2054
 
    takes_options = [
2055
 
            Option('null',
2056
 
                   help='Write an ascii NUL (\\0) separator '
2057
 
                   'between files rather than a newline.')
2058
 
            ]
 
2036
    takes_options = ['directory', 'null']
2059
2037
 
2060
2038
    @display_command
2061
 
    def run(self, null=False):
2062
 
        wt = WorkingTree.open_containing(u'.')[0]
2063
 
        wt.lock_read()
2064
 
        self.add_cleanup(wt.unlock)
 
2039
    def run(self, null=False, directory=u'.'):
 
2040
        wt = WorkingTree.open_containing(directory)[0]
 
2041
        self.add_cleanup(wt.lock_read().unlock)
2065
2042
        basis = wt.basis_tree()
2066
 
        basis.lock_read()
2067
 
        self.add_cleanup(basis.unlock)
 
2043
        self.add_cleanup(basis.lock_read().unlock)
2068
2044
        basis_inv = basis.inventory
2069
2045
        inv = wt.inventory
2070
2046
        for file_id in inv:
2073
2049
            if inv.is_root(file_id) and len(basis_inv) == 0:
2074
2050
                continue
2075
2051
            path = inv.id2path(file_id)
2076
 
            if not os.access(osutils.abspath(path), os.F_OK):
 
2052
            if not os.access(osutils.pathjoin(wt.basedir, path), os.F_OK):
2077
2053
                continue
2078
2054
            if null:
2079
2055
                self.outf.write(path + '\0')
2279
2255
                   help='Show just the specified revision.'
2280
2256
                   ' See also "help revisionspec".'),
2281
2257
            'log-format',
 
2258
            RegistryOption('authors',
 
2259
                'What names to list as authors - first, all or committer.',
 
2260
                title='Authors',
 
2261
                lazy_registry=('bzrlib.log', 'author_list_registry'),
 
2262
            ),
2282
2263
            Option('levels',
2283
2264
                   short_name='n',
2284
2265
                   help='Number of levels to display - 0 for all, 1 for flat.',
2319
2300
            limit=None,
2320
2301
            show_diff=False,
2321
2302
            include_merges=False,
 
2303
            authors=None,
2322
2304
            exclude_common_ancestry=False,
2323
2305
            ):
2324
2306
        from bzrlib.log import (
2352
2334
        if file_list:
2353
2335
            # find the file ids to log and check for directory filtering
2354
2336
            b, file_info_list, rev1, rev2 = _get_info_for_log_files(
2355
 
                revision, file_list)
2356
 
            self.add_cleanup(b.unlock)
 
2337
                revision, file_list, self.add_cleanup)
2357
2338
            for relpath, file_id, kind in file_info_list:
2358
2339
                if file_id is None:
2359
2340
                    raise errors.BzrCommandError(
2377
2358
                location = '.'
2378
2359
            dir, relpath = bzrdir.BzrDir.open_containing(location)
2379
2360
            b = dir.open_branch()
2380
 
            b.lock_read()
2381
 
            self.add_cleanup(b.unlock)
 
2361
            self.add_cleanup(b.lock_read().unlock)
2382
2362
            rev1, rev2 = _get_revision_range(revision, b, self.name())
2383
2363
 
2384
2364
        # Decide on the type of delta & diff filtering to use
2404
2384
                        show_timezone=timezone,
2405
2385
                        delta_format=get_verbosity_level(),
2406
2386
                        levels=levels,
2407
 
                        show_advice=levels is None)
 
2387
                        show_advice=levels is None,
 
2388
                        author_list_handler=authors)
2408
2389
 
2409
2390
        # Choose the algorithm for doing the logging. It's annoying
2410
2391
        # having multiple code paths like this but necessary until
2508
2489
        tree, relpath = WorkingTree.open_containing(filename)
2509
2490
        file_id = tree.path2id(relpath)
2510
2491
        b = tree.branch
2511
 
        b.lock_read()
2512
 
        self.add_cleanup(b.unlock)
 
2492
        self.add_cleanup(b.lock_read().unlock)
2513
2493
        touching_revs = log.find_touching_revisions(b, file_id)
2514
2494
        for revno, revision_id, what in touching_revs:
2515
2495
            self.outf.write("%6d %s\n" % (revno, what))
2528
2508
                   help='Recurse into subdirectories.'),
2529
2509
            Option('from-root',
2530
2510
                   help='Print paths relative to the root of the branch.'),
2531
 
            Option('unknown', help='Print unknown files.'),
 
2511
            Option('unknown', short_name='u',
 
2512
                help='Print unknown files.'),
2532
2513
            Option('versioned', help='Print versioned files.',
2533
2514
                   short_name='V'),
2534
 
            Option('ignored', help='Print ignored files.'),
2535
 
            Option('null',
2536
 
                   help='Write an ascii NUL (\\0) separator '
2537
 
                   'between files rather than a newline.'),
2538
 
            Option('kind',
 
2515
            Option('ignored', short_name='i',
 
2516
                help='Print ignored files.'),
 
2517
            Option('kind', short_name='k',
2539
2518
                   help='List entries of a particular kind: file, directory, symlink.',
2540
2519
                   type=unicode),
 
2520
            'null',
2541
2521
            'show-ids',
 
2522
            'directory',
2542
2523
            ]
2543
2524
    @display_command
2544
2525
    def run(self, revision=None, verbose=False,
2545
2526
            recursive=False, from_root=False,
2546
2527
            unknown=False, versioned=False, ignored=False,
2547
 
            null=False, kind=None, show_ids=False, path=None):
 
2528
            null=False, kind=None, show_ids=False, path=None, directory=None):
2548
2529
 
2549
2530
        if kind and kind not in ('file', 'directory', 'symlink'):
2550
2531
            raise errors.BzrCommandError('invalid kind specified')
2562
2543
                raise errors.BzrCommandError('cannot specify both --from-root'
2563
2544
                                             ' and PATH')
2564
2545
            fs_path = path
2565
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
2566
 
            fs_path)
 
2546
        tree, branch, relpath = \
 
2547
            _open_directory_or_containing_tree_or_branch(fs_path, directory)
2567
2548
 
2568
2549
        # Calculate the prefix to use
2569
2550
        prefix = None
2584
2565
                view_str = views.view_display_str(view_files)
2585
2566
                note("Ignoring files outside view. View is %s" % view_str)
2586
2567
 
2587
 
        tree.lock_read()
2588
 
        self.add_cleanup(tree.unlock)
 
2568
        self.add_cleanup(tree.lock_read().unlock)
2589
2569
        for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
2590
2570
            from_dir=relpath, recursive=recursive):
2591
2571
            # Apply additional masking
2638
2618
 
2639
2619
    hidden = True
2640
2620
    _see_also = ['ls']
 
2621
    takes_options = ['directory']
2641
2622
 
2642
2623
    @display_command
2643
 
    def run(self):
2644
 
        for f in WorkingTree.open_containing(u'.')[0].unknowns():
 
2624
    def run(self, directory=u'.'):
 
2625
        for f in WorkingTree.open_containing(directory)[0].unknowns():
2645
2626
            self.outf.write(osutils.quotefn(f) + '\n')
2646
2627
 
2647
2628
 
2712
2693
 
2713
2694
    _see_also = ['status', 'ignored', 'patterns']
2714
2695
    takes_args = ['name_pattern*']
2715
 
    takes_options = [
 
2696
    takes_options = ['directory',
2716
2697
        Option('default-rules',
2717
2698
               help='Display the default ignore rules that bzr uses.')
2718
2699
        ]
2719
2700
 
2720
 
    def run(self, name_pattern_list=None, default_rules=None):
 
2701
    def run(self, name_pattern_list=None, default_rules=None,
 
2702
            directory=u'.'):
2721
2703
        from bzrlib import ignores
2722
2704
        if default_rules is not None:
2723
2705
            # dump the default rules and exit
2734
2716
                (len(name_pattern) > 1 and name_pattern[1] == ':')):
2735
2717
                raise errors.BzrCommandError(
2736
2718
                    "NAME_PATTERN should not be an absolute path")
2737
 
        tree, relpath = WorkingTree.open_containing(u'.')
 
2719
        tree, relpath = WorkingTree.open_containing(directory)
2738
2720
        ignores.tree_ignores_add_patterns(tree, name_pattern_list)
2739
2721
        ignored = globbing.Globster(name_pattern_list)
2740
2722
        matches = []
2766
2748
 
2767
2749
    encoding_type = 'replace'
2768
2750
    _see_also = ['ignore', 'ls']
 
2751
    takes_options = ['directory']
2769
2752
 
2770
2753
    @display_command
2771
 
    def run(self):
2772
 
        tree = WorkingTree.open_containing(u'.')[0]
2773
 
        tree.lock_read()
2774
 
        self.add_cleanup(tree.unlock)
 
2754
    def run(self, directory=u'.'):
 
2755
        tree = WorkingTree.open_containing(directory)[0]
 
2756
        self.add_cleanup(tree.lock_read().unlock)
2775
2757
        for path, file_class, kind, file_id, entry in tree.list_files():
2776
2758
            if file_class != 'I':
2777
2759
                continue
2788
2770
    """
2789
2771
    hidden = True
2790
2772
    takes_args = ['revno']
 
2773
    takes_options = ['directory']
2791
2774
 
2792
2775
    @display_command
2793
 
    def run(self, revno):
 
2776
    def run(self, revno, directory=u'.'):
2794
2777
        try:
2795
2778
            revno = int(revno)
2796
2779
        except ValueError:
2797
2780
            raise errors.BzrCommandError("not a valid revision-number: %r"
2798
2781
                                         % revno)
2799
 
        revid = WorkingTree.open_containing(u'.')[0].branch.get_rev_id(revno)
 
2782
        revid = WorkingTree.open_containing(directory)[0].branch.get_rev_id(revno)
2800
2783
        self.outf.write("%s\n" % revid)
2801
2784
 
2802
2785
 
2829
2812
      =================       =========================
2830
2813
    """
2831
2814
    takes_args = ['dest', 'branch_or_subdir?']
2832
 
    takes_options = [
 
2815
    takes_options = ['directory',
2833
2816
        Option('format',
2834
2817
               help="Type of file to export to.",
2835
2818
               type=unicode),
2844
2827
                    'revision in which it was changed.'),
2845
2828
        ]
2846
2829
    def run(self, dest, branch_or_subdir=None, revision=None, format=None,
2847
 
        root=None, filters=False, per_file_timestamps=False):
 
2830
        root=None, filters=False, per_file_timestamps=False, directory=u'.'):
2848
2831
        from bzrlib.export import export
2849
2832
 
2850
2833
        if branch_or_subdir is None:
2851
 
            tree = WorkingTree.open_containing(u'.')[0]
 
2834
            tree = WorkingTree.open_containing(directory)[0]
2852
2835
            b = tree.branch
2853
2836
            subdir = None
2854
2837
        else:
2873
2856
    """
2874
2857
 
2875
2858
    _see_also = ['ls']
2876
 
    takes_options = [
 
2859
    takes_options = ['directory',
2877
2860
        Option('name-from-revision', help='The path name in the old tree.'),
2878
2861
        Option('filters', help='Apply content filters to display the '
2879
2862
                'convenience form.'),
2884
2867
 
2885
2868
    @display_command
2886
2869
    def run(self, filename, revision=None, name_from_revision=False,
2887
 
            filters=False):
 
2870
            filters=False, directory=None):
2888
2871
        if revision is not None and len(revision) != 1:
2889
2872
            raise errors.BzrCommandError("bzr cat --revision takes exactly"
2890
2873
                                         " one revision specifier")
2891
2874
        tree, branch, relpath = \
2892
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
2893
 
        branch.lock_read()
2894
 
        self.add_cleanup(branch.unlock)
 
2875
            _open_directory_or_containing_tree_or_branch(filename, directory)
 
2876
        self.add_cleanup(branch.lock_read().unlock)
2895
2877
        return self._run(tree, branch, relpath, filename, revision,
2896
2878
                         name_from_revision, filters)
2897
2879
 
2900
2882
        if tree is None:
2901
2883
            tree = b.basis_tree()
2902
2884
        rev_tree = _get_one_revision_tree('cat', revision, branch=b)
2903
 
        rev_tree.lock_read()
2904
 
        self.add_cleanup(rev_tree.unlock)
 
2885
        self.add_cleanup(rev_tree.lock_read().unlock)
2905
2886
 
2906
2887
        old_file_id = rev_tree.path2id(relpath)
2907
2888
 
3380
3361
 
3381
3362
    _see_also = ['info']
3382
3363
    takes_args = ['nickname?']
3383
 
    def run(self, nickname=None):
3384
 
        branch = Branch.open_containing(u'.')[0]
 
3364
    takes_options = ['directory']
 
3365
    def run(self, nickname=None, directory=u'.'):
 
3366
        branch = Branch.open_containing(directory)[0]
3385
3367
        if nickname is None:
3386
3368
            self.printme(branch)
3387
3369
        else:
3612
3594
            self.additional_selftest_args['runner_class'] = SubUnitBzrRunner
3613
3595
            # On Windows, disable automatic conversion of '\n' to '\r\n' in
3614
3596
            # stdout, which would corrupt the subunit stream. 
3615
 
            if sys.platform == "win32" and sys.stdout.fileno() >= 0:
 
3597
            # FIXME: This has been fixed in subunit trunk (>0.0.5) so the
 
3598
            # following code can be deleted when it's sufficiently deployed
 
3599
            # -- vila/mgz 20100514
 
3600
            if (sys.platform == "win32"
 
3601
                and getattr(sys.stdout, 'fileno', None) is not None):
3616
3602
                import msvcrt
3617
3603
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
3618
3604
        if parallel:
3690
3676
 
3691
3677
        branch1 = Branch.open_containing(branch)[0]
3692
3678
        branch2 = Branch.open_containing(other)[0]
3693
 
        branch1.lock_read()
3694
 
        self.add_cleanup(branch1.unlock)
3695
 
        branch2.lock_read()
3696
 
        self.add_cleanup(branch2.unlock)
 
3679
        self.add_cleanup(branch1.lock_read().unlock)
 
3680
        self.add_cleanup(branch2.lock_read().unlock)
3697
3681
        last1 = ensure_null(branch1.last_revision())
3698
3682
        last2 = ensure_null(branch2.last_revision())
3699
3683
 
3793
3777
                ' completely merged into the source, pull from the'
3794
3778
                ' source rather than merging.  When this happens,'
3795
3779
                ' you do not need to commit the result.'),
3796
 
        Option('directory',
 
3780
        custom_help('directory',
3797
3781
               help='Branch to merge into, '
3798
 
                    'rather than the one containing the working directory.',
3799
 
               short_name='d',
3800
 
               type=unicode,
3801
 
               ),
 
3782
                    'rather than the one containing the working directory.'),
3802
3783
        Option('preview', help='Instead of merging, show a diff of the'
3803
3784
               ' merge.'),
3804
3785
        Option('interactive', help='Select changes interactively.',
3837
3818
            unversioned_filter=tree.is_ignored, view_info=view_info)
3838
3819
        pb = ui.ui_factory.nested_progress_bar()
3839
3820
        self.add_cleanup(pb.finished)
3840
 
        tree.lock_write()
3841
 
        self.add_cleanup(tree.unlock)
 
3821
        self.add_cleanup(tree.lock_write().unlock)
3842
3822
        if location is not None:
3843
3823
            try:
3844
3824
                mergeable = bundle.read_mergeable_from_url(location,
4100
4080
        if merge_type is None:
4101
4081
            merge_type = _mod_merge.Merge3Merger
4102
4082
        tree, file_list = tree_files(file_list)
4103
 
        tree.lock_write()
4104
 
        self.add_cleanup(tree.unlock)
 
4083
        self.add_cleanup(tree.lock_write().unlock)
4105
4084
        parents = tree.get_parent_ids()
4106
4085
        if len(parents) != 2:
4107
4086
            raise errors.BzrCommandError("Sorry, remerge only works after normal"
4217
4196
    def run(self, revision=None, no_backup=False, file_list=None,
4218
4197
            forget_merges=None):
4219
4198
        tree, file_list = tree_files(file_list)
4220
 
        tree.lock_tree_write()
4221
 
        self.add_cleanup(tree.unlock)
 
4199
        self.add_cleanup(tree.lock_tree_write().unlock)
4222
4200
        if forget_merges:
4223
4201
            tree.set_parent_ids(tree.get_parent_ids()[:1])
4224
4202
        else:
4360
4338
            restrict = 'remote'
4361
4339
 
4362
4340
        local_branch = Branch.open_containing(u".")[0]
4363
 
        local_branch.lock_read()
4364
 
        self.add_cleanup(local_branch.unlock)
 
4341
        self.add_cleanup(local_branch.lock_read().unlock)
4365
4342
 
4366
4343
        parent = local_branch.get_parent()
4367
4344
        if other_branch is None:
4378
4355
        if remote_branch.base == local_branch.base:
4379
4356
            remote_branch = local_branch
4380
4357
        else:
4381
 
            remote_branch.lock_read()
4382
 
            self.add_cleanup(remote_branch.unlock)
 
4358
            self.add_cleanup(remote_branch.lock_read().unlock)
4383
4359
 
4384
4360
        local_revid_range = _revision_range_to_revid_range(
4385
4361
            _get_revision_range(my_revision, local_branch,
4440
4416
            message("Branches are up to date.\n")
4441
4417
        self.cleanup_now()
4442
4418
        if not status_code and parent is None and other_branch is not None:
4443
 
            local_branch.lock_write()
4444
 
            self.add_cleanup(local_branch.unlock)
 
4419
            self.add_cleanup(local_branch.lock_write().unlock)
4445
4420
            # handle race conditions - a parent might be set while we run.
4446
4421
            if local_branch.get_parent() is None:
4447
4422
                local_branch.set_parent(remote_branch.base)
4547
4522
            b = Branch.open_containing(branch)[0]
4548
4523
        else:
4549
4524
            b = Branch.open(branch)
4550
 
        b.lock_read()
4551
 
        self.add_cleanup(b.unlock)
 
4525
        self.add_cleanup(b.lock_read().unlock)
4552
4526
        if revision is None:
4553
4527
            rev_id = b.last_revision()
4554
4528
        else:
4578
4552
                     Option('long', help='Show commit date in annotations.'),
4579
4553
                     'revision',
4580
4554
                     'show-ids',
 
4555
                     'directory',
4581
4556
                     ]
4582
4557
    encoding_type = 'exact'
4583
4558
 
4584
4559
    @display_command
4585
4560
    def run(self, filename, all=False, long=False, revision=None,
4586
 
            show_ids=False):
 
4561
            show_ids=False, directory=None):
4587
4562
        from bzrlib.annotate import annotate_file, annotate_file_tree
4588
4563
        wt, branch, relpath = \
4589
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
4564
            _open_directory_or_containing_tree_or_branch(filename, directory)
4590
4565
        if wt is not None:
4591
 
            wt.lock_read()
4592
 
            self.add_cleanup(wt.unlock)
 
4566
            self.add_cleanup(wt.lock_read().unlock)
4593
4567
        else:
4594
 
            branch.lock_read()
4595
 
            self.add_cleanup(branch.unlock)
 
4568
            self.add_cleanup(branch.lock_read().unlock)
4596
4569
        tree = _get_one_revision_tree('annotate', revision, branch=branch)
4597
 
        tree.lock_read()
4598
 
        self.add_cleanup(tree.unlock)
 
4570
        self.add_cleanup(tree.lock_read().unlock)
4599
4571
        if wt is not None:
4600
4572
            file_id = wt.path2id(relpath)
4601
4573
        else:
4619
4591
 
4620
4592
    hidden = True # is this right ?
4621
4593
    takes_args = ['revision_id*']
4622
 
    takes_options = ['revision']
 
4594
    takes_options = ['directory', 'revision']
4623
4595
 
4624
 
    def run(self, revision_id_list=None, revision=None):
 
4596
    def run(self, revision_id_list=None, revision=None, directory=u'.'):
4625
4597
        if revision_id_list is not None and revision is not None:
4626
4598
            raise errors.BzrCommandError('You can only supply one of revision_id or --revision')
4627
4599
        if revision_id_list is None and revision is None:
4628
4600
            raise errors.BzrCommandError('You must supply either --revision or a revision_id')
4629
 
        b = WorkingTree.open_containing(u'.')[0].branch
4630
 
        b.lock_write()
4631
 
        self.add_cleanup(b.unlock)
 
4601
        b = WorkingTree.open_containing(directory)[0].branch
 
4602
        self.add_cleanup(b.lock_write().unlock)
4632
4603
        return self._run(b, revision_id_list, revision)
4633
4604
 
4634
4605
    def _run(self, b, revision_id_list, revision):
4693
4664
 
4694
4665
    _see_also = ['checkouts', 'unbind']
4695
4666
    takes_args = ['location?']
4696
 
    takes_options = []
 
4667
    takes_options = ['directory']
4697
4668
 
4698
 
    def run(self, location=None):
4699
 
        b, relpath = Branch.open_containing(u'.')
 
4669
    def run(self, location=None, directory=u'.'):
 
4670
        b, relpath = Branch.open_containing(directory)
4700
4671
        if location is None:
4701
4672
            try:
4702
4673
                location = b.get_old_bound_location()
4729
4700
 
4730
4701
    _see_also = ['checkouts', 'bind']
4731
4702
    takes_args = []
4732
 
    takes_options = []
 
4703
    takes_options = ['directory']
4733
4704
 
4734
 
    def run(self):
4735
 
        b, relpath = Branch.open_containing(u'.')
 
4705
    def run(self, directory=u'.'):
 
4706
        b, relpath = Branch.open_containing(directory)
4736
4707
        if not b.unbind():
4737
4708
            raise errors.BzrCommandError('Local branch is not bound')
4738
4709
 
4784
4755
            b = control.open_branch()
4785
4756
 
4786
4757
        if tree is not None:
4787
 
            tree.lock_write()
4788
 
            self.add_cleanup(tree.unlock)
 
4758
            self.add_cleanup(tree.lock_write().unlock)
4789
4759
        else:
4790
 
            b.lock_write()
4791
 
            self.add_cleanup(b.unlock)
 
4760
            self.add_cleanup(b.lock_write().unlock)
4792
4761
        return self._run(b, tree, dry_run, verbose, revision, force, local=local)
4793
4762
 
4794
4763
    def _run(self, b, tree, dry_run, verbose, revision, force, local=False):
4902
4871
                    'result in a dynamically allocated port.  The default port '
4903
4872
                    'depends on the protocol.',
4904
4873
               type=str),
4905
 
        Option('directory',
4906
 
               help='Serve contents of this directory.',
4907
 
               type=unicode),
 
4874
        custom_help('directory',
 
4875
               help='Serve contents of this directory.'),
4908
4876
        Option('allow-writes',
4909
4877
               help='By default the server is a readonly server.  Supplying '
4910
4878
                    '--allow-writes enables write access to the contents of '
5339
5307
        Option('delete',
5340
5308
            help='Delete this tag rather than placing it.',
5341
5309
            ),
5342
 
        Option('directory',
5343
 
            help='Branch in which to place the tag.',
5344
 
            short_name='d',
5345
 
            type=unicode,
5346
 
            ),
 
5310
        custom_help('directory',
 
5311
            help='Branch in which to place the tag.'),
5347
5312
        Option('force',
5348
5313
            help='Replace existing tags.',
5349
5314
            ),
5357
5322
            revision=None,
5358
5323
            ):
5359
5324
        branch, relpath = Branch.open_containing(directory)
5360
 
        branch.lock_write()
5361
 
        self.add_cleanup(branch.unlock)
 
5325
        self.add_cleanup(branch.lock_write().unlock)
5362
5326
        if delete:
5363
5327
            if tag_name is None:
5364
5328
                raise errors.BzrCommandError("No tag specified to delete.")
5392
5356
 
5393
5357
    _see_also = ['tag']
5394
5358
    takes_options = [
5395
 
        Option('directory',
5396
 
            help='Branch whose tags should be displayed.',
5397
 
            short_name='d',
5398
 
            type=unicode,
5399
 
            ),
 
5359
        custom_help('directory',
 
5360
            help='Branch whose tags should be displayed.'),
5400
5361
        RegistryOption.from_kwargs('sort',
5401
5362
            'Sort tags by different criteria.', title='Sorting',
5402
5363
            alpha='Sort tags lexicographically (default).',
5419
5380
        if not tags:
5420
5381
            return
5421
5382
 
5422
 
        branch.lock_read()
5423
 
        self.add_cleanup(branch.unlock)
 
5383
        self.add_cleanup(branch.lock_read().unlock)
5424
5384
        if revision:
5425
5385
            graph = branch.repository.get_graph()
5426
5386
            rev1, rev2 = _get_revision_range(revision, branch, self.name())
5904
5864
 
5905
5865
    def run_for_list(self):
5906
5866
        tree = WorkingTree.open_containing('.')[0]
5907
 
        tree.lock_read()
5908
 
        self.add_cleanup(tree.unlock)
 
5867
        self.add_cleanup(tree.lock_read().unlock)
5909
5868
        manager = tree.get_shelf_manager()
5910
5869
        shelves = manager.active_shelves()
5911
5870
        if len(shelves) == 0:
5966
5925
 
5967
5926
    To check what clean-tree will do, use --dry-run.
5968
5927
    """
5969
 
    takes_options = [Option('ignored', help='Delete all ignored files.'),
 
5928
    takes_options = ['directory',
 
5929
                     Option('ignored', help='Delete all ignored files.'),
5970
5930
                     Option('detritus', help='Delete conflict files, merge'
5971
5931
                            ' backups, and failed selftest dirs.'),
5972
5932
                     Option('unknown',
5975
5935
                            ' deleting them.'),
5976
5936
                     Option('force', help='Do not prompt before deleting.')]
5977
5937
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False,
5978
 
            force=False):
 
5938
            force=False, directory=u'.'):
5979
5939
        from bzrlib.clean_tree import clean_tree
5980
5940
        if not (unknown or ignored or detritus):
5981
5941
            unknown = True
5982
5942
        if dry_run:
5983
5943
            force = True
5984
 
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus,
5985
 
                   dry_run=dry_run, no_prompt=force)
 
5944
        clean_tree(directory, unknown=unknown, ignored=ignored,
 
5945
                   detritus=detritus, dry_run=dry_run, no_prompt=force)
5986
5946
 
5987
5947
 
5988
5948
class cmd_reference(Command):