gitRouter.py 7.9 KB

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