forked from open-webui/openapi-servers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
276 lines (206 loc) · 7.23 KB
/
main.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import logging
from pathlib import Path
from typing import List, Optional
from enum import Enum
import git
from pydantic import BaseModel, Field
app = FastAPI(
title="Git Management API",
version="0.1.0",
description="An API to manage Git repositories with explicit endpoints, inputs, and outputs for better OpenAPI schemas.",
)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ----------------- ENUMS -----------------
class GitTools(str, Enum):
STATUS = "status"
DIFF_UNSTAGED = "diff_unstaged"
DIFF_STAGED = "diff_staged"
DIFF = "diff"
COMMIT = "commit"
ADD = "add"
RESET = "reset"
LOG = "log"
CREATE_BRANCH = "create_branch"
CHECKOUT = "checkout"
SHOW = "show"
INIT = "init"
# ----------------- MODELS -----------------
class GitRepoPath(BaseModel):
repo_path: str = Field(..., description="File system path to the Git repository.")
class GitStatusRequest(GitRepoPath):
pass
class GitDiffUnstagedRequest(GitRepoPath):
pass
class GitDiffStagedRequest(GitRepoPath):
pass
class GitDiffRequest(GitRepoPath):
target: str = Field(..., description="The branch or commit to diff against.")
class GitCommitRequest(GitRepoPath):
message: str = Field(..., description="Commit message for recording the change.")
class GitAddRequest(GitRepoPath):
files: List[str] = Field(
..., description="List of file paths to add to the staging area."
)
class GitResetRequest(GitRepoPath):
pass
class GitLogRequest(GitRepoPath):
max_count: int = Field(10, description="Maximum number of commits to retrieve.")
class GitCreateBranchRequest(GitRepoPath):
branch_name: str = Field(..., description="Name of the branch to create.")
base_branch: Optional[str] = Field(
None, description="Optional base branch name to create the new branch from."
)
class GitCheckoutRequest(GitRepoPath):
branch_name: str = Field(..., description="Branch name to checkout.")
class GitShowRequest(GitRepoPath):
revision: str = Field(
..., description="The commit hash or branch/tag name to show."
)
class GitInitRequest(GitRepoPath):
pass
class TextResponse(BaseModel):
result: str = Field(..., description="Description of the operation result.")
class LogResponse(BaseModel):
commits: List[str] = Field(
..., description="A list of formatted commit log entries."
)
# ----------------- UTILITY FUNCTIONS -----------------
def get_repo(repo_path: str) -> git.Repo:
try:
return git.Repo(repo_path)
except git.InvalidGitRepositoryError:
raise HTTPException(
status_code=400, detail=f"Invalid Git repository at '{repo_path}'"
)
# ----------------- API ENDPOINTS -----------------
@app.post(
"/status",
response_model=TextResponse,
description="Get the current status of the Git repository.",
)
def get_status(request: GitStatusRequest):
repo = get_repo(request.repo_path)
status = repo.git.status()
return TextResponse(result=status)
@app.post(
"/diff_unstaged",
response_model=TextResponse,
description="Get differences of unstaged changes.",
)
def diff_unstaged(request: GitDiffUnstagedRequest):
repo = get_repo(request.repo_path)
diff = repo.git.diff()
return TextResponse(result=diff)
@app.post(
"/diff_staged",
response_model=TextResponse,
description="Get differences of staged changes.",
)
def diff_staged(request: GitDiffStagedRequest):
repo = get_repo(request.repo_path)
diff = repo.git.diff("--cached")
return TextResponse(result=diff)
@app.post(
"/diff",
response_model=TextResponse,
description="Get comparison between two branches or commits.",
)
def diff_target(request: GitDiffRequest):
repo = get_repo(request.repo_path)
diff = repo.git.diff(request.target)
return TextResponse(result=diff)
@app.post(
"/commit",
response_model=TextResponse,
description="Commit staged changes to the repository.",
)
def commit_changes(request: GitCommitRequest):
repo = get_repo(request.repo_path)
commit = repo.index.commit(request.message)
return TextResponse(result=f"Committed changes with hash {commit.hexsha}")
@app.post("/add", response_model=TextResponse, description="Stage files for commit.")
def add_files(request: GitAddRequest):
repo = get_repo(request.repo_path)
repo.index.add(request.files)
return TextResponse(result="Files staged successfully.")
@app.post(
"/reset", response_model=TextResponse, description="Unstage all staged changes."
)
def reset_changes(request: GitResetRequest):
repo = get_repo(request.repo_path)
repo.index.reset()
return TextResponse(result="All staged changes reset.")
@app.post(
"/log",
response_model=LogResponse,
description="Get recent commit history of the repository.",
)
def get_log(request: GitLogRequest):
repo = get_repo(request.repo_path)
commits = [
f"Commit: {commit.hexsha}\n"
f"Author: {commit.author}\n"
f"Date: {commit.authored_datetime}\n"
f"Message: {commit.message.strip()}\n"
for commit in repo.iter_commits(max_count=request.max_count)
]
return LogResponse(commits=commits)
@app.post(
"/create_branch", response_model=TextResponse, description="Create a new branch."
)
def create_branch(request: GitCreateBranchRequest):
repo = get_repo(request.repo_path)
if request.base_branch is None:
base_branch = repo.active_branch
else:
base_branch = repo.refs[request.base_branch]
repo.create_head(request.branch_name, base_branch)
return TextResponse(
result=f"Created branch '{request.branch_name}' from '{base_branch}'."
)
@app.post(
"/checkout", response_model=TextResponse, description="Checkout an existing branch."
)
def checkout_branch(request: GitCheckoutRequest):
repo = get_repo(request.repo_path)
repo.git.checkout(request.branch_name)
return TextResponse(result=f"Switched to branch '{request.branch_name}'.")
@app.post(
"/show",
response_model=TextResponse,
description="Show details and diff of a specific commit.",
)
def show_revision(request: GitShowRequest):
repo = get_repo(request.repo_path)
commit = repo.commit(request.revision)
details = (
f"Commit: {commit.hexsha}\n"
f"Author: {commit.author}\n"
f"Date: {commit.authored_datetime}\n"
f"Message: {commit.message.strip()}\n"
)
diff = commit.diff(
commit.parents[0] if commit.parents else git.NULL_TREE, create_patch=True
)
diff_text = "\n".join(d.diff.decode("utf-8") for d in diff)
return TextResponse(result=details + "\n" + diff_text)
@app.post(
"/init", response_model=TextResponse, description="Initialize a new Git repository."
)
def init_repo(request: GitInitRequest):
try:
repo = git.Repo.init(path=request.repo_path, mkdir=True)
return TextResponse(
result=f"Initialized empty Git repository at '{repo.git_dir}'"
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))