~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Parth Malwankar
  • Date: 2010-05-05 14:11:13 UTC
  • mfrom: (5211 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5213.
  • Revision ID: parth.malwankar@gmail.com-20100505141113-c21oicoxzb3u6if6
merged in changes from trunk.

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
 
                self.add_cleanup(wt.lock_read().unlock)
 
539
                wt.lock_read()
532
540
            except (errors.NoWorkingTree, errors.NotLocalUrl):
533
541
                raise errors.NoWorkingTree(location)
 
542
            self.add_cleanup(wt.unlock)
534
543
            revid = wt.last_revision()
535
544
            try:
536
545
                revno_t = wt.branch.revision_id_to_dotted_revno(revid)
539
548
            revno = ".".join(str(n) for n in revno_t)
540
549
        else:
541
550
            b = Branch.open_containing(location)[0]
542
 
            self.add_cleanup(b.lock_read().unlock)
 
551
            b.lock_read()
 
552
            self.add_cleanup(b.unlock)
543
553
            revno = b.revno()
544
554
        self.cleanup_now()
545
555
        self.outf.write(str(revno) + '\n')
552
562
    takes_args = ['revision_info*']
553
563
    takes_options = [
554
564
        'revision',
555
 
        Option('directory',
 
565
        custom_help('directory',
556
566
            help='Branch to examine, '
557
 
                 'rather than the one containing the working directory.',
558
 
            short_name='d',
559
 
            type=unicode,
560
 
            ),
 
567
                 'rather than the one containing the working directory.'),
561
568
        Option('tree', help='Show revno of working tree'),
562
569
        ]
563
570
 
568
575
        try:
569
576
            wt = WorkingTree.open_containing(directory)[0]
570
577
            b = wt.branch
571
 
            self.add_cleanup(wt.lock_read().unlock)
 
578
            wt.lock_read()
 
579
            self.add_cleanup(wt.unlock)
572
580
        except (errors.NoWorkingTree, errors.NotLocalUrl):
573
581
            wt = None
574
582
            b = Branch.open_containing(directory)[0]
575
 
            self.add_cleanup(b.lock_read().unlock)
 
583
            b.lock_read()
 
584
            self.add_cleanup(b.unlock)
576
585
        revision_ids = []
577
586
        if revision is not None:
578
587
            revision_ids.extend(rev.as_revision_id(b) for rev in revision)
677
686
                should_print=(not is_quiet()))
678
687
 
679
688
        if base_tree:
680
 
            self.add_cleanup(base_tree.lock_read().unlock)
 
689
            base_tree.lock_read()
 
690
            self.add_cleanup(base_tree.unlock)
681
691
        tree, file_list = tree_files_for_add(file_list)
682
692
        added, ignored = tree.smart_add(file_list, not
683
693
            no_recurse, action=action, save=not dry_run)
755
765
 
756
766
        revision = _get_one_revision('inventory', revision)
757
767
        work_tree, file_list = tree_files(file_list)
758
 
        self.add_cleanup(work_tree.lock_read().unlock)
 
768
        work_tree.lock_read()
 
769
        self.add_cleanup(work_tree.unlock)
759
770
        if revision is not None:
760
771
            tree = revision.as_tree(work_tree.branch)
761
772
 
762
773
            extra_trees = [work_tree]
763
 
            self.add_cleanup(tree.lock_read().unlock)
 
774
            tree.lock_read()
 
775
            self.add_cleanup(tree.unlock)
764
776
        else:
765
777
            tree = work_tree
766
778
            extra_trees = []
826
838
        if len(names_list) < 2:
827
839
            raise errors.BzrCommandError("missing file argument")
828
840
        tree, rel_names = tree_files(names_list, canonicalize=False)
829
 
        self.add_cleanup(tree.lock_tree_write().unlock)
 
841
        tree.lock_tree_write()
 
842
        self.add_cleanup(tree.unlock)
830
843
        self._run(tree, names_list, rel_names, after)
831
844
 
832
845
    def run_auto(self, names_list, after, dry_run):
837
850
            raise errors.BzrCommandError('--after cannot be specified with'
838
851
                                         ' --auto.')
839
852
        work_tree, file_list = tree_files(names_list, default_branch='.')
840
 
        self.add_cleanup(work_tree.lock_tree_write().unlock)
 
853
        work_tree.lock_tree_write()
 
854
        self.add_cleanup(work_tree.unlock)
841
855
        rename_map.RenameMap.guess_renames(work_tree, dry_run)
842
856
 
843
857
    def _run(self, tree, names_list, rel_names, after):
951
965
    takes_options = ['remember', 'overwrite', 'revision',
952
966
        custom_help('verbose',
953
967
            help='Show logs of pulled revisions.'),
954
 
        Option('directory',
 
968
        custom_help('directory',
955
969
            help='Branch to pull into, '
956
 
                 'rather than the one containing the working directory.',
957
 
            short_name='d',
958
 
            type=unicode,
959
 
            ),
 
970
                 'rather than the one containing the working directory.'),
960
971
        Option('local',
961
972
            help="Perform a local pull in a bound "
962
973
                 "branch.  Local pulls are not applied to "
977
988
        try:
978
989
            tree_to = WorkingTree.open_containing(directory)[0]
979
990
            branch_to = tree_to.branch
980
 
            self.add_cleanup(tree_to.lock_write().unlock)
 
991
            tree_to.lock_write()
 
992
            self.add_cleanup(tree_to.unlock)
981
993
        except errors.NoWorkingTree:
982
994
            tree_to = None
983
995
            branch_to = Branch.open_containing(directory)[0]
984
 
            self.add_cleanup(branch_to.lock_write().unlock)
 
996
            branch_to.lock_write()
 
997
            self.add_cleanup(branch_to.unlock)
985
998
 
986
999
        if local and not branch_to.get_bound_location():
987
1000
            raise errors.LocalRequiresBoundBranch()
1018
1031
        else:
1019
1032
            branch_from = Branch.open(location,
1020
1033
                possible_transports=possible_transports)
1021
 
            self.add_cleanup(branch_from.lock_read().unlock)
 
1034
            branch_from.lock_read()
 
1035
            self.add_cleanup(branch_from.unlock)
1022
1036
 
1023
1037
            if branch_to.get_parent() is None or remember:
1024
1038
                branch_to.set_parent(branch_from.base)
1076
1090
        Option('create-prefix',
1077
1091
               help='Create the path leading up to the branch '
1078
1092
                    'if it does not already exist.'),
1079
 
        Option('directory',
 
1093
        custom_help('directory',
1080
1094
            help='Branch to push from, '
1081
 
                 'rather than the one containing the working directory.',
1082
 
            short_name='d',
1083
 
            type=unicode,
1084
 
            ),
 
1095
                 'rather than the one containing the working directory.'),
1085
1096
        Option('use-existing-dir',
1086
1097
               help='By default push will fail if the target'
1087
1098
                    ' directory exists, but does not already'
1207
1218
        accelerator_tree, br_from = bzrdir.BzrDir.open_tree_or_branch(
1208
1219
            from_location)
1209
1220
        revision = _get_one_revision('branch', revision)
1210
 
        self.add_cleanup(br_from.lock_read().unlock)
 
1221
        br_from.lock_read()
 
1222
        self.add_cleanup(br_from.unlock)
1211
1223
        if revision is not None:
1212
1224
            revision_id = revision.as_revision_id(br_from)
1213
1225
        else:
1337
1349
            except errors.NoWorkingTree:
1338
1350
                source.bzrdir.create_workingtree(revision_id)
1339
1351
                return
 
1352
 
 
1353
        if not lightweight:
 
1354
            message = ('Copying history to "%s". '
 
1355
                'To checkout without local history use --lightweight.' % to_location)
 
1356
            ui.ui_factory.show_message(message)
1340
1357
        source.create_checkout(to_location, revision_id, lightweight,
1341
1358
                               accelerator_tree, hardlink)
1342
1359
 
1353
1370
    @display_command
1354
1371
    def run(self, dir=u'.'):
1355
1372
        tree = WorkingTree.open_containing(dir)[0]
1356
 
        self.add_cleanup(tree.lock_read().unlock)
 
1373
        tree.lock_read()
 
1374
        self.add_cleanup(tree.unlock)
1357
1375
        new_inv = tree.inventory
1358
1376
        old_tree = tree.basis_tree()
1359
 
        self.add_cleanup(old_tree.lock_read().unlock)
 
1377
        old_tree.lock_read()
 
1378
        self.add_cleanup(old_tree.unlock)
1360
1379
        old_inv = old_tree.inventory
1361
1380
        renames = []
1362
1381
        iterator = tree.iter_changes(old_tree, include_unchanged=True)
1400
1419
        master = branch.get_master_branch(
1401
1420
            possible_transports=possible_transports)
1402
1421
        if master is not None:
 
1422
            tree.lock_write()
1403
1423
            branch_location = master.base
1404
 
            tree.lock_write()
1405
1424
        else:
 
1425
            tree.lock_tree_write()
1406
1426
            branch_location = tree.branch.base
1407
 
            tree.lock_tree_write()
1408
1427
        self.add_cleanup(tree.unlock)
1409
1428
        # get rid of the final '/' and be ready for display
1410
1429
        branch_location = urlutils.unescape_for_display(
1530
1549
        if file_list is not None:
1531
1550
            file_list = [f for f in file_list]
1532
1551
 
1533
 
        self.add_cleanup(tree.lock_write().unlock)
 
1552
        tree.lock_write()
 
1553
        self.add_cleanup(tree.unlock)
1534
1554
        # Heuristics should probably all move into tree.remove_smart or
1535
1555
        # some such?
1536
1556
        if new:
1987
2007
    # level of effort but possibly much less IO.  (Or possibly not,
1988
2008
    # if the directories are very large...)
1989
2009
    _see_also = ['status', 'ls']
1990
 
    takes_options = ['show-ids']
 
2010
    takes_options = ['directory', 'show-ids']
1991
2011
 
1992
2012
    @display_command
1993
 
    def run(self, show_ids=False):
1994
 
        tree = WorkingTree.open_containing(u'.')[0]
1995
 
        self.add_cleanup(tree.lock_read().unlock)
 
2013
    def run(self, show_ids=False, directory=u'.'):
 
2014
        tree = WorkingTree.open_containing(directory)[0]
 
2015
        tree.lock_read()
 
2016
        self.add_cleanup(tree.unlock)
1996
2017
        old = tree.basis_tree()
1997
 
        self.add_cleanup(old.lock_read().unlock)
 
2018
        old.lock_read()
 
2019
        self.add_cleanup(old.unlock)
1998
2020
        for path, ie in old.inventory.iter_entries():
1999
2021
            if not tree.has_id(ie.file_id):
2000
2022
                self.outf.write(path)
2010
2032
 
2011
2033
    hidden = True
2012
2034
    _see_also = ['status', 'ls']
2013
 
    takes_options = [
 
2035
    takes_options = ['directory',
2014
2036
            Option('null',
2015
2037
                   help='Write an ascii NUL (\\0) separator '
2016
2038
                   'between files rather than a newline.')
2017
2039
            ]
2018
2040
 
2019
2041
    @display_command
2020
 
    def run(self, null=False):
2021
 
        tree = WorkingTree.open_containing(u'.')[0]
 
2042
    def run(self, null=False, directory=u'.'):
 
2043
        tree = WorkingTree.open_containing(directory)[0]
2022
2044
        td = tree.changes_from(tree.basis_tree())
2023
2045
        for path, id, kind, text_modified, meta_modified in td.modified:
2024
2046
            if null:
2033
2055
 
2034
2056
    hidden = True
2035
2057
    _see_also = ['status', 'ls']
2036
 
    takes_options = [
 
2058
    takes_options = ['directory',
2037
2059
            Option('null',
2038
2060
                   help='Write an ascii NUL (\\0) separator '
2039
2061
                   'between files rather than a newline.')
2040
2062
            ]
2041
2063
 
2042
2064
    @display_command
2043
 
    def run(self, null=False):
2044
 
        wt = WorkingTree.open_containing(u'.')[0]
2045
 
        self.add_cleanup(wt.lock_read().unlock)
 
2065
    def run(self, null=False, directory=u'.'):
 
2066
        wt = WorkingTree.open_containing(directory)[0]
 
2067
        wt.lock_read()
 
2068
        self.add_cleanup(wt.unlock)
2046
2069
        basis = wt.basis_tree()
2047
 
        self.add_cleanup(basis.lock_read().unlock)
 
2070
        basis.lock_read()
 
2071
        self.add_cleanup(basis.unlock)
2048
2072
        basis_inv = basis.inventory
2049
2073
        inv = wt.inventory
2050
2074
        for file_id in inv:
2053
2077
            if inv.is_root(file_id) and len(basis_inv) == 0:
2054
2078
                continue
2055
2079
            path = inv.id2path(file_id)
2056
 
            if not os.access(osutils.abspath(path), os.F_OK):
 
2080
            if not os.access(osutils.pathjoin(wt.basedir, path), os.F_OK):
2057
2081
                continue
2058
2082
            if null:
2059
2083
                self.outf.write(path + '\0')
2259
2283
                   help='Show just the specified revision.'
2260
2284
                   ' See also "help revisionspec".'),
2261
2285
            'log-format',
 
2286
            RegistryOption('authors',
 
2287
                'What names to list as authors - first, all or committer.',
 
2288
                title='Authors',
 
2289
                lazy_registry=('bzrlib.log', 'author_list_registry'),
 
2290
            ),
2262
2291
            Option('levels',
2263
2292
                   short_name='n',
2264
2293
                   help='Number of levels to display - 0 for all, 1 for flat.',
2299
2328
            limit=None,
2300
2329
            show_diff=False,
2301
2330
            include_merges=False,
 
2331
            authors=None,
2302
2332
            exclude_common_ancestry=False,
2303
2333
            ):
2304
2334
        from bzrlib.log import (
2332
2362
        if file_list:
2333
2363
            # find the file ids to log and check for directory filtering
2334
2364
            b, file_info_list, rev1, rev2 = _get_info_for_log_files(
2335
 
                revision, file_list, self.add_cleanup)
 
2365
                revision, file_list)
 
2366
            self.add_cleanup(b.unlock)
2336
2367
            for relpath, file_id, kind in file_info_list:
2337
2368
                if file_id is None:
2338
2369
                    raise errors.BzrCommandError(
2356
2387
                location = '.'
2357
2388
            dir, relpath = bzrdir.BzrDir.open_containing(location)
2358
2389
            b = dir.open_branch()
2359
 
            self.add_cleanup(b.lock_read().unlock)
 
2390
            b.lock_read()
 
2391
            self.add_cleanup(b.unlock)
2360
2392
            rev1, rev2 = _get_revision_range(revision, b, self.name())
2361
2393
 
2362
2394
        # Decide on the type of delta & diff filtering to use
2382
2414
                        show_timezone=timezone,
2383
2415
                        delta_format=get_verbosity_level(),
2384
2416
                        levels=levels,
2385
 
                        show_advice=levels is None)
 
2417
                        show_advice=levels is None,
 
2418
                        author_list_handler=authors)
2386
2419
 
2387
2420
        # Choose the algorithm for doing the logging. It's annoying
2388
2421
        # having multiple code paths like this but necessary until
2486
2519
        tree, relpath = WorkingTree.open_containing(filename)
2487
2520
        file_id = tree.path2id(relpath)
2488
2521
        b = tree.branch
2489
 
        self.add_cleanup(b.lock_read().unlock)
 
2522
        b.lock_read()
 
2523
        self.add_cleanup(b.unlock)
2490
2524
        touching_revs = log.find_touching_revisions(b, file_id)
2491
2525
        for revno, revision_id, what in touching_revs:
2492
2526
            self.outf.write("%6d %s\n" % (revno, what))
2516
2550
                   help='List entries of a particular kind: file, directory, symlink.',
2517
2551
                   type=unicode),
2518
2552
            'show-ids',
 
2553
            'directory',
2519
2554
            ]
2520
2555
    @display_command
2521
2556
    def run(self, revision=None, verbose=False,
2522
2557
            recursive=False, from_root=False,
2523
2558
            unknown=False, versioned=False, ignored=False,
2524
 
            null=False, kind=None, show_ids=False, path=None):
 
2559
            null=False, kind=None, show_ids=False, path=None, directory=None):
2525
2560
 
2526
2561
        if kind and kind not in ('file', 'directory', 'symlink'):
2527
2562
            raise errors.BzrCommandError('invalid kind specified')
2539
2574
                raise errors.BzrCommandError('cannot specify both --from-root'
2540
2575
                                             ' and PATH')
2541
2576
            fs_path = path
2542
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
2543
 
            fs_path)
 
2577
        tree, branch, relpath = \
 
2578
            _open_directory_or_containing_tree_or_branch(fs_path, directory)
2544
2579
 
2545
2580
        # Calculate the prefix to use
2546
2581
        prefix = None
2561
2596
                view_str = views.view_display_str(view_files)
2562
2597
                note("Ignoring files outside view. View is %s" % view_str)
2563
2598
 
2564
 
        self.add_cleanup(tree.lock_read().unlock)
 
2599
        tree.lock_read()
 
2600
        self.add_cleanup(tree.unlock)
2565
2601
        for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
2566
2602
            from_dir=relpath, recursive=recursive):
2567
2603
            # Apply additional masking
2614
2650
 
2615
2651
    hidden = True
2616
2652
    _see_also = ['ls']
 
2653
    takes_options = ['directory']
2617
2654
 
2618
2655
    @display_command
2619
 
    def run(self):
2620
 
        for f in WorkingTree.open_containing(u'.')[0].unknowns():
 
2656
    def run(self, directory=u'.'):
 
2657
        for f in WorkingTree.open_containing(directory)[0].unknowns():
2621
2658
            self.outf.write(osutils.quotefn(f) + '\n')
2622
2659
 
2623
2660
 
2688
2725
 
2689
2726
    _see_also = ['status', 'ignored', 'patterns']
2690
2727
    takes_args = ['name_pattern*']
2691
 
    takes_options = [
 
2728
    takes_options = ['directory',
2692
2729
        Option('default-rules',
2693
2730
               help='Display the default ignore rules that bzr uses.')
2694
2731
        ]
2695
2732
 
2696
 
    def run(self, name_pattern_list=None, default_rules=None):
 
2733
    def run(self, name_pattern_list=None, default_rules=None,
 
2734
            directory=u'.'):
2697
2735
        from bzrlib import ignores
2698
2736
        if default_rules is not None:
2699
2737
            # dump the default rules and exit
2710
2748
                (len(name_pattern) > 1 and name_pattern[1] == ':')):
2711
2749
                raise errors.BzrCommandError(
2712
2750
                    "NAME_PATTERN should not be an absolute path")
2713
 
        tree, relpath = WorkingTree.open_containing(u'.')
 
2751
        tree, relpath = WorkingTree.open_containing(directory)
2714
2752
        ignores.tree_ignores_add_patterns(tree, name_pattern_list)
2715
2753
        ignored = globbing.Globster(name_pattern_list)
2716
2754
        matches = []
2742
2780
 
2743
2781
    encoding_type = 'replace'
2744
2782
    _see_also = ['ignore', 'ls']
 
2783
    takes_options = ['directory']
2745
2784
 
2746
2785
    @display_command
2747
 
    def run(self):
2748
 
        tree = WorkingTree.open_containing(u'.')[0]
2749
 
        self.add_cleanup(tree.lock_read().unlock)
 
2786
    def run(self, directory=u'.'):
 
2787
        tree = WorkingTree.open_containing(directory)[0]
 
2788
        tree.lock_read()
 
2789
        self.add_cleanup(tree.unlock)
2750
2790
        for path, file_class, kind, file_id, entry in tree.list_files():
2751
2791
            if file_class != 'I':
2752
2792
                continue
2763
2803
    """
2764
2804
    hidden = True
2765
2805
    takes_args = ['revno']
 
2806
    takes_options = ['directory']
2766
2807
 
2767
2808
    @display_command
2768
 
    def run(self, revno):
 
2809
    def run(self, revno, directory=u'.'):
2769
2810
        try:
2770
2811
            revno = int(revno)
2771
2812
        except ValueError:
2772
2813
            raise errors.BzrCommandError("not a valid revision-number: %r"
2773
2814
                                         % revno)
2774
 
        revid = WorkingTree.open_containing(u'.')[0].branch.get_rev_id(revno)
 
2815
        revid = WorkingTree.open_containing(directory)[0].branch.get_rev_id(revno)
2775
2816
        self.outf.write("%s\n" % revid)
2776
2817
 
2777
2818
 
2804
2845
      =================       =========================
2805
2846
    """
2806
2847
    takes_args = ['dest', 'branch_or_subdir?']
2807
 
    takes_options = [
 
2848
    takes_options = ['directory',
2808
2849
        Option('format',
2809
2850
               help="Type of file to export to.",
2810
2851
               type=unicode),
2819
2860
                    'revision in which it was changed.'),
2820
2861
        ]
2821
2862
    def run(self, dest, branch_or_subdir=None, revision=None, format=None,
2822
 
        root=None, filters=False, per_file_timestamps=False):
 
2863
        root=None, filters=False, per_file_timestamps=False, directory=u'.'):
2823
2864
        from bzrlib.export import export
2824
2865
 
2825
2866
        if branch_or_subdir is None:
2826
 
            tree = WorkingTree.open_containing(u'.')[0]
 
2867
            tree = WorkingTree.open_containing(directory)[0]
2827
2868
            b = tree.branch
2828
2869
            subdir = None
2829
2870
        else:
2848
2889
    """
2849
2890
 
2850
2891
    _see_also = ['ls']
2851
 
    takes_options = [
 
2892
    takes_options = ['directory',
2852
2893
        Option('name-from-revision', help='The path name in the old tree.'),
2853
2894
        Option('filters', help='Apply content filters to display the '
2854
2895
                'convenience form.'),
2859
2900
 
2860
2901
    @display_command
2861
2902
    def run(self, filename, revision=None, name_from_revision=False,
2862
 
            filters=False):
 
2903
            filters=False, directory=None):
2863
2904
        if revision is not None and len(revision) != 1:
2864
2905
            raise errors.BzrCommandError("bzr cat --revision takes exactly"
2865
2906
                                         " one revision specifier")
2866
2907
        tree, branch, relpath = \
2867
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
2868
 
        self.add_cleanup(branch.lock_read().unlock)
 
2908
            _open_directory_or_containing_tree_or_branch(filename, directory)
 
2909
        branch.lock_read()
 
2910
        self.add_cleanup(branch.unlock)
2869
2911
        return self._run(tree, branch, relpath, filename, revision,
2870
2912
                         name_from_revision, filters)
2871
2913
 
2874
2916
        if tree is None:
2875
2917
            tree = b.basis_tree()
2876
2918
        rev_tree = _get_one_revision_tree('cat', revision, branch=b)
2877
 
        self.add_cleanup(rev_tree.lock_read().unlock)
 
2919
        rev_tree.lock_read()
 
2920
        self.add_cleanup(rev_tree.unlock)
2878
2921
 
2879
2922
        old_file_id = rev_tree.path2id(relpath)
2880
2923
 
3353
3396
 
3354
3397
    _see_also = ['info']
3355
3398
    takes_args = ['nickname?']
3356
 
    def run(self, nickname=None):
3357
 
        branch = Branch.open_containing(u'.')[0]
 
3399
    takes_options = ['directory']
 
3400
    def run(self, nickname=None, directory=u'.'):
 
3401
        branch = Branch.open_containing(directory)[0]
3358
3402
        if nickname is None:
3359
3403
            self.printme(branch)
3360
3404
        else:
3663
3707
 
3664
3708
        branch1 = Branch.open_containing(branch)[0]
3665
3709
        branch2 = Branch.open_containing(other)[0]
3666
 
        self.add_cleanup(branch1.lock_read().unlock)
3667
 
        self.add_cleanup(branch2.lock_read().unlock)
 
3710
        branch1.lock_read()
 
3711
        self.add_cleanup(branch1.unlock)
 
3712
        branch2.lock_read()
 
3713
        self.add_cleanup(branch2.unlock)
3668
3714
        last1 = ensure_null(branch1.last_revision())
3669
3715
        last2 = ensure_null(branch2.last_revision())
3670
3716
 
3764
3810
                ' completely merged into the source, pull from the'
3765
3811
                ' source rather than merging.  When this happens,'
3766
3812
                ' you do not need to commit the result.'),
3767
 
        Option('directory',
 
3813
        custom_help('directory',
3768
3814
               help='Branch to merge into, '
3769
 
                    'rather than the one containing the working directory.',
3770
 
               short_name='d',
3771
 
               type=unicode,
3772
 
               ),
 
3815
                    'rather than the one containing the working directory.'),
3773
3816
        Option('preview', help='Instead of merging, show a diff of the'
3774
3817
               ' merge.'),
3775
3818
        Option('interactive', help='Select changes interactively.',
3808
3851
            unversioned_filter=tree.is_ignored, view_info=view_info)
3809
3852
        pb = ui.ui_factory.nested_progress_bar()
3810
3853
        self.add_cleanup(pb.finished)
3811
 
        self.add_cleanup(tree.lock_write().unlock)
 
3854
        tree.lock_write()
 
3855
        self.add_cleanup(tree.unlock)
3812
3856
        if location is not None:
3813
3857
            try:
3814
3858
                mergeable = bundle.read_mergeable_from_url(location,
4070
4114
        if merge_type is None:
4071
4115
            merge_type = _mod_merge.Merge3Merger
4072
4116
        tree, file_list = tree_files(file_list)
4073
 
        self.add_cleanup(tree.lock_write().unlock)
 
4117
        tree.lock_write()
 
4118
        self.add_cleanup(tree.unlock)
4074
4119
        parents = tree.get_parent_ids()
4075
4120
        if len(parents) != 2:
4076
4121
            raise errors.BzrCommandError("Sorry, remerge only works after normal"
4186
4231
    def run(self, revision=None, no_backup=False, file_list=None,
4187
4232
            forget_merges=None):
4188
4233
        tree, file_list = tree_files(file_list)
4189
 
        self.add_cleanup(tree.lock_tree_write().unlock)
 
4234
        tree.lock_tree_write()
 
4235
        self.add_cleanup(tree.unlock)
4190
4236
        if forget_merges:
4191
4237
            tree.set_parent_ids(tree.get_parent_ids()[:1])
4192
4238
        else:
4328
4374
            restrict = 'remote'
4329
4375
 
4330
4376
        local_branch = Branch.open_containing(u".")[0]
4331
 
        self.add_cleanup(local_branch.lock_read().unlock)
 
4377
        local_branch.lock_read()
 
4378
        self.add_cleanup(local_branch.unlock)
4332
4379
 
4333
4380
        parent = local_branch.get_parent()
4334
4381
        if other_branch is None:
4345
4392
        if remote_branch.base == local_branch.base:
4346
4393
            remote_branch = local_branch
4347
4394
        else:
4348
 
            self.add_cleanup(remote_branch.lock_read().unlock)
 
4395
            remote_branch.lock_read()
 
4396
            self.add_cleanup(remote_branch.unlock)
4349
4397
 
4350
4398
        local_revid_range = _revision_range_to_revid_range(
4351
4399
            _get_revision_range(my_revision, local_branch,
4406
4454
            message("Branches are up to date.\n")
4407
4455
        self.cleanup_now()
4408
4456
        if not status_code and parent is None and other_branch is not None:
4409
 
            self.add_cleanup(local_branch.lock_write().unlock)
 
4457
            local_branch.lock_write()
 
4458
            self.add_cleanup(local_branch.unlock)
4410
4459
            # handle race conditions - a parent might be set while we run.
4411
4460
            if local_branch.get_parent() is None:
4412
4461
                local_branch.set_parent(remote_branch.base)
4512
4561
            b = Branch.open_containing(branch)[0]
4513
4562
        else:
4514
4563
            b = Branch.open(branch)
4515
 
        self.add_cleanup(b.lock_read().unlock)
 
4564
        b.lock_read()
 
4565
        self.add_cleanup(b.unlock)
4516
4566
        if revision is None:
4517
4567
            rev_id = b.last_revision()
4518
4568
        else:
4542
4592
                     Option('long', help='Show commit date in annotations.'),
4543
4593
                     'revision',
4544
4594
                     'show-ids',
 
4595
                     'directory',
4545
4596
                     ]
4546
4597
    encoding_type = 'exact'
4547
4598
 
4548
4599
    @display_command
4549
4600
    def run(self, filename, all=False, long=False, revision=None,
4550
 
            show_ids=False):
 
4601
            show_ids=False, directory=None):
4551
4602
        from bzrlib.annotate import annotate_file, annotate_file_tree
4552
4603
        wt, branch, relpath = \
4553
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
4604
            _open_directory_or_containing_tree_or_branch(filename, directory)
4554
4605
        if wt is not None:
4555
 
            self.add_cleanup(wt.lock_read().unlock)
 
4606
            wt.lock_read()
 
4607
            self.add_cleanup(wt.unlock)
4556
4608
        else:
4557
 
            self.add_cleanup(branch.lock_read().unlock)
 
4609
            branch.lock_read()
 
4610
            self.add_cleanup(branch.unlock)
4558
4611
        tree = _get_one_revision_tree('annotate', revision, branch=branch)
4559
 
        self.add_cleanup(tree.lock_read().unlock)
 
4612
        tree.lock_read()
 
4613
        self.add_cleanup(tree.unlock)
4560
4614
        if wt is not None:
4561
4615
            file_id = wt.path2id(relpath)
4562
4616
        else:
4580
4634
 
4581
4635
    hidden = True # is this right ?
4582
4636
    takes_args = ['revision_id*']
4583
 
    takes_options = ['revision']
 
4637
    takes_options = ['directory', 'revision']
4584
4638
 
4585
 
    def run(self, revision_id_list=None, revision=None):
 
4639
    def run(self, revision_id_list=None, revision=None, directory=u'.'):
4586
4640
        if revision_id_list is not None and revision is not None:
4587
4641
            raise errors.BzrCommandError('You can only supply one of revision_id or --revision')
4588
4642
        if revision_id_list is None and revision is None:
4589
4643
            raise errors.BzrCommandError('You must supply either --revision or a revision_id')
4590
 
        b = WorkingTree.open_containing(u'.')[0].branch
4591
 
        self.add_cleanup(b.lock_write().unlock)
 
4644
        b = WorkingTree.open_containing(directory)[0].branch
 
4645
        b.lock_write()
 
4646
        self.add_cleanup(b.unlock)
4592
4647
        return self._run(b, revision_id_list, revision)
4593
4648
 
4594
4649
    def _run(self, b, revision_id_list, revision):
4653
4708
 
4654
4709
    _see_also = ['checkouts', 'unbind']
4655
4710
    takes_args = ['location?']
4656
 
    takes_options = []
 
4711
    takes_options = ['directory']
4657
4712
 
4658
 
    def run(self, location=None):
4659
 
        b, relpath = Branch.open_containing(u'.')
 
4713
    def run(self, location=None, directory=u'.'):
 
4714
        b, relpath = Branch.open_containing(directory)
4660
4715
        if location is None:
4661
4716
            try:
4662
4717
                location = b.get_old_bound_location()
4689
4744
 
4690
4745
    _see_also = ['checkouts', 'bind']
4691
4746
    takes_args = []
4692
 
    takes_options = []
 
4747
    takes_options = ['directory']
4693
4748
 
4694
 
    def run(self):
4695
 
        b, relpath = Branch.open_containing(u'.')
 
4749
    def run(self, directory=u'.'):
 
4750
        b, relpath = Branch.open_containing(directory)
4696
4751
        if not b.unbind():
4697
4752
            raise errors.BzrCommandError('Local branch is not bound')
4698
4753
 
4744
4799
            b = control.open_branch()
4745
4800
 
4746
4801
        if tree is not None:
4747
 
            self.add_cleanup(tree.lock_write().unlock)
 
4802
            tree.lock_write()
 
4803
            self.add_cleanup(tree.unlock)
4748
4804
        else:
4749
 
            self.add_cleanup(b.lock_write().unlock)
 
4805
            b.lock_write()
 
4806
            self.add_cleanup(b.unlock)
4750
4807
        return self._run(b, tree, dry_run, verbose, revision, force, local=local)
4751
4808
 
4752
4809
    def _run(self, b, tree, dry_run, verbose, revision, force, local=False):
4860
4917
                    'result in a dynamically allocated port.  The default port '
4861
4918
                    'depends on the protocol.',
4862
4919
               type=str),
4863
 
        Option('directory',
4864
 
               help='Serve contents of this directory.',
4865
 
               type=unicode),
 
4920
        custom_help('directory',
 
4921
               help='Serve contents of this directory.'),
4866
4922
        Option('allow-writes',
4867
4923
               help='By default the server is a readonly server.  Supplying '
4868
4924
                    '--allow-writes enables write access to the contents of '
5297
5353
        Option('delete',
5298
5354
            help='Delete this tag rather than placing it.',
5299
5355
            ),
5300
 
        Option('directory',
5301
 
            help='Branch in which to place the tag.',
5302
 
            short_name='d',
5303
 
            type=unicode,
5304
 
            ),
 
5356
        custom_help('directory',
 
5357
            help='Branch in which to place the tag.'),
5305
5358
        Option('force',
5306
5359
            help='Replace existing tags.',
5307
5360
            ),
5315
5368
            revision=None,
5316
5369
            ):
5317
5370
        branch, relpath = Branch.open_containing(directory)
5318
 
        self.add_cleanup(branch.lock_write().unlock)
 
5371
        branch.lock_write()
 
5372
        self.add_cleanup(branch.unlock)
5319
5373
        if delete:
5320
5374
            if tag_name is None:
5321
5375
                raise errors.BzrCommandError("No tag specified to delete.")
5349
5403
 
5350
5404
    _see_also = ['tag']
5351
5405
    takes_options = [
5352
 
        Option('directory',
5353
 
            help='Branch whose tags should be displayed.',
5354
 
            short_name='d',
5355
 
            type=unicode,
5356
 
            ),
 
5406
        custom_help('directory',
 
5407
            help='Branch whose tags should be displayed.'),
5357
5408
        RegistryOption.from_kwargs('sort',
5358
5409
            'Sort tags by different criteria.', title='Sorting',
5359
5410
            alpha='Sort tags lexicographically (default).',
5376
5427
        if not tags:
5377
5428
            return
5378
5429
 
5379
 
        self.add_cleanup(branch.lock_read().unlock)
 
5430
        branch.lock_read()
 
5431
        self.add_cleanup(branch.unlock)
5380
5432
        if revision:
5381
5433
            graph = branch.repository.get_graph()
5382
5434
            rev1, rev2 = _get_revision_range(revision, branch, self.name())
5860
5912
 
5861
5913
    def run_for_list(self):
5862
5914
        tree = WorkingTree.open_containing('.')[0]
5863
 
        self.add_cleanup(tree.lock_read().unlock)
 
5915
        tree.lock_read()
 
5916
        self.add_cleanup(tree.unlock)
5864
5917
        manager = tree.get_shelf_manager()
5865
5918
        shelves = manager.active_shelves()
5866
5919
        if len(shelves) == 0:
5921
5974
 
5922
5975
    To check what clean-tree will do, use --dry-run.
5923
5976
    """
5924
 
    takes_options = [Option('ignored', help='Delete all ignored files.'),
 
5977
    takes_options = ['directory',
 
5978
                     Option('ignored', help='Delete all ignored files.'),
5925
5979
                     Option('detritus', help='Delete conflict files, merge'
5926
5980
                            ' backups, and failed selftest dirs.'),
5927
5981
                     Option('unknown',
5930
5984
                            ' deleting them.'),
5931
5985
                     Option('force', help='Do not prompt before deleting.')]
5932
5986
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False,
5933
 
            force=False):
 
5987
            force=False, directory=u'.'):
5934
5988
        from bzrlib.clean_tree import clean_tree
5935
5989
        if not (unknown or ignored or detritus):
5936
5990
            unknown = True
5937
5991
        if dry_run:
5938
5992
            force = True
5939
 
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus,
5940
 
                   dry_run=dry_run, no_prompt=force)
 
5993
        clean_tree(directory, unknown=unknown, ignored=ignored,
 
5994
                   detritus=detritus, dry_run=dry_run, no_prompt=force)
5941
5995
 
5942
5996
 
5943
5997
class cmd_reference(Command):