Browse Source

增加log内容

fulian23 4 months ago
parent
commit
9b4d82d298
4 changed files with 64 additions and 28 deletions
  1. 6 0
      .idea/CopilotChatHistory.xml
  2. 26 10
      api/gitRouter.py
  3. 2 2
      demo.py
  4. 30 16
      gittest.py

+ 6 - 0
.idea/CopilotChatHistory.xml

@@ -3,6 +3,12 @@
   <component name="CopilotChatHistory">
     <option name="conversations">
       <list>
+        <Conversation>
+          <option name="createTime" value="1742724021680" />
+          <option name="id" value="0195c2723db07a689c3ba36c6545be58" />
+          <option name="title" value="新对话 2025年3月23日 18:00:21" />
+          <option name="updateTime" value="1742724021680" />
+        </Conversation>
         <Conversation>
           <option name="createTime" value="1742705963507" />
           <option name="id" value="0195c15eb1f3711f9472b5b245e8f1c8" />

+ 26 - 10
api/gitRouter.py

@@ -1,4 +1,4 @@
-import os, json,hashlib
+import os, json,hashlib,re
 from fastapi import APIRouter, BackgroundTasks
 from base_config import path, avatar_url
 
@@ -32,6 +32,20 @@ def get_repo(uuid, repo_url):
     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()
 
 
@@ -50,22 +64,23 @@ async def clone(request: RequestBody, background_tasks: BackgroundTasks):
 
 @gitrouter.post("/log")
 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)
     repo = get_repo(request.uuid, request.repo_url)
     if not repo:
         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")
     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,
-                "local_path": local_path, "git_log": log,"email":email,"avatar":avatar}
+                "local_path": local_path, "git_log": log}
     return response
 
 
@@ -112,7 +127,8 @@ async def change(request: CommitHash):
 
     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:
         print(f"文件 {diff.a_path} ({diff.change_type}):")

+ 2 - 2
demo.py

@@ -13,8 +13,8 @@ monkey_patch_for_docs_ui(app)
 register_tortoise(app=app, config=TORTOISE_ORM)
 
 @app.get("/user/{id}")
-async def test(id: str):
-    user= await Users.get(uuid="9992cddb-b7d1-99ec-1bd2-35fdc177e623")
+async def test(id: int):
+    user= await Users.get(id=id)
     print(type(user))
     return user
 app.include_router(gitrouter,prefix="/git")

+ 30 - 16
gittest.py

@@ -1,24 +1,38 @@
-from git import Repo
+import re
+import json
 
 
-def debug_commit_changes(repo_path, commit_hash, target_files=None):
-    repo = Repo(repo_path)
-    commit = repo.commit(commit_hash)
+def git_stats_to_json(text):
+    # 正则表达式匹配任意顺序的统计项
+    pattern = r",?\s*(\d+)\s*files changed|,?\s*(\d+)\s*insertions\(\+\)|,?\s*(\d+)\s+deletions\(\-\)"
 
-    if not commit.parents:
-        print("Initial commit, no parent to compare")
-        return
+    # 动态提取并合并结果
+    data = re.findall(pattern, text)
 
-    parent = commit.parents[0]
-    # 强制生成文本差异,忽略重命名
-    diffs = parent.diff(commit, create_patch=True, no_renames=True)
+    # print(data)
+    result = {}
+    for item in data:
+        if item[0]:  # 处理files_changed字段
+            result["files_changed"] = int(item[0])
+        if item[1]:  # 处理insertions字段
+            result["insertions"] = int(item[1])
+        if item[2]:  # 处理deletions字段
+            result["deletions"] = int(item[2])
+    # json_output = json.dumps(result, indent=4)
+    # print(json_output)
 
-    for diff in diffs:
-        path = diff.a_path if diff.a_path else diff.b_path
-        print(f"文件: {path} ({diff.change_type})")
 
-        print(f"代码变更: {diff.diff.decode('utf-8')}")
+    # 生成格式化JSON
+    return json.dumps(result, indent=4, ensure_ascii=False)
 
 
-# 调用示例
-debug_commit_changes(r"C:\Users\32965\repo\9992cddb-b7d1-99ec-1bd2-35fdc177e623\test", "a0126f5620377c4894394c918786c6e98d218b6c", target_files=["README.md"])
+# 测试不同顺序的输入
+test_cases = [
+    " 96 deletions(-), 2539 insertions(+), 11 files changed",
+    " 11 files changed, 96 deletions(-)",
+    " 2539 insertions(+), 96 deletions(-), 11 files changed"
+
+]
+
+for text in test_cases:
+    print(git_stats_to_json(text))