/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2010-04-21 11:27:04 UTC
  • mto: This revision was merged to the branch mainline in revision 5189.
  • Revision ID: mbp@canonical.com-20100421112704-zijso22b6pdevrxy
Simplify various code to use user_url

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
50
50
from bzrlib.hooks import HookPoint, Hooks
51
51
from bzrlib.inter import InterObject
52
 
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
 
52
from bzrlib.lock import _RelockDebugMixin
53
53
from bzrlib import registry
54
54
from bzrlib.symbol_versioning import (
55
55
    deprecated_in,
283
283
        new_history.reverse()
284
284
        return new_history
285
285
 
286
 
    def lock_write(self, token=None):
287
 
        """Lock the branch for write operations.
288
 
 
289
 
        :param token: A token to permit reacquiring a previously held and
290
 
            preserved lock.
291
 
        :return: A BranchWriteLockResult.
292
 
        """
 
286
    def lock_write(self):
293
287
        raise NotImplementedError(self.lock_write)
294
288
 
295
289
    def lock_read(self):
296
 
        """Lock the branch for read operations.
297
 
 
298
 
        :return: A bzrlib.lock.LogicalLockResult.
299
 
        """
300
290
        raise NotImplementedError(self.lock_read)
301
291
 
302
292
    def unlock(self):
427
417
            * 'include' - the stop revision is the last item in the result
428
418
            * 'with-merges' - include the stop revision and all of its
429
419
              merged revisions in the result
430
 
            * 'with-merges-without-common-ancestry' - filter out revisions 
431
 
              that are in both ancestries
432
420
        :param direction: either 'reverse' or 'forward':
433
421
            * reverse means return the start_revision_id first, i.e.
434
422
              start at the most recent revision and go backwards in history
465
453
            stop_revision_id, stop_rule)
466
454
        # Make sure we don't return revisions that are not part of the
467
455
        # start_revision_id ancestry.
468
 
        filtered = self._filter_start_non_ancestors(filtered)
 
456
        filtered = self._filter_non_ancestors(filtered)
469
457
        if direction == 'reverse':
470
458
            return filtered
471
459
        if direction == 'forward':
508
496
                       node.end_of_merge)
509
497
                if rev_id == stop_revision_id:
510
498
                    return
511
 
        elif stop_rule == 'with-merges-without-common-ancestry':
512
 
            # We want to exclude all revisions that are already part of the
513
 
            # stop_revision_id ancestry.
514
 
            graph = self.repository.get_graph()
515
 
            ancestors = graph.find_unique_ancestors(start_revision_id,
516
 
                                                    [stop_revision_id])
517
 
            for node in rev_iter:
518
 
                rev_id = node.key[-1]
519
 
                if rev_id not in ancestors:
520
 
                    continue
521
 
                yield (rev_id, node.merge_depth, node.revno,
522
 
                       node.end_of_merge)
523
499
        elif stop_rule == 'with-merges':
524
500
            stop_rev = self.repository.get_revision(stop_revision_id)
525
501
            if stop_rev.parent_ids:
548
524
        else:
549
525
            raise ValueError('invalid stop_rule %r' % stop_rule)
550
526
 
551
 
    def _filter_start_non_ancestors(self, rev_iter):
 
527
    def _filter_non_ancestors(self, rev_iter):
552
528
        # If we started from a dotted revno, we want to consider it as a tip
553
529
        # and don't want to yield revisions that are not part of its
554
530
        # ancestry. Given the order guaranteed by the merge sort, we will see
645
621
        :param pb: An optional progress bar to use.
646
622
        :return: None
647
623
        """
648
 
        if self.base == from_branch.base:
 
624
        if self.control_url == from_branch.control_url:
649
625
            return (0, [])
650
626
        if pb is not None:
651
627
            symbol_versioning.warn(
804
780
                # stream from one of them to the other.  This does mean doing
805
781
                # separate SSH connection setup, but unstacking is not a
806
782
                # common operation so it's tolerable.
807
 
                new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
 
783
                new_bzrdir = bzrdir.BzrDir.open_from_transport(
 
784
                    self.bzrdir.user_transport)
808
785
                new_repository = new_bzrdir.find_repository()
809
786
                self.repository = new_repository
810
787
                if self.repository._fallback_repositories:
1235
1212
            if repository_policy is not None:
1236
1213
                repository_policy.configure_branch(result)
1237
1214
            self.copy_content_into(result, revision_id=revision_id)
1238
 
            result.set_parent(self.bzrdir.root_transport.base)
 
1215
            result.set_parent(self.bzrdir.user_url)
1239
1216
        finally:
1240
1217
            result.unlock()
1241
1218
        return result
1428
1405
        :return: A branch associated with the file_id
1429
1406
        """
1430
1407
        # FIXME should provide multiple branches, based on config
1431
 
        return Branch.open(self.bzrdir.root_transport.clone(path).base,
 
1408
        return Branch.open(self.user_transport.clone(path).base,
1432
1409
                           possible_transports=possible_transports)
1433
1410
 
1434
1411
    def supports_tags(self):
1564
1541
        """Return the short format description for this format."""
1565
1542
        raise NotImplementedError(self.get_format_description)
1566
1543
 
1567
 
    def _run_post_branch_init_hooks(self, a_bzrdir, name, branch):
1568
 
        hooks = Branch.hooks['post_branch_init']
1569
 
        if not hooks:
1570
 
            return
1571
 
        params = BranchInitHookParams(self, a_bzrdir, name, branch)
1572
 
        for hook in hooks:
1573
 
            hook(params)
1574
 
 
1575
1544
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1576
1545
                           lock_type='metadir', set_format=True):
1577
1546
        """Initialize a branch in a bzrdir, with specified files
1613
1582
        finally:
1614
1583
            if lock_taken:
1615
1584
                control_files.unlock()
1616
 
        branch = self.open(a_bzrdir, name, _found=True)
1617
 
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
1618
 
        return branch
 
1585
        return self.open(a_bzrdir, name, _found=True)
1619
1586
 
1620
1587
    def initialize(self, a_bzrdir, name=None):
1621
1588
        """Create a branch of this format in a_bzrdir.
1781
1748
            "should return a tag name or None if no tag name could be "
1782
1749
            "determined. The first non-None tag name returned will be used.",
1783
1750
            (2, 2), None))
1784
 
        self.create_hook(HookPoint('post_branch_init',
1785
 
            "Called after new branch initialization completes. "
1786
 
            "post_branch_init is called with a "
1787
 
            "bzrlib.branch.BranchInitHookParams. "
1788
 
            "Note that init, branch and checkout (both heavyweight and "
1789
 
            "lightweight) will all trigger this hook.", (2, 2), None))
1790
 
        self.create_hook(HookPoint('post_switch',
1791
 
            "Called after a checkout switches branch. "
1792
 
            "post_switch is called with a "
1793
 
            "bzrlib.branch.SwitchHookParams.", (2, 2), None))
1794
1751
 
1795
1752
 
1796
1753
 
1836
1793
            self.old_revno, self.old_revid, self.new_revno, self.new_revid)
1837
1794
 
1838
1795
 
1839
 
class BranchInitHookParams(object):
1840
 
    """Object holding parameters passed to *_branch_init hooks.
1841
 
 
1842
 
    There are 4 fields that hooks may wish to access:
1843
 
 
1844
 
    :ivar format: the branch format
1845
 
    :ivar bzrdir: the BzrDir where the branch will be/has been initialized
1846
 
    :ivar name: name of colocated branch, if any (or None)
1847
 
    :ivar branch: the branch created
1848
 
 
1849
 
    Note that for lightweight checkouts, the bzrdir and format fields refer to
1850
 
    the checkout, hence they are different from the corresponding fields in
1851
 
    branch, which refer to the original branch.
1852
 
    """
1853
 
 
1854
 
    def __init__(self, format, a_bzrdir, name, branch):
1855
 
        """Create a group of BranchInitHook parameters.
1856
 
 
1857
 
        :param format: the branch format
1858
 
        :param a_bzrdir: the BzrDir where the branch will be/has been
1859
 
            initialized
1860
 
        :param name: name of colocated branch, if any (or None)
1861
 
        :param branch: the branch created
1862
 
 
1863
 
        Note that for lightweight checkouts, the bzrdir and format fields refer
1864
 
        to the checkout, hence they are different from the corresponding fields
1865
 
        in branch, which refer to the original branch.
1866
 
        """
1867
 
        self.format = format
1868
 
        self.bzrdir = a_bzrdir
1869
 
        self.name = name
1870
 
        self.branch = branch
1871
 
 
1872
 
    def __eq__(self, other):
1873
 
        return self.__dict__ == other.__dict__
1874
 
 
1875
 
    def __repr__(self):
1876
 
        if self.branch:
1877
 
            return "<%s of %s>" % (self.__class__.__name__, self.branch)
1878
 
        else:
1879
 
            return "<%s of format:%s bzrdir:%s>" % (
1880
 
                self.__class__.__name__, self.branch,
1881
 
                self.format, self.bzrdir)
1882
 
 
1883
 
 
1884
 
class SwitchHookParams(object):
1885
 
    """Object holding parameters passed to *_switch hooks.
1886
 
 
1887
 
    There are 4 fields that hooks may wish to access:
1888
 
 
1889
 
    :ivar control_dir: BzrDir of the checkout to change
1890
 
    :ivar to_branch: branch that the checkout is to reference
1891
 
    :ivar force: skip the check for local commits in a heavy checkout
1892
 
    :ivar revision_id: revision ID to switch to (or None)
1893
 
    """
1894
 
 
1895
 
    def __init__(self, control_dir, to_branch, force, revision_id):
1896
 
        """Create a group of SwitchHook parameters.
1897
 
 
1898
 
        :param control_dir: BzrDir of the checkout to change
1899
 
        :param to_branch: branch that the checkout is to reference
1900
 
        :param force: skip the check for local commits in a heavy checkout
1901
 
        :param revision_id: revision ID to switch to (or None)
1902
 
        """
1903
 
        self.control_dir = control_dir
1904
 
        self.to_branch = to_branch
1905
 
        self.force = force
1906
 
        self.revision_id = revision_id
1907
 
 
1908
 
    def __eq__(self, other):
1909
 
        return self.__dict__ == other.__dict__
1910
 
 
1911
 
    def __repr__(self):
1912
 
        return "<%s for %s to (%s, %s)>" % (self.__class__.__name__,
1913
 
            self.control_dir, self.to_branch,
1914
 
            self.revision_id)
1915
 
 
1916
 
 
1917
1796
class BzrBranchFormat4(BranchFormat):
1918
1797
    """Bzr branch format 4.
1919
1798
 
2188
2067
        branch_transport.put_bytes('location',
2189
2068
            target_branch.bzrdir.user_url)
2190
2069
        branch_transport.put_bytes('format', self.get_format_string())
2191
 
        branch = self.open(
 
2070
        return self.open(
2192
2071
            a_bzrdir, name, _found=True,
2193
2072
            possible_transports=[target_branch.bzrdir.root_transport])
2194
 
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
2195
 
        return branch
2196
2073
 
2197
2074
    def __init__(self):
2198
2075
        super(BranchReferenceFormat, self).__init__()
2275
2152
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2276
2153
 
2277
2154
 
2278
 
class BranchWriteLockResult(LogicalLockResult):
2279
 
    """The result of write locking a branch.
2280
 
 
2281
 
    :ivar branch_token: The token obtained from the underlying branch lock, or
2282
 
        None.
2283
 
    :ivar unlock: A callable which will unlock the lock.
2284
 
    """
2285
 
 
2286
 
    def __init__(self, unlock, branch_token):
2287
 
        LogicalLockResult.__init__(self, unlock)
2288
 
        self.branch_token = branch_token
2289
 
 
2290
 
    def __repr__(self):
2291
 
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
2292
 
            self.unlock)
2293
 
 
2294
 
 
2295
2155
class BzrBranch(Branch, _RelockDebugMixin):
2296
2156
    """A branch stored in the actual filesystem.
2297
2157
 
2351
2211
        return self.control_files.is_locked()
2352
2212
 
2353
2213
    def lock_write(self, token=None):
2354
 
        """Lock the branch for write operations.
2355
 
 
2356
 
        :param token: A token to permit reacquiring a previously held and
2357
 
            preserved lock.
2358
 
        :return: A BranchWriteLockResult.
2359
 
        """
2360
2214
        if not self.is_locked():
2361
2215
            self._note_lock('w')
2362
2216
        # All-in-one needs to always unlock/lock.
2368
2222
        else:
2369
2223
            took_lock = False
2370
2224
        try:
2371
 
            return BranchWriteLockResult(self.unlock,
2372
 
                self.control_files.lock_write(token=token))
 
2225
            return self.control_files.lock_write(token=token)
2373
2226
        except:
2374
2227
            if took_lock:
2375
2228
                self.repository.unlock()
2376
2229
            raise
2377
2230
 
2378
2231
    def lock_read(self):
2379
 
        """Lock the branch for read operations.
2380
 
 
2381
 
        :return: A bzrlib.lock.LogicalLockResult.
2382
 
        """
2383
2232
        if not self.is_locked():
2384
2233
            self._note_lock('r')
2385
2234
        # All-in-one needs to always unlock/lock.
2392
2241
            took_lock = False
2393
2242
        try:
2394
2243
            self.control_files.lock_read()
2395
 
            return LogicalLockResult(self.unlock)
2396
2244
        except:
2397
2245
            if took_lock:
2398
2246
                self.repository.unlock()
2921
2769
        return stacked_url
2922
2770
 
2923
2771
    def _get_append_revisions_only(self):
2924
 
        return self.get_config(
2925
 
            ).get_user_option_as_bool('append_revisions_only')
 
2772
        value = self.get_config().get_user_option('append_revisions_only')
 
2773
        return value == 'True'
2926
2774
 
2927
2775
    @needs_write_lock
2928
2776
    def generate_revision_history(self, revision_id, last_rev=None,