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
17
"""Tests for interfacing with a Git Repository"""
20
from dulwich.repo import (
30
from bzrlib.repository import (
35
from bzrlib.plugins.git import (
40
from bzrlib.plugins.git.mapping import (
43
from bzrlib.plugins.git.object_store import (
46
from bzrlib.plugins.git.push import (
47
MissingObjectsIterator,
50
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
51
"""Feature tests for GitRepository."""
53
_test_needs_features = [tests.GitCommandFeature]
56
builder = tests.GitBranchBuilder()
57
builder.set_file('a', 'text for a\n', False)
58
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
59
mapping = builder.finish()
60
return mapping[commit_handle]
62
def test_open_existing(self):
63
GitRepo.init(self.test_dir)
65
repo = Repository.open('.')
66
self.assertIsInstance(repo, repository.GitRepository)
68
def test_has_git_repo(self):
69
GitRepo.init(self.test_dir)
71
repo = Repository.open('.')
72
self.assertIsInstance(repo._git, dulwich.repo.BaseRepo)
74
def test_has_revision(self):
75
GitRepo.init(self.test_dir)
76
commit_id = self._do_commit()
77
repo = Repository.open('.')
78
self.assertFalse(repo.has_revision('foobar'))
79
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
80
self.assertTrue(repo.has_revision(revid))
82
def test_has_revisions(self):
83
GitRepo.init(self.test_dir)
84
commit_id = self._do_commit()
85
repo = Repository.open('.')
86
self.assertEquals(set(), repo.has_revisions(['foobar']))
87
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
88
self.assertEquals(set([revid]), repo.has_revisions(['foobar', revid]))
90
def test_get_revision(self):
91
# GitRepository.get_revision gives a Revision object.
93
# Create a git repository with a revision.
94
GitRepo.init(self.test_dir)
95
commit_id = self._do_commit()
97
# Get the corresponding Revision object.
98
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
99
repo = Repository.open('.')
100
rev = repo.get_revision(revid)
101
self.assertIsInstance(rev, revision.Revision)
103
def test_get_revision_unknown(self):
104
GitRepo.init(self.test_dir)
106
repo = Repository.open('.')
107
self.assertRaises(errors.NoSuchRevision, repo.get_revision, "bla")
109
def simple_commit(self):
110
# Create a git repository with some interesting files in a revision.
111
GitRepo.init(self.test_dir)
112
builder = tests.GitBranchBuilder()
113
builder.set_file('data', 'text\n', False)
114
builder.set_file('executable', 'content', True)
115
builder.set_link('link', 'broken')
116
builder.set_file('subdir/subfile', 'subdir text\n', False)
117
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
118
timestamp=1205433193)
119
mapping = builder.finish()
120
return mapping[commit_handle]
123
commit_id = self.simple_commit()
124
repo = Repository.open('.')
127
def test_revision_tree(self):
128
commit_id = self.simple_commit()
129
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
130
repo = Repository.open('.')
131
tree = repo.revision_tree(revid)
132
self.assertEquals(tree.get_revision_id(), revid)
133
self.assertEquals("text\n", tree.get_file_text(tree.path2id("data")))
135
def test_get_inventory(self):
136
# GitRepository.get_inventory gives a GitInventory object with
137
# plausible entries for typical cases.
139
commit_id = self.simple_commit()
141
# Get the corresponding Inventory object.
142
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
143
repo = Repository.open('.')
144
inv = repo.get_inventory(revid)
145
self.assertIsInstance(inv, inventory.Inventory)
146
printed_inv = '\n'.join(
147
repr((path, entry.executable, entry))
148
for path, entry in inv.iter_entries())
149
self.assertEqualDiff(
151
"('', False, GitInventoryDirectory('TREE_ROOT', u'', parent_id=None,"
152
" revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
153
"(u'data', False, GitInventoryFile('data', u'data',"
154
" parent_id='TREE_ROOT',"
155
" sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
156
"(u'executable', True, GitInventoryFile('executable', u'executable',"
157
" parent_id='TREE_ROOT',"
158
" sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
159
"(u'link', False, GitInventoryLink('link', u'link',"
160
" parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
161
"(u'subdir', False, GitInventoryDirectory('subdir', u'subdir',"
162
" parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
163
"(u'subdir/subfile', False, GitInventoryFile('subdir/subfile',"
164
" u'subfile', parent_id='subdir',"
165
" sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))")
168
class TestGitRepository(tests.TestCaseWithTransport):
170
_test_needs_features = [tests.GitCommandFeature]
172
def _do_commit(self):
173
builder = tests.GitBranchBuilder()
174
builder.set_file('a', 'text for a\n', False)
175
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
176
mapping = builder.finish()
177
return mapping[commit_handle]
180
tests.TestCaseWithTransport.setUp(self)
181
dulwich.repo.Repo.create(self.test_dir)
182
self.git_repo = Repository.open(self.test_dir)
184
def test_supports_rich_root(self):
186
self.assertEqual(repo.supports_rich_root(), True)
188
def test_get_signature_text(self):
189
self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
191
def test_has_signature_for_revision_id(self):
192
self.assertEquals(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
194
def test_all_revision_ids_none(self):
195
self.assertEquals(set([]), self.git_repo.all_revision_ids())
197
def test_all_revision_ids(self):
198
commit_id = self._do_commit()
199
self.assertEquals(set(["git-v1:%s" % commit_id]),
200
self.git_repo.all_revision_ids())
202
def test_get_ancestry_null(self):
203
self.assertEquals([None, revision.NULL_REVISION], self.git_repo.get_ancestry(revision.NULL_REVISION))
205
def assertIsNullInventory(self, inv):
206
self.assertEqual(inv.root, None)
207
self.assertEqual(inv.revision_id, revision.NULL_REVISION)
208
self.assertEqual(list(inv.iter_entries()), [])
210
def test_get_inventory_none(self):
211
# GitRepository.get_inventory(None) returns the null inventory.
213
inv = repo.get_inventory(revision.NULL_REVISION)
214
self.assertIsNullInventory(inv)
216
def test_revision_tree_none(self):
217
# GitRepository.revision_tree(None) returns the null tree.
219
tree = repo.revision_tree(revision.NULL_REVISION)
220
self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
221
self.assertIsNullInventory(tree.inventory)
223
def test_get_parent_map_null(self):
224
self.assertEquals({revision.NULL_REVISION: ()},
225
self.git_repo.get_parent_map([revision.NULL_REVISION]))
228
class GitRepositoryFormat(tests.TestCase):
231
super(GitRepositoryFormat, self).setUp()
232
self.format = repository.GitRepositoryFormat()
234
def test_get_format_description(self):
235
self.assertEquals("Git Repository", self.format.get_format_description())
238
class RevisionGistImportTests(tests.TestCaseWithTransport):
241
tests.TestCaseWithTransport.setUp(self)
242
self.git_path = os.path.join(self.test_dir, "git")
243
os.mkdir(self.git_path)
244
dulwich.repo.Repo.create(self.git_path)
245
self.git_repo = Repository.open(self.git_path)
246
self.bzr_tree = self.make_branch_and_tree("bzr")
249
return InterRepository.get(self.bzr_tree.branch.repository,
252
def object_iter(self):
253
store = BazaarObjectStore(self.bzr_tree.branch.repository, default_mapping)
254
store_iterator = MissingObjectsIterator(store, self.bzr_tree.branch.repository)
255
return store, store_iterator
257
def import_rev(self, revid, parent_lookup=None):
258
store, store_iter = self.object_iter()
259
store._cache.idmap.start_write_group()
261
return store_iter.import_revision(revid, roundtrip=False)
263
store._cache.idmap.abort_write_group()
266
store._cache.idmap.commit_write_group()
268
def test_pointless(self):
269
revid = self.bzr_tree.commit("pointless", timestamp=1205433193,
271
committer="Jelmer Vernooij <jelmer@samba.org>")
272
self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14",
273
self.import_rev(revid))
274
self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14",
275
self.import_rev(revid))
278
class ForeignTestsRepositoryFactory(object):
280
def make_repository(self, transport):
281
return dir.LocalGitBzrDirFormat().initialize_on_transport(transport).open_repository()