#!/usr/bin/env python3 # Copyright 2015, 2019-2020 Codethink Limited # gzip archive frontend for git-fast-import import os.path import struct import subprocess import sys from gzip import GzipFile branch_name = "master" branch_ref = "refs/heads/%s" % branch_name committer_name = "Lorry Gzip Importer" committer_email = "lorry-gzip-importer@lorry" # The size of a gzip file is stored in the last 4 bytes def uncompressedsize(filename): with open(filename, "rb") as f: f.seek(-4, 2) return struct.unpack("I", f.read(4))[0] def export(zipfile, fast_import): def printlines(list): for str in list: fast_import.write(str.encode("utf-8") + b"\n") # 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() + b"\n") committer = committer_name + " <" + committer_email + "> %d +0000" % mtime 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()