git
78 строк · 2.2 Кб
1#!/usr/bin/env python
2
3## zip archive frontend for git-fast-import
4##
5## For example:
6##
7## mkdir project; cd project; git init
8## python import-zips.py *.zip
9## git log --stat import-zips
10
11from os import popen, path
12from sys import argv, exit, hexversion, stderr
13from time import mktime
14from zipfile import ZipFile
15
16if hexversion < 0x01060000:
17# The limiter is the zipfile module
18stderr.write("import-zips.py: requires Python 1.6.0 or later.\n")
19exit(1)
20
21if len(argv) < 2:
22print 'usage:', argv[0], '<zipfile>...'
23exit(1)
24
25branch_ref = 'refs/heads/import-zips'
26committer_name = 'Z Ip Creator'
27committer_email = 'zip@example.com'
28
29fast_import = popen('git fast-import --quiet', 'w')
30def printlines(list):
31for str in list:
32fast_import.write(str + "\n")
33
34for zipfile in argv[1:]:
35commit_time = 0
36next_mark = 1
37common_prefix = None
38mark = dict()
39
40zip = ZipFile(zipfile, 'r')
41for name in zip.namelist():
42if name.endswith('/'):
43continue
44info = zip.getinfo(name)
45
46if commit_time < info.date_time:
47commit_time = info.date_time
48if common_prefix == None:
49common_prefix = name[:name.rfind('/') + 1]
50else:
51while not name.startswith(common_prefix):
52last_slash = common_prefix[:-1].rfind('/') + 1
53common_prefix = common_prefix[:last_slash]
54
55mark[name] = ':' + str(next_mark)
56next_mark += 1
57
58printlines(('blob', 'mark ' + mark[name], \
59'data ' + str(info.file_size)))
60fast_import.write(zip.read(name) + "\n")
61
62committer = committer_name + ' <' + committer_email + '> %d +0000' % \
63mktime(commit_time + (0, 0, 0))
64
65printlines(('commit ' + branch_ref, 'committer ' + committer, \
66'data <<EOM', 'Imported from ' + zipfile + '.', 'EOM', \
67'', 'deleteall'))
68
69for name in mark.keys():
70fast_import.write('M 100644 ' + mark[name] + ' ' +
71name[len(common_prefix):] + "\n")
72
73printlines(('', 'tag ' + path.basename(zipfile), \
74'from ' + branch_ref, 'tagger ' + committer, \
75'data <<EOM', 'Package ' + zipfile, 'EOM', ''))
76
77if fast_import.close():
78exit(1)
79