~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/interrepository_implementations/test_fetch.py

Merge up bzr.dev.

Show diffs side-by-side

added added

removed removed

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