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"""
28
from bzrlib.plugins.git import tests
29
from bzrlib.plugins.git import (
36
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
37
"""Feature tests for GitRepository."""
39
_test_needs_features = [tests.GitCommandFeature]
41
def test_open_existing(self):
44
repo = repository.Repository.open('.')
45
self.assertIsInstance(repo, git_repository.GitRepository)
47
def test_has_git_repo(self):
50
repo = repository.Repository.open('.')
51
self.assertIsInstance(repo._git, git.repo.Repo)
53
def test_get_revision(self):
54
# GitRepository.get_revision gives a Revision object.
56
# Create a git repository with a revision.
58
builder = tests.GitBranchBuilder()
59
builder.set_file('a', 'text for a\n', False)
60
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
61
mapping = builder.finish()
62
commit_id = mapping[commit_handle]
64
# Get the corresponding Revision object.
65
revid = ids.convert_revision_id_git_to_bzr(commit_id)
66
repo = repository.Repository.open('.')
67
rev = repo.get_revision(revid)
68
self.assertIsInstance(rev, revision.Revision)
70
def simple_commit(self):
71
# Create a git repository with some interesting files in a revision.
73
builder = tests.GitBranchBuilder()
74
builder.set_file('data', 'text\n', False)
75
builder.set_file('executable', 'content', True)
76
builder.set_link('link', 'broken')
77
builder.set_file('subdir/subfile', 'subdir text\n', False)
78
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
80
mapping = builder.finish()
81
return mapping[commit_handle]
83
def test_revision_tree(self):
84
commit_id = self.simple_commit()
85
revid = ids.convert_revision_id_git_to_bzr(commit_id)
86
repo = repository.Repository.open('.')
87
tree = repo.revision_tree(revid)
88
self.assertEquals(tree.get_revision_id(), revid)
90
def test_get_inventory(self):
91
# GitRepository.get_inventory gives a GitInventory object with
92
# plausible entries for typical cases.
94
commit_id = self.simple_commit()
96
# Get the corresponding Inventory object.
97
revid = ids.convert_revision_id_git_to_bzr(commit_id)
98
repo = repository.Repository.open('.')
99
inv = repo.get_inventory(revid)
100
self.assertIsInstance(inv, inventory.Inventory)
101
printed_inv = '\n'.join(
102
repr((path, entry.executable, entry))
103
for path, entry in inv.iter_entries())
104
self.assertEqualDiff(
106
"('', False, InventoryDirectory('TREE_ROOT', u'', parent_id=None,"
107
" revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
108
"(u'data', False, InventoryFile('data', u'data',"
109
" parent_id='TREE_ROOT',"
110
" sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5))\n"
111
"(u'executable', True, InventoryFile('executable', u'executable',"
112
" parent_id='TREE_ROOT',"
113
" sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7))\n"
114
"(u'link', False, InventoryLink('link', u'link',"
115
" parent_id='TREE_ROOT', revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
116
"(u'subdir', False, InventoryDirectory('subdir', u'subdir',"
117
" parent_id='TREE_ROOT', revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
118
"(u'subdir/subfile', False, InventoryFile('subdir/subfile',"
119
" u'subfile', parent_id='subdir',"
120
" sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12))")
123
class TestGitRepository(tests.TestCaseWithTransport):
126
tests.TestCaseWithTransport.setUp(self)
127
git.repo.Repo.create(self.test_dir)
128
self.git_repo = repository.Repository.open(self.test_dir)
130
def test_supports_rich_root(self):
131
# GitRepository.supports_rich_root is False, at least for now.
133
self.assertEqual(repo.supports_rich_root(), False)
135
def test_get_signature_text(self):
136
self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
138
def test_has_signature_for_revision_id(self):
139
self.assertEquals(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
141
def test_all_revision_ids_none(self):
142
self.assertEquals(set(), self.git_repo.all_revision_ids())
144
def test_get_ancestry_null(self):
145
self.assertEquals([None], self.git_repo.get_ancestry(revision.NULL_REVISION))
147
def assertIsNullInventory(self, inv):
148
self.assertEqual(inv.root, None)
149
self.assertEqual(inv.revision_id, revision.NULL_REVISION)
150
self.assertEqual(list(inv.iter_entries()), [])
152
def test_get_inventory_none(self):
153
# GitRepository.get_inventory(None) returns the null inventory.
155
inv = repo.get_inventory(revision.NULL_REVISION)
156
self.assertIsNullInventory(inv)
158
def test_revision_tree_none(self):
159
# GitRepository.revision_tree(None) returns the null tree.
161
tree = repo.revision_tree(revision.NULL_REVISION)
162
self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
163
self.assertIsNullInventory(tree.inventory)
165
def test_get_parent_map_null(self):
166
self.assertEquals({revision.NULL_REVISION: ()},
167
self.git_repo.get_parent_map([revision.NULL_REVISION]))
170
class GitRepositoryFormat(tests.TestCase):
173
super(GitRepositoryFormat, self).setUp()
174
self.format = git_repository.GitFormat()
176
def test_get_format_description(self):
177
self.assertEquals("Git Repository", self.format.get_format_description())