1
# Copyright (C) 2005, 2006, 2008 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
26
from bzrlib.errors import (
29
from bzrlib.revision import (
33
from bzrlib.tests import (
36
from bzrlib.tests.interrepository_implementations import (
37
TestCaseWithInterRepository,
39
from bzrlib.tests.interrepository_implementations.test_interrepository import (
40
check_repo_format_for_funky_id_on_win32
44
class TestInterRepository(TestCaseWithInterRepository):
47
tree_a = self.make_branch_and_tree('a')
48
self.build_tree(['a/foo'])
49
tree_a.add('foo', 'file1')
50
tree_a.commit('rev1', rev_id='rev1')
51
def check_push_rev1(repo):
52
# ensure the revision is missing.
53
self.assertRaises(NoSuchRevision, repo.get_revision, 'rev1')
54
# fetch with a limit of NULL_REVISION and an explicit progress bar.
55
repo.fetch(tree_a.branch.repository,
56
revision_id=NULL_REVISION,
57
pb=bzrlib.progress.DummyProgress())
58
# nothing should have been pushed
59
self.assertFalse(repo.has_revision('rev1'))
60
# fetch with a default limit (grab everything)
61
repo.fetch(tree_a.branch.repository)
62
# check that b now has all the data from a's first commit.
63
rev = repo.get_revision('rev1')
64
tree = repo.revision_tree('rev1')
66
self.addCleanup(tree.unlock)
67
tree.get_file_text('file1')
69
if tree.inventory[file_id].kind == "file":
70
tree.get_file(file_id).read()
72
# makes a target version repo
73
repo_b = self.make_to_repository('b')
74
check_push_rev1(repo_b)
76
def test_fetch_missing_basis_text(self):
77
"""If fetching a delta, we should die if a basis is not present."""
78
tree = self.make_branch_and_tree('tree')
79
self.build_tree(['tree/a'])
80
tree.add(['a'], ['a-id'])
81
tree.commit('one', rev_id='rev-one')
82
self.build_tree_contents([('tree/a', 'new contents\n')])
83
tree.commit('two', rev_id='rev-two')
85
to_repo = self.make_to_repository('to_repo')
86
# We build a broken revision so that we can test the fetch code dies
87
# properly. So copy the inventory and revision, but not the text.
90
to_repo.start_write_group()
91
inv = tree.branch.repository.get_inventory('rev-one')
92
to_repo.add_inventory('rev-one', inv, [])
93
rev = tree.branch.repository.get_revision('rev-one')
94
to_repo.add_revision('rev-one', rev, inv=inv)
95
to_repo.commit_write_group()
99
# Implementations can either ensure that the target of the delta is
100
# reconstructable, or raise an exception (which stream based copies
103
to_repo.fetch(tree.branch.repository, 'rev-two')
104
except errors.RevisionNotPresent, e:
105
# If an exception is raised, the revision should not be in the
107
self.assertRaises((errors.NoSuchRevision, errors.RevisionNotPresent),
108
to_repo.revision_tree, 'rev-two')
110
# If not exception is raised, then the text should be
114
rt = to_repo.revision_tree('rev-two')
115
self.assertEqual('new contents\n',
116
rt.get_file_text('a-id'))
120
def test_fetch_missing_revision_same_location_fails(self):
121
repo_a = self.make_repository('.')
122
repo_b = repository.Repository.open('.')
124
self.assertRaises(errors.NoSuchRevision, repo_b.fetch, repo_a, revision_id='XXX')
125
except errors.LockError, e:
126
check_old_format_lock_error(self.repository_format)
128
def test_fetch_same_location_trivial_works(self):
129
repo_a = self.make_repository('.')
130
repo_b = repository.Repository.open('.')
133
except errors.LockError, e:
134
check_old_format_lock_error(self.repository_format)
136
def test_fetch_missing_text_other_location_fails(self):
137
source_tree = self.make_branch_and_tree('source')
138
source = source_tree.branch.repository
139
target = self.make_to_repository('target')
141
# start by adding a file so the data knit for the file exists in
142
# repositories that have specific files for each fileid.
143
self.build_tree(['source/id'])
144
source_tree.add(['id'], ['id'])
145
source_tree.commit('a', rev_id='a')
146
# now we manually insert a revision with an inventory referencing
147
# 'id' at revision 'b', but we do not insert revision b.
148
# this should ensure that the new versions of files are being checked
149
# for during pull operations
150
inv = source.get_inventory('a')
152
self.addCleanup(source.unlock)
153
source.start_write_group()
154
inv['id'].revision = 'b'
155
inv.revision_id = 'b'
156
sha1 = source.add_inventory('b', inv, ['a'])
157
rev = Revision(timestamp=0,
159
committer="Foo Bar <foo@example.com>",
163
rev.parent_ids = ['a']
164
source.add_revision('b', rev)
165
source.commit_write_group()
166
self.assertRaises(errors.RevisionNotPresent, target.fetch, source)
167
self.assertFalse(target.has_revision('b'))
169
def test_fetch_funky_file_id(self):
170
from_tree = self.make_branch_and_tree('tree')
171
if sys.platform == 'win32':
172
from_repo = from_tree.branch.repository
173
check_repo_format_for_funky_id_on_win32(from_repo)
174
self.build_tree(['tree/filename'])
175
from_tree.add('filename', 'funky-chars<>%&;"\'')
176
from_tree.commit('commit filename')
177
to_repo = self.make_to_repository('to')
178
to_repo.fetch(from_tree.branch.repository, from_tree.get_parent_ids()[0])
180
def test_fetch_revision_hash(self):
181
"""Ensure that inventory hashes are updated by fetch"""
182
from_tree = self.make_branch_and_tree('tree')
183
from_tree.commit('foo', rev_id='foo-id')
184
to_repo = self.make_to_repository('to')
185
to_repo.fetch(from_tree.branch.repository)
186
recorded_inv_sha1 = to_repo.get_inventory_sha1('foo-id')
187
xml = to_repo.get_inventory_xml('foo-id')
188
computed_inv_sha1 = osutils.sha_string(xml)
189
self.assertEqual(computed_inv_sha1, recorded_inv_sha1)
192
class TestFetchDependentData(TestCaseWithInterRepository):
194
def test_reference(self):
195
from_tree = self.make_branch_and_tree('tree')
196
to_repo = self.make_to_repository('to')
197
if (not from_tree.supports_tree_reference() or
198
not from_tree.branch.repository._format.supports_tree_reference or
199
not to_repo._format.supports_tree_reference):
200
raise TestNotApplicable("Need subtree support.")
201
subtree = self.make_branch_and_tree('tree/subtree')
202
subtree.commit('subrev 1')
203
from_tree.add_reference(subtree)
204
tree_rev = from_tree.commit('foo')
205
# now from_tree has a last-modified of subtree of the rev id of the
206
# commit for foo, and a reference revision of the rev id of the commit
208
to_repo.fetch(from_tree.branch.repository, tree_rev)
209
# to_repo should have a file_graph for from_tree.path2id('subtree') and
211
file_id = from_tree.path2id('subtree')
214
self.assertEqual({(file_id, tree_rev):()},
215
to_repo.texts.get_parent_map([(file_id, tree_rev)]))