1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
OUTPUT1 = """# Bazaar merge directive format 1
27
# revision_id: example:
28
# target_branch: http://example.com
30
# timestamp: 1970-01-01 00:09:33 +0002
34
OUTPUT1_2 = """# Bazaar merge directive format 2 (Bazaar 0.90)
35
# revision_id: example:
36
# target_branch: http://example.com
38
# timestamp: 1970-01-01 00:09:33 +0002
39
# base_revision_id: null:
44
OUTPUT2 = """# Bazaar merge directive format 1
45
# revision_id: example:
46
# target_branch: http://example.com
48
# timestamp: 1970-01-01 00:09:33 +0002
49
# source_branch: http://example.org
54
OUTPUT2_2 = """# Bazaar merge directive format 2 (Bazaar 0.90)
55
# revision_id: example:
56
# target_branch: http://example.com
58
# timestamp: 1970-01-01 00:09:33 +0002
59
# source_branch: http://example.org
61
# base_revision_id: null:
67
I was thinking today about creating a merge directive.
73
(I've pasted it in the body of this message)
77
# Bazaar merge directive format 1\r
78
# revision_id: example:
79
# target_branch: http://example.com
81
# timestamp: 1970-01-01 00:09:33 +0002
82
# source_branch: http://example.org
85
booga""".splitlines(True)
89
I was thinking today about creating a merge directive.
95
(I've pasted it in the body of this message)
99
# Bazaar merge directive format 2 (Bazaar 0.90)\r
100
# revision_id: example:
101
# target_branch: http://example.com
102
# testament_sha1: sha
103
# timestamp: 1970-01-01 00:09:33 +0002
104
# source_branch: http://example.org
105
# base_revision_id: null:
109
booga""".splitlines(True)
113
I was thinking today about creating a merge directive.
119
(I've pasted it in the body of this message)
123
# Bazaar merge directive format 2 (Bazaar 0.19)\r
124
# revision_id: example:
125
# target_branch: http://example.com
126
# testament_sha1: sha
127
# timestamp: 1970-01-01 00:09:33 +0002
128
# source_branch: http://example.org
129
# base_revision_id: null:
133
booga""".splitlines(True)
136
OLD_DIRECTIVE_2 = """# Bazaar merge directive format 2 (Bazaar 0.19)
137
# revision_id: abentley@panoramicfeedback.com-20070807234458-\
139
# target_branch: http://panoramicfeedback.com/opensource/bzr/repo\
141
# testament_sha1: d825a5cdb267a90ec2ba86b00895f3d8a9bed6bf
142
# timestamp: 2007-08-10 16:15:02 -0400
143
# source_branch: http://panoramicfeedback.com/opensource/bzr/repo\
145
# base_revision_id: abentley@panoramicfeedback.com-20070731163346-\
151
class TestMergeDirective(object):
153
def test_merge_source(self):
156
self.assertRaises(errors.NoMergeSource, self.make_merge_directive,
157
'example:', 'sha', time, timezone, 'http://example.com')
158
self.assertRaises(errors.NoMergeSource, self.make_merge_directive,
159
'example:', 'sha', time, timezone, 'http://example.com',
161
self.make_merge_directive('example:', 'sha', time, timezone,
162
'http://example.com', source_branch='http://example.org')
163
md = self.make_merge_directive('null:', 'sha', time, timezone,
164
'http://example.com', patch='blah', patch_type='bundle')
165
self.assertIs(None, md.source_branch)
166
md2 = self.make_merge_directive('null:', 'sha', time, timezone,
167
'http://example.com', patch='blah', patch_type='bundle',
169
self.assertEqual('bar', md2.source_branch)
171
def test_serialization(self):
174
md = self.make_merge_directive('example:', 'sha', time, timezone,
175
'http://example.com', patch='booga', patch_type='bundle')
176
self.assertEqualDiff(self.OUTPUT1, ''.join(md.to_lines()))
177
md = self.make_merge_directive('example:', 'sha', time, timezone,
178
'http://example.com', source_branch="http://example.org",
179
patch='booga', patch_type='diff', message="Hi mom!")
180
self.assertEqualDiff(self.OUTPUT2, ''.join(md.to_lines()))
182
def test_deserialize_junk(self):
184
self.assertRaises(errors.NotAMergeDirective,
185
merge_directive.MergeDirective.from_lines, 'lala')
187
def test_deserialize_empty(self):
188
self.assertRaises(errors.NotAMergeDirective,
189
merge_directive.MergeDirective.from_lines, [])
191
def test_deserialize_leading_junk(self):
192
md = merge_directive.MergeDirective.from_lines(self.INPUT1)
193
self.assertEqual('example:', md.revision_id)
194
self.assertEqual('sha', md.testament_sha1)
195
self.assertEqual('http://example.com', md.target_branch)
196
self.assertEqual('http://example.org', md.source_branch)
197
self.assertEqual(453, md.time)
198
self.assertEqual(120, md.timezone)
199
self.assertEqual('booga', md.patch)
200
self.assertEqual('diff', md.patch_type)
201
self.assertEqual('Hi mom!', md.message)
203
def test_roundtrip(self):
205
timezone = 7.5 * 3600
206
md = self.make_merge_directive('example:', 'sha', time, timezone,
207
'http://example.com', source_branch="http://example.org",
208
patch='booga', patch_type='diff')
209
md2 = merge_directive.MergeDirective.from_lines(md.to_lines())
210
self.assertEqual('example:', md2.revision_id)
211
self.assertIsInstance(md2.revision_id, str)
212
self.assertEqual('sha', md2.testament_sha1)
213
self.assertEqual('http://example.com', md2.target_branch)
214
self.assertEqual('http://example.org', md2.source_branch)
215
self.assertEqual(time, md2.time)
216
self.assertEqual(timezone, md2.timezone)
217
self.assertEqual('diff', md2.patch_type)
218
self.assertEqual('booga', md2.patch)
219
self.assertEqual(None, md2.message)
220
self.set_bundle(md, "# Bazaar revision bundle v0.9\n#\n")
221
md.message = "Hi mom!"
222
lines = md.to_lines()
223
md3 = merge_directive.MergeDirective.from_lines(lines)
224
self.assertEqual("# Bazaar revision bundle v0.9\n#\n", md3.bundle)
225
self.assertEqual("bundle", md3.patch_type)
226
self.assertContainsRe(md3.to_lines()[0],
227
'^# Bazaar merge directive format ')
228
self.assertEqual("Hi mom!", md3.message)
230
self.assertIs(None, md3.get_raw_bundle())
231
md4 = merge_directive.MergeDirective.from_lines(md3.to_lines())
232
self.assertIs(None, md4.patch_type)
235
class TestMergeDirective1(tests.TestCase, TestMergeDirective):
236
"""Test merge directive format 1"""
244
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
245
target_branch, patch=None, patch_type=None,
246
source_branch=None, message=None):
247
return merge_directive.MergeDirective(revision_id, testament_sha1,
248
time, timezone, target_branch, patch, patch_type,
249
source_branch, message)
252
def set_bundle(md, value):
255
def test_require_patch(self):
258
self.assertRaises(errors.PatchMissing, merge_directive.MergeDirective,
259
'example:', 'sha', time, timezone, 'http://example.com',
261
md = merge_directive.MergeDirective('example:', 'sha1', time, timezone,
262
'http://example.com', source_branch="http://example.org",
263
patch='', patch_type='diff')
264
self.assertEqual(md.patch, '')
267
class TestMergeDirective2(tests.TestCase, TestMergeDirective):
268
"""Test merge directive format 2"""
276
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
277
target_branch, patch=None, patch_type=None,
278
source_branch=None, message=None, base_revision_id='null:'):
279
if patch_type == 'bundle':
284
return merge_directive.MergeDirective2(revision_id, testament_sha1,
285
time, timezone, target_branch, patch, source_branch, message,
286
bundle, base_revision_id)
289
def set_bundle(md, value):
293
EMAIL1 = """From: "J. Random Hacker" <jrandom@example.com>
294
Subject: Commit of rev2a
296
User-Agent: Bazaar \(.*\)
298
# Bazaar merge directive format 1
300
# target_branch: (.|\n)*
302
# timestamp: 1970-01-01 00:08:56 \\+0001
303
# source_branch: (.|\n)*
307
EMAIL1_2 = """From: "J. Random Hacker" <jrandom@example.com>
308
Subject: Commit of rev2a
310
User-Agent: Bazaar \(.*\)
312
# Bazaar merge directive format 2 \\(Bazaar 0.90\\)
314
# target_branch: (.|\n)*
316
# timestamp: 1970-01-01 00:08:56 \\+0001
317
# source_branch: (.|\n)*
321
EMAIL2 = """From: "J. Random Hacker" <jrandom@example.com>
322
Subject: Commit of rev2a with special message
324
User-Agent: Bazaar \(.*\)
326
# Bazaar merge directive format 1
328
# target_branch: (.|\n)*
330
# timestamp: 1970-01-01 00:08:56 \\+0001
331
# source_branch: (.|\n)*
332
# message: Commit of rev2a with special message
335
EMAIL2_2 = """From: "J. Random Hacker" <jrandom@example.com>
336
Subject: Commit of rev2a with special message
338
User-Agent: Bazaar \(.*\)
340
# Bazaar merge directive format 2 \\(Bazaar 0.90\\)
342
# target_branch: (.|\n)*
344
# timestamp: 1970-01-01 00:08:56 \\+0001
345
# source_branch: (.|\n)*
346
# message: Commit of rev2a with special message
349
class TestMergeDirectiveBranch(object):
351
def make_trees(self):
352
tree_a = self.make_branch_and_tree('tree_a')
353
tree_a.branch.get_config().set_user_option('email',
354
'J. Random Hacker <jrandom@example.com>')
355
self.build_tree_contents([('tree_a/file', 'content_a\ncontent_b\n')])
357
tree_a.commit('message', rev_id='rev1')
358
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
359
branch_c = tree_a.bzrdir.sprout('branch_c').open_branch()
360
tree_b.commit('message', rev_id='rev2b')
361
self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c \n')])
362
tree_a.commit('Commit of rev2a', rev_id='rev2a')
363
return tree_a, tree_b, branch_c
365
def test_empty_target(self):
366
tree_a, tree_b, branch_c = self.make_trees()
367
tree_d = self.make_branch_and_tree('tree_d')
368
md2 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
369
tree_d.branch.base, patch_type='diff',
370
public_branch=tree_a.branch.base)
372
def test_generate_patch(self):
373
tree_a, tree_b, branch_c = self.make_trees()
374
md2 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
375
tree_b.branch.base, patch_type='diff',
376
public_branch=tree_a.branch.base)
377
self.assertNotContainsRe(md2.patch, 'Bazaar revision bundle')
378
self.assertContainsRe(md2.patch, '\\+content_c')
379
self.assertNotContainsRe(md2.patch, '\\+\\+\\+ b/')
380
self.assertContainsRe(md2.patch, '\\+\\+\\+ file')
382
def test_public_branch(self):
383
tree_a, tree_b, branch_c = self.make_trees()
384
self.assertRaises(errors.PublicBranchOutOfDate,
385
self.from_objects, tree_a.branch.repository, 'rev2a', 500, 144,
386
tree_b.branch.base, public_branch=branch_c.base, patch_type='diff')
387
self.assertRaises(errors.PublicBranchOutOfDate,
388
self.from_objects, tree_a.branch.repository, 'rev2a', 500, 144,
389
tree_b.branch.base, public_branch=branch_c.base, patch_type=None)
390
# public branch is not checked if patch format is bundle.
391
md1 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
392
tree_b.branch.base, public_branch=branch_c.base)
393
# public branch is provided with a bundle, despite possibly being out
394
# of date, because it's not required if a bundle is present.
395
self.assertEqual(md1.source_branch, branch_c.base)
396
# Once we update the public branch, we can generate a diff.
397
branch_c.pull(tree_a.branch)
398
md3 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
399
tree_b.branch.base, patch_type=None, public_branch=branch_c.base)
401
def test_use_public_submit_branch(self):
402
tree_a, tree_b, branch_c = self.make_trees()
403
branch_c.pull(tree_a.branch)
404
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
405
tree_b.branch.base, patch_type=None, public_branch=branch_c.base)
406
self.assertEqual(md.target_branch, tree_b.branch.base)
407
tree_b.branch.set_public_branch('http://example.com')
408
md2 = self.from_objects(
409
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
410
patch_type=None, public_branch=branch_c.base)
411
self.assertEqual(md2.target_branch, 'http://example.com')
413
def test_message(self):
414
tree_a, tree_b, branch_c = self.make_trees()
415
md3 = self.from_objects(tree_a.branch.repository, 'rev1', 500, 120,
416
tree_b.branch.base, patch_type=None, public_branch=branch_c.base,
417
message='Merge message')
419
self.assertIs(None, md3.patch)
420
self.assertEqual('Merge message', md3.message)
422
def test_generate_bundle(self):
423
tree_a, tree_b, branch_c = self.make_trees()
424
md1 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
425
tree_b.branch.base, public_branch=branch_c.base)
427
self.assertContainsRe(md1.get_raw_bundle(), 'Bazaar revision bundle')
428
self.assertContainsRe(md1.patch, '\\+content_c')
429
self.assertNotContainsRe(md1.patch, '\\+content_a')
430
self.assertContainsRe(md1.patch, '\\+content_c')
431
self.assertNotContainsRe(md1.patch, '\\+content_a')
433
def test_broken_bundle(self):
434
tree_a, tree_b, branch_c = self.make_trees()
435
md1 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
436
tree_b.branch.base, public_branch=branch_c.base)
437
lines = md1.to_lines()
438
lines = [l.replace('\n', '\r\n') for l in lines]
439
md2 = merge_directive.MergeDirective.from_lines(lines)
440
self.assertEqual('rev2a', md2.revision_id)
442
def test_signing(self):
445
class FakeBranch(object):
446
def get_config(self):
448
def gpg_signing_command(self):
450
md = self.make_merge_directive('example:', 'sha', time, timezone,
451
'http://example.com', source_branch="http://example.org",
452
patch='booga', patch_type='diff')
453
old_strategy = gpg.GPGStrategy
454
gpg.GPGStrategy = gpg.LoopbackGPGStrategy
456
signed = md.to_signed(FakeBranch())
458
gpg.GPGStrategy = old_strategy
459
self.assertContainsRe(signed, '^-----BEGIN PSEUDO-SIGNED CONTENT')
460
self.assertContainsRe(signed, 'example.org')
461
self.assertContainsRe(signed, 'booga')
463
def test_email(self):
464
tree_a, tree_b, branch_c = self.make_trees()
465
md = self.from_objects(tree_a.branch.repository, 'rev2a', 476, 60,
466
tree_b.branch.base, patch_type=None,
467
public_branch=tree_a.branch.base)
468
message = md.to_email('pqm@example.com', tree_a.branch)
469
self.assertContainsRe(message.as_string(), self.EMAIL1)
470
md.message = 'Commit of rev2a with special message'
471
message = md.to_email('pqm@example.com', tree_a.branch)
472
self.assertContainsRe(message.as_string(), self.EMAIL2)
474
def test_install_revisions_branch(self):
475
tree_a, tree_b, branch_c = self.make_trees()
476
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
477
tree_b.branch.base, patch_type=None,
478
public_branch=tree_a.branch.base)
479
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
480
revision = md.install_revisions(tree_b.branch.repository)
481
self.assertEqual('rev2a', revision)
482
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
484
def test_get_merge_request(self):
485
tree_a, tree_b, branch_c = self.make_trees()
486
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
487
tree_b.branch.base, patch_type='bundle',
488
public_branch=tree_a.branch.base)
489
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
490
md.install_revisions(tree_b.branch.repository)
491
base, revision, verified = md.get_merge_request(
492
tree_b.branch.repository)
493
if isinstance(md, merge_directive.MergeDirective):
494
self.assertIs(None, base)
495
self.assertEqual('inapplicable', verified)
497
self.assertEqual('rev1', base)
498
self.assertEqual('verified', verified)
499
self.assertEqual('rev2a', revision)
500
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
501
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
502
tree_b.branch.base, patch_type=None,
503
public_branch=tree_a.branch.base)
504
base, revision, verified = md.get_merge_request(
505
tree_b.branch.repository)
506
if isinstance(md, merge_directive.MergeDirective):
507
self.assertIs(None, base)
508
self.assertEqual('inapplicable', verified)
510
self.assertEqual('rev1', base)
511
self.assertEqual('inapplicable', verified)
512
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
513
tree_b.branch.base, patch_type='diff',
514
public_branch=tree_a.branch.base)
515
base, revision, verified = md.get_merge_request(
516
tree_b.branch.repository)
517
if isinstance(md, merge_directive.MergeDirective):
518
self.assertIs(None, base)
519
self.assertEqual('inapplicable', verified)
521
self.assertEqual('rev1', base)
522
self.assertEqual('verified', verified)
524
base, revision, verified = md.get_merge_request(
525
tree_b.branch.repository)
526
if isinstance(md, merge_directive.MergeDirective):
527
self.assertIs(None, base)
528
self.assertEqual('inapplicable', verified)
530
self.assertEqual('rev1', base)
531
self.expectFailure('Patch verification is disabled',
532
self.assertEqual, 'failed', verified)
534
def test_install_revisions_bundle(self):
535
tree_a, tree_b, branch_c = self.make_trees()
536
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
537
tree_b.branch.base, patch_type='bundle',
538
public_branch=tree_a.branch.base)
539
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
540
revision = md.install_revisions(tree_b.branch.repository)
541
self.assertEqual('rev2a', revision)
542
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
544
def test_get_target_revision_nofetch(self):
545
tree_a, tree_b, branch_c = self.make_trees()
546
tree_b.branch.fetch(tree_a.branch)
547
md = self.from_objects( tree_a.branch.repository, 'rev2a', 500, 36,
548
tree_b.branch.base, patch_type=None,
549
public_branch=tree_a.branch.base)
550
md.source_branch = '/dev/null'
551
revision = md.install_revisions(tree_b.branch.repository)
552
self.assertEqual('rev2a', revision)
555
class TestMergeDirective1Branch(tests.TestCaseWithTransport,
556
TestMergeDirectiveBranch):
557
"""Test merge directive format 1 with a branch"""
563
def from_objects(self, repository, revision_id, time, timezone,
564
target_branch, patch_type='bundle', local_target_branch=None,
565
public_branch=None, message=None):
566
return merge_directive.MergeDirective.from_objects(
567
repository, revision_id, time, timezone, target_branch,
568
patch_type, local_target_branch, public_branch, message)
570
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
571
target_branch, patch=None, patch_type=None,
572
source_branch=None, message=None):
573
return merge_directive.MergeDirective(revision_id, testament_sha1,
574
time, timezone, target_branch, patch, patch_type,
575
source_branch, message)
578
class TestMergeDirective2Branch(tests.TestCaseWithTransport,
579
TestMergeDirectiveBranch):
580
"""Test merge directive format 2 with a branch"""
586
def from_objects(self, repository, revision_id, time, timezone,
587
target_branch, patch_type='bundle', local_target_branch=None,
588
public_branch=None, message=None, base_revision_id=None):
589
include_patch = (patch_type in ('bundle', 'diff'))
590
include_bundle = (patch_type == 'bundle')
591
assert patch_type in ('bundle', 'diff', None)
592
return merge_directive.MergeDirective2.from_objects(
593
repository, revision_id, time, timezone, target_branch,
594
include_patch, include_bundle, local_target_branch, public_branch,
595
message, base_revision_id)
597
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
598
target_branch, patch=None, patch_type=None,
599
source_branch=None, message=None, base_revision_id='null:'):
600
if patch_type == 'bundle':
605
return merge_directive.MergeDirective2(revision_id, testament_sha1,
606
time, timezone, target_branch, patch, source_branch, message,
607
bundle, base_revision_id)
609
def test_base_revision(self):
610
tree_a, tree_b, branch_c = self.make_trees()
611
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
612
tree_b.branch.base, patch_type='bundle',
613
public_branch=tree_a.branch.base, base_revision_id=None)
614
self.assertEqual('rev1', md.base_revision_id)
615
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
616
tree_b.branch.base, patch_type='bundle',
617
public_branch=tree_a.branch.base, base_revision_id='null:')
618
self.assertEqual('null:', md.base_revision_id)
619
lines = md.to_lines()
620
md2 = merge_directive.MergeDirective.from_lines(lines)
621
self.assertEqual(md2.base_revision_id, md.base_revision_id)
623
def test_patch_verification(self):
624
tree_a, tree_b, branch_c = self.make_trees()
625
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
626
tree_b.branch.base, patch_type='bundle',
627
public_branch=tree_a.branch.base)
628
lines = md.to_lines()
629
md2 = merge_directive.MergeDirective.from_lines(lines)
630
md2._verify_patch(tree_a.branch.repository)
631
# Stript trailing whitespace
632
md2.patch = md2.patch.replace(' \n', '\n')
633
md2._verify_patch(tree_a.branch.repository)
634
# Convert to Mac line-endings
635
md2.patch = md2.patch.replace('\n', '\r')
636
self.assertTrue(md2._verify_patch(tree_a.branch.repository))
637
# Convert to DOS line-endings
638
md2.patch = md2.patch.replace('\r', '\r\n')
639
self.assertTrue(md2._verify_patch(tree_a.branch.repository))
640
md2.patch = md2.patch.replace('content_c', 'content_d')
641
self.assertFalse(md2._verify_patch(tree_a.branch.repository))
644
class TestParseOldMergeDirective2(tests.TestCase):
646
def test_parse_old_merge_directive(self):
647
md = merge_directive.MergeDirective.from_lines(INPUT1_2_OLD)
648
self.assertEqual('example:', md.revision_id)
649
self.assertEqual('sha', md.testament_sha1)
650
self.assertEqual('http://example.com', md.target_branch)
651
self.assertEqual('http://example.org', md.source_branch)
652
self.assertEqual(453, md.time)
653
self.assertEqual(120, md.timezone)
654
self.assertEqual('booga', md.patch)
655
self.assertEqual('diff', md.patch_type)
656
self.assertEqual('Hi mom!', md.message)