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
|
#!/usr/bin/env python3
# gzip archive frontend for git-fast-import
from gzip import GzipFile
import os
import os.path
import struct
import sys
if len(sys.argv) < 2:
print('usage:', sys.argv[0], '<gzipfile>...')
sys.exit(1)
branch_name = 'master'
branch_ref = 'refs/heads/%s' % branch_name
committer_name = 'Lorry Gzip Importer'
committer_email = 'lorry-gzip-importer@lorry'
fast_import = os.popen('git fast-import --quiet', 'w')
def printlines(list):
for str in list:
fast_import.write(str + "\n")
# The size of a gzip file is stored in the last 4 bytes
def uncompressedsize(filename):
with open(filename) as f:
f.seek(-4, 2)
return struct.unpack('I', f.read(4))[0]
for zipfile in sys.argv[1:]:
# Gzip does have an encoded mtime, however Python's GzipFile
# just ignores it, so we just yank the mtime of the zip file itself.
mtime = os.path.getmtime(zipfile);
file_size = uncompressedsize (zipfile);
zip = GzipFile(zipfile, 'rb')
printlines(('blob', 'mark :1', 'data ' + str(file_size)))
fast_import.write(zip.read() + "\n")
committer = committer_name + ' <' + committer_email + '> %d +0000' % \
mtime
zipfile_basename = os.path.basename(zipfile)
printlines(('commit ' + branch_ref, 'committer ' + committer, \
'data <<EOM', 'Imported from ' + zipfile_basename + '.', 'EOM', \
'', 'deleteall'))
last_dot = zipfile_basename[:-1].rfind('.')
unzipped_file = zipfile_basename[:last_dot]
fast_import.write('M 100644 :1 ' + unzipped_file + '\n');
zipname, _ = os.path.splitext(zipfile_basename)
printlines(('', 'tag ' + zipname, \
'from ' + branch_ref, 'tagger ' + committer, \
'data <<EOM', 'Package ' + zipfile, 'EOM', ''))
if fast_import.close():
sys.exit(1)
|