forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (37 loc) · 1.13 KB
/
main.py
File metadata and controls
41 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import FastAPI, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
from git import Repo
from pathlib import Path
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
REPO_DIR = Path("repo")
@app.post("/commit")
async def commit_file(file: UploadFile, message: str = Form(...)):
REPO_DIR.mkdir(exist_ok=True)
file_path = REPO_DIR / file.filename
with open(file_path, "wb") as f:
f.write(await file.read())
repo = Repo.init(REPO_DIR) if not (REPO_DIR / ".git").exists() else Repo(REPO_DIR)
repo.index.add([str(file_path)])
repo.index.commit(message)
return {"status": "success", "message": f"Committed: {file.filename}"}
@app.get("/log")
def git_log():
repo = Repo(REPO_DIR)
return {
"commits": [
{
"hash": c.hexsha,
"author": c.author.name,
"message": c.message.strip(),
"date": c.committed_datetime.isoformat(),
}
for c in repo.iter_commits()
]
}