533
576
result.set_parent(self.bzrdir.root_transport.base)
537
def copy_content_into(self, destination, revision_id=None):
538
"""Copy the content of self into destination.
540
revision_id: if not None, the revision history in the new branch will
541
be truncated to end with revision_id.
579
def _synchronize_history(self, destination, revision_id):
580
"""Synchronize last revision and revision history between branches.
582
This version is most efficient when the destination is also a
583
BzrBranch5, but works for BzrBranch6 as long as the revision
584
history is the true lefthand parent history, and all of the revisions
585
are in the destination's repository. If not, set_revision_history
588
:param destination: The branch to copy the history into
589
:param revision_id: The revision-id to truncate history at. May
590
be None to copy complete history.
543
592
new_history = self.revision_history()
544
593
if revision_id is not None:
594
revision_id = osutils.safe_revision_id(revision_id)
546
596
new_history = new_history[:new_history.index(revision_id) + 1]
547
597
except ValueError:
548
598
rev = self.repository.get_revision(revision_id)
549
599
new_history = rev.get_history(self.repository)[1:]
550
600
destination.set_revision_history(new_history)
603
def copy_content_into(self, destination, revision_id=None):
604
"""Copy the content of self into destination.
606
revision_id: if not None, the revision history in the new branch will
607
be truncated to end with revision_id.
609
self._synchronize_history(destination, revision_id)
552
611
parent = self.get_parent()
553
612
except errors.InaccessibleParent, e:
676
739
def get_format_description(self):
677
740
"""Return the short format description for this format."""
678
raise NotImplementedError(self.get_format_string)
741
raise NotImplementedError(self.get_format_description)
743
def _initialize_helper(self, a_bzrdir, utf8_files, lock_type='metadir',
745
"""Initialize a branch in a bzrdir, with specified files
747
:param a_bzrdir: The bzrdir to initialize the branch in
748
:param utf8_files: The files to create as a list of
749
(filename, content) tuples
750
:param set_format: If True, set the format with
751
self.get_format_string. (BzrBranch4 has its format set
753
:return: a branch in this format
755
mutter('creating branch %r in %s', self, a_bzrdir.transport.base)
756
branch_transport = a_bzrdir.get_branch_transport(self)
758
'metadir': ('lock', lockdir.LockDir),
759
'branch4': ('branch-lock', lockable_files.TransportLock),
761
lock_name, lock_class = lock_map[lock_type]
762
control_files = lockable_files.LockableFiles(branch_transport,
763
lock_name, lock_class)
764
control_files.create_lock()
765
control_files.lock_write()
767
control_files.put_utf8('format', self.get_format_string())
769
for file, content in utf8_files:
770
control_files.put_utf8(file, content)
772
control_files.unlock()
773
return self.open(a_bzrdir, _found=True)
680
775
def initialize(self, a_bzrdir):
681
776
"""Create a branch of this format in a_bzrdir."""
714
809
def __str__(self):
715
810
return self.get_format_string().rstrip()
812
def supports_tags(self):
813
"""True if this format supports tags stored in the branch"""
814
return False # by default
816
# XXX: Probably doesn't really belong here -- mbp 20070212
817
def _initialize_control_files(self, a_bzrdir, utf8_files, lock_filename,
819
branch_transport = a_bzrdir.get_branch_transport(self)
820
control_files = lockable_files.LockableFiles(branch_transport,
821
lock_filename, lock_class)
822
control_files.create_lock()
823
control_files.lock_write()
825
for filename, content in utf8_files:
826
control_files.put_utf8(filename, content)
828
control_files.unlock()
831
class BranchHooks(dict):
832
"""A dictionary mapping hook name to a list of callables for branch hooks.
834
e.g. ['set_rh'] Is the list of items to be called when the
835
set_revision_history function is invoked.
839
"""Create the default hooks.
841
These are all empty initially, because by default nothing should get
845
# Introduced in 0.15:
846
# invoked whenever the revision history has been set
847
# with set_revision_history. The api signature is
848
# (branch, revision_history), and the branch will
851
# invoked after a push operation completes.
852
# the api signature is
854
# containing the members
855
# (source, local, master, old_revno, old_revid, new_revno, new_revid)
856
# where local is the local branch or None, master is the target
857
# master branch, and the rest should be self explanatory. The source
858
# is read locked and the target branches write locked. Source will
859
# be the local low-latency branch.
860
self['post_push'] = []
861
# invoked after a pull operation completes.
862
# the api signature is
864
# containing the members
865
# (source, local, master, old_revno, old_revid, new_revno, new_revid)
866
# where local is the local branch or None, master is the target
867
# master branch, and the rest should be self explanatory. The source
868
# is read locked and the target branches write locked. The local
869
# branch is the low-latency branch.
870
self['post_pull'] = []
871
# invoked after a commit operation completes.
872
# the api signature is
873
# (local, master, old_revno, old_revid, new_revno, new_revid)
874
# old_revid is NULL_REVISION for the first commit to a branch.
875
self['post_commit'] = []
876
# invoked after a uncommit operation completes.
877
# the api signature is
878
# (local, master, old_revno, old_revid, new_revno, new_revid) where
879
# local is the local branch or None, master is the target branch,
880
# and an empty branch recieves new_revno of 0, new_revid of None.
881
self['post_uncommit'] = []
883
def install_hook(self, hook_name, a_callable):
884
"""Install a_callable in to the hook hook_name.
886
:param hook_name: A hook name. See the __init__ method of BranchHooks
887
for the complete list of hooks.
888
:param a_callable: The callable to be invoked when the hook triggers.
889
The exact signature will depend on the hook - see the __init__
890
method of BranchHooks for details on each hook.
893
self[hook_name].append(a_callable)
895
raise errors.UnknownHook('branch', hook_name)
898
# install the default hooks into the Branch class.
899
Branch.hooks = BranchHooks()
718
902
class BzrBranchFormat4(BranchFormat):
719
903
"""Bzr branch format 4.
828
990
a_bzrdir=a_bzrdir,
829
991
_repository=a_bzrdir.find_repository())
832
return "Bazaar-NG Metadir branch format 5"
994
class BzrBranchFormat6(BzrBranchFormat5):
995
"""Branch format with last-revision
997
Unlike previous formats, this has no explicit revision history. Instead,
998
this just stores the last-revision, and the left-hand history leading
999
up to there is the history.
1001
This format was introduced in bzr 0.15
1004
def get_format_string(self):
1005
"""See BranchFormat.get_format_string()."""
1006
return "Bazaar-NG branch format 6\n"
1008
def get_format_description(self):
1009
"""See BranchFormat.get_format_description()."""
1010
return "Branch format 6"
1012
def initialize(self, a_bzrdir):
1013
"""Create a branch of this format in a_bzrdir."""
1014
utf8_files = [('last-revision', '0 null:\n'),
1015
('branch-name', ''),
1016
('branch.conf', ''),
1019
return self._initialize_helper(a_bzrdir, utf8_files)
1021
def open(self, a_bzrdir, _found=False):
1022
"""Return the branch object for a_bzrdir
1024
_found is a private parameter, do not use it. It is used to indicate
1025
if format probing has already be done.
1028
format = BranchFormat.find_format(a_bzrdir)
1029
assert format.__class__ == self.__class__
1030
transport = a_bzrdir.get_branch_transport(None)
1031
control_files = lockable_files.LockableFiles(transport, 'lock',
1033
return BzrBranch6(_format=self,
1034
_control_files=control_files,
1036
_repository=a_bzrdir.find_repository())
1038
def supports_tags(self):
835
1042
class BranchReferenceFormat(BranchFormat):
1186
1430
return self.bzrdir.open_workingtree()
1188
1432
@needs_write_lock
1189
def pull(self, source, overwrite=False, stop_revision=None):
1190
"""See Branch.pull."""
1433
def pull(self, source, overwrite=False, stop_revision=None,
1434
_hook_master=None, _run_hooks=True):
1437
:param _hook_master: Private parameter - set the branch to
1438
be supplied as the master to push hooks.
1439
:param _run_hooks: Private parameter - allow disabling of
1440
hooks, used when pushing to a master branch.
1442
result = PullResult()
1443
result.source_branch = source
1444
result.target_branch = self
1191
1445
source.lock_read()
1193
old_count = len(self.revision_history())
1447
result.old_revno, result.old_revid = self.last_revision_info()
1195
1449
self.update_revisions(source, stop_revision)
1196
1450
except DivergedBranches:
1197
1451
if not overwrite:
1200
self.set_revision_history(source.revision_history())
1201
new_count = len(self.revision_history())
1202
return new_count - old_count
1454
if stop_revision is None:
1455
stop_revision = source.last_revision()
1456
self.generate_revision_history(stop_revision)
1457
result.tag_conflicts = source.tags.merge_to(self.tags)
1458
result.new_revno, result.new_revid = self.last_revision_info()
1460
result.master_branch = _hook_master
1461
result.local_branch = self
1463
result.master_branch = self
1464
result.local_branch = None
1466
for hook in Branch.hooks['post_pull']:
1204
1469
source.unlock()
1472
def _get_parent_location(self):
1473
_locs = ['parent', 'pull', 'x-pull']
1476
return self.control_files.get(l).read().strip('\n')
1482
def push(self, target, overwrite=False, stop_revision=None,
1483
_hook_master=None, _run_hooks=True):
1486
:param _hook_master: Private parameter - set the branch to
1487
be supplied as the master to push hooks.
1488
:param _run_hooks: Private parameter - allow disabling of
1489
hooks, used when pushing to a master branch.
1491
result = PushResult()
1492
result.source_branch = self
1493
result.target_branch = target
1496
result.old_revno, result.old_revid = target.last_revision_info()
1498
target.update_revisions(self, stop_revision)
1499
except DivergedBranches:
1503
target.set_revision_history(self.revision_history())
1504
result.tag_conflicts = self.tags.merge_to(target.tags)
1505
result.new_revno, result.new_revid = target.last_revision_info()
1507
result.master_branch = _hook_master
1508
result.local_branch = target
1510
result.master_branch = target
1511
result.local_branch = None
1513
for hook in Branch.hooks['post_push']:
1206
1519
def get_parent(self):
1207
1520
"""See Branch.get_parent."""
1209
_locs = ['parent', 'pull', 'x-pull']
1210
1522
assert self.base[-1] == '/'
1213
parent = self.control_files.get(l).read().strip('\n')
1216
# This is an old-format absolute path to a local branch
1217
# turn it into a url
1218
if parent.startswith('/'):
1219
parent = urlutils.local_path_to_url(parent.decode('utf8'))
1221
return urlutils.join(self.base[:-1], parent)
1222
except errors.InvalidURLJoin, e:
1223
raise errors.InaccessibleParent(parent, self.base)
1523
parent = self._get_parent_location()
1526
# This is an old-format absolute path to a local branch
1527
# turn it into a url
1528
if parent.startswith('/'):
1529
parent = urlutils.local_path_to_url(parent.decode('utf8'))
1531
return urlutils.join(self.base[:-1], parent)
1532
except errors.InvalidURLJoin, e:
1533
raise errors.InaccessibleParent(parent, self.base)
1226
1535
def get_push_location(self):
1227
1536
"""See Branch.get_push_location."""
1279
1593
_repository=_repository)
1281
1595
@needs_write_lock
1282
def pull(self, source, overwrite=False, stop_revision=None):
1283
"""Updates branch.pull to be bound branch aware."""
1596
def pull(self, source, overwrite=False, stop_revision=None,
1598
"""Extends branch.pull to be bound branch aware.
1600
:param _run_hooks: Private parameter used to force hook running
1601
off during bound branch double-pushing.
1284
1603
bound_location = self.get_bound_location()
1285
if source.base != bound_location:
1604
master_branch = None
1605
if bound_location and source.base != bound_location:
1286
1606
# not pulling from master, so we need to update master.
1287
1607
master_branch = self.get_master_branch()
1289
master_branch.pull(source)
1290
source = master_branch
1291
return super(BzrBranch5, self).pull(source, overwrite, stop_revision)
1608
master_branch.lock_write()
1611
# pull from source into master.
1612
master_branch.pull(source, overwrite, stop_revision,
1614
return super(BzrBranch5, self).pull(source, overwrite,
1615
stop_revision, _hook_master=master_branch,
1616
_run_hooks=_run_hooks)
1619
master_branch.unlock()
1622
def push(self, target, overwrite=False, stop_revision=None):
1623
"""Updates branch.push to be bound branch aware."""
1624
bound_location = target.get_bound_location()
1625
master_branch = None
1626
if bound_location and target.base != bound_location:
1627
# not pushing to master, so we need to update master.
1628
master_branch = target.get_master_branch()
1629
master_branch.lock_write()
1632
# push into the master from this branch.
1633
super(BzrBranch5, self).push(master_branch, overwrite,
1634
stop_revision, _run_hooks=False)
1635
# and push into the target branch from this. Note that we push from
1636
# this branch again, because its considered the highest bandwidth
1638
return super(BzrBranch5, self).push(target, overwrite,
1639
stop_revision, _hook_master=master_branch)
1642
master_branch.unlock()
1293
1644
def get_bound_location(self):
1749
class BzrBranchExperimental(BzrBranch5):
1750
"""Bzr experimental branch format
1753
- a revision-history file.
1755
- a lock dir guarding the branch itself
1756
- all of this stored in a branch/ subdirectory
1757
- works with shared repositories.
1758
- a tag dictionary in the branch
1760
This format is new in bzr 0.15, but shouldn't be used for real data,
1763
This class acts as it's own BranchFormat.
1766
_matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1769
def get_format_string(cls):
1770
"""See BranchFormat.get_format_string()."""
1771
return "Bazaar-NG branch format experimental\n"
1774
def get_format_description(cls):
1775
"""See BranchFormat.get_format_description()."""
1776
return "Experimental branch format"
1779
def _initialize_control_files(cls, a_bzrdir, utf8_files, lock_filename,
1781
branch_transport = a_bzrdir.get_branch_transport(cls)
1782
control_files = lockable_files.LockableFiles(branch_transport,
1783
lock_filename, lock_class)
1784
control_files.create_lock()
1785
control_files.lock_write()
1787
for filename, content in utf8_files:
1788
control_files.put_utf8(filename, content)
1790
control_files.unlock()
1793
def initialize(cls, a_bzrdir):
1794
"""Create a branch of this format in a_bzrdir."""
1795
utf8_files = [('format', cls.get_format_string()),
1796
('revision-history', ''),
1797
('branch-name', ''),
1800
cls._initialize_control_files(a_bzrdir, utf8_files,
1801
'lock', lockdir.LockDir)
1802
return cls.open(a_bzrdir, _found=True)
1805
def open(cls, a_bzrdir, _found=False):
1806
"""Return the branch object for a_bzrdir
1808
_found is a private parameter, do not use it. It is used to indicate
1809
if format probing has already be done.
1812
format = BranchFormat.find_format(a_bzrdir)
1813
assert format.__class__ == cls
1814
transport = a_bzrdir.get_branch_transport(None)
1815
control_files = lockable_files.LockableFiles(transport, 'lock',
1817
return cls(_format=cls,
1818
_control_files=control_files,
1820
_repository=a_bzrdir.find_repository())
1823
def is_supported(cls):
1826
def _make_tags(self):
1827
return BasicTags(self)
1830
def supports_tags(cls):
1834
BranchFormat.register_format(BzrBranchExperimental)
1837
class BzrBranch6(BzrBranch5):
1840
def last_revision_info(self):
1841
revision_string = self.control_files.get('last-revision').read()
1842
revno, revision_id = revision_string.rstrip('\n').split(' ', 1)
1843
revision_id = cache_utf8.get_cached_utf8(revision_id)
1845
return revno, revision_id
1847
def last_revision(self):
1848
"""Return last revision id, or None"""
1849
revision_id = self.last_revision_info()[1]
1850
if revision_id == _mod_revision.NULL_REVISION:
1854
def _write_last_revision_info(self, revno, revision_id):
1855
"""Simply write out the revision id, with no checks.
1857
Use set_last_revision_info to perform this safely.
1859
Does not update the revision_history cache.
1860
Intended to be called by set_last_revision_info and
1861
_write_revision_history.
1863
if revision_id is None:
1864
revision_id = 'null:'
1865
out_string = '%d %s\n' % (revno, revision_id)
1866
self.control_files.put_bytes('last-revision', out_string)
1869
def set_last_revision_info(self, revno, revision_id):
1870
revision_id = osutils.safe_revision_id(revision_id)
1871
if self._get_append_revisions_only():
1872
self._check_history_violation(revision_id)
1873
self._write_last_revision_info(revno, revision_id)
1874
transaction = self.get_transaction()
1875
cached_history = transaction.map.find_revision_history()
1876
if cached_history is not None:
1877
transaction.map.remove_object(cached_history)
1879
def _check_history_violation(self, revision_id):
1880
last_revision = self.last_revision()
1881
if last_revision is None:
1883
if last_revision not in self._lefthand_history(revision_id):
1884
raise errors.AppendRevisionsOnlyViolation(self.base)
1886
def _gen_revision_history(self):
1887
"""Generate the revision history from last revision
1889
history = list(self.repository.iter_reverse_revision_history(
1890
self.last_revision()))
1894
def _write_revision_history(self, history):
1895
"""Factored out of set_revision_history.
1897
This performs the actual writing to disk, with format-specific checks.
1898
It is intended to be called by BzrBranch5.set_revision_history.
1900
if len(history) == 0:
1901
last_revision = 'null:'
1903
if history != self._lefthand_history(history[-1]):
1904
raise errors.NotLefthandHistory(history)
1905
last_revision = history[-1]
1906
if self._get_append_revisions_only():
1907
self._check_history_violation(last_revision)
1908
self._write_last_revision_info(len(history), last_revision)
1911
def append_revision(self, *revision_ids):
1912
revision_ids = [osutils.safe_revision_id(r) for r in revision_ids]
1913
if len(revision_ids) == 0:
1915
prev_revno, prev_revision = self.last_revision_info()
1916
for revision in self.repository.get_revisions(revision_ids):
1917
if prev_revision == _mod_revision.NULL_REVISION:
1918
if revision.parent_ids != []:
1919
raise errors.NotLeftParentDescendant(self, prev_revision,
1920
revision.revision_id)
1922
if revision.parent_ids[0] != prev_revision:
1923
raise errors.NotLeftParentDescendant(self, prev_revision,
1924
revision.revision_id)
1925
prev_revision = revision.revision_id
1926
self.set_last_revision_info(prev_revno + len(revision_ids),
1929
def _set_config_location(self, name, url, config=None,
1930
make_relative=False):
1932
config = self.get_config()
1936
url = urlutils.relative_url(self.base, url)
1937
config.set_user_option(name, url)
1940
def _get_config_location(self, name, config=None):
1942
config = self.get_config()
1943
location = config.get_user_option(name)
1949
def _set_parent_location(self, url):
1950
"""Set the parent branch"""
1951
self._set_config_location('parent_location', url, make_relative=True)
1954
def _get_parent_location(self):
1955
"""Set the parent branch"""
1956
return self._get_config_location('parent_location')
1958
def set_push_location(self, location):
1959
"""See Branch.set_push_location."""
1960
self._set_config_location('push_location', location)
1962
def set_bound_location(self, location):
1963
"""See Branch.set_push_location."""
1965
config = self.get_config()
1966
if location is None:
1967
if config.get_user_option('bound') != 'True':
1970
config.set_user_option('bound', 'False')
1973
self._set_config_location('bound_location', location,
1975
config.set_user_option('bound', 'True')
1978
def _get_bound_location(self, bound):
1979
"""Return the bound location in the config file.
1981
Return None if the bound parameter does not match"""
1982
config = self.get_config()
1983
config_bound = (config.get_user_option('bound') == 'True')
1984
if config_bound != bound:
1986
return self._get_config_location('bound_location', config=config)
1988
def get_bound_location(self):
1989
"""See Branch.set_push_location."""
1990
return self._get_bound_location(True)
1992
def get_old_bound_location(self):
1993
"""See Branch.get_old_bound_location"""
1994
return self._get_bound_location(False)
1996
def set_append_revisions_only(self, enabled):
2001
self.get_config().set_user_option('append_revisions_only', value)
2003
def _get_append_revisions_only(self):
2004
value = self.get_config().get_user_option('append_revisions_only')
2005
return value == 'True'
2007
def _synchronize_history(self, destination, revision_id):
2008
"""Synchronize last revision and revision history between branches.
2010
This version is most efficient when the destination is also a
2011
BzrBranch6, but works for BzrBranch5, as long as the destination's
2012
repository contains all the lefthand ancestors of the intended
2013
last_revision. If not, set_last_revision_info will fail.
2015
:param destination: The branch to copy the history into
2016
:param revision_id: The revision-id to truncate history at. May
2017
be None to copy complete history.
2019
if revision_id is None:
2020
revno, revision_id = self.last_revision_info()
2022
revno = self.revision_id_to_revno(revision_id)
2023
destination.set_last_revision_info(revno, revision_id)
2025
def _make_tags(self):
2026
return BasicTags(self)
1398
2029
class BranchTestProviderAdapter(object):
1399
2030
"""A tool to generate a suite testing multiple branch formats at once.
1418
2049
new_test.bzrdir_format = bzrdir_format
1419
2050
new_test.branch_format = branch_format
1420
2051
def make_new_test_id():
1421
new_id = "%s(%s)" % (new_test.id(), branch_format.__class__.__name__)
2052
# the format can be either a class or an instance
2053
name = getattr(branch_format, '__name__',
2054
branch_format.__class__.__name__)
2055
new_id = "%s(%s)" % (new_test.id(), name)
1422
2056
return lambda: new_id
1423
2057
new_test.id = make_new_test_id()
1424
2058
result.addTest(new_test)
2062
######################################################################
2063
# results of operations
2066
class _Result(object):
2068
def _show_tag_conficts(self, to_file):
2069
if not getattr(self, 'tag_conflicts', None):
2071
to_file.write('Conflicting tags:\n')
2072
for name, value1, value2 in self.tag_conflicts:
2073
to_file.write(' %s\n' % (name, ))
2076
class PullResult(_Result):
2077
"""Result of a Branch.pull operation.
2079
:ivar old_revno: Revision number before pull.
2080
:ivar new_revno: Revision number after pull.
2081
:ivar old_revid: Tip revision id before pull.
2082
:ivar new_revid: Tip revision id after pull.
2083
:ivar source_branch: Source (local) branch object.
2084
:ivar master_branch: Master branch of the target, or None.
2085
:ivar target_branch: Target/destination branch object.
2089
# DEPRECATED: pull used to return the change in revno
2090
return self.new_revno - self.old_revno
2092
def report(self, to_file):
2093
if self.old_revid == self.new_revid:
2094
to_file.write('No revisions to pull.\n')
2096
to_file.write('Now on revision %d.\n' % self.new_revno)
2097
self._show_tag_conficts(to_file)
2100
class PushResult(_Result):
2101
"""Result of a Branch.push operation.
2103
:ivar old_revno: Revision number before push.
2104
:ivar new_revno: Revision number after push.
2105
:ivar old_revid: Tip revision id before push.
2106
:ivar new_revid: Tip revision id after push.
2107
:ivar source_branch: Source branch object.
2108
:ivar master_branch: Master branch of the target, or None.
2109
:ivar target_branch: Target/destination branch object.
2113
# DEPRECATED: push used to return the change in revno
2114
return self.new_revno - self.old_revno
2116
def report(self, to_file):
2117
"""Write a human-readable description of the result."""
2118
if self.old_revid == self.new_revid:
2119
to_file.write('No new revisions to push.\n')
2121
to_file.write('Pushed up to revision %d.\n' % self.new_revno)
2122
self._show_tag_conficts(to_file)
1428
2125
class BranchCheckResult(object):
1429
2126
"""Results of checking branch consistency.