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]
55
def test_open_existing(self):
56
GitRepo.init(self.test_dir)
58
repo = Repository.open('.')
59
self.assertIsInstance(repo, repository.GitRepository)
61
def test_has_git_repo(self):
62
GitRepo.init(self.test_dir)
64
repo = Repository.open('.')
65
self.assertIsInstance(repo._git, dulwich.repo.Repo)
67
def test_get_revision(self):
68
# GitRepository.get_revision gives a Revision object.
70
# Create a git repository with a revision.
71
GitRepo.init(self.test_dir)
72
builder = tests.GitBranchBuilder()
73
builder.set_file('a', 'text for a\n', False)
74
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
75
mapping = builder.finish()
76
commit_id = mapping[commit_handle]
78
# Get the corresponding Revision object.
79
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
80
repo = Repository.open('.')
81
rev = repo.get_revision(revid)
82
self.assertIsInstance(rev, revision.Revision)
84
def test_get_revision_unknown(self):
85
GitRepo.init(self.test_dir)
87
repo = Repository.open('.')
88
self.assertRaises(errors.NoSuchRevision, repo.get_revision, "bla")
90
def simple_commit(self):
91
# Create a git repository with some interesting files in a revision.
92
GitRepo.init(self.test_dir)
93
builder = tests.GitBranchBuilder()
94
builder.set_file('data', 'text\n', False)
95
builder.set_file('executable', 'content', True)
96
builder.set_link('link', 'broken')
97
builder.set_file('subdir/subfile', 'subdir text\n', False)
98
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
100
mapping = builder.finish()
101
return mapping[commit_handle]
103
def test_revision_tree(self):
104
commit_id = self.simple_commit()
105
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
106
repo = Repository.open('.')
107
tree = repo.revision_tree(revid)
108
self.assertEquals(tree.get_revision_id(), revid)
109
self.assertEquals("text\n", tree.get_file_text(tree.path2id("data")))
111
def test_get_inventory(self):
112
# GitRepository.get_inventory gives a GitInventory object with
113
# plausible entries for typical cases.
115
commit_id = self.simple_commit()
117
# Get the corresponding Inventory object.
118
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
119
repo = Repository.open('.')
120
inv = repo.get_inventory(revid)
121
self.assertIsInstance(inv, inventory.Inventory)
122
printed_inv = '\n'.join(
123
repr((path, entry.executable, entry))
124
for path, entry in inv.iter_entries())
125
self.assertEqualDiff(
127
"('', False, GitInventoryDirectory('TREE_ROOT', u'', parent_id=None,"
128
" revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
129
"(u'data', False, GitInventoryFile('data', u'data',"
130
" parent_id='TREE_ROOT',"
131
" sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
132
"(u'executable', True, GitInventoryFile('executable', u'executable',"
133
" parent_id='TREE_ROOT',"
134
" sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
135
"(u'link', False, GitInventoryLink('link', u'link',"
136
" parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
137
"(u'subdir', False, GitInventoryDirectory('subdir', u'subdir',"
138
" parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
139
"(u'subdir/subfile', False, GitInventoryFile('subdir/subfile',"
140
" u'subfile', parent_id='subdir',"
141
" sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12, revision="+default_mapping.revid_prefix+":69c39cfa65962f3cf16b9b3eb08a15954e9d8590))")
144
class TestGitRepository(tests.TestCaseWithTransport):
147
tests.TestCaseWithTransport.setUp(self)
148
dulwich.repo.Repo.create(self.test_dir)
149
self.git_repo = Repository.open(self.test_dir)
151
def test_supports_rich_root(self):
153
self.assertEqual(repo.supports_rich_root(), True)
155
def test_get_signature_text(self):
156
self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
158
def test_has_signature_for_revision_id(self):
159
self.assertEquals(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
161
def test_all_revision_ids_none(self):
162
self.assertEquals(set([]), self.git_repo.all_revision_ids())
164
def test_get_ancestry_null(self):
165
self.assertEquals([None, revision.NULL_REVISION], self.git_repo.get_ancestry(revision.NULL_REVISION))
167
def assertIsNullInventory(self, inv):
168
self.assertEqual(inv.root, None)
169
self.assertEqual(inv.revision_id, revision.NULL_REVISION)
170
self.assertEqual(list(inv.iter_entries()), [])
172
def test_get_inventory_none(self):
173
# GitRepository.get_inventory(None) returns the null inventory.
175
inv = repo.get_inventory(revision.NULL_REVISION)
176
self.assertIsNullInventory(inv)
178
def test_revision_tree_none(self):
179
# GitRepository.revision_tree(None) returns the null tree.
181
tree = repo.revision_tree(revision.NULL_REVISION)
182
self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
183
self.assertIsNullInventory(tree.inventory)
185
def test_get_parent_map_null(self):
186
self.assertEquals({revision.NULL_REVISION: ()},
187
self.git_repo.get_parent_map([revision.NULL_REVISION]))
190
class GitRepositoryFormat(tests.TestCase):
193
super(GitRepositoryFormat, self).setUp()
194
self.format = repository.GitRepositoryFormat()
196
def test_get_format_description(self):
197
self.assertEquals("Git Repository", self.format.get_format_description())
200
class RevisionGistImportTests(tests.TestCaseWithTransport):
203
tests.TestCaseWithTransport.setUp(self)
204
self.git_path = os.path.join(self.test_dir, "git")
205
os.mkdir(self.git_path)
206
dulwich.repo.Repo.create(self.git_path)
207
self.git_repo = Repository.open(self.git_path)
208
self.bzr_tree = self.make_branch_and_tree("bzr")
211
return InterRepository.get(self.bzr_tree.branch.repository,
214
def object_iter(self):
215
store = BazaarObjectStore(self.bzr_tree.branch.repository, default_mapping)
216
store_iterator = MissingObjectsIterator(store, self.bzr_tree.branch.repository)
217
return store, store_iterator
219
def import_rev(self, revid, parent_lookup=None):
220
store, store_iter = self.object_iter()
221
store._idmap.start_write_group()
223
return store_iter.import_revision(revid)
225
store._idmap.abort_write_group()
228
store._idmap.commit_write_group()
230
def test_pointless(self):
231
revid = self.bzr_tree.commit("pointless", timestamp=1205433193,
233
committer="Jelmer Vernooij <jelmer@samba.org>")
234
self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14",
235
self.import_rev(revid))
236
self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14",
237
self.import_rev(revid))
240
class ForeignTestsRepositoryFactory(object):
242
def make_repository(self, transport):
243
return dir.LocalGitBzrDirFormat().initialize_on_transport(transport).open_repository()