diff options
Diffstat (limited to 'lorry.raw-file-importer')
-rwxr-xr-x | lorry.raw-file-importer | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/lorry.raw-file-importer b/lorry.raw-file-importer index bdb006e..c438e60 100755 --- a/lorry.raw-file-importer +++ b/lorry.raw-file-importer @@ -11,36 +11,39 @@ import subprocess import sys import time -branch_name = 'master' -branch_ref = 'refs/heads/%s' % branch_name -committer_name = 'Lorry Raw File Importer' -committer_email = 'lorry-raw-file-importer@lorry' +branch_name = "master" +branch_ref = "refs/heads/%s" % branch_name +committer_name = "Lorry Raw File Importer" +committer_email = "lorry-raw-file-importer@lorry" + def commit_lfs_gitattributes(fast_import): commit_time = int(time.time()) commit = ( - 'commit {ref}\n' - 'committer {committer_name} <{committer_email}> {commit_time} +0000\n' - 'data <<EOM\n' - 'Ensure LFS is configured\n' - 'EOM\n' - 'M 100644 inline .gitattributes\n' - 'data <<EOM\n' - '* filter=lfs diff=lfs merge=lfs -text\n' - '.gitattributes filter diff merge text=auto\n' - 'EOM\n' - '\n' + "commit {ref}\n" + "committer {committer_name} <{committer_email}> {commit_time} +0000\n" + "data <<EOM\n" + "Ensure LFS is configured\n" + "EOM\n" + "M 100644 inline .gitattributes\n" + "data <<EOM\n" + "* filter=lfs diff=lfs merge=lfs -text\n" + ".gitattributes filter diff merge text=auto\n" + "EOM\n" + "\n" ).format( ref=branch_ref, - committer_name=committer_name, committer_email=committer_email, commit_time=commit_time, + committer_name=committer_name, + committer_email=committer_email, + commit_time=commit_time, ) - fast_import.write(commit.encode('utf-8')) + fast_import.write(commit.encode("utf-8")) def commit_lfs_file(raw_file, relative_path, last_commit, fast_import): # git-lfs-pointer of the file ret = subprocess.run( - ['git', 'lfs', 'pointer', '--file', raw_file], + ["git", "lfs", "pointer", "--file", raw_file], stdout=subprocess.PIPE, check=True, ) @@ -49,11 +52,11 @@ def commit_lfs_file(raw_file, relative_path, last_commit, fast_import): # sha256sum of the file # slightly wasteful because git-lfs-pointer also generates a sha256sum - with open(raw_file, 'rb') as f: + with open(raw_file, "rb") as f: shasum = hashlib.sha256(f.read()).hexdigest() # Add the file to the repo - out_dir = os.path.join('lfs', 'objects', shasum[0:2], shasum[2:4]) + out_dir = os.path.join("lfs", "objects", shasum[0:2], shasum[2:4]) os.makedirs(out_dir, exist_ok=True) shutil.copyfile(raw_file, os.path.join(out_dir, shasum)) @@ -66,33 +69,37 @@ def commit_lfs_file(raw_file, relative_path, last_commit, fast_import): print("error: unsafe path: {}".format(path), file=sys.stderr) sys.exit(1) - fromline = 'from {}\n'.format(last_commit) if last_commit else '' + fromline = "from {}\n".format(last_commit) if last_commit else "" commit = ( - 'commit {ref}\n' - 'committer {committer_name} <{committer_email}> {commit_time} +0000\n' - 'data <<EOM\n' - 'import {basename}\n' - 'EOM\n' - '{fromline}' - 'M 100644 inline {path}\n' - 'data {datasize}\n' - '{data}\n' - '\n' + "commit {ref}\n" + "committer {committer_name} <{committer_email}> {commit_time} +0000\n" + "data <<EOM\n" + "import {basename}\n" + "EOM\n" + "{fromline}" + "M 100644 inline {path}\n" + "data {datasize}\n" + "{data}\n" + "\n" ).format( ref=branch_ref, - committer_name=committer_name, committer_email=committer_email, commit_time=commit_time, + committer_name=committer_name, + committer_email=committer_email, + commit_time=commit_time, basename=basename, fromline=fromline, path=path, - datasize=datasize, data=pointer_digest.decode('utf-8')) - fast_import.write(commit.encode('utf-8')) + datasize=datasize, + data=pointer_digest.decode("utf-8"), + ) + fast_import.write(commit.encode("utf-8")) def get_last_commit(): # show the full hash of the latest commit out = subprocess.run( - ['git', 'rev-parse', branch_ref], + ["git", "rev-parse", branch_ref], stdout=subprocess.PIPE, universal_newlines=True, ) @@ -106,15 +113,16 @@ def get_last_commit(): def main(): if len(sys.argv) != 3: - print('usage:', sys.argv[0], '<file>', '<relative path>') + print("usage:", sys.argv[0], "<file>", "<relative path>") sys.exit(1) raw_file = sys.argv[1] relpath = sys.argv[2] last_commit = get_last_commit() - with subprocess.Popen('git fast-import --quiet', shell=True, - stdin=subprocess.PIPE) as import_proc: + with subprocess.Popen( + "git fast-import --quiet", shell=True, stdin=subprocess.PIPE + ) as import_proc: if not last_commit: commit_lfs_gitattributes(import_proc.stdin) |