~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_repository/test_commit_builder.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-17 18:13:57 UTC
  • mfrom: (5268.7.29 transport-segments)
  • Revision ID: pqm@pqm.ubuntu.com-20110817181357-y5q5eth1hk8bl3om
(jelmer) Allow specifying the colocated branch to use in the branch URL,
 and retrieving the branch name using ControlDir._get_selected_branch.
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
39
39
        branch = self.make_branch('.')
40
40
        branch.repository.lock_write()
41
41
        builder = branch.repository.get_commit_builder(
42
 
            branch, [], branch.get_config_stack())
 
42
            branch, [], branch.get_config())
43
43
        self.assertIsInstance(builder, repository.CommitBuilder)
44
44
        self.assertTrue(builder.random_revid)
45
45
        branch.repository.commit_write_group()
49
49
        if builder.record_root_entry is True:
50
50
            tree.lock_read()
51
51
            try:
52
 
                ie = tree.root_inventory.root
 
52
                ie = tree.inventory.root
53
53
            finally:
54
54
                tree.unlock()
55
55
            parent_tree = tree.branch.repository.revision_tree(
148
148
        rev = tree.branch.repository.get_revision(rev_id)
149
149
        self.assertEqual('foo bar blah', rev.message)
150
150
 
151
 
    def test_updates_branch(self):
152
 
        tree = self.make_branch_and_tree(".")
153
 
        tree.lock_write()
154
 
        try:
155
 
            builder = tree.branch.get_commit_builder([])
156
 
            list(builder.record_iter_changes(tree, tree.last_revision(),
157
 
                tree.iter_changes(tree.basis_tree())))
158
 
            builder.finish_inventory()
159
 
            will_update_branch = builder.updates_branch
160
 
            rev_id = builder.commit('might update the branch')
161
 
        finally:
162
 
            tree.unlock()
163
 
        actually_updated_branch = (tree.branch.last_revision() == rev_id)
164
 
        self.assertEqual(actually_updated_branch, will_update_branch)
165
 
 
166
151
    def test_commit_with_revision_id_record_entry_contents(self):
167
152
        tree = self.make_branch_and_tree(".")
168
153
        tree.lock_write()
233
218
        # on deserialisation, but thats all the current contract guarantees
234
219
        # anyway.
235
220
        self.assertEqual(revision_id,
236
 
            tree.branch.repository.revision_tree(revision_id).get_revision_id())
 
221
            tree.branch.repository.get_inventory(revision_id).revision_id)
237
222
 
238
223
    def test_commit_without_root_errors(self):
239
224
        tree = self.make_branch_and_tree(".")
248
233
                except:
249
234
                    builder.abort()
250
235
                    raise
251
 
                else:
252
 
                    builder.commit("msg")
253
236
            self.assertRaises(errors.RootMissing, do_commit)
254
237
        finally:
255
238
            tree.unlock()
264
247
            if not builder.supports_record_entry_contents:
265
248
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
266
249
                    "record_entry_contents")
267
 
            entry = tree.root_inventory['foo-id']
 
250
            entry = tree.inventory['foo-id']
268
251
            self.assertRaises(errors.RootMissing,
269
252
                builder.record_entry_contents, entry, [], 'foo', tree,
270
253
                    tree.path_content_summary('foo'))
284
267
            if not builder.supports_record_entry_contents:
285
268
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
286
269
                    "record_entry_contents")
287
 
            builder.will_record_deletes()
288
270
            ie = inventory.make_entry('directory', '', None,
289
271
                    tree.get_root_id())
290
272
            delta, version_recorded, fs_hash = builder.record_entry_contents(
291
 
                ie, [parent_tree.root_inventory], '', tree,
 
273
                ie, [parent_tree.inventory], '', tree,
292
274
                tree.path_content_summary(''))
293
275
            # Regardless of repository root behaviour we should consider this a
294
276
            # pointless commit.
300
282
            if got_new_revision:
301
283
                self.assertEqual(('', '', ie.file_id, ie), delta)
302
284
                # The delta should be tracked
303
 
                self.assertEqual(delta, builder.get_basis_delta()[-1])
 
285
                self.assertEqual(delta, builder._basis_delta[-1])
304
286
            else:
305
287
                self.assertEqual(None, delta)
306
288
            # Directories do not get hashed.
324
306
            # pointless commit.
325
307
            self.assertFalse(builder.any_changes())
326
308
            builder.finish_inventory()
327
 
            builder.commit('')
328
309
            builder_tree = builder.revision_tree()
329
310
            new_root_id = builder_tree.get_root_id()
330
311
            new_root_revision = builder_tree.get_file_revision(new_root_id)
335
316
                # We should see a new root revision
336
317
                self.assertNotEqual(old_revision_id, new_root_revision)
337
318
        finally:
 
319
            builder.abort()
338
320
            tree.unlock()
339
321
 
340
322
    def test_commit_record_entry_contents(self):
375
357
                if not builder.supports_record_entry_contents:
376
358
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
377
359
                        "support record_entry_contents")
378
 
                parent_invs = [basis.root_inventory]
 
360
                parent_invs = [basis.inventory]
379
361
                builder.will_record_deletes()
380
362
                if builder.record_root_entry:
381
 
                    ie = basis.root_inventory.root.copy()
 
363
                    ie = basis.inventory.root.copy()
382
364
                    delta, _, _ = builder.record_entry_contents(ie, parent_invs,
383
365
                        '', tree, tree.path_content_summary(''))
384
366
                    if delta is not None:
435
417
                        "support record_entry_contents")
436
418
                builder.will_record_deletes()
437
419
                if builder.record_root_entry is True:
438
 
                    parent_invs = [basis.root_inventory]
439
 
                    del basis.root_inventory.root.children['foo']
440
 
                    builder.record_entry_contents(basis.root_inventory.root,
 
420
                    parent_invs = [basis.inventory]
 
421
                    del basis.inventory.root.children['foo']
 
422
                    builder.record_entry_contents(basis.inventory.root,
441
423
                        parent_invs, '', tree, tree.path_content_summary(''))
442
424
                # the delta should be returned, and recorded in _basis_delta
443
425
                delta = builder.record_delete("foo", "foo-id")
444
426
                self.assertEqual(("foo", None, "foo-id", None), delta)
445
 
                self.assertEqual(delta, builder.get_basis_delta()[-1])
 
427
                self.assertEqual(delta, builder._basis_delta[-1])
446
428
                builder.finish_inventory()
447
429
                rev_id2 = builder.commit('delete foo')
448
430
            except:
464
446
        try:
465
447
            builder = tree.branch.get_commit_builder([rev_id])
466
448
            try:
467
 
                builder.will_record_deletes()
468
449
                delete_change = ('foo-id', ('foo', None), True, (True, False),
469
450
                    (tree.path2id(''), None), ('foo', None), ('file', None),
470
451
                    (False, None))
471
452
                list(builder.record_iter_changes(tree, rev_id,
472
453
                    [delete_change]))
473
454
                self.assertEqual(("foo", None, "foo-id", None),
474
 
                    builder.get_basis_delta()[0])
 
455
                    builder._basis_delta[0])
475
456
                self.assertTrue(builder.any_changes())
476
457
                builder.finish_inventory()
477
458
                rev_id2 = builder.commit('delete foo')
868
849
                if not builder.supports_record_entry_contents:
869
850
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
870
851
                        "support record_entry_contents")
871
 
                builder.will_record_deletes()
872
852
                parent_tree = tree.basis_tree()
873
853
                parent_tree.lock_read()
874
854
                self.addCleanup(parent_tree.unlock)
875
 
                parent_invs = [parent_tree.root_inventory]
 
855
                parent_invs = [parent_tree.inventory]
876
856
                for parent_id in parent_ids[1:]:
877
857
                    parent_invs.append(tree.branch.repository.revision_tree(
878
 
                        parent_id).root_inventory)
 
858
                        parent_id).inventory)
879
859
                # root
880
860
                builder.record_entry_contents(
881
861
                    inventory.make_entry('directory', '', None,
882
862
                        tree.get_root_id()), parent_invs, '', tree,
883
863
                        tree.path_content_summary(''))
884
864
                def commit_id(file_id):
885
 
                    old_ie = tree.root_inventory[file_id]
 
865
                    old_ie = tree.inventory[file_id]
886
866
                    path = tree.id2path(file_id)
887
867
                    ie = inventory.make_entry(tree.kind(file_id), old_ie.name,
888
868
                        old_ie.parent_id, file_id)
894
874
                        tree, content_summary)
895
875
 
896
876
                file_id = tree.path2id(new_name)
897
 
                parent_id = tree.root_inventory[file_id].parent_id
 
877
                parent_id = tree.inventory[file_id].parent_id
898
878
                if parent_id != tree.get_root_id():
899
879
                    commit_id(parent_id)
900
880
                # because a change of some sort is meant to have occurred,
916
896
                if delta_against_basis:
917
897
                    expected_delta = (name, new_name, file_id, new_entry)
918
898
                    # The delta should be recorded
919
 
                    self.assertEqual(expected_delta,
920
 
                        builder.get_basis_delta()[-1])
 
899
                    self.assertEqual(expected_delta, builder._basis_delta[-1])
921
900
                else:
922
901
                    expected_delta = None
923
902
                self.assertEqual(expected_delta, delta)
958
937
            # record_entry_contents.
959
938
            parent_ids = tree.get_parent_ids()
960
939
            builder = tree.branch.get_commit_builder(parent_ids)
961
 
            builder.will_record_deletes()
962
940
            parent_tree = tree.basis_tree()
963
941
            parent_tree.lock_read()
964
942
            self.addCleanup(parent_tree.unlock)
980
958
                self.assertEqualStat(result[2][1], tree_file_stat[1])
981
959
            else:
982
960
                self.assertEqual([], result)
 
961
            delta = builder._basis_delta
 
962
            delta_dict = dict((change[2], change) for change in delta)
 
963
            version_recorded = (file_id in delta_dict and
 
964
                delta_dict[file_id][3] is not None and
 
965
                delta_dict[file_id][3].revision == builder._new_revision_id)
 
966
            if records_version:
 
967
                self.assertTrue(version_recorded)
 
968
            else:
 
969
                self.assertFalse(version_recorded)
983
970
            self.assertIs(None, builder.new_inventory)
984
971
            builder.finish_inventory()
985
972
            if tree.branch.repository._format.supports_full_versioned_files:
988
975
                                [inv_key])[inv_key]
989
976
                self.assertEqual(inv_sha1, builder.inv_sha1)
990
977
            self.assertIs(None, builder.new_inventory)
991
 
            rev2 = builder.commit('')
992
 
            delta = builder.get_basis_delta()
993
 
            delta_dict = dict((change[2], change) for change in delta)
994
 
            version_recorded = (file_id in delta_dict and
995
 
                delta_dict[file_id][3] is not None and
996
 
                delta_dict[file_id][3].revision == rev2)
997
 
            if records_version:
998
 
                self.assertTrue(version_recorded)
999
 
            else:
1000
 
                self.assertFalse(version_recorded)
1001
 
 
1002
 
            new_inventory = builder.revision_tree().root_inventory
 
978
            new_inventory = builder.revision_tree().inventory
1003
979
            new_entry = new_inventory[file_id]
1004
980
            if delta_against_basis:
1005
981
                expected_delta = (name, new_name, file_id, new_entry)
1007
983
            else:
1008
984
                expected_delta = None
1009
985
                self.assertFalse(version_recorded)
 
986
            rev2 = builder.commit('')
1010
987
            tree.set_parent_ids([rev2])
1011
988
        except:
1012
989
            builder.abort()
1301
1278
        make_before(path)
1302
1279
 
1303
1280
        def change_kind():
1304
 
            if osutils.file_kind(path) == "directory":
1305
 
                osutils.rmtree(path)
1306
 
            else:
1307
 
                osutils.delete_any(path)
 
1281
            osutils.delete_any(path)
1308
1282
            make_after(path)
1309
1283
 
1310
1284
        self._add_commit_change_check_changed(tree, path, change_kind,
1315
1289
            expect_fs_hash=True)
1316
1290
 
1317
1291
    def test_last_modified_dir_file_ric(self):
1318
 
        try:
1319
 
            self._check_kind_change(self.make_dir, self.make_file,
1320
 
                expect_fs_hash=True,
1321
 
                mini_commit=self.mini_commit_record_iter_changes)
1322
 
        except errors.UnsupportedKindChange:
1323
 
            raise tests.TestSkipped(
1324
 
                "tree does not support changing entry kind from "
1325
 
                "directory to file")
 
1292
        self._check_kind_change(self.make_dir, self.make_file,
 
1293
            expect_fs_hash=True,
 
1294
            mini_commit=self.mini_commit_record_iter_changes)
1326
1295
 
1327
1296
    def test_last_modified_dir_link(self):
1328
1297
        self._check_kind_change(self.make_dir, self.make_link)
1329
1298
 
1330
1299
    def test_last_modified_dir_link_ric(self):
1331
 
        try:
1332
 
            self._check_kind_change(self.make_dir, self.make_link,
1333
 
                mini_commit=self.mini_commit_record_iter_changes)
1334
 
        except errors.UnsupportedKindChange:
1335
 
            raise tests.TestSkipped(
1336
 
                "tree does not support changing entry kind from "
1337
 
                "directory to link")
 
1300
        self._check_kind_change(self.make_dir, self.make_link,
 
1301
            mini_commit=self.mini_commit_record_iter_changes)
1338
1302
 
1339
1303
    def test_last_modified_link_file(self):
1340
1304
        self._check_kind_change(self.make_link, self.make_file,
1371
1335
        branch.repository.lock_write()
1372
1336
        self.addCleanup(branch.repository.unlock)
1373
1337
        self.assertRaises(ValueError, branch.repository.get_commit_builder,
1374
 
            branch, [], branch.get_config_stack(),
 
1338
            branch, [], branch.get_config(),
1375
1339
            revprops={'invalid': u'property\rwith\r\ninvalid chars'})
1376
1340
 
1377
1341
    def test_commit_builder_commit_with_invalid_message(self):
1379
1343
        branch.repository.lock_write()
1380
1344
        self.addCleanup(branch.repository.unlock)
1381
1345
        builder = branch.repository.get_commit_builder(branch, [],
1382
 
            branch.get_config_stack())
 
1346
            branch.get_config())
1383
1347
        self.addCleanup(branch.repository.abort_write_group)
1384
1348
        self.assertRaises(ValueError, builder.commit,
1385
1349
            u'Invalid\r\ncommit message\r\n')
1391
1355
        self.addCleanup(branch.repository.unlock)
1392
1356
        self.assertRaises(UnicodeDecodeError,
1393
1357
            branch.repository.get_commit_builder,
1394
 
            branch, [], branch.get_config_stack(),
 
1358
            branch, [], branch.get_config(),
1395
1359
            committer="Erik B\xe5gfors <erik@example.com>")
1396
1360
 
1397
1361
    def test_stacked_repositories_reject_commit_builder(self):
1408
1372
        self.addCleanup(repo_local.lock_write().unlock)
1409
1373
        if not repo_local._format.supports_chks:
1410
1374
            self.assertRaises(errors.BzrError, repo_local.get_commit_builder,
1411
 
                branch, [], branch.get_config_stack())
 
1375
                branch, [], branch.get_config())
1412
1376
        else:
1413
1377
            builder = repo_local.get_commit_builder(branch, [],
1414
 
                                                    branch.get_config_stack())
 
1378
                                                    branch.get_config())
1415
1379
            builder.abort()
1416
1380
 
1417
1381
    def test_committer_no_username(self):