34
34
class TestMerge(ExternalBase):
36
def example_branch(test):
38
file('hello', 'wt').write('foo')
39
test.runbzr('add hello')
40
test.runbzr('commit -m setup hello')
41
file('goodbye', 'wt').write('baz')
42
test.runbzr('add goodbye')
43
test.runbzr('commit -m setup goodbye')
36
def example_branch(self, path='.'):
37
tree = self.make_branch_and_tree(path)
38
self.build_tree_contents([
39
(pathjoin(path, 'hello'), 'foo'),
40
(pathjoin(path, 'goodbye'), 'baz')])
42
tree.commit(message='setup')
44
tree.commit(message='setup')
45
47
def test_merge_reprocess(self):
46
48
d = BzrDir.create_standalone_workingtree('.')
48
self.run_bzr('merge', '.', '--reprocess', '--merge-type', 'weave')
50
self.run_bzr('merge . --reprocess --merge-type weave')
50
52
def test_merge(self):
51
53
from bzrlib.branch import Branch
57
self.runbzr('branch a b')
59
file('goodbye', 'wt').write('quux')
60
self.runbzr(['commit', '-m', "more u's are always good"])
63
file('hello', 'wt').write('quuux')
55
a_tree = self.example_branch('a')
56
ancestor = a_tree.branch.revno()
57
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
58
self.build_tree_contents([('b/goodbye', 'quux')])
59
b_tree.commit(message="more u's are always good")
61
self.build_tree_contents([('a/hello', 'quuux')])
64
62
# We can't merge when there are in-tree changes
65
self.runbzr('merge ../b', retcode=3)
66
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
67
self.runbzr('merge ../b -r last:1..last:1 --merge-type blooof',
64
self.run_bzr('merge ../b', retcode=3)
65
a = WorkingTree.open('.')
66
a_tip = a.commit("Like an epidemic of u's")
67
self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
69
self.runbzr('merge ../b -r last:1..last:1 --merge-type merge3')
70
self.runbzr('revert --no-backup')
71
self.runbzr('merge ../b -r last:1..last:1 --merge-type weave')
72
self.runbzr('revert --no-backup')
73
self.runbzr('merge ../b -r last:1..last:1 --reprocess')
74
self.runbzr('revert --no-backup')
75
self.runbzr('merge ../b -r last:1')
69
self.run_bzr('merge ../b -r last:1..last:1 --merge-type merge3')
70
a_tree.revert(backups=False)
71
self.run_bzr('merge ../b -r last:1..last:1 --merge-type weave')
72
a_tree.revert(backups=False)
73
self.run_bzr_error(['Show-base is not supported for this merge type'],
74
'merge ../b -r last:1..last:1 --merge-type weave'
76
a_tree.revert(backups=False)
77
self.run_bzr('merge ../b -r last:1..last:1 --reprocess')
78
a_tree.revert(backups=False)
79
self.run_bzr('merge ../b -r last:1')
76
80
self.check_file_contents('goodbye', 'quux')
77
81
# Merging a branch pulls its revision into the tree
78
a = WorkingTree.open('.')
79
82
b = Branch.open('../b')
80
a.branch.repository.get_revision_xml(b.last_revision())
81
self.log('pending merges: %s', a.pending_merges())
82
self.assertEquals(a.pending_merges(),
84
self.runbzr('commit -m merged')
85
self.runbzr('merge ../b -r last:1')
86
self.assertEqual(a.pending_merges(), [])
83
b_tip = b.last_revision()
84
self.failUnless(a.branch.repository.has_revision(b_tip))
85
self.assertEqual([a_tip, b_tip], a.get_parent_ids())
86
a_tree.revert(backups=False)
87
out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
88
self.assertTrue("Not a branch" in err)
89
self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
90
%(ancestor,b.revno()))
91
self.assertEquals(a.get_parent_ids(),
92
[a.branch.last_revision(), b.last_revision()])
93
self.check_file_contents('goodbye', 'quux')
94
a_tree.revert(backups=False)
95
self.run_bzr('merge -r revno:%d:../b'%b.revno())
96
self.assertEquals(a.get_parent_ids(),
97
[a.branch.last_revision(), b.last_revision()])
98
a_tip = a.commit('merged')
99
self.run_bzr('merge ../b -r last:1')
100
self.assertEqual([a_tip], a.get_parent_ids())
88
102
def test_merge_with_missing_file(self):
89
103
"""Merge handles missing file conflicts"""
93
print >> file('sub/a.txt', 'wb'), "hello"
94
print >> file('b.txt', 'wb'), "hello"
95
print >> file('sub/c.txt', 'wb'), "hello"
98
self.runbzr(('commit', '-m', 'added a'))
99
self.runbzr('branch . ../b')
100
print >> file('sub/a.txt', 'ab'), "there"
101
print >> file('b.txt', 'ab'), "there"
102
print >> file('sub/c.txt', 'ab'), "there"
103
self.runbzr(('commit', '-m', 'Added there'))
104
os.unlink('sub/a.txt')
105
os.unlink('sub/c.txt')
108
self.runbzr(('commit', '-m', 'Removed a.txt'))
110
print >> file('sub/a.txt', 'ab'), "something"
111
print >> file('b.txt', 'ab'), "something"
112
print >> file('sub/c.txt', 'ab'), "something"
113
self.runbzr(('commit', '-m', 'Modified a.txt'))
114
self.runbzr('merge ../a/', retcode=1)
115
self.assert_(os.path.exists('sub/a.txt.THIS'))
116
self.assert_(os.path.exists('sub/a.txt.BASE'))
104
self.build_tree_contents([
107
('a/sub/a.txt', 'hello\n'),
108
('a/b.txt', 'hello\n'),
109
('a/sub/c.txt', 'hello\n')])
110
a_tree = self.make_branch_and_tree('a')
111
a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
112
a_tree.commit(message='added a')
113
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
114
self.build_tree_contents([
115
('a/sub/a.txt', 'hello\nthere\n'),
116
('a/b.txt', 'hello\nthere\n'),
117
('a/sub/c.txt', 'hello\nthere\n')])
118
a_tree.commit(message='Added there')
119
os.remove('a/sub/a.txt')
120
os.remove('a/sub/c.txt')
123
a_tree.commit(message='Removed a.txt')
124
self.build_tree_contents([
125
('b/sub/a.txt', 'hello\nsomething\n'),
126
('b/b.txt', 'hello\nsomething\n'),
127
('b/sub/c.txt', 'hello\nsomething\n')])
128
b_tree.commit(message='Modified a.txt')
130
self.run_bzr('merge ../a/', retcode=1)
131
self.failUnlessExists('sub/a.txt.THIS')
132
self.failUnlessExists('sub/a.txt.BASE')
118
self.runbzr('merge ../b/', retcode=1)
119
self.assert_(os.path.exists('sub/a.txt.OTHER'))
120
self.assert_(os.path.exists('sub/a.txt.BASE'))
134
self.run_bzr('merge ../b/', retcode=1)
135
self.failUnlessExists('sub/a.txt.OTHER')
136
self.failUnlessExists('sub/a.txt.BASE')
122
138
def test_merge_remember(self):
123
139
"""Merge changes from one branch to another and test parent location."""
142
158
self.assertEqual(None, branch_b.get_parent())
143
159
# test merge for failure without parent set
144
160
os.chdir('branch_b')
145
out = self.runbzr('merge', retcode=3)
161
out = self.run_bzr('merge', retcode=3)
146
162
self.assertEquals(out,
147
163
('','bzr: ERROR: No location specified or remembered\n'))
148
164
# test implicit --remember when no parent set, this merge conflicts
149
165
self.build_tree(['d'])
151
out = self.runbzr('merge ../branch_a', retcode=3)
152
self.assertEquals(out,
153
('','bzr: ERROR: Working tree has uncommitted changes.\n'))
167
self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
154
169
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
155
170
# test implicit --remember after resolving conflict
156
171
tree_b.commit('commit d')
157
out, err = self.runbzr('merge')
172
out, err = self.run_bzr('merge')
159
174
base = urlutils.local_path_from_url(branch_a.base)
160
175
self.assertEquals(out, 'Merging from remembered location %s\n' % (base,))
161
self.assertEquals(err, 'All changes applied successfully.\n')
176
self.assertEquals(err, '+N b\nAll changes applied successfully.\n')
162
177
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
163
# re-open tree as external runbzr modified it
178
# re-open tree as external run_bzr modified it
164
179
tree_b = branch_b.bzrdir.open_workingtree()
165
180
tree_b.commit('merge branch_a')
166
181
# test explicit --remember
167
out, err = self.runbzr('merge ../branch_c --remember')
182
out, err = self.run_bzr('merge ../branch_c --remember')
168
183
self.assertEquals(out, '')
169
self.assertEquals(err, 'All changes applied successfully.\n')
184
self.assertEquals(err, '+N c\nAll changes applied successfully.\n')
170
185
self.assertEquals(abspath(branch_b.get_parent()),
171
186
abspath(branch_c.bzrdir.root_transport.base))
172
# re-open tree as external runbzr modified it
187
# re-open tree as external run_bzr modified it
173
188
tree_b = branch_b.bzrdir.open_workingtree()
174
189
tree_b.commit('merge branch_c')
176
191
def test_merge_bundle(self):
177
192
from bzrlib.testament import Testament
178
193
tree_a = self.make_branch_and_tree('branch_a')
179
f = file('branch_a/a', 'wb')
194
self.build_tree_contents([('branch_a/a', 'hello')])
183
196
tree_a.commit('message')
185
198
tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
186
f = file('branch_a/a', 'wb')
199
self.build_tree_contents([('branch_a/a', 'hey there')])
189
200
tree_a.commit('message')
191
f = file('branch_b/a', 'wb')
202
self.build_tree_contents([('branch_b/a', 'goodbye')])
194
203
tree_b.commit('message')
195
204
os.chdir('branch_b')
196
file('../bundle', 'wb').write(self.runbzr('bundle ../branch_a')[0])
205
self.run_bzr('bundle ../branch_a -o ../bundle')
197
206
os.chdir('../branch_a')
198
self.runbzr('merge ../bundle', retcode=1)
199
testament_a = Testament.from_revision(tree_a.branch.repository,
200
tree_b.last_revision())
207
self.run_bzr('merge ../bundle', retcode=1)
208
testament_a = Testament.from_revision(tree_a.branch.repository,
209
tree_b.get_parent_ids()[0])
201
210
testament_b = Testament.from_revision(tree_b.branch.repository,
202
tree_b.last_revision())
211
tree_b.get_parent_ids()[0])
203
212
self.assertEqualDiff(testament_a.as_text(),
204
213
testament_b.as_text())
205
214
tree_a.set_conflicts(ConflictList())
206
215
tree_a.commit('message')
207
216
# it is legal to attempt to merge an already-merged bundle
208
output = self.runbzr('merge ../bundle')[1]
217
output = self.run_bzr('merge ../bundle')[1]
209
218
# but it does nothing
210
self.assertFalse(compare_trees(tree_a.basis_tree(),
211
tree_a).has_changed())
219
self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
212
220
self.assertEqual('Nothing to do.\n', output)
222
def test_merge_uncommitted(self):
223
"""Check that merge --uncommitted behaves properly"""
224
tree_a = self.make_branch_and_tree('a')
225
self.build_tree(['a/file_1', 'a/file_2'])
226
tree_a.add(['file_1', 'file_2'])
227
tree_a.commit('commit 1')
228
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
229
self.failUnlessExists('b/file_1')
230
tree_a.rename_one('file_1', 'file_i')
231
tree_a.commit('commit 2')
232
tree_a.rename_one('file_2', 'file_ii')
234
self.run_bzr('merge a --uncommitted -d b')
235
self.failUnlessExists('b/file_1')
236
self.failUnlessExists('b/file_ii')
238
self.run_bzr_error(('Cannot use --uncommitted and --revision',),
239
'merge /a --uncommitted -r1 -d b')
241
def pullable_branch(self):
242
tree_a = self.make_branch_and_tree('a')
243
self.build_tree(['a/file'])
245
self.id1 = tree_a.commit('commit 1')
247
tree_b = self.make_branch_and_tree('b')
248
tree_b.pull(tree_a.branch)
249
file('b/file', 'wb').write('foo')
250
self.id2 = tree_b.commit('commit 2')
252
def test_merge_pull(self):
253
self.pullable_branch()
255
(out, err) = self.run_bzr('merge --pull ../b')
256
self.assertContainsRe(out, 'Now on revision 2\\.')
257
tree_a = WorkingTree.open('.')
258
self.assertEqual([self.id2], tree_a.get_parent_ids())
260
def test_merge_kind_change(self):
261
tree_a = self.make_branch_and_tree('tree_a')
262
self.build_tree_contents([('tree_a/file', 'content_1')])
263
tree_a.add('file', 'file-id')
264
tree_a.commit('added file')
265
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
266
os.unlink('tree_a/file')
267
self.build_tree(['tree_a/file/'])
268
tree_a.commit('changed file to directory')
270
self.run_bzr('merge ../tree_a')
271
self.assertEqual('directory', file_kind('file'))
273
self.assertEqual('file', file_kind('file'))
274
self.build_tree_contents([('file', 'content_2')])
275
tree_b.commit('content change')
276
self.run_bzr('merge ../tree_a', retcode=1)
277
self.assertEqual(tree_b.conflicts(),
278
[ContentsConflict('file', file_id='file-id')])
280
def test_directive_cherrypick(self):
281
source = self.make_branch_and_tree('source')
282
self.build_tree(['source/a'])
284
source.commit('Added a', rev_id='rev1')
285
self.build_tree(['source/b'])
287
source.commit('Added b', rev_id='rev2')
288
target = self.make_branch_and_tree('target')
289
target.commit('empty commit')
290
self.write_directive('directive', source.branch, 'target', 'rev2',
292
self.run_bzr('merge -d target directive')
293
self.failIfExists('target/a')
294
self.failUnlessExists('target/b')
296
def write_directive(self, filename, source, target, revision_id,
297
base_revision_id=None, mangle_patch=False):
298
md = merge_directive.MergeDirective2.from_objects(
299
source.repository, revision_id, 0, 0, target,
300
base_revision_id=base_revision_id)
303
self.build_tree_contents([(filename, ''.join(md.to_lines()))])
305
def test_directive_verify_warning(self):
306
source = self.make_branch_and_tree('source')
307
self.build_tree(['source/a'])
309
source.commit('Added a', rev_id='rev1')
310
target = self.make_branch_and_tree('target')
311
target.commit('empty commit')
312
self.write_directive('directive', source.branch, 'target', 'rev1')
313
err = self.run_bzr('merge -d target directive')[1]
314
self.assertNotContainsRe(err, 'Preview patch does not match changes')
316
self.write_directive('directive', source.branch, 'target', 'rev1',
318
err = self.run_bzr('merge -d target directive')[1]
319
self.assertContainsRe(err, 'Preview patch does not match changes')
321
def test_merge_arbitrary(self):
322
target = self.make_branch_and_tree('target')
323
target.commit('empty')
324
# We need a revision that has no integer revno
325
branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
326
self.build_tree(['branch_a/file1'])
327
branch_a.add('file1')
328
branch_a.commit('added file1', rev_id='rev2a')
329
branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
330
self.build_tree(['branch_b/file2'])
331
branch_b.add('file2')
332
branch_b.commit('added file2', rev_id='rev2b')
333
branch_b.merge_from_branch(branch_a.branch)
334
self.failUnlessExists('branch_b/file1')
335
branch_b.commit('merged branch_a', rev_id='rev3b')
337
# It works if the revid has an interger revno
338
self.run_bzr('merge -d target -r revid:rev2a branch_a')
339
self.failUnlessExists('target/file1')
340
self.failIfExists('target/file2')
343
# It should work if the revid has no integer revno
344
self.run_bzr('merge -d target -r revid:rev2a branch_b')
345
self.failUnlessExists('target/file1')
346
self.failIfExists('target/file2')
348
def assertDirectoryContent(self, directory, entries, message=''):
349
"""Assert whether entries (file or directories) exist in a directory.
351
It also checks that there are no extra entries.
353
ondisk = os.listdir(directory)
354
if set(ondisk) == set(entries):
358
raise AssertionError(
359
'%s"%s" directory content is different:\na = %s\nb = %s\n'
360
% (message, directory, sorted(entries), sorted(ondisk)))
362
def test_cherrypicking_merge(self):
364
source = self.make_branch_and_tree('source')
365
for f in ('a', 'b', 'c', 'd'):
366
self.build_tree(['source/'+f])
368
source.commit('added '+f, rev_id='rev_'+f)
370
target = source.bzrdir.sprout('target', 'rev_a').open_workingtree()
371
self.assertDirectoryContent('target', ['.bzr', 'a'])
373
self.run_bzr('merge -d target -r revid:rev_b..revid:rev_c source')
374
self.assertDirectoryContent('target', ['.bzr', 'a', 'c'])
377
self.run_bzr('merge -d target -r revid:rev_b..revid:rev_d source')
378
self.assertDirectoryContent('target', ['.bzr', 'a', 'c', 'd'])
380
# pick 1 revision with option --changes
381
self.run_bzr('merge -d target -c revid:rev_d source')
382
self.assertDirectoryContent('target', ['.bzr', 'a', 'd'])