From fc8b95365576aee844f79156aade87aa441607da Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 24 Sep 2024 21:01:00 +0000 Subject: [PATCH] curl-based git-lfs pull --- data/gitea-lfs-pull.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 data/gitea-lfs-pull.py diff --git a/data/gitea-lfs-pull.py b/data/gitea-lfs-pull.py new file mode 100755 index 0000000..12f6a52 --- /dev/null +++ b/data/gitea-lfs-pull.py @@ -0,0 +1,51 @@ +#!/bin/env python3 +# curl based lfs link fetcher, for systems that don't have git-lfs easily available (haiku, for example) + +from subprocess import getoutput +from urllib.parse import urlsplit +from getpass import getpass +from os import walk +import random, string, json, mimetypes + +def randomword(length): + letters = string.ascii_lowercase + return ''.join(random.choice(letters) for i in range(length)) + + +if __name__ == "__main__": + origin = getoutput(["git remote get-url origin"]) + print("Origin:", origin) + parts = urlsplit(origin) + location = parts.netloc + + name = input("Username: ") + passw = getpass("Password: ") + + query_data = '{"name": "%s"}' % (randomword(6),) + query = f"curl -s -H 'Content-Type: application/json' -d '{query_data}' -u '{name}:{passw}' https://{location}/api/v1/users/{name}/tokens" + token_response = getoutput(query) + token = json.loads(token_response)["sha1"] + + for root, dirs, files in walk("."): + for file in files: + path = root + '/' + file + with open(path, 'r') as f: + file_result = getoutput(f"file -b {path}") + if file_result != "ASCII text": + continue + header = f.readline() + if header != "version https://git-lfs.github.com/spec/v1\n": + continue + sha = f.readline().split(':')[1].strip() + size = f.readline().split(' ')[1].strip() + + query_data = '{"operation": "download", "transfer": ["basic"], "objects": [{"oid": "%s", "size": %s}]}' % (sha, size) + query = f"curl -s -X POST -H 'Authorization: token {token}' -H 'Accept: application/vnd.git-lfs+json' -H 'Content-type: application/json' -d '{query_data}' {origin}.git/info/lfs/objects/batch" + response = getoutput(query) + + href = json.loads(response)["objects"][0]["actions"]["download"]["href"] + query = f"curl -s -X GET -H 'Authorization: token {token}' {href} > {path}" + response = getoutput(query) + + print("Processed", path) +