|
@@ -1,4 +1,4 @@
|
|
-import os, json,hashlib
|
|
|
|
|
|
+import os, json,hashlib,re
|
|
from fastapi import APIRouter, BackgroundTasks
|
|
from fastapi import APIRouter, BackgroundTasks
|
|
from base_config import path, avatar_url
|
|
from base_config import path, avatar_url
|
|
|
|
|
|
@@ -32,6 +32,20 @@ def get_repo(uuid, repo_url):
|
|
return Repo(path)
|
|
return Repo(path)
|
|
|
|
|
|
|
|
|
|
|
|
+def git_stats_to_json(text):
|
|
|
|
+ pattern = r",?\s*(\d+)\s*files changed|,?\s*(\d+)\s*insertions\(\+\)|,?\s*(\d+)\s+deletions\(\-\)"
|
|
|
|
+ data = re.findall(pattern, text)
|
|
|
|
+ result = {}
|
|
|
|
+ for item in data:
|
|
|
|
+ if item[0]:
|
|
|
|
+ result["files_changed"] = int(item[0])
|
|
|
|
+ if item[1]:
|
|
|
|
+ result["insertions"] = int(item[1])
|
|
|
|
+ if item[2]:
|
|
|
|
+ result["deletions"] = int(item[2])
|
|
|
|
+
|
|
|
|
+ return result
|
|
|
|
+
|
|
gitrouter = APIRouter()
|
|
gitrouter = APIRouter()
|
|
|
|
|
|
|
|
|
|
@@ -50,22 +64,23 @@ async def clone(request: RequestBody, background_tasks: BackgroundTasks):
|
|
|
|
|
|
@gitrouter.post("/log")
|
|
@gitrouter.post("/log")
|
|
async def log(request: RequestBody):
|
|
async def log(request: RequestBody):
|
|
- user=await Users.get(uuid=request.uuid)
|
|
|
|
- email = user.email
|
|
|
|
- # email = "gshn666@qq.com"
|
|
|
|
- email_md5 = hashlib.md5(email.encode(encoding='UTF-8')).hexdigest()
|
|
|
|
- avatar = avatar_url+email_md5+"?d=identicon"
|
|
|
|
local_path, _ = generate_repo_path(request.uuid, request.repo_url)
|
|
local_path, _ = generate_repo_path(request.uuid, request.repo_url)
|
|
repo = get_repo(request.uuid, request.repo_url)
|
|
repo = get_repo(request.uuid, request.repo_url)
|
|
if not repo:
|
|
if not repo:
|
|
return {"status": "404", "msg": "仓库不存在", "uuid": request.uuid, "repo_url": request.repo_url,
|
|
return {"status": "404", "msg": "仓库不存在", "uuid": request.uuid, "repo_url": request.repo_url,
|
|
- "local_path": local_path,"email":email,"avatar":avatar}
|
|
|
|
|
|
+ "local_path": local_path}
|
|
|
|
|
|
- log_ = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
|
|
|
|
|
|
+ log_ = repo.git.log('--pretty={"commit":"%h","author":"%an","email":"%ce","summary":"%s","date":"%cd"}', max_count=50,
|
|
date='format:%Y-%m-%d %H:%M').split("\n")
|
|
date='format:%Y-%m-%d %H:%M').split("\n")
|
|
log = list(map(json.loads, log_))
|
|
log = list(map(json.loads, log_))
|
|
|
|
+ for i in log:
|
|
|
|
+ email = i["email"]
|
|
|
|
+ email_md5 = hashlib.md5(email.encode(encoding='UTF-8')).hexdigest()
|
|
|
|
+ i["avatar"] = avatar_url+email_md5+"?d=identicon"
|
|
|
|
+ status=repo.git.execute(["git", "show",i["commit"] , "--shortstat"]).split("\n")[-1]
|
|
|
|
+ i["change"]=git_stats_to_json(status)
|
|
response = {"status": "200", "msg": "成功获取日志", "uuid": request.uuid, "repo_url": request.repo_url,
|
|
response = {"status": "200", "msg": "成功获取日志", "uuid": request.uuid, "repo_url": request.repo_url,
|
|
- "local_path": local_path, "git_log": log,"email":email,"avatar":avatar}
|
|
|
|
|
|
+ "local_path": local_path, "git_log": log}
|
|
return response
|
|
return response
|
|
|
|
|
|
|
|
|
|
@@ -112,7 +127,8 @@ async def change(request: CommitHash):
|
|
|
|
|
|
parent = commit.parents[0]
|
|
parent = commit.parents[0]
|
|
|
|
|
|
- diffs = parent.diff(commit,create_patch=True, no_renames=True)
|
|
|
|
|
|
+ diffs = commit.diff(commit,create_patch=True, no_renames=True)
|
|
|
|
+ print(diffs)
|
|
|
|
|
|
for diff in diffs:
|
|
for diff in diffs:
|
|
print(f"文件 {diff.a_path} ({diff.change_type}):")
|
|
print(f"文件 {diff.a_path} ({diff.change_type}):")
|