|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +from http.server import HTTPServer, BaseHTTPRequestHandler |
| 5 | + |
| 6 | +PORT = 8080 |
| 7 | +PROJECTS_ROOT = os.getenv("GIT_PROJECT_ROOT", "/git-repos") |
| 8 | + |
| 9 | +# ignore permissions |
| 10 | +subprocess.run(['git', 'config', '--global', '--add', 'safe.directory', '*']) |
| 11 | +subprocess.run(['git', 'config', '--system', '--add', 'safe.directory', '*']) |
| 12 | + |
| 13 | +backend = subprocess.check_output(['git', '--exec-path']).decode().strip() |
| 14 | +backend_path = os.path.join(backend, 'git-http-backend') |
| 15 | + |
| 16 | +class GitRequestHandler(BaseHTTPRequestHandler): |
| 17 | + def do_GET(self): |
| 18 | + if self.path == "/" or self.path.endswith("/"): |
| 19 | + self.list_repos() |
| 20 | + else: |
| 21 | + self.handle_git() |
| 22 | + |
| 23 | + def list_repos(self): |
| 24 | + self.send_response(200) |
| 25 | + self.send_header("Content-type", "text/html") |
| 26 | + self.end_headers() |
| 27 | + |
| 28 | + repos = [d for d in os.listdir(PROJECTS_ROOT) if os.path.isdir(os.path.join(PROJECTS_ROOT, d))] |
| 29 | + |
| 30 | + html = f"<html><body><h1>Available Repositories</h1><ul>" |
| 31 | + for r in repos: |
| 32 | + html += f"<li><a href='/{r}/info/refs?service=git-upload-pack'>{r}</a></li>" |
| 33 | + html += "</ul><p>Clone via: <code>git clone http://localhost:8080/YOUR_REPO_NAME</code></p></body></html>" |
| 34 | + |
| 35 | + self.wfile.write(html.encode()) |
| 36 | + |
| 37 | + def do_POST(self): |
| 38 | + self.handle_git() |
| 39 | + |
| 40 | + def handle_git(self): |
| 41 | + env = { |
| 42 | + 'REQUEST_METHOD': self.command, |
| 43 | + 'GIT_PROJECT_ROOT': PROJECTS_ROOT, |
| 44 | + 'GIT_HTTP_EXPORT_ALL': '1', |
| 45 | + 'PATH_INFO': self.path.split('?')[0], |
| 46 | + 'QUERY_STRING': self.path.split('?')[1] if '?' in self.path else '', |
| 47 | + 'CONTENT_TYPE': self.headers.get('Content-Type', ''), |
| 48 | + 'REMOTE_ADDR': self.client_address[0], |
| 49 | + } |
| 50 | + |
| 51 | + content_length = int(self.headers.get('Content-Length', 0)) |
| 52 | + input_data = self.rfile.read(content_length) if content_length > 0 else None |
| 53 | + |
| 54 | + proc = subprocess.Popen( |
| 55 | + [backend_path], |
| 56 | + env=env, |
| 57 | + stdin=subprocess.PIPE, |
| 58 | + stdout=subprocess.PIPE, |
| 59 | + stderr=subprocess.PIPE |
| 60 | + ) |
| 61 | + |
| 62 | + stdout_data, stderr_data = proc.communicate(input=input_data) |
| 63 | + |
| 64 | + header_part, _, body = stdout_data.partition(b'\r\n\r\n') |
| 65 | + |
| 66 | + self.send_response(200) |
| 67 | + for line in header_part.split(b'\r\n'): |
| 68 | + if b':' in line: |
| 69 | + key, val = line.split(b':', 1) |
| 70 | + self.send_header(key.decode().strip(), val.decode().strip()) |
| 71 | + self.end_headers() |
| 72 | + self.wfile.write(body) |
| 73 | + |
| 74 | + if stderr_data: |
| 75 | + print(f"Git Error: {stderr_data.decode()}", file=sys.stderr) |
| 76 | + |
| 77 | +print(f"") |
| 78 | +print(f"--- Filesystem git HTTP bridge ---") |
| 79 | +print(f"Backend: {backend_path}") |
| 80 | +print(f"Serving from: {PROJECTS_ROOT}") |
| 81 | +print(f"URL: http://localhost:{PORT}/<your-repo-name>") |
| 82 | +print(f"----------------------------------") |
| 83 | +print(f"") |
| 84 | + |
| 85 | +HTTPServer(('0.0.0.0', PORT), GitRequestHandler).serve_forever() |
0 commit comments