3 ## zip archive frontend for git-fast-import
7 ## mkdir project; cd project; git init
8 ## python import-zips.py *.zip
9 ## git log --stat import-zips
11 from os
import popen
, path
12 from sys
import argv
, exit
13 from time
import mktime
14 from zipfile
import ZipFile
17 print 'Usage:', argv
[0], '<zipfile>...'
20 branch_ref
= 'refs/heads/import-zips'
21 committer_name
= 'Z Ip Creator'
22 committer_email
= 'zip@example.com'
24 fast_import
= popen('git fast-import --quiet', 'w')
27 fast_import
.write(str + "\n")
29 for zipfile
in argv
[1:]:
35 zip = ZipFile(zipfile
, 'r')
36 for name
in zip.namelist():
37 if name
.endswith('/'):
39 info
= zip.getinfo(name
)
41 if commit_time
< info
.date_time
:
42 commit_time
= info
.date_time
43 if common_prefix
== None:
44 common_prefix
= name
[:name
.rfind('/') + 1]
46 while not name
.startswith(common_prefix
):
47 common_prefix
= name
[:name
.rfind('/') + 1]
49 mark
[name
] = ':' + str(next_mark
)
52 printlines(('blob', 'mark ' + mark
[name
], \
53 'data ' + str(info
.file_size
)))
54 fast_import
.write(zip.read(name
) + "\n")
56 committer
= committer_name
+ ' <' + committer_email
+ '> %d +0000' % \
57 mktime(commit_time
+ (0, 0, 0))
59 printlines(('commit ' + branch_ref
, 'committer ' + committer
, \
60 'data <<EOM', 'Imported from ' + zipfile
+ '.', 'EOM', \
63 for name
in mark
.keys():
64 fast_import
.write('M 100644 ' + mark
[name
] + ' ' +
65 name
[len(common_prefix
):] + "\n")
67 printlines(('', 'tag ' + path
.basename(zipfile
), \
68 'from ' + branch_ref
, 'tagger ' + committer
, \
69 'data <<EOM', 'Package ' + zipfile
, 'EOM', ''))
71 if fast_import
.close():