Skip to content

Instantly share code, notes, and snippets.

@jesopo
Last active August 4, 2024 13:45
Show Gist options
  • Save jesopo/298ce60eaa4678902ce5294805fc77c1 to your computer and use it in GitHub Desktop.
Save jesopo/298ce60eaa4678902ce5294805fc77c1 to your computer and use it in GitHub Desktop.
Testing raw git HTTP protocol with python3 and Requests
#!/usr/bin/env python3
# $ chmod +x git-clone.py
# $ ./git-clone.py https://github.com/jesopo/bitbot.git
import sys
import requests
repo = sys.argv[1]
refs = requests.get(repo + "/info/refs?service=git-upload-pack").text
refs_lines = refs.split("\n")
shas = []
for line in refs_lines[2:]:
if line == "0000":
break
else:
shas.append(line[4:].split(" ", 1)[0])
print("found %d refs" % len(shas))
req_lines = [
"want %s multi_ack side-band-64k ofs-delta" % shas[0]]
for sha in shas[1:]:
req_lines.append("want %s" % sha)
for i, line in enumerate(req_lines):
header = "{:x}".format(len(line)+4+1).zfill(4)
req_lines[i] = "%s%s" % (header, line)
req_lines.append("00000009done")
req_lines.append("")
req = "\n".join(req_lines)
out = requests.post(repo+"/git-upload-pack", data=req,
headers={"Content-Type": "application/x-git-upload-pack-request"}).content
print("got pack (%d bytes)" % len(out))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment