File: test_missing_obj_finder.py

package info (click to toggle)
dulwich 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,388 kB
  • sloc: python: 99,991; makefile: 163; sh: 67
file content (311 lines) | stat: -rw-r--r-- 11,370 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# test_missing_obj_finder.py -- tests for MissingObjectFinder
# Copyright (C) 2012 syntevo GmbH
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#

from dulwich.object_store import MemoryObjectStore, MissingObjectFinder
from dulwich.objects import Blob
from dulwich.tests.utils import build_commit_graph, make_object, make_tag

from . import TestCase


class MissingObjectFinderTest(TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.store = MemoryObjectStore()
        self.commits = []

    def cmt(self, n):
        return self.commits[n - 1]

    def assertMissingMatch(self, haves, wants, expected) -> None:
        for sha, path in MissingObjectFinder(self.store, haves, wants, shallow=set()):
            self.assertIn(
                sha, expected, f"({sha},{path}) erroneously reported as missing"
            )
            expected.remove(sha)

        self.assertEqual(
            len(expected),
            0,
            f"some objects are not reported as missing: {expected}",
        )


class MOFLinearRepoTest(MissingObjectFinderTest):
    def setUp(self) -> None:
        super().setUp()
        # present in 1, removed in 3
        f1_1 = make_object(Blob, data=b"f1")
        # present in all revisions, changed in 2 and 3
        f2_1 = make_object(Blob, data=b"f2")
        f2_2 = make_object(Blob, data=b"f2-changed")
        f2_3 = make_object(Blob, data=b"f2-changed-again")
        # added in 2, left unmodified in 3
        f3_2 = make_object(Blob, data=b"f3")

        commit_spec = [[1], [2, 1], [3, 2]]
        trees = {
            1: [(b"f1", f1_1), (b"f2", f2_1)],
            2: [(b"f1", f1_1), (b"f2", f2_2), (b"f3", f3_2)],
            3: [(b"f2", f2_3), (b"f3", f3_2)],
        }
        # commit 1: f1 and f2
        # commit 2: f3 added, f2 changed. Missing shall report commit id and a
        # tree referenced by commit
        # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall
        # be reported as modified
        self.commits = build_commit_graph(self.store, commit_spec, trees)
        self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
        self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
        self.missing_1_3 = [
            self.cmt(2).id,
            self.cmt(3).id,
            self.cmt(2).tree,
            self.cmt(3).tree,
            f2_2.id,
            f3_2.id,
            f2_3.id,
        ]

    def test_1_to_2(self) -> None:
        self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id], self.missing_1_2)

    def test_2_to_3(self) -> None:
        self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id], self.missing_2_3)

    def test_1_to_3(self) -> None:
        self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id], self.missing_1_3)

    def test_bogus_haves(self) -> None:
        """Ensure non-existent SHA in haves are tolerated."""
        bogus_sha = self.cmt(2).id[::-1]
        haves = [self.cmt(1).id, bogus_sha]
        wants = [self.cmt(3).id]
        self.assertMissingMatch(haves, wants, self.missing_1_3)

    def test_bogus_wants_failure(self) -> None:
        """Ensure non-existent SHA in wants are not tolerated."""
        bogus_sha = self.cmt(2).id[::-1]
        haves = [self.cmt(1).id]
        wants = [self.cmt(3).id, bogus_sha]
        self.assertRaises(
            KeyError, MissingObjectFinder, self.store, haves, wants, shallow=set()
        )

    def test_no_changes(self) -> None:
        self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])


class MOFMergeForkRepoTest(MissingObjectFinderTest):
    # 1 --- 2 --- 4 --- 6 --- 7
    #          \        /
    #           3  ---
    #            \
    #             5

    def setUp(self) -> None:
        super().setUp()
        f1_1 = make_object(Blob, data=b"f1")
        f1_2 = make_object(Blob, data=b"f1-2")
        f1_4 = make_object(Blob, data=b"f1-4")
        f1_7 = make_object(Blob, data=b"f1-2")  # same data as in rev 2
        f2_1 = make_object(Blob, data=b"f2")
        f2_3 = make_object(Blob, data=b"f2-3")
        f3_3 = make_object(Blob, data=b"f3")
        f3_5 = make_object(Blob, data=b"f3-5")
        commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
        trees = {
            1: [(b"f1", f1_1), (b"f2", f2_1)],
            2: [(b"f1", f1_2), (b"f2", f2_1)],  # f1 changed
            # f3 added, f2 changed
            3: [(b"f1", f1_2), (b"f2", f2_3), (b"f3", f3_3)],
            4: [(b"f1", f1_4), (b"f2", f2_1)],  # f1 changed
            5: [(b"f1", f1_2), (b"f3", f3_5)],  # f2 removed, f3 changed
            # merged 3 and 4
            6: [(b"f1", f1_4), (b"f2", f2_3), (b"f3", f3_3)],
            # f1 changed to match rev2. f3 removed
            7: [(b"f1", f1_7), (b"f2", f2_3)],
        }
        self.commits = build_commit_graph(self.store, commit_spec, trees)

        self.f1_2_id = f1_2.id
        self.f1_4_id = f1_4.id
        self.f1_7_id = f1_7.id
        self.f2_3_id = f2_3.id
        self.f3_3_id = f3_3.id

        self.assertEqual(f1_2.id, f1_7.id, "[sanity]")

    def test_have6_want7(self) -> None:
        # have 6, want 7. Ideally, shall not report f1_7 as it's the same as
        # f1_2, however, to do so, MissingObjectFinder shall not record trees
        # of common commits only, but also all parent trees and tree items,
        # which is an overkill (i.e. in sha_done it records f1_4 as known, and
        # doesn't record f1_2 was known prior to that, hence can't detect f1_7
        # is in fact f1_2 and shall not be reported)
        self.assertMissingMatch(
            [self.cmt(6).id],
            [self.cmt(7).id],
            [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id],
        )

    def test_have4_want7(self) -> None:
        # have 4, want 7. Shall not include rev5 as it is not in the tree
        # between 4 and 7 (well, it is, but its SHA's are irrelevant for 4..7
        # commit hierarchy)
        self.assertMissingMatch(
            [self.cmt(4).id],
            [self.cmt(7).id],
            [
                self.cmt(7).id,
                self.cmt(6).id,
                self.cmt(3).id,
                self.cmt(7).tree,
                self.cmt(6).tree,
                self.cmt(3).tree,
                self.f2_3_id,
                self.f3_3_id,
            ],
        )

    def test_have1_want6(self) -> None:
        # have 1, want 6. Shall not include rev5
        self.assertMissingMatch(
            [self.cmt(1).id],
            [self.cmt(6).id],
            [
                self.cmt(6).id,
                self.cmt(4).id,
                self.cmt(3).id,
                self.cmt(2).id,
                self.cmt(6).tree,
                self.cmt(4).tree,
                self.cmt(3).tree,
                self.cmt(2).tree,
                self.f1_2_id,
                self.f1_4_id,
                self.f2_3_id,
                self.f3_3_id,
            ],
        )

    def test_have3_want6(self) -> None:
        # have 3, want 7. Shall not report rev2 and its tree, because
        # haves(3) means has parents, i.e. rev2, too
        # BUT shall report any changes descending rev2 (excluding rev3)
        # Shall NOT report f1_7 as it's technically == f1_2
        self.assertMissingMatch(
            [self.cmt(3).id],
            [self.cmt(7).id],
            [
                self.cmt(7).id,
                self.cmt(6).id,
                self.cmt(4).id,
                self.cmt(7).tree,
                self.cmt(6).tree,
                self.cmt(4).tree,
                self.f1_4_id,
            ],
        )

    def test_have5_want7(self) -> None:
        # have 5, want 7. Common parent is rev2, hence children of rev2 from
        # a descent line other than rev5 shall be reported
        # expects f1_4 from rev6. f3_5 is known in rev5;
        # f1_7 shall be the same as f1_2 (known, too)
        self.assertMissingMatch(
            [self.cmt(5).id],
            [self.cmt(7).id],
            [
                self.cmt(7).id,
                self.cmt(6).id,
                self.cmt(4).id,
                self.cmt(7).tree,
                self.cmt(6).tree,
                self.cmt(4).tree,
                self.f1_4_id,
            ],
        )


class MOFTagsTest(MissingObjectFinderTest):
    def setUp(self) -> None:
        super().setUp()
        f1_1 = make_object(Blob, data=b"f1")
        commit_spec = [[1]]
        trees = {1: [(b"f1", f1_1)]}
        self.commits = build_commit_graph(self.store, commit_spec, trees)

        self._normal_tag = make_tag(self.cmt(1))
        self.store.add_object(self._normal_tag)

        self._tag_of_tag = make_tag(self._normal_tag)
        self.store.add_object(self._tag_of_tag)

        self._tag_of_tree = make_tag(self.store[self.cmt(1).tree])
        self.store.add_object(self._tag_of_tree)

        self._tag_of_blob = make_tag(f1_1)
        self.store.add_object(self._tag_of_blob)

        self._tag_of_tag_of_blob = make_tag(self._tag_of_blob)
        self.store.add_object(self._tag_of_tag_of_blob)

        self.f1_1_id = f1_1.id

    def test_tagged_commit(self) -> None:
        # The user already has the tagged commit, all they want is the tag,
        # so send them only the tag object.
        self.assertMissingMatch(
            [self.cmt(1).id], [self._normal_tag.id], [self._normal_tag.id]
        )

    # The remaining cases are unusual, but do happen in the wild.
    def test_tagged_tag(self) -> None:
        # User already has tagged tag, send only tag of tag
        self.assertMissingMatch(
            [self._normal_tag.id], [self._tag_of_tag.id], [self._tag_of_tag.id]
        )
        # User needs both tags, but already has commit
        self.assertMissingMatch(
            [self.cmt(1).id],
            [self._tag_of_tag.id],
            [self._normal_tag.id, self._tag_of_tag.id],
        )

    def test_tagged_tree(self) -> None:
        self.assertMissingMatch(
            [],
            [self._tag_of_tree.id],
            [self._tag_of_tree.id, self.cmt(1).tree, self.f1_1_id],
        )

    def test_tagged_blob(self) -> None:
        self.assertMissingMatch(
            [], [self._tag_of_blob.id], [self._tag_of_blob.id, self.f1_1_id]
        )

    def test_tagged_tagged_blob(self) -> None:
        self.assertMissingMatch(
            [],
            [self._tag_of_tag_of_blob.id],
            [self._tag_of_tag_of_blob.id, self._tag_of_blob.id, self.f1_1_id],
        )