1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from bzrlib import (
errors,
gpg,
merge_directive,
tests,
)
OUTPUT1 = """# Bazaar merge directive format 1
# revision_id: example:
# target_branch: http://example.com
# testament_sha1: sha
# timestamp: 1970-01-01 00:09:33 +0002
#\x20
booga"""
OUTPUT2 = """# Bazaar merge directive format 1
# revision_id: example:
# target_branch: http://example.com
# testament_sha1: sha
# timestamp: 1970-01-01 00:09:33 +0002
# source_branch: http://example.org
# message: Hi mom!
#\x20
booga"""
INPUT1 = """
I was thinking today about creating a merge directive.
So I did.
Here it is.
(I've pasted it in the body of this message)
Aaron
# Bazaar merge directive format 1\r
# revision_id: example:
# target_branch: http://example.com
# testament_sha1: sha
# timestamp: 1970-01-01 00:09:33 +0002
# source_branch: http://example.org
# message: Hi mom!
#\x20
booga""".splitlines(True)
class TestMergeDirective(tests.TestCase):
def test_merge_source(self):
time = 500000.0
timezone = 5 * 3600
self.assertRaises(errors.NoMergeSource, merge_directive.MergeDirective,
'example:', 'sha', time, timezone, 'http://example.com')
self.assertRaises(errors.NoMergeSource, merge_directive.MergeDirective,
'example:', 'sha', time, timezone, 'http://example.com',
patch_type='diff')
merge_directive.MergeDirective('example:', 'sha', time, timezone,
'http://example.com', source_branch='http://example.org')
md = merge_directive.MergeDirective('null:', 'sha', time, timezone,
'http://example.com', patch='blah', patch_type='bundle')
self.assertIs(None, md.source_branch)
md2 = merge_directive.MergeDirective('null:', 'sha', time, timezone,
'http://example.com', patch='blah', patch_type='bundle',
source_branch='bar')
self.assertEqual('bar', md2.source_branch)
def test_require_patch(self):
time = 500.0
timezone = 120
self.assertRaises(errors.PatchMissing, merge_directive.MergeDirective,
'example:', 'sha', time, timezone, 'http://example.com',
patch_type='bundle')
md = merge_directive.MergeDirective('example:', 'sha1', time, timezone,
'http://example.com', source_branch="http://example.org",
patch='', patch_type='diff')
self.assertEqual(md.patch, '')
def test_serialization(self):
time = 453
timezone = 120
md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
'http://example.com', patch='booga', patch_type='bundle')
self.assertEqualDiff(OUTPUT1, ''.join(md.to_lines()))
md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
'http://example.com', source_branch="http://example.org",
patch='booga', patch_type='diff', message="Hi mom!")
self.assertEqualDiff(OUTPUT2, ''.join(md.to_lines()))
def test_deserialize_junk(self):
time = 501
self.assertRaises(errors.NotAMergeDirective,
merge_directive.MergeDirective.from_lines, 'lala')
def test_deserialize_empty(self):
self.assertRaises(errors.NotAMergeDirective,
merge_directive.MergeDirective.from_lines, [])
def test_deserialize_leading_junk(self):
md = merge_directive.MergeDirective.from_lines(INPUT1)
self.assertEqual('example:', md.revision_id)
self.assertEqual('sha', md.testament_sha1)
self.assertEqual('http://example.com', md.target_branch)
self.assertEqual('http://example.org', md.source_branch)
self.assertEqual(453, md.time)
self.assertEqual(120, md.timezone)
self.assertEqual('booga', md.patch)
self.assertEqual('diff', md.patch_type)
self.assertEqual('Hi mom!', md.message)
def test_roundtrip(self):
time = 500000
timezone = 7.5 * 3600
md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
'http://example.com', source_branch="http://example.org",
patch='booga', patch_type='diff')
md2 = merge_directive.MergeDirective.from_lines(md.to_lines())
self.assertEqual('example:', md2.revision_id)
self.assertIsInstance(md2.revision_id, str)
self.assertEqual('sha', md2.testament_sha1)
self.assertEqual('http://example.com', md2.target_branch)
self.assertEqual('http://example.org', md2.source_branch)
self.assertEqual(time, md2.time)
self.assertEqual(timezone, md2.timezone)
self.assertEqual('diff', md2.patch_type)
self.assertEqual('booga', md2.patch)
self.assertEqual(None, md2.message)
md.patch = "# Bazaar revision bundle v0.9\n#\n"
md.message = "Hi mom!"
md3 = merge_directive.MergeDirective.from_lines(md.to_lines())
self.assertEqual("# Bazaar revision bundle v0.9\n#\n", md3.patch)
self.assertEqual("bundle", md3.patch_type)
self.assertContainsRe(md3.to_lines()[0],
'^# Bazaar merge directive format ')
self.assertEqual("Hi mom!", md3.message)
md3.patch_type = None
md3.patch = None
md4 = merge_directive.MergeDirective.from_lines(md3.to_lines())
self.assertIs(None, md4.patch_type)
EMAIL1 = """To: pqm@example.com
From: J. Random Hacker <jrandom@example.com>
Subject: Commit of rev2a
# Bazaar merge directive format 1
# revision_id: rev2a
# target_branch: (.|\n)*
# testament_sha1: .*
# timestamp: 1970-01-01 00:08:56 \\+0001
# source_branch: (.|\n)*
"""
EMAIL2 = """To: pqm@example.com
From: J. Random Hacker <jrandom@example.com>
Subject: Commit of rev2a with special message
# Bazaar merge directive format 1
# revision_id: rev2a
# target_branch: (.|\n)*
# testament_sha1: .*
# timestamp: 1970-01-01 00:08:56 \\+0001
# source_branch: (.|\n)*
# message: Commit of rev2a with special message
"""
class TestMergeDirectiveBranch(tests.TestCaseWithTransport):
def make_trees(self):
tree_a = self.make_branch_and_tree('tree_a')
tree_a.branch.get_config().set_user_option('email',
'J. Random Hacker <jrandom@example.com>')
self.build_tree_contents([('tree_a/file', 'content_a\ncontent_b\n')])
tree_a.add('file')
tree_a.commit('message', rev_id='rev1')
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
branch_c = tree_a.bzrdir.sprout('branch_c').open_branch()
tree_b.commit('message', rev_id='rev2b')
self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c\n')])
tree_a.commit('Commit of rev2a', rev_id='rev2a')
return tree_a, tree_b, branch_c
def test_generate_patch(self):
tree_a, tree_b, branch_c = self.make_trees()
md2 = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 120, tree_b.branch.base,
patch_type='diff', public_branch=tree_a.branch.base)
self.assertNotContainsRe(md2.patch, 'Bazaar revision bundle')
self.assertContainsRe(md2.patch, '\\+content_c')
self.assertNotContainsRe(md2.patch, '\\+\\+\\+ b/')
self.assertContainsRe(md2.patch, '\\+\\+\\+ file')
def test_public_branch(self):
tree_a, tree_b, branch_c = self.make_trees()
self.assertRaises(errors.PublicBranchOutOfDate,
merge_directive.MergeDirective.from_objects,
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
public_branch=branch_c.base, patch_type='diff')
# public branch is not checked if patch format is bundle.
md1 = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
public_branch=branch_c.base)
# public branch is provided with a bundle, despite possibly being out
# of date, because it's not required if a bundle is present.
self.assertEqual(md1.source_branch, branch_c.base)
# Once we update the public branch, we can generate a diff.
branch_c.pull(tree_a.branch)
md3 = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
patch_type=None, public_branch=branch_c.base)
def test_use_public_submit_branch(self):
tree_a, tree_b, branch_c = self.make_trees()
branch_c.pull(tree_a.branch)
md = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
patch_type=None, public_branch=branch_c.base)
self.assertEqual(md.target_branch, tree_b.branch.base)
tree_b.branch.set_public_branch('http://example.com')
md2 = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
patch_type=None, public_branch=branch_c.base)
self.assertEqual(md2.target_branch, 'http://example.com')
def test_message(self):
tree_a, tree_b, branch_c = self.make_trees()
md3 = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 120, tree_b.branch.base,
patch_type=None, public_branch=branch_c.base,
message='Merge message')
md3.to_lines()
self.assertIs(None, md3.patch)
self.assertEqual('Merge message', md3.message)
def test_generate_bundle(self):
tree_a, tree_b, branch_c = self.make_trees()
md1 = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 120, tree_b.branch.base,
public_branch=branch_c.base)
self.assertContainsRe(md1.patch, 'Bazaar revision bundle')
self.assertContainsRe(md1.patch, '\\+content_c')
self.assertNotContainsRe(md1.patch, '\\+content_a')
self.assertContainsRe(md1.patch, '\\+content_c')
self.assertNotContainsRe(md1.patch, '\\+content_a')
def test_signing(self):
time = 453
timezone = 7200
class FakeBranch(object):
def get_config(self):
return self
def gpg_signing_command(self):
return 'loopback'
md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
'http://example.com', source_branch="http://example.org",
patch='booga', patch_type='diff')
old_strategy = gpg.GPGStrategy
gpg.GPGStrategy = gpg.LoopbackGPGStrategy
try:
signed = md.to_signed(FakeBranch())
finally:
gpg.GPGStrategy = old_strategy
self.assertContainsRe(signed, '^-----BEGIN PSEUDO-SIGNED CONTENT')
self.assertContainsRe(signed, 'example.org')
self.assertContainsRe(signed, 'booga')
def test_email(self):
tree_a, tree_b, branch_c = self.make_trees()
md = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 476, 60, tree_b.branch.base,
patch_type=None, public_branch=tree_a.branch.base)
message = md.to_email('pqm@example.com', tree_a.branch)
self.assertContainsRe(message.as_string(), EMAIL1)
md.message = 'Commit of rev2a with special message'
message = md.to_email('pqm@example.com', tree_a.branch)
self.assertContainsRe(message.as_string(), EMAIL2)
def test_install_revisions_branch(self):
tree_a, tree_b, branch_c = self.make_trees()
md = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
patch_type=None, public_branch=tree_a.branch.base)
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
revision = md.install_revisions(tree_b.branch.repository)
self.assertEqual('rev2a', revision)
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
def test_install_revisions_bundle(self):
tree_a, tree_b, branch_c = self.make_trees()
md = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
patch_type='bundle', public_branch=tree_a.branch.base)
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
revision = md.install_revisions(tree_b.branch.repository)
self.assertEqual('rev2a', revision)
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
def test_get_target_revision_nofetch(self):
tree_a, tree_b, branch_c = self.make_trees()
tree_b.branch.fetch(tree_a.branch)
md = merge_directive.MergeDirective.from_objects(
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
patch_type=None, public_branch=tree_a.branch.base)
md.source_branch = '/dev/null'
revision = md.install_revisions(tree_b.branch.repository)
self.assertEqual('rev2a', revision)
|