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"""
24
from bzrlib.repository import Repository
26
from bzrlib.plugins.git import (
32
from bzrlib.plugins.git.mapping import default_mapping
35
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
36
"""Feature tests for GitRepository."""
38
_test_needs_features = [tests.GitCommandFeature]
40
def test_open_existing(self):
43
repo = Repository.open('.')
44
self.assertIsInstance(repo, repository.GitRepository)
46
def test_has_git_repo(self):
49
repo = Repository.open('.')
50
self.assertIsInstance(repo._git, git.repo.Repo)
52
def test_get_revision(self):
53
# GitRepository.get_revision gives a Revision object.
55
# Create a git repository with a revision.
57
builder = tests.GitBranchBuilder()
58
builder.set_file('a', 'text for a\n', False)
59
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
60
mapping = builder.finish()
61
commit_id = mapping[commit_handle]
63
# Get the corresponding Revision object.
64
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
65
repo = Repository.open('.')
66
rev = repo.get_revision(revid)
67
self.assertIsInstance(rev, revision.Revision)
69
def test_get_revision_unknown(self):
72
repo = Repository.open('.')
73
self.assertRaises(errors.NoSuchRevision, repo.get_revision, "bla")
75
def simple_commit(self):
76
# Create a git repository with some interesting files in a revision.
78
builder = tests.GitBranchBuilder()
79
builder.set_file('data', 'text\n', False)
80
builder.set_file('executable', 'content', True)
81
builder.set_link('link', 'broken')
82
builder.set_file('subdir/subfile', 'subdir text\n', False)
83
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
85
mapping = builder.finish()
86
return mapping[commit_handle]
88
def test_revision_tree(self):
89
commit_id = self.simple_commit()
90
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
91
repo = Repository.open('.')
92
tree = repo.revision_tree(revid)
93
self.assertEquals(tree.get_revision_id(), revid)
94
self.assertEquals("text\n", tree.get_file_text(tree.path2id("data")))
96
def test_get_inventory(self):
97
# GitRepository.get_inventory gives a GitInventory object with
98
# plausible entries for typical cases.
100
commit_id = self.simple_commit()
102
# Get the corresponding Inventory object.
103
revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
104
repo = Repository.open('.')
105
inv = repo.get_inventory(revid)
106
self.assertIsInstance(inv, inventory.Inventory)
107
printed_inv = '\n'.join(
108
repr((path, entry.executable, entry))
109
for path, entry in inv.iter_entries())
110
self.assertEqualDiff(
112
"('', False, InventoryDirectory('TREE_ROOT', u'', parent_id=None,"
113
" revision='git-experimental:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
114
"(u'data', False, InventoryFile('data', u'data',"
115
" parent_id='TREE_ROOT',"
116
" sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5))\n"
117
"(u'executable', True, InventoryFile('executable', u'executable',"
118
" parent_id='TREE_ROOT',"
119
" sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7))\n"
120
"(u'link', False, InventoryLink('link', u'link',"
121
" parent_id='TREE_ROOT', revision='git-experimental:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
122
"(u'subdir', False, InventoryDirectory('subdir', u'subdir',"
123
" parent_id='TREE_ROOT', revision='git-experimental:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
124
"(u'subdir/subfile', False, InventoryFile('subdir/subfile',"
125
" u'subfile', parent_id='subdir',"
126
" sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12))")
129
class TestGitRepository(tests.TestCaseWithTransport):
132
tests.TestCaseWithTransport.setUp(self)
133
git.repo.Repo.create(self.test_dir)
134
self.git_repo = Repository.open(self.test_dir)
136
def test_supports_rich_root(self):
138
self.assertEqual(repo.supports_rich_root(), True)
140
def test_get_signature_text(self):
141
self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
143
def test_has_signature_for_revision_id(self):
144
self.assertEquals(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
146
def test_all_revision_ids_none(self):
147
self.assertEquals(set(), self.git_repo.all_revision_ids())
149
def test_get_ancestry_null(self):
150
self.assertEquals([None], self.git_repo.get_ancestry(revision.NULL_REVISION))
152
def assertIsNullInventory(self, inv):
153
self.assertEqual(inv.root, None)
154
self.assertEqual(inv.revision_id, revision.NULL_REVISION)
155
self.assertEqual(list(inv.iter_entries()), [])
157
def test_get_inventory_none(self):
158
# GitRepository.get_inventory(None) returns the null inventory.
160
inv = repo.get_inventory(revision.NULL_REVISION)
161
self.assertIsNullInventory(inv)
163
def test_revision_tree_none(self):
164
# GitRepository.revision_tree(None) returns the null tree.
166
tree = repo.revision_tree(revision.NULL_REVISION)
167
self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
168
self.assertIsNullInventory(tree.inventory)
170
def test_get_parent_map_null(self):
171
self.assertEquals({revision.NULL_REVISION: ()},
172
self.git_repo.get_parent_map([revision.NULL_REVISION]))
175
class GitRepositoryFormat(tests.TestCase):
178
super(GitRepositoryFormat, self).setUp()
179
self.format = repository.GitFormat()
181
def test_get_format_description(self):
182
self.assertEquals("Git Repository", self.format.get_format_description())