~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2008-05-02 02:31:14 UTC
  • mfrom: (3399 +trunk)
  • mto: (3408.1.1 doc)
  • mto: This revision was merged to the branch mainline in revision 3409.
  • Revision ID: mbp@sourcefrog.net-20080502023114-y2gcg3w3jc770j9m
merge trunk

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 copy the missing basis text, or raise an
 
97
        # exception
 
98
        try:
 
99
            to_repo.fetch(tree.branch.repository, 'rev-two')
 
100
        except errors.RevisionNotPresent, e:
 
101
            # If an exception is raised, the revision should not be in the
 
102
            # target.
 
103
            self.assertRaises(
 
104
                (errors.NoSuchRevision, errors.RevisionNotPresent),
 
105
                 to_repo.revision_tree, 'rev-two')
 
106
        else:
 
107
            # If not exception is raised, then the basis text should be
 
108
            # available.
 
109
            to_repo.lock_read()
 
110
            try:
 
111
                rt = to_repo.revision_tree('rev-one')
 
112
                self.assertEqual('contents of tree/a\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,
 
122
                              revision_id='XXX')
 
123
        except errors.LockError, e:
 
124
            check_old_format_lock_error(self.repository_format)
 
125
 
 
126
    def test_fetch_same_location_trivial_works(self):
 
127
        repo_a = self.make_repository('.')
 
128
        repo_b = repository.Repository.open('.')
 
129
        try:
 
130
            repo_a.fetch(repo_b)
 
131
        except errors.LockError, e:
 
132
            check_old_format_lock_error(self.repository_format)
 
133
 
 
134
    def test_fetch_missing_text_other_location_fails(self):
 
135
        source_tree = self.make_branch_and_tree('source')
 
136
        source = source_tree.branch.repository
 
137
        target = self.make_to_repository('target')
 
138
    
 
139
        # start by adding a file so the data knit for the file exists in
 
140
        # repositories that have specific files for each fileid.
 
141
        self.build_tree(['source/id'])
 
142
        source_tree.add(['id'], ['id'])
 
143
        source_tree.commit('a', rev_id='a')
 
144
        # now we manually insert a revision with an inventory referencing
 
145
        # 'id' at revision 'b', but we do not insert revision b.
 
146
        # this should ensure that the new versions of files are being checked
 
147
        # for during pull operations
 
148
        inv = source.get_inventory('a')
 
149
        source.lock_write()
 
150
        self.addCleanup(source.unlock)
 
151
        source.start_write_group()
 
152
        inv['id'].revision = 'b'
 
153
        inv.revision_id = 'b'
 
154
        sha1 = source.add_inventory('b', inv, ['a'])
 
155
        rev = Revision(timestamp=0,
 
156
                       timezone=None,
 
157
                       committer="Foo Bar <foo@example.com>",
 
158
                       message="Message",
 
159
                       inventory_sha1=sha1,
 
160
                       revision_id='b')
 
161
        rev.parent_ids = ['a']
 
162
        source.add_revision('b', rev)
 
163
        source.commit_write_group()
 
164
        self.assertRaises(errors.RevisionNotPresent, target.fetch, source)
 
165
        self.assertFalse(target.has_revision('b'))
 
166
 
 
167
    def test_fetch_funky_file_id(self):
 
168
        from_tree = self.make_branch_and_tree('tree')
 
169
        if sys.platform == 'win32':
 
170
            from_repo = from_tree.branch.repository
 
171
            check_repo_format_for_funky_id_on_win32(from_repo)
 
172
        self.build_tree(['tree/filename'])
 
173
        from_tree.add('filename', 'funky-chars<>%&;"\'')
 
174
        from_tree.commit('commit filename')
 
175
        to_repo = self.make_to_repository('to')
 
176
        to_repo.fetch(from_tree.branch.repository,
 
177
                      from_tree.get_parent_ids()[0])
 
178
 
 
179
    def test_fetch_revision_hash(self):
 
180
        """Ensure that inventory hashes are updated by fetch"""
 
181
        from_tree = self.make_branch_and_tree('tree')
 
182
        from_tree.commit('foo', rev_id='foo-id')
 
183
        to_repo = self.make_to_repository('to')
 
184
        to_repo.fetch(from_tree.branch.repository)
 
185
        recorded_inv_sha1 = to_repo.get_inventory_sha1('foo-id')
 
186
        xml = to_repo.get_inventory_xml('foo-id')
 
187
        computed_inv_sha1 = osutils.sha_string(xml)
 
188
        self.assertEqual(computed_inv_sha1, recorded_inv_sha1)
 
189
 
 
190
 
 
191
class TestFetchDependentData(TestCaseWithInterRepository):
 
192
 
 
193
    def test_reference(self):
 
194
        from_tree = self.make_branch_and_tree('tree')
 
195
        to_repo = self.make_to_repository('to')
 
196
        if (not from_tree.supports_tree_reference() or
 
197
            not from_tree.branch.repository._format.supports_tree_reference or
 
198
            not to_repo._format.supports_tree_reference):
 
199
            raise TestNotApplicable("Need subtree support.")
 
200
        subtree = self.make_branch_and_tree('tree/subtree')
 
201
        subtree.commit('subrev 1')
 
202
        from_tree.add_reference(subtree)
 
203
        tree_rev = from_tree.commit('foo')
 
204
        # now from_tree has a last-modified of subtree of the rev id of the
 
205
        # commit for foo, and a reference revision of the rev id of the commit
 
206
        # for subrev 1
 
207
        to_repo.fetch(from_tree.branch.repository, tree_rev)
 
208
        # to_repo should have a file_graph for from_tree.path2id('subtree') and
 
209
        # revid tree_rev.
 
210
        to_repo.lock_read()
 
211
        try:
 
212
            file_vf = to_repo.weave_store.get_weave(
 
213
                from_tree.path2id('subtree'), to_repo.get_transaction())
 
214
            self.assertEqual([tree_rev], file_vf.get_ancestry([tree_rev]))
 
215
        finally:
 
216
            to_repo.unlock()