#!/usr/bin/env python3 ## Note: Modified for Baserock lorry. ## zip archive frontend for git-fast-import ## ## For example: ## ## mkdir project; cd project; git init ## python import-zips.py *.zip ## git log --stat import-zips import os.path import subprocess import sys import time from zipfile import ZipFile branch_name = 'master' branch_ref = 'refs/heads/%s' % branch_name committer_name = 'Lorry Zip Importer' committer_email = 'lorry-zip-importer@lorry' def export(zipfile, fast_import): def printlines(list): for str in list: fast_import.write(str.encode('utf-8') + b"\n") commit_time = 0 next_mark = 1 common_prefix = None mark = dict() zip = ZipFile(zipfile, 'r') for name in zip.namelist(): if name.endswith('/'): continue info = zip.getinfo(name) if commit_time < info.date_time: commit_time = info.date_time if common_prefix == None: common_prefix = name[:name.rfind('/') + 1] else: while not name.startswith(common_prefix): last_slash = common_prefix[:-1].rfind('/') + 1 common_prefix = common_prefix[:last_slash] mark[name] = ':' + str(next_mark) next_mark += 1 printlines(('blob', 'mark ' + mark[name], \ 'data ' + str(info.file_size))) fast_import.write(zip.read(name) + b"\n") committer = committer_name + ' <' + committer_email + '> %d +0000' % \ time.mktime(commit_time + (0, 0, 0)) zipfile_basename = os.path.basename(zipfile) printlines(('commit ' + branch_ref, 'committer ' + committer, \ 'data <...') sys.exit(1) with subprocess.Popen('git fast-import --quiet', shell=True, stdin=subprocess.PIPE) as import_proc: for zipfile in sys.argv[1:]: export(zipfile, import_proc.stdin) import_proc.stdin.close() if import_proc.wait() != 0: sys.exit(1) main()