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"""
27
from bzrlib.plugins.git import tests
28
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_model(self):
50
repo = repository.Repository.open('.')
51
self.assertIsInstance(repo._git, model.GitModel)
53
def test_revision_graph(self):
55
builder = tests.GitBranchBuilder()
56
builder.set_file('a', 'text for a\n', False)
57
commit1_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
58
builder.set_file('a', 'new a\n', False)
59
commit2_handle = builder.commit('Joe Foo <joe@foo.com>', u'new a')
60
builder.set_file('b', 'text for b\n', False)
61
commit3_handle = builder.commit('Jerry Bar <jerry@foo.com>', u'b',
63
commit4_handle = builder.commit('Jerry Bar <jerry@foo.com>', u'merge',
65
merge=[commit2_handle],)
67
mapping = builder.finish()
68
commit1_id = mapping[commit1_handle]
69
commit2_id = mapping[commit2_handle]
70
commit3_id = mapping[commit3_handle]
71
commit4_id = mapping[commit4_handle]
73
revisions = tests.run_git('rev-list', '--topo-order',
75
revisions = revisions.splitlines()
76
self.assertEqual([commit4_id, commit2_id, commit3_id, commit1_id],
78
bzr_revisions = [ids.convert_revision_id_git_to_bzr(r) for r in revisions]
79
graph = {bzr_revisions[0]:[bzr_revisions[2], bzr_revisions[1]],
80
bzr_revisions[1]:[bzr_revisions[3]],
81
bzr_revisions[2]:[bzr_revisions[3]],
85
repo = repository.Repository.open('.')
86
self.assertEqual(graph, repo.get_revision_graph(bzr_revisions[0]))
87
self.assertEqual({bzr_revisions[3]:[]},
88
repo.get_revision_graph(bzr_revisions[3]))
90
def test_get_revision(self):
91
# GitRepository.get_revision gives a Revision object.
93
# Create a git repository with a revision.
95
builder = tests.GitBranchBuilder()
96
builder.set_file('a', 'text for a\n', False)
97
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
98
mapping = builder.finish()
99
commit_id = mapping[commit_handle]
101
# Get the corresponding Revision object.
102
revid = ids.convert_revision_id_git_to_bzr(commit_id)
103
repo = repository.Repository.open('.')
104
rev = repo.get_revision(revid)
105
self.assertIsInstance(rev, revision.Revision)
107
def test_get_inventory(self):
108
# GitRepository.get_inventory gives a GitInventory object with
109
# plausible entries for typical cases.
111
# Create a git repository with some interesting files in a revision.
112
tests.run_git('init')
113
builder = tests.GitBranchBuilder()
114
builder.set_file('data', 'text\n', False)
115
builder.set_file('executable', 'content', True)
116
builder.set_link('link', 'broken')
117
builder.set_file('subdir/subfile', 'subdir text\n', False)
118
commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
119
timestamp=1205433193)
120
mapping = builder.finish()
121
commit_id = mapping[commit_handle]
123
# Get the corresponding Inventory object.
124
revid = ids.convert_revision_id_git_to_bzr(commit_id)
125
repo = repository.Repository.open('.')
126
inv = repo.get_inventory(revid)
127
self.assertIsInstance(inv, inventory.Inventory)
128
printed_inv = '\n'.join(
129
repr((path, entry.executable, entry))
130
for path, entry in inv.iter_entries())
131
self.assertEqualDiff(
133
"('', False, InventoryDirectory('TREE_ROOT', u'', parent_id=None,"
134
" revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
135
"(u'data', False, InventoryFile('data', u'data',"
136
" parent_id='TREE_ROOT',"
137
" sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5))\n"
138
"(u'executable', True, InventoryFile('executable', u'executable',"
139
" parent_id='TREE_ROOT',"
140
" sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7))\n"
141
"(u'link', False, InventoryLink('link', u'link',"
142
" parent_id='TREE_ROOT', revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
143
"(u'subdir', False, InventoryDirectory('subdir', u'subdir',"
144
" parent_id='TREE_ROOT', revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
145
"(u'subdir/subfile', False, InventoryFile('subdir/subfile',"
146
" u'subfile', parent_id='subdir',"
147
" sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12))")
150
class MemoryGitRepository(git_repository.GitRepository):
151
"""A git repository without real git data on disk."""
154
def _make_model(klass, transport):
158
class MemoryGitDir(git_dir.GitDir):
159
"""A git tree with real data on disk."""
161
_gitrepository_class = MemoryGitRepository
164
class MemoryGitBzrDirFormat(git_dir.GitBzrDirFormat):
165
"""Format for a git tree without real data on disk."""
167
_gitdir_class = MemoryGitDir
170
class TestGitRepository(tests.TestCaseWithTransport):
173
tests.TestCaseWithTransport.setUp(self)
174
self.transport = self.get_transport()
175
self.transport.mkdir('.git')
176
self.git_dir = MemoryGitBzrDirFormat().open(self.transport)
177
self.git_repo = self.git_dir.open_repository()
179
def test_supports_rich_root(self):
180
# GitRepository.supports_rich_root is False, at least for now.
182
self.assertEqual(repo.supports_rich_root(), False)
184
def assertIsNullInventory(self, inv):
185
self.assertEqual(inv.root, None)
186
self.assertEqual(inv.revision_id, revision.NULL_REVISION)
187
self.assertEqual(list(inv.iter_entries()), [])
189
def test_get_inventory_none(self):
190
# GitRepository.get_inventory(None) returns the null inventory.
192
inv = repo.get_inventory(None)
193
self.assertIsNullInventory(inv)
195
def test_revision_tree_none(self):
196
# GitRepository.revision_tree(None) returns the null tree.
198
tree = repo.revision_tree(None)
199
self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
200
self.assertIsNullInventory(tree.inventory)
203
class TestGitRepositoryParseRev(tests.TestCase):
204
"""Unit tests for GitRepository._parse_rev."""
206
def test_base_commit(self):
207
# GitRepository._parse_rev works for a simple base commit.
208
rev = git_repository.GitRepository._parse_rev([
209
"873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
210
"tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
211
"author Jane Bar <jane@bar.com> 1198784533 +0200\n",
212
"committer Joe Foo <joe@foo.com> 1198784532 +0100\n",
216
self.assertEqual(rev.revision_id,
217
'git-experimental-r:873a8ae0d682b0e63e9795bc53056d32ed3de93f')
218
self.assertEqual(rev.parent_ids, [])
219
self.assertEqual(rev.committer, 'Joe Foo <joe@foo.com>')
220
self.assertEqual(repr(rev.timestamp), '1198784532.0')
221
self.assertEqual(repr(rev.timezone), '3600')
222
self.assertEqual(rev.message, 'message\n')
225
{'git-tree-id': 'aaff74984cccd156a469afa7d9ab10e4777beb24',
226
'author': 'Jane Bar <jane@bar.com>',
227
'git-author-timestamp': '1198784533',
228
'git-author-timezone': '+0200'})
230
def test_merge_commit(self):
231
# Multi-parent commits (merges) are parsed correctly.
232
rev = git_repository.GitRepository._parse_rev([
233
"873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
234
"tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
235
"parent 263ed20f0d4898be994404ca418bafe8e89abb8a\n",
236
"parent 546563eb8f3e94a557f3bb779b6e5a2bd9658752\n",
237
"parent 3116d42db7b5c5e69e58f651721e179791479c23\n",
238
"author Jane Bar <jane@bar.com> 1198784533 +0200\n",
239
"committer Joe Foo <joe@foo.com> 1198784532 +0100\n",
243
# Git records merges in the same way as bzr. The first parent is the
244
# commit base, the following parents are the ordered merged revisions.
247
['git-experimental-r:263ed20f0d4898be994404ca418bafe8e89abb8a',
248
'git-experimental-r:546563eb8f3e94a557f3bb779b6e5a2bd9658752',
249
'git-experimental-r:3116d42db7b5c5e69e58f651721e179791479c23'])
251
def test_redundant_spaces(self):
252
# Redundant spaces in author and committer are preserved.
253
rev = git_repository.GitRepository._parse_rev([
254
"873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
255
"tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
256
"author Jane Bar <jane@bar.com> 1198784533 +0200\n",
257
"committer Joe Foo <joe@foo.com> 1198784532 +0100\n",
261
self.assertEqual(rev.committer, ' Joe Foo <joe@foo.com> ')
263
rev.properties['author'], ' Jane Bar <jane@bar.com> ')
265
def test_no_committer(self):
266
# If committer is not set, then author is used.
268
# Folks in #git say that git fsck would likely accept commits that do
269
# not set committer, but that author is a mandatory value.
270
rev = git_repository.GitRepository._parse_rev([
271
"873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
272
"tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
273
"author Jane Bar <jane@bar.com> 1198784533 +0200\n",
277
self.assertEqual(rev.committer, 'Jane Bar <jane@bar.com>')
278
self.assertEqual(repr(rev.timestamp), '1198784533.0')
279
self.assertEqual(repr(rev.timezone), '7200')
280
self.assertEqual(rev.properties['author'], 'Jane Bar <jane@bar.com>')
281
self.assertEqual(rev.properties['git-author-timestamp'], '1198784533')
282
self.assertEqual(rev.properties['git-author-timezone'], '+0200')
284
def test_parse_tz(self):
285
# Simple tests for the _parse_tz helper.
286
parse_tz = git_repository.GitRepository._parse_tz
287
self.assertEqual(repr(parse_tz('+0000')), '0')
288
self.assertEqual(repr(parse_tz('+0001')), '60')
289
self.assertEqual(repr(parse_tz('-0001')), '-60')
290
self.assertEqual(repr(parse_tz('+0100')), '3600')
291
self.assertEqual(repr(parse_tz('-0100')), '-3600')
292
self.assertEqual(repr(parse_tz('+9959')), '359940')
293
self.assertEqual(repr(parse_tz('-9959')), '-359940')