gitRouter.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import asyncio
  2. import os, json,hashlib,re,shutil,time
  3. from fastapi import APIRouter, BackgroundTasks
  4. from base_config import path, avatar_url
  5. from git import Repo, GitCommandError
  6. from pydantic import BaseModel
  7. from models.gitModels import Repos
  8. class RequestBody(BaseModel):
  9. uuid: str
  10. repo_url: str
  11. class CommitHash(BaseModel):
  12. uuid: str
  13. repo_url: str
  14. commit_hash: str
  15. def generate_repo_path(uuid, repo_url):
  16. repo_name = repo_url.split("/")[-1].replace(".git", "")
  17. base_path = os.path.join(path, uuid)
  18. return os.path.join(base_path, repo_name), repo_name
  19. def get_repo(uuid, repo_url):
  20. path, _ = generate_repo_path(uuid, repo_url)
  21. if not os.path.exists(path):
  22. return 0
  23. return Repo(path)
  24. def git_stats_to_json(text):
  25. pattern = r",?\s*(\d+)\s*files changed|,?\s*(\d+)\s*insertions\(\+\)|,?\s*(\d+)\s+deletions\(\-\)"
  26. data = re.findall(pattern, text)
  27. result = {}
  28. for item in data:
  29. if item[0]:
  30. result["files_changed"] = int(item[0])
  31. if item[1]:
  32. result["insertions"] = int(item[1])
  33. if item[2]:
  34. result["deletions"] = int(item[2])
  35. return result
  36. gitrouter = APIRouter()
  37. async def clone_task(repo_url, local_path,uuid,repo_name):
  38. current_time = int(time.time())
  39. print(f"开始克隆仓库: {repo_url}")
  40. try:
  41. loop = asyncio.get_event_loop()
  42. await loop.run_in_executor(None, Repo.clone_from, repo_url, local_path)
  43. await Repos.filter(create_user=uuid,name=repo_name).update(path=local_path, state=1, update_time=current_time)
  44. except:
  45. await Repos.filter(create_user=uuid,name=repo_name).update(path=local_path, state=0, update_time=current_time)
  46. shutil.rmtree(local_path)
  47. @gitrouter.post("/clone")
  48. async def clone(request: RequestBody, background_tasks: BackgroundTasks):
  49. local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
  50. if os.path.exists(local_path):
  51. return {"status": "400", "msg": "仓库已存在", "uuid": request.uuid, "repo_url": request.repo_url,
  52. "path": local_path}
  53. else:
  54. background_tasks.add_task(clone_task, request.repo_url, local_path, request.uuid, repo_name)
  55. response = {"status": "200", "msg": "成功创建克隆任务", "uuid": request.uuid, "repo_name": repo_name,
  56. "local_path": local_path}
  57. return response
  58. @gitrouter.post("/log")
  59. async def log(request: RequestBody):
  60. local_path, _ = generate_repo_path(request.uuid, request.repo_url)
  61. repo = get_repo(request.uuid, request.repo_url)
  62. if not repo:
  63. return {
  64. "status": "404",
  65. "msg": "仓库不存在",
  66. "uuid": request.uuid,
  67. "repo_url": request.repo_url,
  68. "local_path": local_path
  69. }
  70. # 使用git log --numstat一次性获取所有必要信息
  71. git_log_format = '--pretty=format:%h|%an|%ce|%s|%cd'
  72. try:
  73. log_output = repo.git.log(
  74. git_log_format,
  75. '--numstat',
  76. '--no-renames',
  77. date='format:%Y-%m-%d %H:%M'
  78. )
  79. except GitCommandError as e:
  80. return {"status": "500", "msg": f"获取日志失败: {str(e)}"}
  81. log = []
  82. current_commit = None
  83. for line in log_output.split('\n'):
  84. if not line.strip():
  85. continue # 跳过空行
  86. if '\t' in line and len(line.split('\t')) == 3:
  87. # 处理numstat行,例如 "10\t5\tfile.txt"
  88. if current_commit is None:
  89. continue # 防止数据错误
  90. insertions_str, deletions_str, _ = line.split('\t')
  91. try:
  92. insertions = int(insertions_str) if insertions_str != '-' else 0
  93. deletions = int(deletions_str) if deletions_str != '-' else 0
  94. except ValueError:
  95. insertions, deletions = 0, 0
  96. current_commit['change']['insertions'] += insertions
  97. current_commit['change']['deletions'] += deletions
  98. current_commit['change']['files'] += 1
  99. else:
  100. # 处理提交信息行,例如 "abc123|Author|email|summary|2023-10-01 12:34"
  101. if current_commit is not None:
  102. # 生成avatar的md5
  103. email = current_commit['email']
  104. email_md5 = hashlib.md5(email.encode('utf-8')).hexdigest()
  105. current_commit['avatar'] = f"{avatar_url}{email_md5}?d=identicon"
  106. log.append(current_commit)
  107. try:
  108. commit_hash, author, email, summary, date = line.split('|', 4)
  109. current_commit = {
  110. "commit": commit_hash,
  111. "author": author,
  112. "email": email,
  113. "summary": summary,
  114. "date": date,
  115. "avatar": "",
  116. "change": {
  117. "insertions": 0,
  118. "deletions": 0,
  119. "files": 0
  120. }
  121. }
  122. except ValueError:
  123. current_commit = None # 忽略格式错误行
  124. # 添加最后一个提交
  125. if current_commit is not None:
  126. email = current_commit['email']
  127. email_md5 = hashlib.md5(email.encode('utf-8')).hexdigest()
  128. current_commit['avatar'] = f"{avatar_url}{email_md5}?d=identicon"
  129. log.append(current_commit)
  130. # 按时间倒序排列(git log默认最新在前)
  131. return {
  132. "status": "200",
  133. "msg": "成功获取日志",
  134. "uuid": request.uuid,
  135. "repo_url": request.repo_url,
  136. "local_path": local_path,
  137. "git_log": log
  138. }
  139. @gitrouter.post("/status")
  140. async def status(request: RequestBody):
  141. repo = get_repo(request.uuid, request.repo_url)
  142. # 手动获取所有数据
  143. active_branch = repo.active_branch
  144. tracking_branch = active_branch.tracking_branch()
  145. ahead = sum(1 for _ in repo.iter_commits(f"{active_branch}..{tracking_branch}"))
  146. behind = sum(1 for _ in repo.iter_commits(f"{tracking_branch}..{active_branch}"))
  147. conflicts = repo.index.unmerged_blobs()
  148. conflicted = [path for path, entries in conflicts.items()]
  149. created_files = repo.untracked_files
  150. current = repo.active_branch.name
  151. head_commit = repo.head.commit
  152. tree = head_commit.tree
  153. all_files = [item.path for item in tree.traverse() if item.type == 'blob']
  154. diffs = repo.index.diff(None)
  155. deleted = [d.a_path for d in diffs if d.change_type == 'D']
  156. detached = repo.head.is_detached
  157. ignored_files = repo.git.execute(["git", "ls-files", "--others", "--ignored", "--exclude-standard"]).split("\n")
  158. modified_files = [d.a_path for d in diffs]
  159. untracked_files = repo.untracked_files
  160. staged_entries = repo.index.entries
  161. staged = [path[0] for path, _ in staged_entries.items()]
  162. tracking = active_branch.tracking_branch().name
  163. status = {"ahead": ahead, "behind": behind, "conflicted": conflicted, "created": created_files,
  164. "current": current, "deleted": deleted, "detached": detached, "files": all_files,
  165. "ignored": ignored_files,
  166. "modified": modified_files, "not_added": untracked_files, "staged": staged, "tracking": tracking}
  167. return status
  168. @gitrouter.post("/change")
  169. async def change(request: CommitHash):
  170. repo = get_repo(request.uuid, request.repo_url)
  171. if not repo:
  172. return {"status": "404", "msg": "仓库不存在", "uuid": request.uuid, "repo_url": request.repo_url}
  173. commit = repo.commit(request.commit_hash)
  174. if not commit.parents:
  175. print("首次提交,无父提交对比")
  176. return
  177. parent = commit.parents[0]
  178. diffs = commit.diff(commit,create_patch=True, no_renames=True)
  179. print(diffs)
  180. for diff in diffs:
  181. print(f"文件 {diff.a_path} ({diff.change_type}):")
  182. print(diff.diff.decode('utf-8'))