File: test_fastexport.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 (309 lines) | stat: -rw-r--r-- 10,522 bytes parent folder | download
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
# test_fastexport.py -- Fast export/import functionality
# Copyright (C) 2010 Jelmer Vernooij <jelmer@jelmer.uk>
#
# 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.
#

import stat
from io import BytesIO

from dulwich.object_store import MemoryObjectStore
from dulwich.objects import ZERO_SHA, Blob, Commit, Tree
from dulwich.repo import MemoryRepo
from dulwich.tests.utils import build_commit_graph, make_commit

from . import DependencyMissing, TestCase


class GitFastExporterTests(TestCase):
    """Tests for the GitFastExporter tests."""

    def setUp(self) -> None:
        super().setUp()
        self.store = MemoryObjectStore()
        self.stream = BytesIO()
        try:
            from dulwich.fastexport import GitFastExporter
        except ImportError as exc:
            raise DependencyMissing("python-fastimport") from exc
        self.fastexporter = GitFastExporter(self.stream, self.store)

    def test_emit_blob(self) -> None:
        b = Blob()
        b.data = b"fooBAR"
        self.fastexporter.emit_blob(b)
        self.assertEqual(b"blob\nmark :1\ndata 6\nfooBAR\n", self.stream.getvalue())

    def test_emit_commit(self) -> None:
        b = Blob()
        b.data = b"FOO"
        t = Tree()
        t.add(b"foo", stat.S_IFREG | 0o644, b.id)
        c = make_commit(
            author=b"Jelmer <jelmer@host>",
            committer=b"Jelmer <jelmer@host>",
            author_time=1271345553,
            commit_time=1271345553,
            author_timezone=0,
            commit_timezone=0,
            message=b"msg",
            tree=t.id,
        )
        self.store.add_objects([(b, None), (t, None), (c, None)])
        self.fastexporter.emit_commit(c, b"refs/heads/master")
        self.assertEqual(
            b"""blob
mark :1
data 3
FOO
commit refs/heads/master
mark :2
author Jelmer <jelmer@host> 1271345553 +0000
committer Jelmer <jelmer@host> 1271345553 +0000
data 3
msg
M 644 :1 foo
""",
            self.stream.getvalue(),
        )


class GitImportProcessorTests(TestCase):
    """Tests for the GitImportProcessor tests."""

    def setUp(self) -> None:
        super().setUp()
        self.repo = MemoryRepo()
        self.addCleanup(self.repo.close)
        try:
            from dulwich.fastexport import GitImportProcessor
        except ImportError as exc:
            raise DependencyMissing("python-fastimport") from exc
        self.processor = GitImportProcessor(self.repo)

    def test_reset_handler(self) -> None:
        from fastimport import commands

        [c1] = build_commit_graph(self.repo.object_store, [[1]])
        cmd = commands.ResetCommand(b"refs/heads/foo", c1.id)
        self.processor.reset_handler(cmd)
        self.assertEqual(c1.id, self.repo.get_refs()[b"refs/heads/foo"])
        self.assertEqual(c1.id, self.processor.last_commit)

    def test_reset_handler_marker(self) -> None:
        from fastimport import commands

        [c1, _c2] = build_commit_graph(self.repo.object_store, [[1], [2]])
        self.processor.markers[b"10"] = c1.id
        cmd = commands.ResetCommand(b"refs/heads/foo", b":10")
        self.processor.reset_handler(cmd)
        self.assertEqual(c1.id, self.repo.get_refs()[b"refs/heads/foo"])

    def test_reset_handler_default(self) -> None:
        from fastimport import commands

        [_c1, _c2] = build_commit_graph(self.repo.object_store, [[1], [2]])
        cmd = commands.ResetCommand(b"refs/heads/foo", None)
        self.processor.reset_handler(cmd)
        self.assertEqual(ZERO_SHA, self.repo.get_refs()[b"refs/heads/foo"])

    def test_commit_handler(self) -> None:
        from fastimport import commands

        cmd = commands.CommitCommand(
            b"refs/heads/foo",
            b"mrkr",
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            b"FOO",
            None,
            [],
            [],
        )
        self.processor.commit_handler(cmd)
        commit = self.repo[self.processor.last_commit]
        self.assertEqual(b"Jelmer <jelmer@samba.org>", commit.author)
        self.assertEqual(b"Jelmer <jelmer@samba.org>", commit.committer)
        self.assertEqual(b"FOO", commit.message)
        self.assertEqual([], commit.parents)
        self.assertEqual(432432432.0, commit.commit_time)
        self.assertEqual(432432432.0, commit.author_time)
        self.assertEqual(3600, commit.commit_timezone)
        self.assertEqual(3600, commit.author_timezone)
        self.assertEqual(commit, self.repo[b"refs/heads/foo"])

    def test_commit_handler_markers(self) -> None:
        from fastimport import commands

        [c1, c2, c3] = build_commit_graph(self.repo.object_store, [[1], [2], [3]])
        self.processor.markers[b"10"] = c1.id
        self.processor.markers[b"42"] = c2.id
        self.processor.markers[b"98"] = c3.id
        cmd = commands.CommitCommand(
            b"refs/heads/foo",
            b"mrkr",
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            b"FOO",
            b":10",
            [b":42", b":98"],
            [],
        )
        self.processor.commit_handler(cmd)
        commit = self.repo[self.processor.last_commit]
        self.assertEqual(c1.id, commit.parents[0])
        self.assertEqual(c2.id, commit.parents[1])
        self.assertEqual(c3.id, commit.parents[2])

    def test_import_stream(self) -> None:
        markers = self.processor.import_stream(
            BytesIO(
                b"""blob
mark :1
data 11
text for a

commit refs/heads/master
mark :2
committer Joe Foo <joe@foo.com> 1288287382 +0000
data 20
<The commit message>
M 100644 :1 a

"""
            )
        )
        self.assertEqual(2, len(markers))
        self.assertIsInstance(self.repo[markers[b"1"]], Blob)
        self.assertIsInstance(self.repo[markers[b"2"]], Commit)

    def test_file_add(self) -> None:
        from fastimport import commands

        cmd = commands.BlobCommand(b"23", b"data")
        self.processor.blob_handler(cmd)
        cmd = commands.CommitCommand(
            b"refs/heads/foo",
            b"mrkr",
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            b"FOO",
            None,
            [],
            [commands.FileModifyCommand(b"path", 0o100644, b":23", None)],
        )
        self.processor.commit_handler(cmd)
        commit = self.repo[self.processor.last_commit]
        self.assertEqual(
            [(b"path", 0o100644, b"6320cd248dd8aeaab759d5871f8781b5c0505172")],
            self.repo[commit.tree].items(),
        )

    def simple_commit(self):
        from fastimport import commands

        cmd = commands.BlobCommand(b"23", b"data")
        self.processor.blob_handler(cmd)
        cmd = commands.CommitCommand(
            b"refs/heads/foo",
            b"mrkr",
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            b"FOO",
            None,
            [],
            [commands.FileModifyCommand(b"path", 0o100644, b":23", None)],
        )
        self.processor.commit_handler(cmd)
        commit = self.repo[self.processor.last_commit]
        return commit

    def make_file_commit(self, file_cmds):
        """Create a trivial commit with the specified file commands.

        Args:
          file_cmds: File commands to run.
        Returns: The created commit object
        """
        from fastimport import commands

        cmd = commands.CommitCommand(
            b"refs/heads/foo",
            b"mrkr",
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
            b"FOO",
            None,
            [],
            file_cmds,
        )
        self.processor.commit_handler(cmd)
        return self.repo[self.processor.last_commit]

    def test_file_copy(self) -> None:
        from fastimport import commands

        self.simple_commit()
        commit = self.make_file_commit([commands.FileCopyCommand(b"path", b"new_path")])
        self.assertEqual(
            [
                (
                    b"new_path",
                    0o100644,
                    b"6320cd248dd8aeaab759d5871f8781b5c0505172",
                ),
                (
                    b"path",
                    0o100644,
                    b"6320cd248dd8aeaab759d5871f8781b5c0505172",
                ),
            ],
            self.repo[commit.tree].items(),
        )

    def test_file_move(self) -> None:
        from fastimport import commands

        self.simple_commit()
        commit = self.make_file_commit(
            [commands.FileRenameCommand(b"path", b"new_path")]
        )
        self.assertEqual(
            [
                (
                    b"new_path",
                    0o100644,
                    b"6320cd248dd8aeaab759d5871f8781b5c0505172",
                ),
            ],
            self.repo[commit.tree].items(),
        )

    def test_file_delete(self) -> None:
        from fastimport import commands

        self.simple_commit()
        commit = self.make_file_commit([commands.FileDeleteCommand(b"path")])
        self.assertEqual([], self.repo[commit.tree].items())

    def test_file_deleteall(self) -> None:
        from fastimport import commands

        self.simple_commit()
        commit = self.make_file_commit([commands.FileDeleteAllCommand()])
        self.assertEqual([], self.repo[commit.tree].items())