Merge lp:~jelmer/brz/switch-self into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/switch-self
Merge into: lp:brz
Diff against target: 187 lines (+93/-6)
7 files modified
breezy/builtins.py (+8/-2)
breezy/bzr/branch.py (+13/-3)
breezy/bzr/bzrdir.py (+3/-0)
breezy/controldir.py (+8/-0)
breezy/tests/blackbox/test_checkout.py (+1/-1)
breezy/tests/blackbox/test_switch.py (+47/-0)
breezy/tests/per_controldir_colo/test_supported.py (+13/-0)
To merge this branch: bzr merge lp:~jelmer/brz/switch-self
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+342486@code.launchpad.net

Commit message

Two switch fixes (relative paths in lightweight checkout URLs of colocated branches (#919169), print error when attempting to create a branch reference loop (#1018628))

Description of the change

Two colocated branch fixes:

* Use relative paths in lightweight checkout URLs of colocated branches (#919169)
* Print an error when attempting to create a branch reference loop (#1018628)

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

Thank you! Great to fix some of these gotchas with colo branches and switch.

review: Approve
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :

Running landing tests failed
https://ci.breezy-vcs.org/job/brz-dev/97/

Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :

Running landing tests failed
https://ci.breezy-vcs.org/job/brz-dev/110/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/builtins.py'
2--- breezy/builtins.py 2018-04-01 18:09:31 +0000
3+++ breezy/builtins.py 2018-04-03 02:46:28 +0000
4@@ -6352,8 +6352,14 @@
5 possible_transports=possible_transports)
6 if revision is not None:
7 revision = revision.as_revision_id(to_branch)
8- switch.switch(control_dir, to_branch, force, revision_id=revision,
9- store_uncommitted=store)
10+ try:
11+ switch.switch(control_dir, to_branch, force, revision_id=revision,
12+ store_uncommitted=store)
13+ except controldir.BranchReferenceLoop:
14+ raise errors.BzrCommandError(
15+ gettext('switching would create a branch reference loop. '
16+ 'Use the "bzr up" command to switch to a '
17+ 'different revision.'))
18 if had_explicit_nick:
19 branch = control_dir.open_branch() #get the new branch!
20 branch.nick = to_branch.nick
21
22=== modified file 'breezy/bzr/branch.py'
23--- breezy/bzr/branch.py 2018-03-25 00:39:16 +0000
24+++ breezy/bzr/branch.py 2018-04-03 02:46:28 +0000
25@@ -946,12 +946,22 @@
26 def get_reference(self, a_controldir, name=None):
27 """See BranchFormat.get_reference()."""
28 transport = a_controldir.get_branch_transport(None, name=name)
29- return transport.get_bytes('location')
30+ url = urlutils.split_segment_parameters(a_controldir.user_url)[0]
31+ return urlutils.join(url, transport.get_bytes('location'))
32+
33+ def _write_reference(self, a_controldir, transport, to_branch):
34+ to_url = to_branch.user_url
35+ if a_controldir.control_url == to_branch.controldir.control_url:
36+ # Write relative paths for colocated branches, but absolute
37+ # paths for everything else. This is for the benefit
38+ # of older bzr versions that don't support relative paths.
39+ to_url = urlutils.relative_url(a_controldir.user_url, to_branch.user_url)
40+ transport.put_bytes('location', to_url)
41
42 def set_reference(self, a_controldir, name, to_branch):
43 """See BranchFormat.set_reference()."""
44 transport = a_controldir.get_branch_transport(None, name=name)
45- location = transport.put_bytes('location', to_branch.base)
46+ self._write_reference(a_controldir, transport, to_branch)
47
48 def initialize(self, a_controldir, name=None, target_branch=None,
49 repository=None, append_revisions_only=None):
50@@ -966,7 +976,7 @@
51 if name is None:
52 name = a_controldir._get_selected_branch()
53 branch_transport = a_controldir.get_branch_transport(self, name=name)
54- branch_transport.put_bytes('location', target_branch.user_url)
55+ self._write_reference(a_controldir, branch_transport, target_branch)
56 branch_transport.put_bytes('format', self.as_string())
57 branch = self.open(a_controldir, name, _found=True,
58 possible_transports=[target_branch.controldir.root_transport])
59
60=== modified file 'breezy/bzr/bzrdir.py'
61--- breezy/bzr/bzrdir.py 2018-03-25 00:39:16 +0000
62+++ breezy/bzr/bzrdir.py 2018-04-03 02:46:28 +0000
63@@ -955,6 +955,9 @@
64
65 def set_branch_reference(self, target_branch, name=None):
66 format = _mod_bzrbranch.BranchReferenceFormat()
67+ if (self.control_url == target_branch.controldir.control_url and
68+ name == target_branch.name):
69+ raise controldir.BranchReferenceLoop(target_branch)
70 return format.initialize(self, target_branch=target_branch, name=name)
71
72 def get_branch_transport(self, branch_format, name=None):
73
74=== modified file 'breezy/controldir.py'
75--- breezy/controldir.py 2018-03-04 19:06:44 +0000
76+++ breezy/controldir.py 2018-04-03 02:46:28 +0000
77@@ -59,6 +59,14 @@
78 errors.BzrError.__init__(self, format=format, url=url)
79
80
81+class BranchReferenceLoop(errors.BzrError):
82+
83+ _fmt = "Can not create branch reference that points at branch itself."
84+
85+ def __init__(self, branch):
86+ errors.BzrError.__init__(self, branch=branch)
87+
88+
89 class ControlComponent(object):
90 """Abstract base class for control directory components.
91
92
93=== modified file 'breezy/tests/blackbox/test_checkout.py'
94--- breezy/tests/blackbox/test_checkout.py 2018-02-18 15:21:06 +0000
95+++ breezy/tests/blackbox/test_checkout.py 2018-04-03 02:46:28 +0000
96@@ -191,7 +191,7 @@
97 # We should always be creating a lighweight checkout for colocated
98 # branches.
99 self.assertEqual(
100- target.open_branch(name='somebranch').base,
101+ target.open_branch(name='somebranch').user_url,
102 target.get_branch_reference(name=""))
103
104
105
106=== modified file 'breezy/tests/blackbox/test_switch.py'
107--- breezy/tests/blackbox/test_switch.py 2018-02-18 15:21:06 +0000
108+++ breezy/tests/blackbox/test_switch.py 2018-04-03 02:46:28 +0000
109@@ -529,3 +529,50 @@
110 self.assertPathDoesNotExist('checkout/a')
111 self.run_bzr(['switch', '-d', 'checkout', 'orig'])
112 self.assertPathDoesNotExist('checkout/a')
113+
114+
115+class TestSwitchStandAloneCorruption(TestCaseWithTransport):
116+
117+ def test_empty_tree_switch(self):
118+ """switch . on an empty tree gets infinite recursion
119+
120+ Inspired by: https://bugs.launchpad.net/bzr/+bug/1018628
121+ """
122+ self.script_runner = script.ScriptRunner()
123+ self.script_runner.run_script(self, '''
124+ $ brz init
125+ Created a standalone tree (format: 2a)
126+ $ brz switch .
127+ 2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.
128+ ''')
129+
130+ def test_switch_on_previous_rev(self):
131+ """switch to previous rev in a standalone directory
132+
133+ Inspired by: https://bugs.launchpad.net/brz/+bug/1018628
134+ """
135+ self.script_runner = script.ScriptRunner()
136+ self.script_runner.run_script(self, '''
137+ $ brz init
138+ Created a standalone tree (format: 2a)
139+ $ brz commit -m 1 --unchanged
140+ $ brz commit -m 2 --unchanged
141+ $ brz switch -r 1
142+ 2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.''',
143+ null_output_matches_anything=True)
144+
145+ def test_switch_create_colo_locks_repo_path(self):
146+ self.script_runner = script.ScriptRunner()
147+ self.script_runner.run_script(self, '''
148+ $ mkdir mywork
149+ $ cd mywork
150+ $ brz init
151+ Created a standalone tree (format: 2a)
152+ $ echo A > a && brz add a && brz commit -m A
153+ $ brz switch -b br1
154+ $ cd ..
155+ $ mv mywork mywork1
156+ $ cd mywork1
157+ $ brz branches
158+ * br1
159+ ''', null_output_matches_anything=True)
160
161=== modified file 'breezy/tests/per_controldir_colo/test_supported.py'
162--- breezy/tests/per_controldir_colo/test_supported.py 2018-03-04 03:27:09 +0000
163+++ breezy/tests/per_controldir_colo/test_supported.py 2018-04-03 02:46:28 +0000
164@@ -17,6 +17,7 @@
165 """Tests for bzr directories that support colocated branches."""
166
167 from breezy.branch import Branch
168+from breezy.controldir import BranchReferenceLoop
169 from breezy import (
170 branchbuilder,
171 errors,
172@@ -166,3 +167,15 @@
173 'Control dir does not support creating branch references.')
174 self.assertEqual(referenced.user_url,
175 repo.controldir.get_branch_reference('foo'))
176+
177+ def test_branch_reference_loop(self):
178+ repo = self.make_repository('repo')
179+ to_branch = self.create_branch(repo.controldir, name='somebranch')
180+ try:
181+ self.assertRaises(
182+ BranchReferenceLoop,
183+ repo.controldir.set_branch_reference,
184+ to_branch, name='somebranch')
185+ except errors.IncompatibleFormat:
186+ raise tests.TestNotApplicable(
187+ 'Control dir does not support creating branch references.')

Subscribers

People subscribed via source and target branches