By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 gitHTTP.py

View raw Download
text/x-script.python • 4.51 kiB
Python script, ASCII text executable
        
            
1
from gitme import app
2
3
import os
4
import config
5
import flask
6
import git
7
import subprocess
8
9
10
@app.route("/git/<username>/<repository>/<path:git_path>", methods=["GET", "POST"])
11
def git_http_backend(username, repository, git_path):
12
repo_path = os.path.join(config.REPOS_PATH, username, repository, ".git")
13
repo = git.Repo(repo_path)
14
git_cmd = repo.git
15
16
if flask.request.method == "GET":
17
result = git_cmd.upload_pack()
18
elif flask.request.method == "POST":
19
result = git_cmd.receive_pack()
20
21
response = flask.Response(result, content_type="application/x-git-" + git_cmd[-1])
22
return response
23
24
25
@app.route("/git/<username>/<repository>/info/refs")
26
def gitInfoRefs(username, repository):
27
serverRepoLocation = os.path.join(config.REPOS_PATH, os.path.join(username, repository))
28
29
app.logger.info(f"Loading {serverRepoLocation}")
30
31
if not os.path.exists(serverRepoLocation):
32
app.logger.error(f"Cannot load {serverRepoLocation}")
33
flask.abort(404)
34
35
repo = git.Repo(serverRepoLocation)
36
text = f"{repo.heads[0].commit.hexsha}\tHEAD\n"
37
for ref in repo.heads:
38
text += f"{ref.commit.hexsha}\trefs/heads/{ref.name}\n"
39
40
app.logger.warning(text)
41
42
return flask.Response(text, content_type="application/x-git-upload-pack-result")
43
44
45
@app.route("/git/<username>/<repository>/objects/info/alternates", methods=["GET"])
46
def gitInfoAlternates(username, repository):
47
# Handle the request and provide the response
48
text = "#Alternate object stores\n"
49
50
return flask.Response(text, content_type="text/plain")
51
52
53
@app.route("/git/<username>/<repository>/objects/info/http-alternates")
54
def gitHttpAlternates(username, repository):
55
# Return an empty response
56
return flask.Response("", content_type="text/plain")
57
58
59
@app.route("/git/<username>/<repository>/HEAD")
60
def gitHead(username, repository):
61
serverRepoLocation = os.path.join(config.REPOS_PATH, os.path.join(username, repository))
62
63
app.logger.info(f"Loading {serverRepoLocation}")
64
65
if not os.path.exists(serverRepoLocation):
66
app.logger.error(f"Cannot load {serverRepoLocation}")
67
flask.abort(404)
68
69
repo = git.Repo(serverRepoLocation)
70
71
text = f"ref: {repo.head.ref}\n"
72
73
return flask.Response(text, content_type="text/plain")
74
75
76
@app.route("/git/<username>/<repository>/objects/info/packs")
77
def gitInfoPacks(username, repository):
78
repo_path = os.path.join(config.REPOS_PATH, username, repository)
79
80
# List the pack files in the repository
81
packs = [f for f in os.listdir(os.path.join(repo_path, "objects", "pack")) if f.endswith(".pack")]
82
83
# Generate a response with the list of pack files
84
response_text = "\n".join(packs) + "\n"
85
86
return flask.Response(response_text, content_type="text/plain")
87
88
89
@app.route("/git/<username>/<repository>/objects/<path:object_path>")
90
def gitObject(username, repository, object_path):
91
repo_path = os.path.join(config.REPOS_PATH, username, repository)
92
object_full_path = os.path.join(repo_path, ".git/objects", object_path)
93
94
# Check if the object file exists
95
if not os.path.exists(object_full_path):
96
return flask.Response("Object not found", status=404, content_type="text/plain")
97
98
# Serve the object as binary data
99
with open(object_full_path, "rb") as object_file:
100
object_data = object_file.read()
101
102
return flask.Response(object_data, content_type="application/octet-stream")
103
104
105
@app.route("/git/<username>/<repository>/git-upload-pack", methods=["POST"])
106
def gitUploadPack(username, repository):
107
repoPath = "/path/to/repos/{}/{}/".format(username, repository)
108
cmd = ["git", "--git-dir", repoPath, "upload-pack"]
109
110
gitProcess = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
111
112
# Forward the Git process input and output
113
inputData = flask.request.get_data()
114
output = gitProcess.communicate(input=inputData)
115
return flask.Response(output[0], content_type="application/x-git-upload-pack-result")
116
117
118
@app.route("/git/<username>/<repository>/git-receive-pack", methods=["POST"])
119
def gitReceivePack(username, repository):
120
repoPath = "/path/to/repos/{}/{}/".format(username, repository)
121
cmd = ["git", "--git-dir", repoPath, "receive-pack"]
122
123
gitProcess = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
124
125
# Forward the Git process input and output
126
inputData = flask.request.get_data()
127
output = gitProcess.communicate(input=inputData)
128
return flask.Response(output[0], content_type="application/x-git-receive-pack-result")
129