1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Tests for the update command of bzr."""
28
from breezy.bzr import (
31
from breezy.tests.script import ScriptRunner
34
class TestUpdate(tests.TestCaseWithTransport):
36
def test_update_standalone_trivial(self):
37
self.make_branch_and_tree('.')
38
out, err = self.run_bzr('update')
40
'Tree is up to date at revision 0 of branch %s\n' % self.test_dir,
42
self.assertEqual('', out)
44
def test_update_quiet(self):
45
self.make_branch_and_tree('.')
46
out, err = self.run_bzr('update --quiet')
47
self.assertEqual('', err)
48
self.assertEqual('', out)
50
def test_update_standalone_trivial_with_alias_up(self):
51
self.make_branch_and_tree('.')
52
out, err = self.run_bzr('up')
53
self.assertEqual('Tree is up to date at revision 0 of branch %s\n'
56
self.assertEqual('', out)
58
def test_update_up_to_date_light_checkout(self):
59
self.make_branch_and_tree('branch')
60
self.run_bzr('checkout --lightweight branch checkout')
61
out, err = self.run_bzr('update checkout')
62
self.assertEqual('Tree is up to date at revision 0 of branch %s\n'
63
% osutils.pathjoin(self.test_dir, 'branch'),
65
self.assertEqual('', out)
67
def test_update_up_to_date_checkout(self):
68
self.make_branch_and_tree('branch')
69
self.run_bzr('checkout branch checkout')
71
sr.run_script(self, '''
73
2>Tree is up to date at revision 0 of branch .../branch
76
def test_update_out_of_date_standalone_tree(self):
77
# FIXME the default format has to change for this to pass
78
# because it currently uses the branch last-revision marker.
79
self.make_branch_and_tree('branch')
81
self.run_bzr('checkout --lightweight branch checkout')
82
self.build_tree(['checkout/file'])
83
self.run_bzr('add checkout/file')
84
self.run_bzr('commit -m add-file checkout')
85
# now branch should be out of date
86
out, err = self.run_bzr('update branch')
87
self.assertEqual('', out)
88
self.assertEqualDiff("""+N file
89
All changes applied successfully.
90
Updated to revision 1 of branch %s
91
""" % osutils.pathjoin(self.test_dir, 'branch',),
93
self.assertPathExists('branch/file')
95
def test_update_out_of_date_light_checkout(self):
96
self.make_branch_and_tree('branch')
98
self.run_bzr('checkout --lightweight branch checkout')
99
self.run_bzr('checkout --lightweight branch checkout2')
100
self.build_tree(['checkout/file'])
101
self.run_bzr('add checkout/file')
102
self.run_bzr('commit -m add-file checkout')
103
# now checkout2 should be out of date
104
out, err = self.run_bzr('update checkout2')
105
self.assertEqualDiff('''+N file
106
All changes applied successfully.
107
Updated to revision 1 of branch %s
108
''' % osutils.pathjoin(self.test_dir, 'branch',),
110
self.assertEqual('', out)
112
def test_update_conflicts_returns_2(self):
113
self.make_branch_and_tree('branch')
115
self.run_bzr('checkout --lightweight branch checkout')
116
self.build_tree(['checkout/file'])
117
self.run_bzr('add checkout/file')
118
self.run_bzr('commit -m add-file checkout')
119
self.run_bzr('checkout --lightweight branch checkout2')
120
# now alter file in checkout
121
with open('checkout/file', 'wt') as a_file:
123
self.run_bzr('commit -m checnge-file checkout')
124
# now checkout2 should be out of date
125
# make a local change to file
126
with open('checkout2/file', 'wt') as a_file:
128
out, err = self.run_bzr('update checkout2', retcode=1)
129
self.assertEqualDiff(''' M file
130
Text conflict in file
131
1 conflicts encountered.
132
Updated to revision 2 of branch %s
133
''' % osutils.pathjoin(self.test_dir, 'branch',),
135
self.assertEqual('', out)
137
def test_smoke_update_checkout_bound_branch_local_commits(self):
138
# smoke test for doing an update of a checkout of a bound
139
# branch with local commits.
140
master = self.make_branch_and_tree('master')
141
master.commit('first commit')
142
# make a bound branch
143
self.run_bzr('checkout master child')
145
self.run_bzr('checkout --lightweight child checkout')
146
# get an object form of the checkout to manipulate
147
wt = workingtree.WorkingTree.open('checkout')
149
with open('master/file', 'wt') as a_file:
152
master_tip = master.commit('add file')
154
with open('child/file_b', 'wt') as a_file:
156
# get an object form of child
157
child = workingtree.WorkingTree.open('child')
158
child.add(['file_b'])
159
child_tip = child.commit('add file_b', local=True)
161
with open('checkout/file_c', 'wt') as a_file:
165
# now, update checkout ->
166
# get all three files and a pending merge.
167
out, err = self.run_bzr('update checkout')
168
self.assertEqual('', out)
169
self.assertEqualDiff("""+N file_b
170
All changes applied successfully.
172
All changes applied successfully.
173
Updated to revision 2 of branch %s
174
Your local commits will now show as pending merges with 'brz status', and can be committed with 'brz commit'.
175
""" % osutils.pathjoin(self.test_dir, 'master',),
177
self.assertEqual([master_tip, child_tip], wt.get_parent_ids())
178
self.assertPathExists('checkout/file')
179
self.assertPathExists('checkout/file_b')
180
self.assertPathExists('checkout/file_c')
181
self.assertTrue(wt.has_filename('file_c'))
183
def test_update_with_merges(self):
184
# Test that 'brz update' works correctly when you have
185
# an update in the master tree, and a lightweight checkout
186
# which has merged another branch
187
master = self.make_branch_and_tree('master')
188
self.build_tree(['master/file'])
190
master.commit('one', rev_id=b'm1')
192
self.build_tree(['checkout1/'])
193
checkout_dir = bzrdir.BzrDirMetaFormat1().initialize('checkout1')
194
checkout_dir.set_branch_reference(master.branch)
195
checkout1 = checkout_dir.create_workingtree(b'm1')
197
# Create a second branch, with an extra commit
198
other = master.controldir.sprout('other').open_workingtree()
199
self.build_tree(['other/file2'])
201
other.commit('other2', rev_id=b'o2')
203
# Create a new commit in the master branch
204
self.build_tree(['master/file3'])
205
master.add(['file3'])
206
master.commit('f3', rev_id=b'm2')
208
# Merge the other branch into checkout
209
os.chdir('checkout1')
210
self.run_bzr('merge ../other')
212
self.assertEqual([b'o2'], checkout1.get_parent_ids()[1:])
214
# At this point, 'commit' should fail, because we are out of date
215
self.run_bzr_error(["please run 'brz update'"],
218
# This should not report about local commits being pending
219
# merges, because they were real merges
220
out, err = self.run_bzr('update')
221
self.assertEqual('', out)
222
self.assertEqualDiff('''+N file3
223
All changes applied successfully.
224
Updated to revision 2 of branch %s
225
''' % osutils.pathjoin(self.test_dir, 'master',),
227
# The pending merges should still be there
228
self.assertEqual([b'o2'], checkout1.get_parent_ids()[1:])
230
def test_readonly_lightweight_update(self):
231
"""Update a light checkout of a readonly branch"""
232
tree = self.make_branch_and_tree('branch')
233
readonly_branch = branch.Branch.open(self.get_readonly_url('branch'))
234
checkout = readonly_branch.create_checkout('checkout',
236
tree.commit('empty commit')
237
self.run_bzr('update checkout')
239
def test_update_with_merge_merged_to_master(self):
240
# Test that 'brz update' works correctly when you have
241
# an update in the master tree, and a [lightweight or otherwise]
242
# checkout which has merge a revision merged to master already.
243
master = self.make_branch_and_tree('master')
244
self.build_tree(['master/file'])
246
master.commit('one', rev_id=b'm1')
248
self.build_tree(['checkout1/'])
249
checkout_dir = bzrdir.BzrDirMetaFormat1().initialize('checkout1')
250
checkout_dir.set_branch_reference(master.branch)
251
checkout1 = checkout_dir.create_workingtree(b'm1')
253
# Create a second branch, with an extra commit
254
other = master.controldir.sprout('other').open_workingtree()
255
self.build_tree(['other/file2'])
257
other.commit('other2', rev_id=b'o2')
259
# Merge the other branch into checkout - 'start reviewing a patch'
260
checkout1.merge_from_branch(other.branch)
261
self.assertEqual([b'o2'], checkout1.get_parent_ids()[1:])
263
# Create a new commit in the master branch - 'someone else lands its'
264
master.merge_from_branch(other.branch)
265
master.commit('f3', rev_id=b'm2')
267
# This should not report about local commits being pending
268
# merges, because they were real merges (but are now gone).
269
# It should perhaps report on them.
270
out, err = self.run_bzr('update', working_dir='checkout1')
271
self.assertEqual('', out)
272
self.assertEqualDiff('''All changes applied successfully.
273
Updated to revision 2 of branch %s
274
''' % osutils.pathjoin(self.test_dir, 'master',),
276
# The pending merges should still be there
277
self.assertEqual([], checkout1.get_parent_ids()[1:])
279
def test_update_dash_r(self):
280
master = self.make_branch_and_tree('master')
282
self.build_tree(['./file1'])
283
master.add(['file1'])
284
master.commit('one', rev_id=b'm1')
285
self.build_tree(['./file2'])
286
master.add(['file2'])
287
master.commit('two', rev_id=b'm2')
290
sr.run_script(self, '''
293
2>All changes applied successfully.
294
2>Updated to revision 1 of .../master
296
self.assertPathExists('./file1')
297
self.assertPathDoesNotExist('./file2')
298
self.assertEqual([b'm1'], master.get_parent_ids())
300
def test_update_dash_r_outside_history(self):
301
"""Ensure that we can update -r to dotted revisions.
303
master = self.make_branch_and_tree('master')
304
self.build_tree(['master/file1'])
305
master.add(['file1'])
306
master.commit('one', rev_id=b'm1')
308
# Create a second branch, with extra commits
309
other = master.controldir.sprout('other').open_workingtree()
310
self.build_tree(['other/file2', 'other/file3'])
312
other.commit('other2', rev_id=b'o2')
314
other.commit('other3', rev_id=b'o3')
317
self.run_bzr('merge ../other')
318
master.commit('merge', rev_id=b'merge')
320
# Switch to o2. file3 was added only in o3 and should be deleted.
321
out, err = self.run_bzr('update -r revid:o2')
322
self.assertContainsRe(err, '-D\\s+file3')
323
self.assertContainsRe(err, 'All changes applied successfully\\.')
324
self.assertContainsRe(err, 'Updated to revision 1.1.1 of branch .*')
326
# Switch back to latest
327
out, err = self.run_bzr('update')
328
self.assertContainsRe(err, '\\+N\\s+file3')
329
self.assertContainsRe(err, 'All changes applied successfully\\.')
330
self.assertContainsRe(err, 'Updated to revision 2 of branch .*')
332
def test_update_dash_r_in_master(self):
333
# Test that 'brz update' works correctly when you have
334
# an update in the master tree,
335
master = self.make_branch_and_tree('master')
336
self.build_tree(['master/file1'])
337
master.add(['file1'])
338
master.commit('one', rev_id=b'm1')
340
self.run_bzr('checkout master checkout')
342
# add a revision in the master.
343
self.build_tree(['master/file2'])
344
master.add(['file2'])
345
master.commit('two', rev_id=b'm2')
349
sr.run_script(self, '''
350
$ brz update -r revid:m2
352
2>All changes applied successfully.
353
2>Updated to revision 2 of branch .../master
356
def test_update_show_base(self):
357
"""brz update support --show-base
359
see https://bugs.launchpad.net/bzr/+bug/202374"""
361
tree = self.make_branch_and_tree('.')
363
with open('hello', 'wt') as f:
368
with open('hello', 'wt') as f:
372
# tree.update() gives no such revision, so ...
373
self.run_bzr(['update', '-r1'])
376
with open('hello', 'wt') as f:
379
out, err = self.run_bzr(['update', '--show-base'], retcode=1)
381
# check for conflict notification
382
self.assertContainsString(err,
383
' M hello\nText conflict in hello\n1 conflicts encountered.\n')
384
with open('hello', 'rb') as f:
385
self.assertEqualDiff(b'<<<<<<< TREE\n'
386
b'fie||||||| BASE-REVISION\n'
388
b'fee>>>>>>> MERGE-SOURCE\n',
391
def test_update_checkout_prevent_double_merge(self):
392
""""Launchpad bug 113809 in brz "update performs two merges"
393
https://launchpad.net/bugs/113809"""
394
master = self.make_branch_and_tree('master')
395
self.build_tree_contents([('master/file', b'initial contents\n')])
397
master.commit('one', rev_id=b'm1')
399
checkout = master.branch.create_checkout('checkout')
400
lightweight = checkout.branch.create_checkout('lightweight',
403
# time to create a mess
404
# add a commit to the master
405
self.build_tree_contents([('master/file', b'master\n')])
406
master.commit('two', rev_id=b'm2')
407
self.build_tree_contents([('master/file', b'master local changes\n')])
409
# local commit on the checkout
410
self.build_tree_contents([('checkout/file', b'checkout\n')])
411
checkout.commit('tree', rev_id=b'c2', local=True)
412
self.build_tree_contents([('checkout/file',
413
b'checkout local changes\n')])
416
self.build_tree_contents([('lightweight/file',
417
b'lightweight local changes\n')])
419
# now update (and get conflicts)
420
out, err = self.run_bzr('update lightweight', retcode=1)
421
self.assertEqual('', out)
422
# NB: these conflicts are actually in the source code
423
self.assertFileEqual('''\
425
lightweight local changes
433
self.build_tree_contents([('lightweight/file',
434
b'lightweight+checkout\n')])
435
self.run_bzr('resolve lightweight/file')
437
# check we get the second conflict
438
out, err = self.run_bzr('update lightweight', retcode=1)
439
self.assertEqual('', out)
440
# NB: these conflicts are actually in the source code
441
self.assertFileEqual('''\
450
def test_no_upgrade_single_file(self):
451
"""There's one basis revision per tree.
453
Since you can't actually change the basis for a single file at the
454
moment, we don't let you think you can.
458
self.make_branch_and_tree('.')
459
self.build_tree_contents([('a/',),
460
('a/file', b'content')])
462
sr.run_script(self, '''
464
2>brz: ERROR: brz update can only update a whole tree, not a file or subdirectory
465
$ brz update ./a/file
466
2>brz: ERROR: brz update can only update a whole tree, not a file or subdirectory
468
2>Tree is up to date at revision 0 of branch ...
471
2>brz: ERROR: brz update can only update a whole tree, not a file or subdirectory
472
# however, you can update the whole tree from a subdirectory
474
2>Tree is up to date at revision 0 of branch ...