/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/test_repository.py

  • Committer: Robert Collins
  • Date: 2007-09-19 05:14:14 UTC
  • mto: (2835.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2836.
  • Revision ID: robertc@robertcollins.net-20070919051414-2tgjqteg7k3ps4h0
* ``pull``, ``merge`` and ``push`` will no longer silently correct some
  repository index errors that occured as a result of the Weave disk format.
  Instead the ``reconcile`` command needs to be run to correct those
  problems if they exist (and it has been able to fix most such problems
  since bzr 0.8). Some new problems have been identified during this release
  and you should run ``bzr check`` once on every repository to see if you
  need to reconcile. If you cannot ``pull`` or ``merge`` from a remote
  repository due to mismatched parent errors - a symptom of index errors -
  you should simply take a full copy of that remote repository to a clean
  directory outside any local repositories, then run reconcile on it, and
  finally pull from it locally. (And naturally email the repositories owner
  to ask them to upgrade and run reconcile).
  (Robert Collins)

* ``VersionedFile.fix_parents`` has been removed as a harmful API.
  ``VersionedFile.join`` will no longer accept different parents on either
  side of a join - it will either ignore them, or error, depending on the
  implementation. See notes when upgrading for more information.
  (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
16
 
 
17
 
"""Tests for interfacing with a Git Repository"""
18
 
 
19
 
import dulwich
20
 
from dulwich.repo import (
21
 
    Repo as GitRepo,
22
 
    )
23
 
import os
24
 
 
25
 
from bzrlib import (
26
 
    errors,
27
 
    inventory,
28
 
    revision,
29
 
    )
30
 
from bzrlib.repository import (
31
 
    InterRepository,
32
 
    Repository,
33
 
    )
34
 
 
35
 
from bzrlib.plugins.git import (
36
 
    dir,
37
 
    repository,
38
 
    tests,
39
 
    )
40
 
from bzrlib.plugins.git.mapping import (
41
 
    default_mapping,
42
 
    )
43
 
from bzrlib.plugins.git.object_store import (
44
 
    BazaarObjectStore,
45
 
    )
46
 
from bzrlib.plugins.git.push import (
47
 
    MissingObjectsIterator,
48
 
    )
49
 
 
50
 
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
51
 
    """Feature tests for GitRepository."""
52
 
 
53
 
    def _do_commit(self):
54
 
        builder = tests.GitBranchBuilder()
55
 
        builder.set_file('a', 'text for a\n', False)
56
 
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
57
 
        mapping = builder.finish()
58
 
        return mapping[commit_handle]
59
 
 
60
 
    def test_open_existing(self):
61
 
        GitRepo.init(self.test_dir)
62
 
 
63
 
        repo = Repository.open('.')
64
 
        self.assertIsInstance(repo, repository.GitRepository)
65
 
 
66
 
    def test_has_git_repo(self):
67
 
        GitRepo.init(self.test_dir)
68
 
 
69
 
        repo = Repository.open('.')
70
 
        self.assertIsInstance(repo._git, dulwich.repo.BaseRepo)
71
 
 
72
 
    def test_has_revision(self):
73
 
        GitRepo.init(self.test_dir)
74
 
        commit_id = self._do_commit()
75
 
        repo = Repository.open('.')
76
 
        self.assertFalse(repo.has_revision('foobar'))
77
 
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
78
 
        self.assertTrue(repo.has_revision(revid))
79
 
 
80
 
    def test_has_revisions(self):
81
 
        GitRepo.init(self.test_dir)
82
 
        commit_id = self._do_commit()
83
 
        repo = Repository.open('.')
84
 
        self.assertEquals(set(), repo.has_revisions(['foobar']))
85
 
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
86
 
        self.assertEquals(set([revid]), repo.has_revisions(['foobar', revid]))
87
 
 
88
 
    def test_get_revision(self):
89
 
        # GitRepository.get_revision gives a Revision object.
90
 
 
91
 
        # Create a git repository with a revision.
92
 
        GitRepo.init(self.test_dir)
93
 
        commit_id = self._do_commit()
94
 
 
95
 
        # Get the corresponding Revision object.
96
 
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
97
 
        repo = Repository.open('.')
98
 
        rev = repo.get_revision(revid)
99
 
        self.assertIsInstance(rev, revision.Revision)
100
 
 
101
 
    def test_get_revision_unknown(self):
102
 
        GitRepo.init(self.test_dir)
103
 
 
104
 
        repo = Repository.open('.')
105
 
        self.assertRaises(errors.NoSuchRevision, repo.get_revision, "bla")
106
 
 
107
 
    def simple_commit(self):
108
 
        # Create a git repository with some interesting files in a revision.
109
 
        GitRepo.init(self.test_dir)
110
 
        builder = tests.GitBranchBuilder()
111
 
        builder.set_file('data', 'text\n', False)
112
 
        builder.set_file('executable', 'content', True)
113
 
        builder.set_link('link', 'broken')
114
 
        builder.set_file('subdir/subfile', 'subdir text\n', False)
115
 
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
116
 
            timestamp=1205433193)
117
 
        mapping = builder.finish()
118
 
        return mapping[commit_handle]
119
 
 
120
 
    def test_pack(self):
121
 
        commit_id = self.simple_commit()
122
 
        repo = Repository.open('.')
123
 
        repo.pack()
124
 
 
125
 
    def test_revision_tree(self):
126
 
        commit_id = self.simple_commit()
127
 
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
128
 
        repo = Repository.open('.')
129
 
        tree = repo.revision_tree(revid)
130
 
        self.assertEquals(tree.get_revision_id(), revid)
131
 
        self.assertEquals("text\n", tree.get_file_text(tree.path2id("data")))
132
 
 
133
 
    def test_get_inventory(self):
134
 
        # GitRepository.get_inventory gives a GitInventory object with
135
 
        # plausible entries for typical cases.
136
 
 
137
 
        commit_id = self.simple_commit()
138
 
 
139
 
        # Get the corresponding Inventory object.
140
 
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
141
 
        repo = Repository.open('.')
142
 
        inv = repo.get_inventory(revid)
143
 
        self.assertIsInstance(inv, inventory.Inventory)
144
 
        printed_inv = '\n'.join(
145
 
            repr((path, entry.executable, entry))
146
 
            for path, entry in inv.iter_entries())
147
 
        self.assertEqualDiff(
148
 
            printed_inv,
149
 
            "('', False, GitInventoryDirectory('TREE_ROOT', u'', parent_id=None,"
150
 
            " revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
151
 
            "(u'data', False, GitInventoryFile('data', u'data',"
152
 
            " parent_id='TREE_ROOT',"
153
 
            " sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
154
 
            "(u'executable', True, GitInventoryFile('executable', u'executable',"
155
 
            " parent_id='TREE_ROOT',"
156
 
            " sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
157
 
            "(u'link', False, GitInventoryLink('link', u'link',"
158
 
            " parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
159
 
            "(u'subdir', False, GitInventoryDirectory('subdir', u'subdir',"
160
 
            " parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
161
 
            "(u'subdir/subfile', False, GitInventoryFile('subdir/subfile',"
162
 
            " u'subfile', parent_id='subdir',"
163
 
            " sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))")
164
 
 
165
 
 
166
 
class TestGitRepository(tests.TestCaseWithTransport):
167
 
 
168
 
    def _do_commit(self):
169
 
        builder = tests.GitBranchBuilder()
170
 
        builder.set_file('a', 'text for a\n', False)
171
 
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
172
 
        mapping = builder.finish()
173
 
        return mapping[commit_handle]
174
 
 
175
 
    def setUp(self):
176
 
        tests.TestCaseWithTransport.setUp(self)
177
 
        dulwich.repo.Repo.create(self.test_dir)
178
 
        self.git_repo = Repository.open(self.test_dir)
179
 
 
180
 
    def test_supports_rich_root(self):
181
 
        repo = self.git_repo
182
 
        self.assertEqual(repo.supports_rich_root(), True)
183
 
 
184
 
    def test_get_signature_text(self):
185
 
        self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
186
 
 
187
 
    def test_has_signature_for_revision_id(self):
188
 
        self.assertEquals(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
189
 
 
190
 
    def test_all_revision_ids_none(self):
191
 
        self.assertEquals(set([]), self.git_repo.all_revision_ids())
192
 
 
193
 
    def test_all_revision_ids(self):
194
 
        commit_id = self._do_commit()
195
 
        self.assertEquals(
196
 
                set([default_mapping.revision_id_foreign_to_bzr(commit_id)]),
197
 
                self.git_repo.all_revision_ids())
198
 
 
199
 
    def test_get_ancestry_null(self):
200
 
        self.assertEquals([None, revision.NULL_REVISION], self.git_repo.get_ancestry(revision.NULL_REVISION))
201
 
 
202
 
    def assertIsNullInventory(self, inv):
203
 
        self.assertEqual(inv.root, None)
204
 
        self.assertEqual(inv.revision_id, revision.NULL_REVISION)
205
 
        self.assertEqual(list(inv.iter_entries()), [])
206
 
 
207
 
    def test_get_inventory_none(self):
208
 
        # GitRepository.get_inventory(None) returns the null inventory.
209
 
        repo = self.git_repo
210
 
        inv = repo.get_inventory(revision.NULL_REVISION)
211
 
        self.assertIsNullInventory(inv)
212
 
 
213
 
    def test_revision_tree_none(self):
214
 
        # GitRepository.revision_tree(None) returns the null tree.
215
 
        repo = self.git_repo
216
 
        tree = repo.revision_tree(revision.NULL_REVISION)
217
 
        self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
218
 
        self.assertIsNullInventory(tree.inventory)
219
 
 
220
 
    def test_get_parent_map_null(self):
221
 
        self.assertEquals({revision.NULL_REVISION: ()}, 
222
 
                           self.git_repo.get_parent_map([revision.NULL_REVISION]))
223
 
 
224
 
 
225
 
class GitRepositoryFormat(tests.TestCase):
226
 
 
227
 
    def setUp(self):
228
 
        super(GitRepositoryFormat, self).setUp()
229
 
        self.format = repository.GitRepositoryFormat()
230
 
 
231
 
    def test_get_format_description(self):
232
 
        self.assertEquals("Git Repository", self.format.get_format_description())
233
 
 
234
 
 
235
 
class RevisionGistImportTests(tests.TestCaseWithTransport):
236
 
 
237
 
    def setUp(self):
238
 
        tests.TestCaseWithTransport.setUp(self)
239
 
        self.git_path = os.path.join(self.test_dir, "git")
240
 
        os.mkdir(self.git_path)
241
 
        dulwich.repo.Repo.create(self.git_path)
242
 
        self.git_repo = Repository.open(self.git_path)
243
 
        self.bzr_tree = self.make_branch_and_tree("bzr")
244
 
 
245
 
    def get_inter(self):
246
 
        return InterRepository.get(self.bzr_tree.branch.repository, 
247
 
                                   self.git_repo)
248
 
 
249
 
    def object_iter(self):
250
 
        store = BazaarObjectStore(self.bzr_tree.branch.repository, default_mapping)
251
 
        store_iterator = MissingObjectsIterator(store, self.bzr_tree.branch.repository)
252
 
        return store, store_iterator
253
 
 
254
 
    def import_rev(self, revid, parent_lookup=None):
255
 
        store, store_iter = self.object_iter()
256
 
        store._cache.idmap.start_write_group()
257
 
        try:
258
 
            return store_iter.import_revision(revid, roundtrip=False)
259
 
        except:
260
 
            store._cache.idmap.abort_write_group()
261
 
            raise
262
 
        else:
263
 
            store._cache.idmap.commit_write_group()
264
 
 
265
 
    def test_pointless(self):
266
 
        revid = self.bzr_tree.commit("pointless", timestamp=1205433193,
267
 
                timezone=0,
268
 
                  committer="Jelmer Vernooij <jelmer@samba.org>")
269
 
        self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14", 
270
 
                self.import_rev(revid))
271
 
        self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14", 
272
 
                self.import_rev(revid))
273
 
 
274
 
 
275
 
class ForeignTestsRepositoryFactory(object):
276
 
 
277
 
    def make_repository(self, transport):
278
 
        return dir.LocalGitControlDirFormat().initialize_on_transport(transport).open_repository()