/
fastexport.py
248 lines (217 loc) · 8.78 KB
/
fastexport.py
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
# __init__.py -- Fast export/import functionality
# Copyright (C) 2010-2013 Jelmer Vernooij <jelmer@jelmer.uk>
#
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as public 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.
#
"""Fast export/import functionality."""
import stat
from typing import Dict, Tuple
from fastimport import commands, parser, processor
from fastimport import errors as fastimport_errors
from .index import commit_tree
from .object_store import iter_tree_contents
from .objects import ZERO_SHA, Blob, Commit, Tag
def split_email(text):
(name, email) = text.rsplit(b" <", 1)
return (name, email.rstrip(b">"))
class GitFastExporter:
"""Generate a fast-export output stream for Git objects."""
def __init__(self, outf, store) -> None:
self.outf = outf
self.store = store
self.markers: Dict[bytes, bytes] = {}
self._marker_idx = 0
def print_cmd(self, cmd):
self.outf.write(getattr(cmd, "__bytes__", cmd.__repr__)() + b"\n")
def _allocate_marker(self):
self._marker_idx += 1
return ("%d" % (self._marker_idx,)).encode("ascii")
def _export_blob(self, blob):
marker = self._allocate_marker()
self.markers[marker] = blob.id
return (commands.BlobCommand(marker, blob.data), marker)
def emit_blob(self, blob):
(cmd, marker) = self._export_blob(blob)
self.print_cmd(cmd)
return marker
def _iter_files(self, base_tree, new_tree):
for (
(old_path, new_path),
(old_mode, new_mode),
(old_hexsha, new_hexsha),
) in self.store.tree_changes(base_tree, new_tree):
if new_path is None:
yield commands.FileDeleteCommand(old_path)
continue
if not stat.S_ISDIR(new_mode):
blob = self.store[new_hexsha]
marker = self.emit_blob(blob)
if old_path != new_path and old_path is not None:
yield commands.FileRenameCommand(old_path, new_path)
if old_mode != new_mode or old_hexsha != new_hexsha:
prefixed_marker = b":" + marker
yield commands.FileModifyCommand(
new_path, new_mode, prefixed_marker, None
)
def _export_commit(self, commit, ref, base_tree=None):
file_cmds = list(self._iter_files(base_tree, commit.tree))
marker = self._allocate_marker()
if commit.parents:
from_ = commit.parents[0]
merges = commit.parents[1:]
else:
from_ = None
merges = []
author, author_email = split_email(commit.author)
committer, committer_email = split_email(commit.committer)
cmd = commands.CommitCommand(
ref,
marker,
(author, author_email, commit.author_time, commit.author_timezone),
(
committer,
committer_email,
commit.commit_time,
commit.commit_timezone,
),
commit.message,
from_,
merges,
file_cmds,
)
return (cmd, marker)
def emit_commit(self, commit, ref, base_tree=None):
cmd, marker = self._export_commit(commit, ref, base_tree)
self.print_cmd(cmd)
return marker
class GitImportProcessor(processor.ImportProcessor):
"""An import processor that imports into a Git repository using Dulwich."""
# FIXME: Batch creation of objects?
def __init__(self, repo, params=None, verbose=False, outf=None) -> None:
processor.ImportProcessor.__init__(self, params, verbose)
self.repo = repo
self.last_commit = ZERO_SHA
self.markers: Dict[bytes, bytes] = {}
self._contents: Dict[bytes, Tuple[int, bytes]] = {}
def lookup_object(self, objectish):
if objectish.startswith(b":"):
return self.markers[objectish[1:]]
return objectish
def import_stream(self, stream):
p = parser.ImportParser(stream)
self.process(p.iter_commands)
return self.markers
def blob_handler(self, cmd):
"""Process a BlobCommand."""
blob = Blob.from_string(cmd.data)
self.repo.object_store.add_object(blob)
if cmd.mark:
self.markers[cmd.mark] = blob.id
def checkpoint_handler(self, cmd):
"""Process a CheckpointCommand."""
def commit_handler(self, cmd):
"""Process a CommitCommand."""
commit = Commit()
if cmd.author is not None:
author = cmd.author
else:
author = cmd.committer
(author_name, author_email, author_timestamp, author_timezone) = author
(
committer_name,
committer_email,
commit_timestamp,
commit_timezone,
) = cmd.committer
commit.author = author_name + b" <" + author_email + b">"
commit.author_timezone = author_timezone
commit.author_time = int(author_timestamp)
commit.committer = committer_name + b" <" + committer_email + b">"
commit.commit_timezone = commit_timezone
commit.commit_time = int(commit_timestamp)
commit.message = cmd.message
commit.parents = []
if cmd.from_:
cmd.from_ = self.lookup_object(cmd.from_)
self._reset_base(cmd.from_)
for filecmd in cmd.iter_files():
if filecmd.name == b"filemodify":
if filecmd.data is not None:
blob = Blob.from_string(filecmd.data)
self.repo.object_store.add(blob)
blob_id = blob.id
else:
blob_id = self.lookup_object(filecmd.dataref)
self._contents[filecmd.path] = (filecmd.mode, blob_id)
elif filecmd.name == b"filedelete":
del self._contents[filecmd.path]
elif filecmd.name == b"filecopy":
self._contents[filecmd.dest_path] = self._contents[filecmd.src_path]
elif filecmd.name == b"filerename":
self._contents[filecmd.new_path] = self._contents[filecmd.old_path]
del self._contents[filecmd.old_path]
elif filecmd.name == b"filedeleteall":
self._contents = {}
else:
raise Exception(f"Command {filecmd.name} not supported")
commit.tree = commit_tree(
self.repo.object_store,
((path, hexsha, mode) for (path, (mode, hexsha)) in self._contents.items()),
)
if self.last_commit != ZERO_SHA:
commit.parents.append(self.last_commit)
for merge in cmd.merges:
commit.parents.append(self.lookup_object(merge))
self.repo.object_store.add_object(commit)
self.repo[cmd.ref] = commit.id
self.last_commit = commit.id
if cmd.mark:
self.markers[cmd.mark] = commit.id
def progress_handler(self, cmd):
"""Process a ProgressCommand."""
def _reset_base(self, commit_id):
if self.last_commit == commit_id:
return
self._contents = {}
self.last_commit = commit_id
if commit_id != ZERO_SHA:
tree_id = self.repo[commit_id].tree
for (
path,
mode,
hexsha,
) in iter_tree_contents(self.repo.object_store, tree_id):
self._contents[path] = (mode, hexsha)
def reset_handler(self, cmd):
"""Process a ResetCommand."""
if cmd.from_ is None:
from_ = ZERO_SHA
else:
from_ = self.lookup_object(cmd.from_)
self._reset_base(from_)
self.repo.refs[cmd.ref] = from_
def tag_handler(self, cmd):
"""Process a TagCommand."""
tag = Tag()
tag.tagger = cmd.tagger
tag.message = cmd.message
tag.name = cmd.tag
self.repo.add_object(tag)
self.repo.refs["refs/tags/" + tag.name] = tag.id
def feature_handler(self, cmd):
"""Process a FeatureCommand."""
raise fastimport_errors.UnknownFeature(cmd.feature_name)