Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions activity/git-widget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head><title>Git Widget</title></head>
<body>
<h2>Commit to Git</h2>
<form id="commitForm">
<input type="file" name="file" required />
<input type="text" name="message" placeholder="Commit message" required />
<button type="submit">Commit</button>
</form>

<h2>History</h2>
<button onclick="fetchLog()">Refresh Log</button>
<ul id="logList"></ul>

<script>
document.getElementById('commitForm').addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(this);
const res = await fetch('http://localhost:8000/commit', { method: 'POST', body: formData });
const data = await res.json();
alert(data.message);
});

async function fetchLog() {
const res = await fetch('http://localhost:8000/log');
const data = await res.json();
const logList = document.getElementById('logList');
logList.innerHTML = '';
data.commits.forEach(c => {
const li = document.createElement('li');
li.textContent = `${c.date} - ${c.message}`;
logList.appendChild(li);
});
}
</script>
</body>
</html>
41 changes: 41 additions & 0 deletions git-backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
]
}
4 changes: 4 additions & 0 deletions git-backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fastapi
uvicorn
gitpython
python-multipart