Merge lp:~jelmer/brz/write-group-context into lp:brz
- write-group-context
- Merge into trunk
Proposed by
Jelmer Vernooij
Status: | Merged |
---|---|
Approved by: | Jelmer Vernooij |
Approved revision: | no longer in the source branch. |
Merge reported by: | The Breezy Bot |
Merged at revision: | not available |
Proposed branch: | lp:~jelmer/brz/write-group-context |
Merge into: | lp:brz |
Diff against target: |
857 lines (+116/-282) 20 files modified
breezy/builtins.py (+4/-21) breezy/bundle/serializer/v4.py (+3/-8) breezy/bzr/vf_repository.py (+2/-7) breezy/commit_signature_commands.py (+2/-7) breezy/plugins/repodebug/missing_keys_for_stacking_fixer.py (+2/-7) breezy/repository.py (+22/-0) breezy/tests/blackbox/test_reconcile.py (+3/-4) breezy/tests/per_controldir/test_controldir.py (+3/-5) breezy/tests/per_interrepository/test_fetch.py (+3/-13) breezy/tests/per_interrepository/test_interrepository.py (+9/-13) breezy/tests/per_pack_repository.py (+4/-19) breezy/tests/per_repository/test_signatures.py (+2/-12) breezy/tests/per_repository_chk/test_supported.py (+15/-51) breezy/tests/per_repository_reference/test_add_inventory.py (+3/-12) breezy/tests/per_repository_reference/test_add_revision.py (+5/-14) breezy/tests/per_repository_reference/test_add_signature_text.py (+6/-14) breezy/tests/per_repository_vf/helpers.py (+5/-13) breezy/tests/per_repository_vf/test_check_reconcile.py (+4/-9) breezy/tests/per_repository_vf/test_repository.py (+6/-21) breezy/tests/test_inv.py (+13/-32) |
To merge this branch: | bzr merge lp:~jelmer/brz/write-group-context |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Martin Packman | Approve | ||
Review via email: mp+358465@code.launchpad.net |
Commit message
Add a WriteGroup() context manager.
Description of the change
Add a WriteGroup() context manager.
To post a comment you must log in.
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote : | # |
Merging failed
https:/
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote : | # |
Merging failed
https:/
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote : | # |
Merging failed
https:/
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote : | # |
Running landing tests failed
https:/
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'breezy/builtins.py' |
2 | --- breezy/builtins.py 2019-05-28 21:53:23 +0000 |
3 | +++ breezy/builtins.py 2019-05-29 03:45:01 +0000 |
4 | @@ -5260,29 +5260,18 @@ |
5 | return self._run(b, revision_id_list, revision) |
6 | |
7 | def _run(self, b, revision_id_list, revision): |
8 | + from .repository import WriteGroup |
9 | gpg_strategy = gpg.GPGStrategy(b.get_config_stack()) |
10 | if revision_id_list is not None: |
11 | - b.repository.start_write_group() |
12 | - try: |
13 | + with WriteGroup(b.repository): |
14 | for revision_id in revision_id_list: |
15 | revision_id = cache_utf8.encode(revision_id) |
16 | b.repository.sign_revision(revision_id, gpg_strategy) |
17 | - except BaseException: |
18 | - b.repository.abort_write_group() |
19 | - raise |
20 | - else: |
21 | - b.repository.commit_write_group() |
22 | elif revision is not None: |
23 | if len(revision) == 1: |
24 | revno, rev_id = revision[0].in_history(b) |
25 | - b.repository.start_write_group() |
26 | - try: |
27 | + with WriteGroup(b.repository): |
28 | b.repository.sign_revision(rev_id, gpg_strategy) |
29 | - except BaseException: |
30 | - b.repository.abort_write_group() |
31 | - raise |
32 | - else: |
33 | - b.repository.commit_write_group() |
34 | elif len(revision) == 2: |
35 | # are they both on rh- if so we can walk between them |
36 | # might be nice to have a range helper for arbitrary |
37 | @@ -5294,16 +5283,10 @@ |
38 | if from_revno is None or to_revno is None: |
39 | raise errors.BzrCommandError( |
40 | gettext('Cannot sign a range of non-revision-history revisions')) |
41 | - b.repository.start_write_group() |
42 | - try: |
43 | + with WriteGroup(b.repository): |
44 | for revno in range(from_revno, to_revno + 1): |
45 | b.repository.sign_revision(b.get_rev_id(revno), |
46 | gpg_strategy) |
47 | - except BaseException: |
48 | - b.repository.abort_write_group() |
49 | - raise |
50 | - else: |
51 | - b.repository.commit_write_group() |
52 | else: |
53 | raise errors.BzrCommandError( |
54 | gettext('Please supply either one revision, or a range.')) |
55 | |
56 | === modified file 'breezy/bundle/serializer/v4.py' |
57 | --- breezy/bundle/serializer/v4.py 2018-11-11 04:08:32 +0000 |
58 | +++ breezy/bundle/serializer/v4.py 2019-05-29 03:45:01 +0000 |
59 | @@ -26,6 +26,7 @@ |
60 | lru_cache, |
61 | multiparent, |
62 | osutils, |
63 | + repository as _mod_repository, |
64 | revision as _mod_revision, |
65 | trace, |
66 | ui, |
67 | @@ -519,14 +520,8 @@ |
68 | |
69 | Must be called with the Repository locked. |
70 | """ |
71 | - self._repository.start_write_group() |
72 | - try: |
73 | - result = self._install_in_write_group() |
74 | - except: |
75 | - self._repository.abort_write_group() |
76 | - raise |
77 | - self._repository.commit_write_group() |
78 | - return result |
79 | + with _mod_repository.WriteGroup(self._repository): |
80 | + return self._install_in_write_group() |
81 | |
82 | def _install_in_write_group(self): |
83 | current_file = None |
84 | |
85 | === modified file 'breezy/bzr/vf_repository.py' |
86 | --- breezy/bzr/vf_repository.py 2019-02-04 19:39:30 +0000 |
87 | +++ breezy/bzr/vf_repository.py 2019-05-29 03:45:01 +0000 |
88 | @@ -68,6 +68,7 @@ |
89 | InterRepository, |
90 | Repository, |
91 | RepositoryFormat, |
92 | + WriteGroup, |
93 | ) |
94 | from .repository import ( |
95 | MetaDirRepository, |
96 | @@ -2863,8 +2864,7 @@ |
97 | Accepts an iterable of revision, tree, signature tuples. The signature |
98 | may be None. |
99 | """ |
100 | - repository.start_write_group() |
101 | - try: |
102 | + with WriteGroup(repository): |
103 | inventory_cache = lru_cache.LRUCache(10) |
104 | for n, (revision, revision_tree, signature) in enumerate(iterable): |
105 | _install_revision(repository, revision, revision_tree, signature, |
106 | @@ -2872,11 +2872,6 @@ |
107 | if pb is not None: |
108 | pb.update(gettext('Transferring revisions'), |
109 | n + 1, num_revisions) |
110 | - except: |
111 | - repository.abort_write_group() |
112 | - raise |
113 | - else: |
114 | - repository.commit_write_group() |
115 | |
116 | |
117 | def _install_revision(repository, rev, revision_tree, signature, |
118 | |
119 | === modified file 'breezy/commit_signature_commands.py' |
120 | --- breezy/commit_signature_commands.py 2018-11-12 01:41:38 +0000 |
121 | +++ breezy/commit_signature_commands.py 2019-05-29 03:45:01 +0000 |
122 | @@ -22,6 +22,7 @@ |
123 | controldir, |
124 | errors, |
125 | gpg, |
126 | + repository as _mod_repository, |
127 | revision as _mod_revision, |
128 | ) |
129 | from .commands import Command |
130 | @@ -66,8 +67,7 @@ |
131 | count = 0 |
132 | with repo.lock_write(): |
133 | graph = repo.get_graph() |
134 | - repo.start_write_group() |
135 | - try: |
136 | + with _mod_repository.WriteGroup(repo): |
137 | for rev_id, parents in graph.iter_ancestry( |
138 | [branch.last_revision()]): |
139 | if _mod_revision.is_null(rev_id): |
140 | @@ -86,11 +86,6 @@ |
141 | count += 1 |
142 | if not dry_run: |
143 | repo.sign_revision(rev_id, gpg_strategy) |
144 | - except BaseException: |
145 | - repo.abort_write_group() |
146 | - raise |
147 | - else: |
148 | - repo.commit_write_group() |
149 | self.outf.write( |
150 | ngettext('Signed %d revision.\n', 'Signed %d revisions.\n', |
151 | count) % count) |
152 | |
153 | === modified file 'breezy/plugins/repodebug/missing_keys_for_stacking_fixer.py' |
154 | --- breezy/plugins/repodebug/missing_keys_for_stacking_fixer.py 2018-11-11 04:08:32 +0000 |
155 | +++ breezy/plugins/repodebug/missing_keys_for_stacking_fixer.py 2019-05-29 03:45:01 +0000 |
156 | @@ -21,6 +21,7 @@ |
157 | from ... import errors |
158 | from ...sixish import viewvalues |
159 | from ...bzr.vf_search import PendingAncestryResult |
160 | +from ...repository import WriteGroup |
161 | from ...revision import NULL_REVISION |
162 | |
163 | |
164 | @@ -77,14 +78,8 @@ |
165 | assert raw_r._format.network_name() == b.repository._format.network_name() |
166 | stream = b.repository.inventories.get_record_stream( |
167 | needed, 'topological', True) |
168 | - raw_r.start_write_group() |
169 | - try: |
170 | + with WriteGroup(raw_r): |
171 | raw_r.inventories.insert_record_stream(stream) |
172 | - except: |
173 | - raw_r.abort_write_group() |
174 | - raise |
175 | - else: |
176 | - raw_r.commit_write_group() |
177 | finally: |
178 | raw_r.unlock() |
179 | b.unlock() |
180 | |
181 | === modified file 'breezy/repository.py' |
182 | --- breezy/repository.py 2019-02-04 19:39:30 +0000 |
183 | +++ breezy/repository.py 2019-05-29 03:45:01 +0000 |
184 | @@ -236,6 +236,28 @@ |
185 | self.unlock) |
186 | |
187 | |
188 | +class WriteGroup(object): |
189 | + """Context manager that manages a write group. |
190 | + |
191 | + Raising an exception will result in the write group being aborted. |
192 | + """ |
193 | + |
194 | + def __init__(self, repository, suppress_errors=False): |
195 | + self.repository = repository |
196 | + self._suppress_errors = suppress_errors |
197 | + |
198 | + def __enter__(self): |
199 | + self.repository.start_write_group() |
200 | + return self |
201 | + |
202 | + def __exit__(self, exc_type, exc_val, exc_tb): |
203 | + if exc_type: |
204 | + self.repository.abort_write_group(self._suppress_errors) |
205 | + return False |
206 | + else: |
207 | + self.repository.commit_write_group() |
208 | + |
209 | + |
210 | ###################################################################### |
211 | # Repositories |
212 | |
213 | |
214 | === modified file 'breezy/tests/blackbox/test_reconcile.py' |
215 | --- breezy/tests/blackbox/test_reconcile.py 2018-11-11 04:08:32 +0000 |
216 | +++ breezy/tests/blackbox/test_reconcile.py 2019-05-29 03:45:01 +0000 |
217 | @@ -21,6 +21,7 @@ |
218 | controldir, |
219 | tests, |
220 | ) |
221 | +from breezy.repository import WriteGroup |
222 | from breezy.bzr import ( |
223 | inventory, |
224 | ) |
225 | @@ -53,10 +54,8 @@ |
226 | inv = inventory.Inventory(revision_id=b'missing') |
227 | inv.root.revision = b'missing' |
228 | repo.lock_write() |
229 | - repo.start_write_group() |
230 | - repo.add_inventory(b'missing', inv, []) |
231 | - repo.commit_write_group() |
232 | - repo.unlock() |
233 | + with repo.lock_write(), WriteGroup(repo): |
234 | + repo.add_inventory(b'missing', inv, []) |
235 | (out, err) = self.run_bzr('reconcile') |
236 | if repo._reconcile_backsup_inventory: |
237 | does_backup_text = ( |
238 | |
239 | === modified file 'breezy/tests/per_controldir/test_controldir.py' |
240 | --- breezy/tests/per_controldir/test_controldir.py 2018-11-11 04:08:32 +0000 |
241 | +++ breezy/tests/per_controldir/test_controldir.py 2019-05-29 03:45:01 +0000 |
242 | @@ -24,6 +24,7 @@ |
243 | errors, |
244 | gpg, |
245 | osutils, |
246 | + repository as _mod_repository, |
247 | revision as _mod_revision, |
248 | transport, |
249 | ui, |
250 | @@ -319,11 +320,8 @@ |
251 | tree_repo = tree.branch.repository |
252 | if not tree_repo._format.supports_revision_signatures: |
253 | self.skipTest('repository format does not support signing') |
254 | - tree_repo.lock_write() |
255 | - tree_repo.start_write_group() |
256 | - tree_repo.sign_revision(rev1, gpg.LoopbackGPGStrategy(None)) |
257 | - tree_repo.commit_write_group() |
258 | - tree_repo.unlock() |
259 | + with tree_repo.lock_write(), _mod_repository.WriteGroup(tree_repo): |
260 | + tree_repo.sign_revision(rev1, gpg.LoopbackGPGStrategy(None)) |
261 | target = self.make_branch('target') |
262 | tree.branch.repository.copy_content_into(target.repository) |
263 | tree.branch.copy_content_into(target) |
264 | |
265 | === modified file 'breezy/tests/per_interrepository/test_fetch.py' |
266 | --- breezy/tests/per_interrepository/test_fetch.py 2018-11-11 04:08:32 +0000 |
267 | +++ breezy/tests/per_interrepository/test_fetch.py 2019-05-29 03:45:01 +0000 |
268 | @@ -32,6 +32,7 @@ |
269 | from breezy.bzr.vf_search import ( |
270 | SearchResult, |
271 | ) |
272 | +from breezy.repository import WriteGroup |
273 | from breezy.revision import ( |
274 | NULL_REVISION, |
275 | Revision, |
276 | @@ -110,8 +111,7 @@ |
277 | source = tree.branch.repository |
278 | source.lock_write() |
279 | self.addCleanup(source.unlock) |
280 | - source.start_write_group() |
281 | - try: |
282 | + with WriteGroup(source): |
283 | # We need two revisions: OLD and NEW. NEW will claim to need a file |
284 | # 'FOO' changed in 'OLD'. OLD will not have that file at all. |
285 | source.texts.insert_record_stream([ |
286 | @@ -133,11 +133,6 @@ |
287 | revision_id=b'new', |
288 | parent_ids=[revid]) |
289 | source.add_revision(rev.revision_id, rev) |
290 | - except: |
291 | - source.abort_write_group() |
292 | - raise |
293 | - else: |
294 | - source.commit_write_group() |
295 | to_repo.fetch(source, b'new') |
296 | to_repo.lock_read() |
297 | self.addCleanup(to_repo.unlock) |
298 | @@ -448,17 +443,12 @@ |
299 | # We build a broken revision so that we can test the fetch code dies |
300 | # properly. So copy the inventory and revision, but not the text. |
301 | with to_repo.lock_write(): |
302 | - to_repo.start_write_group() |
303 | - try: |
304 | + with WriteGroup(to_repo, suppress_errors=True): |
305 | inv = tree.branch.repository.get_inventory(rev1) |
306 | to_repo.add_inventory(rev1, inv, []) |
307 | rev = tree.branch.repository.get_revision(rev1) |
308 | to_repo.add_revision(rev1, rev, inv=inv) |
309 | self.disable_commit_write_group_paranoia(to_repo) |
310 | - to_repo.commit_write_group() |
311 | - except: |
312 | - to_repo.abort_write_group(suppress_errors=True) |
313 | - raise |
314 | |
315 | # Implementations can either ensure that the target of the delta is |
316 | # reconstructable, or raise an exception (which stream based copies |
317 | |
318 | === modified file 'breezy/tests/per_interrepository/test_interrepository.py' |
319 | --- breezy/tests/per_interrepository/test_interrepository.py 2018-11-11 04:08:32 +0000 |
320 | +++ breezy/tests/per_interrepository/test_interrepository.py 2019-05-29 03:45:01 +0000 |
321 | @@ -22,6 +22,7 @@ |
322 | import breezy.errors as errors |
323 | import breezy.gpg |
324 | from breezy.bzr.inventory import Inventory |
325 | +from breezy.repository import WriteGroup |
326 | from breezy.revision import NULL_REVISION |
327 | from breezy.tests import ( |
328 | TestNotApplicable, |
329 | @@ -65,13 +66,10 @@ |
330 | tree_a = self.make_branch_and_tree('a') |
331 | self.controldir = tree_a.branch.controldir |
332 | # add a corrupt inventory 'orphan' |
333 | - tree_a.branch.repository.lock_write() |
334 | - tree_a.branch.repository.start_write_group() |
335 | - if tree_a.branch.repository._format.supports_ghosts: |
336 | - inv_file = tree_a.branch.repository.inventories |
337 | - inv_file.add_lines((b'orphan',), [], []) |
338 | - tree_a.branch.repository.commit_write_group() |
339 | - tree_a.branch.repository.unlock() |
340 | + with tree_a.branch.repository.lock_write(), WriteGroup(tree_a.branch.repository): |
341 | + if tree_a.branch.repository._format.supports_ghosts: |
342 | + inv_file = tree_a.branch.repository.inventories |
343 | + inv_file.add_lines((b'orphan',), [], []) |
344 | # add a real revision 'rev1' |
345 | self.rev1 = tree_a.commit('rev1', allow_pointless=True) |
346 | # add a real revision 'rev2' based on rev1 |
347 | @@ -149,12 +147,10 @@ |
348 | 'to repository does not support signatures') |
349 | # and sign 'rev2' |
350 | tree_a = WorkingTree.open('a') |
351 | - tree_a.branch.repository.lock_write() |
352 | - tree_a.branch.repository.start_write_group() |
353 | - tree_a.branch.repository.sign_revision(self.rev2, |
354 | - breezy.gpg.LoopbackGPGStrategy(None)) |
355 | - tree_a.branch.repository.commit_write_group() |
356 | - tree_a.branch.repository.unlock() |
357 | + with tree_a.branch.repository.lock_write(), \ |
358 | + WriteGroup(tree_a.branch.repository): |
359 | + tree_a.branch.repository.sign_revision( |
360 | + self.rev2, breezy.gpg.LoopbackGPGStrategy(None)) |
361 | |
362 | from_repo = self.controldir.open_repository() |
363 | from_signature = from_repo.get_signature_text(self.rev2) |
364 | |
365 | === modified file 'breezy/tests/per_pack_repository.py' |
366 | --- breezy/tests/per_pack_repository.py 2019-01-09 00:04:02 +0000 |
367 | +++ breezy/tests/per_pack_repository.py 2019-05-29 03:45:01 +0000 |
368 | @@ -473,26 +473,15 @@ |
369 | r1 = repository.Repository.open('.') |
370 | r2 = repository.Repository.open('.') |
371 | # add a pack to drop |
372 | - r1.lock_write() |
373 | - try: |
374 | - r1.start_write_group() |
375 | - try: |
376 | + with r1.lock_write(): |
377 | + with repository.WriteGroup(r1): |
378 | self._add_text(r1, b'fileidr1') |
379 | - except: |
380 | - r1.abort_write_group() |
381 | - raise |
382 | - else: |
383 | - r1.commit_write_group() |
384 | r1._pack_collection.ensure_loaded() |
385 | name_to_drop = r1._pack_collection.all_packs()[0].name |
386 | - finally: |
387 | - r1.unlock() |
388 | - r1.lock_write() |
389 | - try: |
390 | + with r1.lock_write(): |
391 | # access enough data to load the names list |
392 | list(r1.all_revision_ids()) |
393 | - r2.lock_write() |
394 | - try: |
395 | + with r2.lock_write(): |
396 | # access enough data to load the names list |
397 | list(r2.all_revision_ids()) |
398 | r1._pack_collection.ensure_loaded() |
399 | @@ -533,10 +522,6 @@ |
400 | r2._pack_collection.names()) |
401 | self.assertEqual(1, len(r1._pack_collection.names())) |
402 | self.assertFalse(name_to_drop in r1._pack_collection.names()) |
403 | - finally: |
404 | - r2.unlock() |
405 | - finally: |
406 | - r1.unlock() |
407 | |
408 | def test_concurrent_pack_triggers_reload(self): |
409 | # create 2 packs, which we will then collapse |
410 | |
411 | === modified file 'breezy/tests/per_repository/test_signatures.py' |
412 | --- breezy/tests/per_repository/test_signatures.py 2018-11-29 23:42:41 +0000 |
413 | +++ breezy/tests/per_repository/test_signatures.py 2019-05-29 03:45:01 +0000 |
414 | @@ -22,7 +22,7 @@ |
415 | tests, |
416 | urlutils, |
417 | ) |
418 | - |
419 | +from breezy.repository import WriteGroup |
420 | from breezy.bzr.testament import Testament |
421 | from breezy.tests import per_repository |
422 | |
423 | @@ -58,24 +58,14 @@ |
424 | def test_store_signature(self): |
425 | wt = self.make_branch_and_tree('.') |
426 | branch = wt.branch |
427 | - branch.lock_write() |
428 | - try: |
429 | - branch.repository.start_write_group() |
430 | + with branch.lock_write(), WriteGroup(branch.repository): |
431 | try: |
432 | branch.repository.store_revision_signature( |
433 | gpg.LoopbackGPGStrategy(None), b'FOO', b'A') |
434 | except errors.NoSuchRevision: |
435 | - branch.repository.abort_write_group() |
436 | raise tests.TestNotApplicable( |
437 | "repository does not support signing non-present" |
438 | "revisions") |
439 | - except: |
440 | - branch.repository.abort_write_group() |
441 | - raise |
442 | - else: |
443 | - branch.repository.commit_write_group() |
444 | - finally: |
445 | - branch.unlock() |
446 | # A signature without a revision should not be accessible. |
447 | self.assertRaises(errors.NoSuchRevision, |
448 | branch.repository.has_signature_for_revision_id, |
449 | |
450 | === modified file 'breezy/tests/per_repository_chk/test_supported.py' |
451 | --- breezy/tests/per_repository_chk/test_supported.py 2018-11-16 11:37:47 +0000 |
452 | +++ breezy/tests/per_repository_chk/test_supported.py 2019-05-29 03:45:01 +0000 |
453 | @@ -38,24 +38,14 @@ |
454 | |
455 | def test_add_bytes_to_chk_bytes_store(self): |
456 | repo = self.make_repository('.') |
457 | - repo.lock_write() |
458 | - try: |
459 | - repo.start_write_group() |
460 | - try: |
461 | - sha1, len, _ = repo.chk_bytes.add_lines((None,), |
462 | - None, [b"foo\n", b"bar\n"], random_id=True) |
463 | - self.assertEqual(b'4e48e2c9a3d2ca8a708cb0cc545700544efb5021', |
464 | - sha1) |
465 | - self.assertEqual( |
466 | - {(b'sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)}, |
467 | - repo.chk_bytes.keys()) |
468 | - except: |
469 | - repo.abort_write_group() |
470 | - raise |
471 | - else: |
472 | - repo.commit_write_group() |
473 | - finally: |
474 | - repo.unlock() |
475 | + with repo.lock_write(), repository.WriteGroup(repo): |
476 | + sha1, len, _ = repo.chk_bytes.add_lines( |
477 | + (None,), None, [b"foo\n", b"bar\n"], random_id=True) |
478 | + self.assertEqual( |
479 | + b'4e48e2c9a3d2ca8a708cb0cc545700544efb5021', sha1) |
480 | + self.assertEqual( |
481 | + {(b'sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)}, |
482 | + repo.chk_bytes.keys()) |
483 | # And after an unlock/lock pair |
484 | with repo.lock_read(): |
485 | self.assertEqual( |
486 | @@ -76,46 +66,25 @@ |
487 | node_sha1 = osutils.sha_strings(node_lines) |
488 | expected_set = {(b'sha1:' + leaf_sha1,), (b'sha1:' + node_sha1,)} |
489 | repo = self.make_repository('.') |
490 | - repo.lock_write() |
491 | - try: |
492 | - repo.start_write_group() |
493 | - try: |
494 | + with repo.lock_write(): |
495 | + with repository.WriteGroup(repo): |
496 | # Internal node pointing at a leaf. |
497 | - repo.chk_bytes.add_lines( |
498 | - (None,), None, node_lines, random_id=True) |
499 | - except: |
500 | - repo.abort_write_group() |
501 | - raise |
502 | - else: |
503 | - repo.commit_write_group() |
504 | - repo.start_write_group() |
505 | - try: |
506 | + repo.chk_bytes.add_lines((None,), None, node_lines, random_id=True) |
507 | + with repository.WriteGroup(repo): |
508 | # Leaf in a separate pack. |
509 | - repo.chk_bytes.add_lines( |
510 | - (None,), None, leaf_lines, random_id=True) |
511 | - except: |
512 | - repo.abort_write_group() |
513 | - raise |
514 | - else: |
515 | - repo.commit_write_group() |
516 | + repo.chk_bytes.add_lines((None,), None, leaf_lines, random_id=True) |
517 | repo.pack() |
518 | self.assertEqual(expected_set, repo.chk_bytes.keys()) |
519 | - finally: |
520 | - repo.unlock() |
521 | # and reopening |
522 | repo = repo.controldir.open_repository() |
523 | - repo.lock_read() |
524 | - try: |
525 | + with repo.lock_read(): |
526 | self.assertEqual(expected_set, repo.chk_bytes.keys()) |
527 | - finally: |
528 | - repo.unlock() |
529 | |
530 | def test_chk_bytes_are_fully_buffered(self): |
531 | repo = self.make_repository('.') |
532 | repo.lock_write() |
533 | self.addCleanup(repo.unlock) |
534 | - repo.start_write_group() |
535 | - try: |
536 | + with repository.WriteGroup(repo): |
537 | sha1, len, _ = repo.chk_bytes.add_lines((None,), |
538 | None, [b"foo\n", b"bar\n"], random_id=True) |
539 | self.assertEqual(b'4e48e2c9a3d2ca8a708cb0cc545700544efb5021', |
540 | @@ -123,11 +92,6 @@ |
541 | self.assertEqual( |
542 | {(b'sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)}, |
543 | repo.chk_bytes.keys()) |
544 | - except: |
545 | - repo.abort_write_group() |
546 | - raise |
547 | - else: |
548 | - repo.commit_write_group() |
549 | # This may not always be correct if we change away from BTreeGraphIndex |
550 | # in the future. But for now, lets check that chk_bytes are fully |
551 | # buffered |
552 | |
553 | === modified file 'breezy/tests/per_repository_reference/test_add_inventory.py' |
554 | --- breezy/tests/per_repository_reference/test_add_inventory.py 2018-11-11 04:08:32 +0000 |
555 | +++ breezy/tests/per_repository_reference/test_add_inventory.py 2019-05-29 03:45:01 +0000 |
556 | @@ -17,6 +17,7 @@ |
557 | """Tests for add_inventory on a repository with external references.""" |
558 | |
559 | from breezy import errors |
560 | +from breezy.repository import WriteGroup |
561 | from breezy.tests.per_repository_reference import ( |
562 | TestCaseWithExternalReferenceRepository, |
563 | ) |
564 | @@ -34,18 +35,8 @@ |
565 | self.addCleanup(tree.unlock) |
566 | base = self.make_repository('base') |
567 | repo = self.make_referring('referring', base) |
568 | - repo.lock_write() |
569 | - try: |
570 | - repo.start_write_group() |
571 | - try: |
572 | - repo.add_inventory(revid, inv, []) |
573 | - except: |
574 | - repo.abort_write_group() |
575 | - raise |
576 | - else: |
577 | - repo.commit_write_group() |
578 | - finally: |
579 | - repo.unlock() |
580 | + with repo.lock_write(), WriteGroup(repo): |
581 | + repo.add_inventory(revid, inv, []) |
582 | repo.lock_read() |
583 | self.addCleanup(repo.unlock) |
584 | inv2 = repo.get_inventory(revid) |
585 | |
586 | === modified file 'breezy/tests/per_repository_reference/test_add_revision.py' |
587 | --- breezy/tests/per_repository_reference/test_add_revision.py 2017-05-21 18:10:28 +0000 |
588 | +++ breezy/tests/per_repository_reference/test_add_revision.py 2019-05-29 03:45:01 +0000 |
589 | @@ -17,6 +17,7 @@ |
590 | """Tests for add_revision on a repository with external references.""" |
591 | |
592 | from breezy import errors |
593 | +from breezy.repository import WriteGroup |
594 | from breezy.tests.per_repository_reference import ( |
595 | TestCaseWithExternalReferenceRepository, |
596 | ) |
597 | @@ -35,20 +36,10 @@ |
598 | rev = tree.branch.repository.get_revision(revid) |
599 | base = self.make_repository('base') |
600 | repo = self.make_referring('referring', base) |
601 | - repo.lock_write() |
602 | - try: |
603 | - repo.start_write_group() |
604 | - try: |
605 | - rev = tree.branch.repository.get_revision(revid) |
606 | - repo.texts.add_lines((inv.root.file_id, revid), [], []) |
607 | - repo.add_revision(revid, rev, inv=inv) |
608 | - except: |
609 | - repo.abort_write_group() |
610 | - raise |
611 | - else: |
612 | - repo.commit_write_group() |
613 | - finally: |
614 | - repo.unlock() |
615 | + with repo.lock_write(), WriteGroup(repo): |
616 | + rev = tree.branch.repository.get_revision(revid) |
617 | + repo.texts.add_lines((inv.root.file_id, revid), [], []) |
618 | + repo.add_revision(revid, rev, inv=inv) |
619 | rev2 = repo.get_revision(revid) |
620 | self.assertEqual(rev, rev2) |
621 | self.assertRaises(errors.NoSuchRevision, base.get_revision, revid) |
622 | |
623 | === modified file 'breezy/tests/per_repository_reference/test_add_signature_text.py' |
624 | --- breezy/tests/per_repository_reference/test_add_signature_text.py 2018-11-11 04:08:32 +0000 |
625 | +++ breezy/tests/per_repository_reference/test_add_signature_text.py 2019-05-29 03:45:01 +0000 |
626 | @@ -17,6 +17,7 @@ |
627 | """Tests for add_signature_text on a repository with external references.""" |
628 | |
629 | from breezy import errors |
630 | +from breezy.repository import WriteGroup |
631 | from breezy.tests.per_repository_reference import ( |
632 | TestCaseWithExternalReferenceRepository, |
633 | ) |
634 | @@ -34,20 +35,11 @@ |
635 | self.addCleanup(tree.unlock) |
636 | base = self.make_repository('base') |
637 | repo = self.make_referring('referring', base) |
638 | - repo.lock_write() |
639 | - try: |
640 | - repo.start_write_group() |
641 | - try: |
642 | - rev = tree.branch.repository.get_revision(revid) |
643 | - repo.texts.add_lines((inv.root.file_id, revid), [], []) |
644 | - repo.add_revision(revid, rev, inv=inv) |
645 | - repo.add_signature_text(revid, b"text") |
646 | - repo.commit_write_group() |
647 | - except: |
648 | - repo.abort_write_group() |
649 | - raise |
650 | - finally: |
651 | - repo.unlock() |
652 | + with repo.lock_write(), WriteGroup(repo): |
653 | + rev = tree.branch.repository.get_revision(revid) |
654 | + repo.texts.add_lines((inv.root.file_id, revid), [], []) |
655 | + repo.add_revision(revid, rev, inv=inv) |
656 | + repo.add_signature_text(revid, b"text") |
657 | repo.get_signature_text(revid) |
658 | self.assertRaises(errors.NoSuchRevision, base.get_signature_text, |
659 | revid) |
660 | |
661 | === modified file 'breezy/tests/per_repository_vf/helpers.py' |
662 | --- breezy/tests/per_repository_vf/helpers.py 2018-11-11 04:08:32 +0000 |
663 | +++ breezy/tests/per_repository_vf/helpers.py 2019-05-29 03:45:01 +0000 |
664 | @@ -20,6 +20,7 @@ |
665 | osutils, |
666 | revision as _mod_revision, |
667 | ) |
668 | +from ...repository import WriteGroup |
669 | from ...bzr import ( |
670 | inventory, |
671 | ) |
672 | @@ -50,9 +51,7 @@ |
673 | "%s isn't a knit format" % self.repository_format) |
674 | |
675 | repo = self.make_repository('broken') |
676 | - repo.lock_write() |
677 | - repo.start_write_group() |
678 | - try: |
679 | + with repo.lock_write(), WriteGroup(repo): |
680 | inv = inventory.Inventory(revision_id=b'revision-id') |
681 | inv.root.revision = b'revision-id' |
682 | inv_sha1 = repo.add_inventory(b'revision-id', inv, []) |
683 | @@ -66,16 +65,9 @@ |
684 | # Manually add the revision text using the RevisionStore API, with |
685 | # bad parents. |
686 | rev_text = repo._serializer.write_revision_to_string(revision) |
687 | - repo.revisions.add_lines((revision.revision_id,), |
688 | - [(b'incorrect-parent',)], |
689 | - osutils.split_lines(rev_text)) |
690 | - except: |
691 | - repo.abort_write_group() |
692 | - repo.unlock() |
693 | - raise |
694 | - else: |
695 | - repo.commit_write_group() |
696 | - repo.unlock() |
697 | + repo.revisions.add_lines( |
698 | + (revision.revision_id,), [(b'incorrect-parent',)], |
699 | + osutils.split_lines(rev_text)) |
700 | |
701 | repo.lock_write() |
702 | self.addCleanup(repo.unlock) |
703 | |
704 | === modified file 'breezy/tests/per_repository_vf/test_check_reconcile.py' |
705 | --- breezy/tests/per_repository_vf/test_check_reconcile.py 2018-11-11 04:08:32 +0000 |
706 | +++ breezy/tests/per_repository_vf/test_check_reconcile.py 2019-05-29 03:45:01 +0000 |
707 | @@ -25,6 +25,7 @@ |
708 | Inventory, |
709 | InventoryFile, |
710 | ) |
711 | +from breezy.repository import WriteGroup |
712 | from breezy.revision import ( |
713 | NULL_REVISION, |
714 | Revision, |
715 | @@ -793,15 +794,9 @@ |
716 | def make_populated_repository(self, factory): |
717 | """Create a new repository populated by the given factory.""" |
718 | repo = self.make_repository('broken-repo') |
719 | - with repo.lock_write(): |
720 | - repo.start_write_group() |
721 | - try: |
722 | - factory(repo) |
723 | - repo.commit_write_group() |
724 | - return repo |
725 | - except: |
726 | - repo.abort_write_group() |
727 | - raise |
728 | + with repo.lock_write(), WriteGroup(repo): |
729 | + factory(repo) |
730 | + return repo |
731 | |
732 | def add_revision(self, repo, revision_id, inv, parent_ids): |
733 | """Add a revision with a given inventory and parents to a repository. |
734 | |
735 | === modified file 'breezy/tests/per_repository_vf/test_repository.py' |
736 | --- breezy/tests/per_repository_vf/test_repository.py 2019-01-01 21:08:01 +0000 |
737 | +++ breezy/tests/per_repository_vf/test_repository.py 2019-05-29 03:45:01 +0000 |
738 | @@ -327,19 +327,9 @@ |
739 | self.controldir = tree_a.branch.controldir |
740 | # add a corrupt inventory 'orphan' |
741 | # this may need some generalising for knits. |
742 | - tree_a.lock_write() |
743 | - try: |
744 | - tree_a.branch.repository.start_write_group() |
745 | - try: |
746 | - inv_file = tree_a.branch.repository.inventories |
747 | - inv_file.add_lines((b'orphan',), [], []) |
748 | - except: |
749 | - tree_a.branch.repository.commit_write_group() |
750 | - raise |
751 | - else: |
752 | - tree_a.branch.repository.abort_write_group() |
753 | - finally: |
754 | - tree_a.unlock() |
755 | + with tree_a.lock_write(), _mod_repository.WriteGroup(tree_a.branch.repository): |
756 | + inv_file = tree_a.branch.repository.inventories |
757 | + inv_file.add_lines((b'orphan',), [], []) |
758 | # add a real revision 'rev1' |
759 | tree_a.commit('rev1', rev_id=b'rev1', allow_pointless=True) |
760 | # add a real revision 'rev2' based on rev1 |
761 | @@ -385,18 +375,13 @@ |
762 | |
763 | def test_reserved_id(self): |
764 | repo = self.make_repository('repository') |
765 | - repo.lock_write() |
766 | - repo.start_write_group() |
767 | - try: |
768 | + with repo.lock_write(), _mod_repository.WriteGroup(repo): |
769 | self.assertRaises(errors.ReservedId, repo.add_inventory, |
770 | b'reserved:', None, None) |
771 | self.assertRaises(errors.ReservedId, repo.add_inventory_by_delta, |
772 | "foo", [], b'reserved:', None) |
773 | - self.assertRaises(errors.ReservedId, repo.add_revision, |
774 | - b'reserved:', None) |
775 | - finally: |
776 | - repo.abort_write_group() |
777 | - repo.unlock() |
778 | + self.assertRaises( |
779 | + errors.ReservedId, repo.add_revision, b'reserved:', None) |
780 | |
781 | |
782 | class TestCaseWithCorruptRepository(TestCaseWithRepository): |
783 | |
784 | === modified file 'breezy/tests/test_inv.py' |
785 | --- breezy/tests/test_inv.py 2018-11-16 11:37:47 +0000 |
786 | +++ breezy/tests/test_inv.py 2019-05-29 03:45:01 +0000 |
787 | @@ -141,8 +141,7 @@ |
788 | |
789 | |
790 | def _create_repo_revisions(repo, basis, delta, invalid_delta): |
791 | - repo.start_write_group() |
792 | - try: |
793 | + with repository.WriteGroup(repo): |
794 | rev = revision.Revision(b'basis', timestamp=0, timezone=None, |
795 | message="", committer="foo@example.com") |
796 | basis.revision_id = b'basis' |
797 | @@ -161,19 +160,13 @@ |
798 | rev = revision.Revision(b'result', timestamp=0, timezone=None, |
799 | message="", committer="foo@example.com") |
800 | repo.add_revision(b'result', rev, result_inv) |
801 | - repo.commit_write_group() |
802 | - except: |
803 | - repo.abort_write_group() |
804 | - raise |
805 | return target_entries |
806 | |
807 | |
808 | def _get_basis_entries(tree): |
809 | basis_tree = tree.basis_tree() |
810 | - basis_tree.lock_read() |
811 | - basis_tree_entries = list(basis_tree.inventory.iter_entries_by_dir()) |
812 | - basis_tree.unlock() |
813 | - return basis_tree_entries |
814 | + with basis_tree.lock_read(): |
815 | + return list(basis_tree.inventory.iter_entries_by_dir()) |
816 | |
817 | |
818 | def _populate_different_tree(tree, basis, delta): |
819 | @@ -253,28 +246,16 @@ |
820 | format = self.format() |
821 | control = self.make_controldir('tree', format=format._matchingcontroldir) |
822 | repo = format.initialize(control) |
823 | - with repo.lock_write(): |
824 | - repo.start_write_group() |
825 | - try: |
826 | - rev = revision.Revision(b'basis', timestamp=0, timezone=None, |
827 | - message="", committer="foo@example.com") |
828 | - basis.revision_id = b'basis' |
829 | - create_texts_for_inv(repo, basis) |
830 | - repo.add_revision(b'basis', rev, basis) |
831 | - repo.commit_write_group() |
832 | - except: |
833 | - repo.abort_write_group() |
834 | - raise |
835 | - with repo.lock_write(): |
836 | - repo.start_write_group() |
837 | - try: |
838 | - inv_sha1 = repo.add_inventory_by_delta(b'basis', delta, |
839 | - b'result', [b'basis']) |
840 | - except: |
841 | - repo.abort_write_group() |
842 | - raise |
843 | - else: |
844 | - repo.commit_write_group() |
845 | + with repo.lock_write(), repository.WriteGroup(repo): |
846 | + rev = revision.Revision( |
847 | + b'basis', timestamp=0, timezone=None, message="", |
848 | + committer="foo@example.com") |
849 | + basis.revision_id = b'basis' |
850 | + create_texts_for_inv(repo, basis) |
851 | + repo.add_revision(b'basis', rev, basis) |
852 | + with repo.lock_write(), repository.WriteGroup(repo): |
853 | + inv_sha1 = repo.add_inventory_by_delta( |
854 | + b'basis', delta, b'result', [b'basis']) |
855 | # Fresh lock, reads disk again. |
856 | repo = repo.controldir.open_repository() |
857 | repo.lock_read() |
Looks reasonable, thanks!