~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-11 11:47:36 UTC
  • mfrom: (5200.3.8 lock_return)
  • Revision ID: pqm@pqm.ubuntu.com-20100511114736-mc1sq9zyo3vufec7
(lifeless) Provide a consistent interface to Tree, Branch,
 Repository where lock methods return an object with an unlock method to
 unlock the lock. This breaks the API for Branch,
 Repository on their lock_write methods. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
536
536
        if tree:
537
537
            try:
538
538
                wt = WorkingTree.open_containing(location)[0]
539
 
                wt.lock_read()
 
539
                self.add_cleanup(wt.lock_read().unlock)
540
540
            except (errors.NoWorkingTree, errors.NotLocalUrl):
541
541
                raise errors.NoWorkingTree(location)
542
 
            self.add_cleanup(wt.unlock)
543
542
            revid = wt.last_revision()
544
543
            try:
545
544
                revno_t = wt.branch.revision_id_to_dotted_revno(revid)
548
547
            revno = ".".join(str(n) for n in revno_t)
549
548
        else:
550
549
            b = Branch.open_containing(location)[0]
551
 
            b.lock_read()
552
 
            self.add_cleanup(b.unlock)
 
550
            self.add_cleanup(b.lock_read().unlock)
553
551
            revno = b.revno()
554
552
        self.cleanup_now()
555
553
        self.outf.write(str(revno) + '\n')
575
573
        try:
576
574
            wt = WorkingTree.open_containing(directory)[0]
577
575
            b = wt.branch
578
 
            wt.lock_read()
579
 
            self.add_cleanup(wt.unlock)
 
576
            self.add_cleanup(wt.lock_read().unlock)
580
577
        except (errors.NoWorkingTree, errors.NotLocalUrl):
581
578
            wt = None
582
579
            b = Branch.open_containing(directory)[0]
583
 
            b.lock_read()
584
 
            self.add_cleanup(b.unlock)
 
580
            self.add_cleanup(b.lock_read().unlock)
585
581
        revision_ids = []
586
582
        if revision is not None:
587
583
            revision_ids.extend(rev.as_revision_id(b) for rev in revision)
686
682
                should_print=(not is_quiet()))
687
683
 
688
684
        if base_tree:
689
 
            base_tree.lock_read()
690
 
            self.add_cleanup(base_tree.unlock)
 
685
            self.add_cleanup(base_tree.lock_read().unlock)
691
686
        tree, file_list = tree_files_for_add(file_list)
692
687
        added, ignored = tree.smart_add(file_list, not
693
688
            no_recurse, action=action, save=not dry_run)
765
760
 
766
761
        revision = _get_one_revision('inventory', revision)
767
762
        work_tree, file_list = tree_files(file_list)
768
 
        work_tree.lock_read()
769
 
        self.add_cleanup(work_tree.unlock)
 
763
        self.add_cleanup(work_tree.lock_read().unlock)
770
764
        if revision is not None:
771
765
            tree = revision.as_tree(work_tree.branch)
772
766
 
773
767
            extra_trees = [work_tree]
774
 
            tree.lock_read()
775
 
            self.add_cleanup(tree.unlock)
 
768
            self.add_cleanup(tree.lock_read().unlock)
776
769
        else:
777
770
            tree = work_tree
778
771
            extra_trees = []
838
831
        if len(names_list) < 2:
839
832
            raise errors.BzrCommandError("missing file argument")
840
833
        tree, rel_names = tree_files(names_list, canonicalize=False)
841
 
        tree.lock_tree_write()
842
 
        self.add_cleanup(tree.unlock)
 
834
        self.add_cleanup(tree.lock_tree_write().unlock)
843
835
        self._run(tree, names_list, rel_names, after)
844
836
 
845
837
    def run_auto(self, names_list, after, dry_run):
850
842
            raise errors.BzrCommandError('--after cannot be specified with'
851
843
                                         ' --auto.')
852
844
        work_tree, file_list = tree_files(names_list, default_branch='.')
853
 
        work_tree.lock_tree_write()
854
 
        self.add_cleanup(work_tree.unlock)
 
845
        self.add_cleanup(work_tree.lock_tree_write().unlock)
855
846
        rename_map.RenameMap.guess_renames(work_tree, dry_run)
856
847
 
857
848
    def _run(self, tree, names_list, rel_names, after):
988
979
        try:
989
980
            tree_to = WorkingTree.open_containing(directory)[0]
990
981
            branch_to = tree_to.branch
991
 
            tree_to.lock_write()
992
 
            self.add_cleanup(tree_to.unlock)
 
982
            self.add_cleanup(tree_to.lock_write().unlock)
993
983
        except errors.NoWorkingTree:
994
984
            tree_to = None
995
985
            branch_to = Branch.open_containing(directory)[0]
996
 
            branch_to.lock_write()
997
 
            self.add_cleanup(branch_to.unlock)
 
986
            self.add_cleanup(branch_to.lock_write().unlock)
998
987
 
999
988
        if local and not branch_to.get_bound_location():
1000
989
            raise errors.LocalRequiresBoundBranch()
1031
1020
        else:
1032
1021
            branch_from = Branch.open(location,
1033
1022
                possible_transports=possible_transports)
1034
 
            branch_from.lock_read()
1035
 
            self.add_cleanup(branch_from.unlock)
 
1023
            self.add_cleanup(branch_from.lock_read().unlock)
1036
1024
 
1037
1025
            if branch_to.get_parent() is None or remember:
1038
1026
                branch_to.set_parent(branch_from.base)
1218
1206
        accelerator_tree, br_from = bzrdir.BzrDir.open_tree_or_branch(
1219
1207
            from_location)
1220
1208
        revision = _get_one_revision('branch', revision)
1221
 
        br_from.lock_read()
1222
 
        self.add_cleanup(br_from.unlock)
 
1209
        self.add_cleanup(br_from.lock_read().unlock)
1223
1210
        if revision is not None:
1224
1211
            revision_id = revision.as_revision_id(br_from)
1225
1212
        else:
1370
1357
    @display_command
1371
1358
    def run(self, dir=u'.'):
1372
1359
        tree = WorkingTree.open_containing(dir)[0]
1373
 
        tree.lock_read()
1374
 
        self.add_cleanup(tree.unlock)
 
1360
        self.add_cleanup(tree.lock_read().unlock)
1375
1361
        new_inv = tree.inventory
1376
1362
        old_tree = tree.basis_tree()
1377
 
        old_tree.lock_read()
1378
 
        self.add_cleanup(old_tree.unlock)
 
1363
        self.add_cleanup(old_tree.lock_read().unlock)
1379
1364
        old_inv = old_tree.inventory
1380
1365
        renames = []
1381
1366
        iterator = tree.iter_changes(old_tree, include_unchanged=True)
1419
1404
        master = branch.get_master_branch(
1420
1405
            possible_transports=possible_transports)
1421
1406
        if master is not None:
1422
 
            tree.lock_write()
1423
1407
            branch_location = master.base
 
1408
            tree.lock_write()
1424
1409
        else:
 
1410
            branch_location = tree.branch.base
1425
1411
            tree.lock_tree_write()
1426
 
            branch_location = tree.branch.base
1427
1412
        self.add_cleanup(tree.unlock)
1428
1413
        # get rid of the final '/' and be ready for display
1429
1414
        branch_location = urlutils.unescape_for_display(
1549
1534
        if file_list is not None:
1550
1535
            file_list = [f for f in file_list]
1551
1536
 
1552
 
        tree.lock_write()
1553
 
        self.add_cleanup(tree.unlock)
 
1537
        self.add_cleanup(tree.lock_write().unlock)
1554
1538
        # Heuristics should probably all move into tree.remove_smart or
1555
1539
        # some such?
1556
1540
        if new:
2012
1996
    @display_command
2013
1997
    def run(self, show_ids=False, directory=u'.'):
2014
1998
        tree = WorkingTree.open_containing(directory)[0]
2015
 
        tree.lock_read()
2016
 
        self.add_cleanup(tree.unlock)
 
1999
        self.add_cleanup(tree.lock_read().unlock)
2017
2000
        old = tree.basis_tree()
2018
 
        old.lock_read()
2019
 
        self.add_cleanup(old.unlock)
 
2001
        self.add_cleanup(old.lock_read().unlock)
2020
2002
        for path, ie in old.inventory.iter_entries():
2021
2003
            if not tree.has_id(ie.file_id):
2022
2004
                self.outf.write(path)
2056
2038
    @display_command
2057
2039
    def run(self, null=False, directory=u'.'):
2058
2040
        wt = WorkingTree.open_containing(directory)[0]
2059
 
        wt.lock_read()
2060
 
        self.add_cleanup(wt.unlock)
 
2041
        self.add_cleanup(wt.lock_read().unlock)
2061
2042
        basis = wt.basis_tree()
2062
 
        basis.lock_read()
2063
 
        self.add_cleanup(basis.unlock)
 
2043
        self.add_cleanup(basis.lock_read().unlock)
2064
2044
        basis_inv = basis.inventory
2065
2045
        inv = wt.inventory
2066
2046
        for file_id in inv:
2354
2334
        if file_list:
2355
2335
            # find the file ids to log and check for directory filtering
2356
2336
            b, file_info_list, rev1, rev2 = _get_info_for_log_files(
2357
 
                revision, file_list)
2358
 
            self.add_cleanup(b.unlock)
 
2337
                revision, file_list, self.add_cleanup)
2359
2338
            for relpath, file_id, kind in file_info_list:
2360
2339
                if file_id is None:
2361
2340
                    raise errors.BzrCommandError(
2379
2358
                location = '.'
2380
2359
            dir, relpath = bzrdir.BzrDir.open_containing(location)
2381
2360
            b = dir.open_branch()
2382
 
            b.lock_read()
2383
 
            self.add_cleanup(b.unlock)
 
2361
            self.add_cleanup(b.lock_read().unlock)
2384
2362
            rev1, rev2 = _get_revision_range(revision, b, self.name())
2385
2363
 
2386
2364
        # Decide on the type of delta & diff filtering to use
2511
2489
        tree, relpath = WorkingTree.open_containing(filename)
2512
2490
        file_id = tree.path2id(relpath)
2513
2491
        b = tree.branch
2514
 
        b.lock_read()
2515
 
        self.add_cleanup(b.unlock)
 
2492
        self.add_cleanup(b.lock_read().unlock)
2516
2493
        touching_revs = log.find_touching_revisions(b, file_id)
2517
2494
        for revno, revision_id, what in touching_revs:
2518
2495
            self.outf.write("%6d %s\n" % (revno, what))
2588
2565
                view_str = views.view_display_str(view_files)
2589
2566
                note("Ignoring files outside view. View is %s" % view_str)
2590
2567
 
2591
 
        tree.lock_read()
2592
 
        self.add_cleanup(tree.unlock)
 
2568
        self.add_cleanup(tree.lock_read().unlock)
2593
2569
        for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
2594
2570
            from_dir=relpath, recursive=recursive):
2595
2571
            # Apply additional masking
2777
2753
    @display_command
2778
2754
    def run(self, directory=u'.'):
2779
2755
        tree = WorkingTree.open_containing(directory)[0]
2780
 
        tree.lock_read()
2781
 
        self.add_cleanup(tree.unlock)
 
2756
        self.add_cleanup(tree.lock_read().unlock)
2782
2757
        for path, file_class, kind, file_id, entry in tree.list_files():
2783
2758
            if file_class != 'I':
2784
2759
                continue
2898
2873
                                         " one revision specifier")
2899
2874
        tree, branch, relpath = \
2900
2875
            _open_directory_or_containing_tree_or_branch(filename, directory)
2901
 
        branch.lock_read()
2902
 
        self.add_cleanup(branch.unlock)
 
2876
        self.add_cleanup(branch.lock_read().unlock)
2903
2877
        return self._run(tree, branch, relpath, filename, revision,
2904
2878
                         name_from_revision, filters)
2905
2879
 
2908
2882
        if tree is None:
2909
2883
            tree = b.basis_tree()
2910
2884
        rev_tree = _get_one_revision_tree('cat', revision, branch=b)
2911
 
        rev_tree.lock_read()
2912
 
        self.add_cleanup(rev_tree.unlock)
 
2885
        self.add_cleanup(rev_tree.lock_read().unlock)
2913
2886
 
2914
2887
        old_file_id = rev_tree.path2id(relpath)
2915
2888
 
3699
3672
 
3700
3673
        branch1 = Branch.open_containing(branch)[0]
3701
3674
        branch2 = Branch.open_containing(other)[0]
3702
 
        branch1.lock_read()
3703
 
        self.add_cleanup(branch1.unlock)
3704
 
        branch2.lock_read()
3705
 
        self.add_cleanup(branch2.unlock)
 
3675
        self.add_cleanup(branch1.lock_read().unlock)
 
3676
        self.add_cleanup(branch2.lock_read().unlock)
3706
3677
        last1 = ensure_null(branch1.last_revision())
3707
3678
        last2 = ensure_null(branch2.last_revision())
3708
3679
 
3843
3814
            unversioned_filter=tree.is_ignored, view_info=view_info)
3844
3815
        pb = ui.ui_factory.nested_progress_bar()
3845
3816
        self.add_cleanup(pb.finished)
3846
 
        tree.lock_write()
3847
 
        self.add_cleanup(tree.unlock)
 
3817
        self.add_cleanup(tree.lock_write().unlock)
3848
3818
        if location is not None:
3849
3819
            try:
3850
3820
                mergeable = bundle.read_mergeable_from_url(location,
4106
4076
        if merge_type is None:
4107
4077
            merge_type = _mod_merge.Merge3Merger
4108
4078
        tree, file_list = tree_files(file_list)
4109
 
        tree.lock_write()
4110
 
        self.add_cleanup(tree.unlock)
 
4079
        self.add_cleanup(tree.lock_write().unlock)
4111
4080
        parents = tree.get_parent_ids()
4112
4081
        if len(parents) != 2:
4113
4082
            raise errors.BzrCommandError("Sorry, remerge only works after normal"
4223
4192
    def run(self, revision=None, no_backup=False, file_list=None,
4224
4193
            forget_merges=None):
4225
4194
        tree, file_list = tree_files(file_list)
4226
 
        tree.lock_tree_write()
4227
 
        self.add_cleanup(tree.unlock)
 
4195
        self.add_cleanup(tree.lock_tree_write().unlock)
4228
4196
        if forget_merges:
4229
4197
            tree.set_parent_ids(tree.get_parent_ids()[:1])
4230
4198
        else:
4366
4334
            restrict = 'remote'
4367
4335
 
4368
4336
        local_branch = Branch.open_containing(u".")[0]
4369
 
        local_branch.lock_read()
4370
 
        self.add_cleanup(local_branch.unlock)
 
4337
        self.add_cleanup(local_branch.lock_read().unlock)
4371
4338
 
4372
4339
        parent = local_branch.get_parent()
4373
4340
        if other_branch is None:
4384
4351
        if remote_branch.base == local_branch.base:
4385
4352
            remote_branch = local_branch
4386
4353
        else:
4387
 
            remote_branch.lock_read()
4388
 
            self.add_cleanup(remote_branch.unlock)
 
4354
            self.add_cleanup(remote_branch.lock_read().unlock)
4389
4355
 
4390
4356
        local_revid_range = _revision_range_to_revid_range(
4391
4357
            _get_revision_range(my_revision, local_branch,
4446
4412
            message("Branches are up to date.\n")
4447
4413
        self.cleanup_now()
4448
4414
        if not status_code and parent is None and other_branch is not None:
4449
 
            local_branch.lock_write()
4450
 
            self.add_cleanup(local_branch.unlock)
 
4415
            self.add_cleanup(local_branch.lock_write().unlock)
4451
4416
            # handle race conditions - a parent might be set while we run.
4452
4417
            if local_branch.get_parent() is None:
4453
4418
                local_branch.set_parent(remote_branch.base)
4553
4518
            b = Branch.open_containing(branch)[0]
4554
4519
        else:
4555
4520
            b = Branch.open(branch)
4556
 
        b.lock_read()
4557
 
        self.add_cleanup(b.unlock)
 
4521
        self.add_cleanup(b.lock_read().unlock)
4558
4522
        if revision is None:
4559
4523
            rev_id = b.last_revision()
4560
4524
        else:
4595
4559
        wt, branch, relpath = \
4596
4560
            _open_directory_or_containing_tree_or_branch(filename, directory)
4597
4561
        if wt is not None:
4598
 
            wt.lock_read()
4599
 
            self.add_cleanup(wt.unlock)
 
4562
            self.add_cleanup(wt.lock_read().unlock)
4600
4563
        else:
4601
 
            branch.lock_read()
4602
 
            self.add_cleanup(branch.unlock)
 
4564
            self.add_cleanup(branch.lock_read().unlock)
4603
4565
        tree = _get_one_revision_tree('annotate', revision, branch=branch)
4604
 
        tree.lock_read()
4605
 
        self.add_cleanup(tree.unlock)
 
4566
        self.add_cleanup(tree.lock_read().unlock)
4606
4567
        if wt is not None:
4607
4568
            file_id = wt.path2id(relpath)
4608
4569
        else:
4634
4595
        if revision_id_list is None and revision is None:
4635
4596
            raise errors.BzrCommandError('You must supply either --revision or a revision_id')
4636
4597
        b = WorkingTree.open_containing(directory)[0].branch
4637
 
        b.lock_write()
4638
 
        self.add_cleanup(b.unlock)
 
4598
        self.add_cleanup(b.lock_write().unlock)
4639
4599
        return self._run(b, revision_id_list, revision)
4640
4600
 
4641
4601
    def _run(self, b, revision_id_list, revision):
4791
4751
            b = control.open_branch()
4792
4752
 
4793
4753
        if tree is not None:
4794
 
            tree.lock_write()
4795
 
            self.add_cleanup(tree.unlock)
 
4754
            self.add_cleanup(tree.lock_write().unlock)
4796
4755
        else:
4797
 
            b.lock_write()
4798
 
            self.add_cleanup(b.unlock)
 
4756
            self.add_cleanup(b.lock_write().unlock)
4799
4757
        return self._run(b, tree, dry_run, verbose, revision, force, local=local)
4800
4758
 
4801
4759
    def _run(self, b, tree, dry_run, verbose, revision, force, local=False):
5360
5318
            revision=None,
5361
5319
            ):
5362
5320
        branch, relpath = Branch.open_containing(directory)
5363
 
        branch.lock_write()
5364
 
        self.add_cleanup(branch.unlock)
 
5321
        self.add_cleanup(branch.lock_write().unlock)
5365
5322
        if delete:
5366
5323
            if tag_name is None:
5367
5324
                raise errors.BzrCommandError("No tag specified to delete.")
5419
5376
        if not tags:
5420
5377
            return
5421
5378
 
5422
 
        branch.lock_read()
5423
 
        self.add_cleanup(branch.unlock)
 
5379
        self.add_cleanup(branch.lock_read().unlock)
5424
5380
        if revision:
5425
5381
            graph = branch.repository.get_graph()
5426
5382
            rev1, rev2 = _get_revision_range(revision, branch, self.name())
5904
5860
 
5905
5861
    def run_for_list(self):
5906
5862
        tree = WorkingTree.open_containing('.')[0]
5907
 
        tree.lock_read()
5908
 
        self.add_cleanup(tree.unlock)
 
5863
        self.add_cleanup(tree.lock_read().unlock)
5909
5864
        manager = tree.get_shelf_manager()
5910
5865
        shelves = manager.active_shelves()
5911
5866
        if len(shelves) == 0: