23
23
from bzrlib import (
29
from bzrlib.branch import Branch
30
34
from bzrlib.directory_service import directories
31
from bzrlib.osutils import pathjoin
32
from bzrlib.tests.blackbox import ExternalBase
33
from bzrlib.uncommit import uncommit
34
from bzrlib.workingtree import WorkingTree
37
class TestPull(ExternalBase):
35
from bzrlib.tests import (
41
class TestPull(tests.TestCaseWithTransport):
39
43
def example_branch(self, path='.'):
40
44
tree = self.make_branch_and_tree(path)
41
45
self.build_tree_contents([
42
(pathjoin(path, 'hello'), 'foo'),
43
(pathjoin(path, 'goodbye'), 'baz')])
46
(osutils.pathjoin(path, 'hello'), 'foo'),
47
(osutils.pathjoin(path, 'goodbye'), 'baz')])
45
49
tree.commit(message='setup')
46
50
tree.add('goodbye')
50
54
def test_pull(self):
51
55
"""Pull changes from one branch to another."""
52
56
a_tree = self.example_branch('a')
54
self.run_bzr('pull', retcode=3)
55
self.run_bzr('missing', retcode=3)
56
self.run_bzr('missing .')
57
self.run_bzr('missing')
57
base_rev = a_tree.branch.last_revision()
58
self.run_bzr('pull', retcode=3, working_dir='a')
59
self.run_bzr('missing', retcode=3, working_dir='a')
60
self.run_bzr('missing .', working_dir='a')
61
self.run_bzr('missing', working_dir='a')
58
62
# this will work on windows because we check for the same branch
59
63
# in pull - if it fails, it is a regression
61
self.run_bzr('pull /', retcode=3)
64
self.run_bzr('pull', working_dir='a')
65
self.run_bzr('pull /', retcode=3, working_dir='a')
62
66
if sys.platform not in ('win32', 'cygwin'):
67
self.run_bzr('pull', working_dir='a')
66
69
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
70
self.run_bzr('pull', working_dir='b')
70
72
b_tree.add('subdir')
71
b_tree.commit(message='blah', allow_pointless=True)
76
self.assertEqual(a.revision_history(), b.revision_history()[:-1])
79
self.run_bzr('pull ../b')
80
self.assertEqual(a.revision_history(), b.revision_history())
73
new_rev = b_tree.commit(message='blah', allow_pointless=True)
75
a = branch.Branch.open('a')
76
b = branch.Branch.open('b')
77
self.assertEqual(a.last_revision(), base_rev)
78
self.assertEqual(b.last_revision(), new_rev)
80
self.run_bzr('pull ../b', working_dir='a')
81
self.assertEqual(a.last_revision(), b.last_revision())
81
82
a_tree.commit(message='blah2', allow_pointless=True)
82
83
b_tree.commit(message='blah3', allow_pointless=True)
85
self.run_bzr('pull ../a', retcode=3)
85
self.run_bzr('pull ../a', retcode=3, working_dir='b')
87
86
b_tree.bzrdir.sprout('overwriteme')
88
os.chdir('overwriteme')
89
self.run_bzr('pull --overwrite ../a')
90
overwritten = Branch.open('.')
91
self.assertEqual(overwritten.revision_history(),
87
self.run_bzr('pull --overwrite ../a', working_dir='overwriteme')
88
overwritten = branch.Branch.open('overwriteme')
89
self.assertEqual(overwritten.last_revision(),
93
91
a_tree.merge_from_branch(b_tree.branch)
94
92
a_tree.commit(message="blah4", allow_pointless=True)
95
os.chdir('../b/subdir')
96
self.run_bzr('pull ../../a')
97
self.assertEqual(a.revision_history()[-1], b.revision_history()[-1])
98
sub_tree = WorkingTree.open_containing('.')[0]
94
self.run_bzr('pull ../../a', working_dir='b/subdir')
95
self.assertEqual(a.last_revision(), b.last_revision())
96
sub_tree = workingtree.WorkingTree.open_containing('b/subdir')[0]
99
97
sub_tree.commit(message="blah5", allow_pointless=True)
100
98
sub_tree.commit(message="blah6", allow_pointless=True)
102
self.run_bzr('pull ../a')
99
self.run_bzr('pull ../a', working_dir='b')
104
100
a_tree.commit(message="blah7", allow_pointless=True)
105
101
a_tree.merge_from_branch(b_tree.branch)
106
102
a_tree.commit(message="blah8", allow_pointless=True)
107
self.run_bzr('pull ../b')
108
self.run_bzr('pull ../b')
103
self.run_bzr('pull ../b', working_dir='a')
104
self.run_bzr('pull ../b', working_dir='a')
110
106
def test_pull_dash_d(self):
111
107
self.example_branch('a')
132
128
b_tree = a_tree.bzrdir.sprout('b',
133
129
revision_id=a_tree.branch.get_rev_id(1)).open_workingtree()
135
self.run_bzr('pull -r 2')
136
a = Branch.open('../a')
130
self.run_bzr('pull -r 2', working_dir='b')
131
a = branch.Branch.open('a')
132
b = branch.Branch.open('b')
138
133
self.assertEqual(a.revno(),4)
139
134
self.assertEqual(b.revno(),2)
140
self.run_bzr('pull -r 3')
135
self.run_bzr('pull -r 3', working_dir='b')
141
136
self.assertEqual(b.revno(),3)
142
self.run_bzr('pull -r 4')
143
self.assertEqual(a.revision_history(), b.revision_history())
137
self.run_bzr('pull -r 4', working_dir='b')
138
self.assertEqual(a.last_revision(), b.last_revision())
140
def test_pull_tags(self):
141
"""Tags are updated by pull, and revisions named in those tags are
144
# Make a source, sprout a target off it
145
builder = self.make_branch_builder('source')
146
source = fixtures.build_branch_with_non_ancestral_rev(builder)
147
source.get_config_stack().set('branch.fetch_tags', True)
148
target_bzrdir = source.bzrdir.sprout('target')
149
source.tags.set_tag('tag-a', 'rev-2')
151
self.run_bzr('pull -d target source')
152
target = target_bzrdir.open_branch()
153
# The tag is present, and so is its revision.
154
self.assertEqual('rev-2', target.tags.lookup_tag('tag-a'))
155
target.repository.get_revision('rev-2')
146
157
def test_overwrite_uptodate(self):
147
158
# Make sure pull --overwrite overwrites
160
171
self.build_tree_contents([('a/foo', 'a third change')])
161
172
a_tree.commit(message='a third change')
163
rev_history_a = a_tree.branch.revision_history()
164
self.assertEqual(len(rev_history_a), 3)
174
self.assertEqual(a_tree.branch.last_revision_info()[0], 3)
166
176
b_tree.merge_from_branch(a_tree.branch)
167
177
b_tree.commit(message='merge')
169
self.assertEqual(len(b_tree.branch.revision_history()), 2)
172
self.run_bzr('pull --overwrite ../a')
173
rev_history_b = b_tree.branch.revision_history()
174
self.assertEqual(len(rev_history_b), 3)
176
self.assertEqual(rev_history_b, rev_history_a)
179
self.assertEqual(b_tree.branch.last_revision_info()[0], 2)
181
self.run_bzr('pull --overwrite ../a', working_dir='b')
182
(last_revinfo_b) = b_tree.branch.last_revision_info()
183
self.assertEqual(last_revinfo_b[0], 3)
184
self.assertEqual(last_revinfo_b[1], a_tree.branch.last_revision())
178
186
def test_overwrite_children(self):
179
187
# Make sure pull --overwrite sets the revision-history
191
199
self.build_tree_contents([('a/foo', 'a third change')])
192
200
a_tree.commit(message='a third change')
194
self.assertEqual(len(a_tree.branch.revision_history()), 3)
202
self.assertEqual(a_tree.branch.last_revision_info()[0], 3)
196
204
b_tree.merge_from_branch(a_tree.branch)
197
205
b_tree.commit(message='merge')
199
self.assertEqual(len(b_tree.branch.revision_history()), 2)
207
self.assertEqual(b_tree.branch.last_revision_info()[0], 2)
201
209
self.build_tree_contents([('a/foo', 'a fourth change\n')])
202
210
a_tree.commit(message='a fourth change')
204
rev_history_a = a_tree.branch.revision_history()
205
self.assertEqual(len(rev_history_a), 4)
212
rev_info_a = a_tree.branch.last_revision_info()
213
self.assertEqual(rev_info_a[0], 4)
207
215
# With convergence, we could just pull over the
208
216
# new change, but with --overwrite, we want to switch our history
210
self.run_bzr('pull --overwrite ../a')
211
rev_history_b = b_tree.branch.revision_history()
212
self.assertEqual(len(rev_history_b), 4)
214
self.assertEqual(rev_history_b, rev_history_a)
217
self.run_bzr('pull --overwrite ../a', working_dir='b')
218
rev_info_b = b_tree.branch.last_revision_info()
219
self.assertEqual(rev_info_b[0], 4)
220
self.assertEqual(rev_info_b, rev_info_a)
216
222
def test_pull_remember(self):
217
223
"""Pull changes from one branch to another and test parent location."""
218
transport = self.get_transport()
224
t = self.get_transport()
219
225
tree_a = self.make_branch_and_tree('branch_a')
220
226
branch_a = tree_a.branch
221
227
self.build_tree(['branch_a/a'])
233
239
branch_b.set_parent(None)
234
240
self.assertEqual(None, branch_b.get_parent())
235
241
# test pull for failure without parent set
237
out = self.run_bzr('pull', retcode=3)
242
out = self.run_bzr('pull', retcode=3, working_dir='branch_b')
238
243
self.assertEqual(out,
239
244
('','bzr: ERROR: No pull location known or specified.\n'))
240
245
# test implicit --remember when no parent set, this pull conflicts
241
self.build_tree(['d'])
246
self.build_tree(['branch_b/d'])
243
248
tree_b.commit('commit d')
244
out = self.run_bzr('pull ../branch_a', retcode=3)
249
out = self.run_bzr('pull ../branch_a', retcode=3,
250
working_dir='branch_b')
245
251
self.assertEqual(out,
246
252
('','bzr: ERROR: These branches have diverged.'
247
253
' Use the missing command to see how.\n'
248
254
'Use the merge command to reconcile them.\n'))
249
255
self.assertEqual(branch_b.get_parent(), parent)
250
256
# test implicit --remember after resolving previous failure
251
uncommit(branch=branch_b, tree=tree_b)
252
transport.delete('branch_b/d')
257
uncommit.uncommit(branch=branch_b, tree=tree_b)
258
t.delete('branch_b/d')
259
self.run_bzr('pull', working_dir='branch_b')
254
260
self.assertEqual(branch_b.get_parent(), parent)
255
261
# test explicit --remember
256
self.run_bzr('pull ../branch_c --remember')
262
self.run_bzr('pull ../branch_c --remember', working_dir='branch_b')
257
263
self.assertEqual(branch_b.get_parent(),
258
264
branch_c.bzrdir.root_transport.base)
261
267
from bzrlib.testament import Testament
262
268
# Build up 2 trees and prepare for a pull
263
269
tree_a = self.make_branch_and_tree('branch_a')
264
f = open('branch_a/a', 'wb')
270
with open('branch_a/a', 'wb') as f:
268
273
tree_a.commit('message')
270
275
tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
272
277
# Make a change to 'a' that 'b' can pull
273
f = open('branch_a/a', 'wb')
278
with open('branch_a/a', 'wb') as f:
276
280
tree_a.commit('message')
278
282
# Create the bundle for 'b' to pull
280
self.run_bzr('bundle ../branch_b -o ../bundle')
283
self.run_bzr('bundle ../branch_b -o ../bundle', working_dir='branch_a')
282
os.chdir('../branch_b')
283
out, err = self.run_bzr('pull ../bundle')
285
out, err = self.run_bzr('pull ../bundle', working_dir='branch_b')
284
286
self.assertEqual(out,
285
287
'Now on revision 2.\n')
286
288
self.assertEqual(err,
287
289
' M a\nAll changes applied successfully.\n')
289
self.assertEqualDiff(tree_a.branch.revision_history(),
290
tree_b.branch.revision_history())
291
self.assertEqualDiff(tree_a.branch.last_revision(),
292
tree_b.branch.last_revision())
292
294
testament_a = Testament.from_revision(tree_a.branch.repository,
293
295
tree_a.get_parent_ids()[0])
358
360
def test_pull_verbose_uses_default_log(self):
359
361
tree = self.example_branch('source')
360
362
target = self.make_branch_and_tree('target')
361
target_config = target.branch.get_config()
362
target_config.set_user_option('log_format', 'short')
363
target_config = target.branch.get_config_stack()
364
target_config.set('log_format', 'short')
363
365
out = self.run_bzr('pull -v source -d target')[0]
364
366
self.assertContainsRe(out, r'\n {4}1 .*\n {6}setup\n')
365
367
self.assertNotContainsRe(
366
368
out, r'revno: 1\ncommitter: .*\nbranch nick: source')
370
def test_pull_smart_bound_branch(self):
371
self.setup_smart_server_with_call_log()
372
parent = self.make_branch_and_tree('parent')
373
parent.commit(message='first commit')
374
child = parent.bzrdir.sprout('child').open_workingtree()
375
child.commit(message='second commit')
376
checkout = parent.branch.create_checkout('checkout')
377
self.run_bzr(['pull', self.get_url('child')], working_dir='checkout')
368
379
def test_pull_smart_stacked_streaming_acceptance(self):
369
380
"""'bzr pull -r 123' works on stacked, smart branches, even when the
370
381
revision specified by the revno is only present in the fallback
392
403
# being too low. If rpc_count increases, more network roundtrips have
393
404
# become necessary for this use case. Please do not adjust this number
394
405
# upwards without agreement from bzr's network support maintainers.
395
self.assertLength(18, self.hpss_calls)
396
remote = Branch.open('stacked')
406
self.assertLength(19, self.hpss_calls)
407
self.assertLength(1, self.hpss_connections)
408
remote = branch.Branch.open('stacked')
397
409
self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
399
411
def test_pull_cross_format_warning(self):
400
412
"""You get a warning for probably slow cross-format pulls.
453
465
out, err = self.run_bzr(['pull', '-d', 'to', 'from'])
454
466
self.assertContainsRe(err,
455
467
"(?m)Fetching into experimental format")
469
def test_pull_show_base(self):
470
"""bzr pull supports --show-base
472
see https://bugs.launchpad.net/bzr/+bug/202374"""
473
# create two trees with conflicts, setup conflict, check that
474
# conflicted file looks correct
475
a_tree = self.example_branch('a')
476
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
478
with open(osutils.pathjoin('a', 'hello'),'wt') as f:
482
with open(osutils.pathjoin('b', 'hello'),'wt') as f:
485
out,err=self.run_bzr(['pull','-d','b','a','--show-base'])
487
# check for message here
490
' M hello\nText conflict in hello\n1 conflicts encountered.\n')
492
self.assertEqualDiff('<<<<<<< TREE\n'
493
'fie||||||| BASE-REVISION\n'
495
'fee>>>>>>> MERGE-SOURCE\n',
496
open(osutils.pathjoin('b', 'hello')).read())
498
def test_pull_show_base_working_tree_only(self):
499
"""--show-base only allowed if there's a working tree
501
see https://bugs.launchpad.net/bzr/+bug/202374"""
502
# create a branch, see that --show-base fails
503
self.make_branch('from')
504
self.make_branch('to')
505
out=self.run_bzr(['pull','-d','to','from','--show-base'],retcode=3)
507
out, ('','bzr: ERROR: Need working tree for --show-base.\n'))
509
def test_pull_tag_conflicts(self):
510
"""pulling tags with conflicts will change the exit code"""
511
# create a branch, see that --show-base fails
512
from_tree = self.make_branch_and_tree('from')
513
from_tree.branch.tags.set_tag("mytag", "somerevid")
514
to_tree = self.make_branch_and_tree('to')
515
to_tree.branch.tags.set_tag("mytag", "anotherrevid")
516
out = self.run_bzr(['pull','-d','to','from'],retcode=1)
517
self.assertEqual(out,
518
('No revisions to pull.\nConflicting tags:\n mytag\n', ''))
520
def test_pull_tag_notification(self):
521
"""pulling tags with conflicts will change the exit code"""
522
# create a branch, see that --show-base fails
523
from_tree = self.make_branch_and_tree('from')
524
from_tree.branch.tags.set_tag("mytag", "somerevid")
525
to_tree = self.make_branch_and_tree('to')
526
out = self.run_bzr(['pull', '-d', 'to', 'from'])
527
self.assertEqual(out,
528
('1 tag(s) updated.\n', ''))
530
def test_pull_tag_overwrite(self):
531
"""pulling tags with --overwrite only reports changed tags."""
532
# create a branch, see that --show-base fails
533
from_tree = self.make_branch_and_tree('from')
534
from_tree.branch.tags.set_tag("mytag", "somerevid")
535
to_tree = self.make_branch_and_tree('to')
536
to_tree.branch.tags.set_tag("mytag", "somerevid")
537
out = self.run_bzr(['pull', '--overwrite', '-d', 'to', 'from'])
538
self.assertEqual(out,
539
('No revisions or tags to pull.\n', ''))
542
class TestPullOutput(script.TestCaseWithTransportAndScript):
544
def test_pull_log_format(self):
547
Created a standalone tree (format: 2a)
552
$ bzr commit -m 'we need some foo'
553
2>Committing to:...trunk/
555
2>Committed revision 1.
558
Created a standalone tree (format: 2a)
560
$ bzr pull -v ../trunk -Olog_format=line
563
1: jrandom@example.com ...we need some foo
565
2>All changes applied successfully.