~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

merge in lalos branch tidyup

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
     splitpath, \
25
25
     sha_file, appendpath, file_kind
26
26
 
27
 
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
28
 
import bzrlib.errors
 
27
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId, \
 
28
     DivergedBranches, NotBranchError
29
29
from bzrlib.textui import show_status
30
30
from bzrlib.revision import Revision
31
31
from bzrlib.delta import compare_trees
49
49
 
50
50
def find_branch(f, **args):
51
51
    if f and (f.startswith('http://') or f.startswith('https://')):
52
 
        import remotebranch 
53
 
        return remotebranch.RemoteBranch(f, **args)
 
52
        from bzrlib.remotebranch import RemoteBranch
 
53
        return RemoteBranch(f, **args)
54
54
    else:
55
55
        return Branch(f, **args)
56
56
 
57
57
 
58
58
def find_cached_branch(f, cache_root, **args):
59
 
    from remotebranch import RemoteBranch
 
59
    from bzrlib.remotebranch import RemoteBranch
60
60
    br = find_branch(f, **args)
61
61
    def cacheify(br, store_name):
62
 
        from meta_store import CachedStore
 
62
        from bzrlib.meta_store import CachedStore
63
63
        cache_path = os.path.join(cache_root, store_name)
64
64
        os.mkdir(cache_path)
65
65
        new_store = CachedStore(getattr(br, store_name), cache_path)
94
94
        if tail:
95
95
            s.insert(0, tail)
96
96
    else:
97
 
        from errors import NotBranchError
98
97
        raise NotBranchError("path %r is not within branch %r" % (rp, base))
99
98
 
100
99
    return os.sep.join(s)
128
127
        head, tail = os.path.split(f)
129
128
        if head == f:
130
129
            # reached the root, whatever that may be
131
 
            raise bzrlib.errors.NotBranchError('%s is not in a branch' % orig_f)
 
130
            raise NotBranchError('%s is not in a branch' % orig_f)
132
131
        f = head
133
132
 
134
133
 
135
134
 
136
 
# XXX: move into bzrlib.errors; subclass BzrError    
137
 
class DivergedBranches(Exception):
138
 
    def __init__(self, branch1, branch2):
139
 
        self.branch1 = branch1
140
 
        self.branch2 = branch2
141
 
        Exception.__init__(self, "These branches have diverged.")
142
 
 
143
135
 
144
136
######################################################################
145
137
# branch objects
194
186
        else:
195
187
            self.base = os.path.realpath(base)
196
188
            if not isdir(self.controlfilename('.')):
197
 
                from errors import NotBranchError
198
189
                raise NotBranchError("not a bzr branch: %s" % quotefn(base),
199
190
                                     ['use "bzr init" to initialize a new working tree',
200
191
                                      'current bzr can only operate from top-of-tree'])
214
205
 
215
206
    def __del__(self):
216
207
        if self._lock_mode or self._lock:
217
 
            from warnings import warn
 
208
            from bzrlib.warnings import warn
218
209
            warn("branch %r was not explicitly unlocked" % self)
219
210
            self._lock.unlock()
220
211
 
222
213
    def lock_write(self):
223
214
        if self._lock_mode:
224
215
            if self._lock_mode != 'w':
225
 
                from errors import LockError
 
216
                from bzrlib.errors import LockError
226
217
                raise LockError("can't upgrade to a write lock from %r" %
227
218
                                self._lock_mode)
228
219
            self._lock_count += 1
248
239
                        
249
240
    def unlock(self):
250
241
        if not self._lock_mode:
251
 
            from errors import LockError
 
242
            from bzrlib.errors import LockError
252
243
            raise LockError('branch %r is not locked' % (self))
253
244
 
254
245
        if self._lock_count > 1:
695
686
 
696
687
    def common_ancestor(self, other, self_revno=None, other_revno=None):
697
688
        """
698
 
        >>> import commit
 
689
        >>> from bzrlib.commit import commit
699
690
        >>> sb = ScratchBranch(files=['foo', 'foo~'])
700
691
        >>> sb.common_ancestor(sb) == (None, None)
701
692
        True
702
 
        >>> commit.commit(sb, "Committing first revision", verbose=False)
 
693
        >>> commit(sb, "Committing first revision", verbose=False)
703
694
        >>> sb.common_ancestor(sb)[0]
704
695
        1
705
696
        >>> clone = sb.clone()
706
 
        >>> commit.commit(sb, "Committing second revision", verbose=False)
 
697
        >>> commit(sb, "Committing second revision", verbose=False)
707
698
        >>> sb.common_ancestor(sb)[0]
708
699
        2
709
700
        >>> sb.common_ancestor(clone)[0]
710
701
        1
711
 
        >>> commit.commit(clone, "Committing divergent second revision", 
 
702
        >>> commit(clone, "Committing divergent second revision", 
712
703
        ...               verbose=False)
713
704
        >>> sb.common_ancestor(clone)[0]
714
705
        1
1094
1085
 
1095
1086
    def working_tree(self):
1096
1087
        """Return a `Tree` for the working copy."""
1097
 
        from workingtree import WorkingTree
 
1088
        from bzrlib.workingtree import WorkingTree
1098
1089
        return WorkingTree(self.base, self.read_working_inventory())
1099
1090
 
1100
1091
 
1491
1482
    return gen_file_id('TREE_ROOT')
1492
1483
 
1493
1484
 
1494
 
def pull_loc(branch):
1495
 
    # TODO: Should perhaps just make attribute be 'base' in
1496
 
    # RemoteBranch and Branch?
1497
 
    if hasattr(branch, "baseurl"):
1498
 
        return branch.baseurl
1499
 
    else:
1500
 
        return branch.base
1501
 
 
1502
 
 
1503
1485
def copy_branch(branch_from, to_location, revision=None):
1504
1486
    """Copy branch_from into the existing directory to_location.
1505
1487
 
1511
1493
        The name of a local directory that exists but is empty.
1512
1494
    """
1513
1495
    from bzrlib.merge import merge
1514
 
    from bzrlib.branch import Branch
1515
1496
 
1516
1497
    assert isinstance(branch_from, Branch)
1517
1498
    assert isinstance(to_location, basestring)
1526
1507
    merge((to_location, -1), (to_location, 0), this_dir=to_location,
1527
1508
          check_clean=False, ignore_zero=True)
1528
1509
    
1529
 
    from_location = pull_loc(branch_from)
1530
 
    br_to.set_parent(pull_loc(branch_from))
 
1510
    from_location = branch_from.base
 
1511
    br_to.set_parent(branch_from.base)
1531
1512