gitRouter.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import os, json,hashlib
  2. from fastapi import APIRouter, BackgroundTasks
  3. from base_config import path, avatar_url
  4. from git import Repo
  5. from pydantic import BaseModel
  6. from models.gitModels import Users
  7. class RequestBody(BaseModel):
  8. uuid: str
  9. repo_url: str
  10. class CommitHash(BaseModel):
  11. uuid: str
  12. repo_url: str
  13. commit_hash: str
  14. def generate_repo_path(uuid, repo_url):
  15. repo_name = repo_url.split("/")[-1].replace(".git", "")
  16. base_path = os.path.join(path, uuid)
  17. return os.path.join(base_path, repo_name), repo_name
  18. def get_repo(uuid, repo_url):
  19. path, _ = generate_repo_path(uuid, repo_url)
  20. if not os.path.exists(path):
  21. return 0
  22. return Repo(path)
  23. gitrouter = APIRouter()
  24. @gitrouter.post("/clone")
  25. async def clone(request: RequestBody, background_tasks: BackgroundTasks):
  26. local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
  27. if os.path.exists(local_path):
  28. return {"status": "400", "msg": "仓库已存在", "uuid": request.uuid, "repo_url": request.repo_url,
  29. "path": local_path}
  30. else:
  31. background_tasks.add_task(Repo.clone_from, request.repo_url, local_path)
  32. response = {"status": "200", "msg": "成功创建克隆任务", "uuid": request.uuid, "repo_name": repo_name,
  33. "local_path": local_path}
  34. return response
  35. @gitrouter.post("/log")
  36. async def log(request: RequestBody):
  37. user=await Users.get(uuid=request.uuid)
  38. email = user.email
  39. # email = "gshn666@qq.com"
  40. email_md5 = hashlib.md5(email.encode(encoding='UTF-8')).hexdigest()
  41. avatar = avatar_url+email_md5+"?d=identicon"
  42. local_path, _ = generate_repo_path(request.uuid, request.repo_url)
  43. repo = get_repo(request.uuid, request.repo_url)
  44. if not repo:
  45. return {"status": "404", "msg": "仓库不存在", "uuid": request.uuid, "repo_url": request.repo_url,
  46. "local_path": local_path,"email":email,"avatar":avatar}
  47. log_ = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
  48. date='format:%Y-%m-%d %H:%M').split("\n")
  49. log = list(map(json.loads, log_))
  50. response = {"status": "200", "msg": "成功获取日志", "uuid": request.uuid, "repo_url": request.repo_url,
  51. "local_path": local_path, "git_log": log,"email":email,"avatar":avatar}
  52. return response
  53. @gitrouter.post("/status")
  54. async def status(request: RequestBody):
  55. repo = get_repo(request.uuid, request.repo_url)
  56. # 手动获取所有数据
  57. active_branch = repo.active_branch
  58. tracking_branch = active_branch.tracking_branch()
  59. ahead = sum(1 for _ in repo.iter_commits(f"{active_branch}..{tracking_branch}"))
  60. behind = sum(1 for _ in repo.iter_commits(f"{tracking_branch}..{active_branch}"))
  61. conflicts = repo.index.unmerged_blobs()
  62. conflicted = [path for path, entries in conflicts.items()]
  63. created_files = repo.untracked_files
  64. current = repo.active_branch.name
  65. head_commit = repo.head.commit
  66. tree = head_commit.tree
  67. all_files = [item.path for item in tree.traverse() if item.type == 'blob']
  68. diffs = repo.index.diff(None)
  69. deleted = [d.a_path for d in diffs if d.change_type == 'D']
  70. detached = repo.head.is_detached
  71. ignored_files = repo.git.execute(["git", "ls-files", "--others", "--ignored", "--exclude-standard"]).split("\n")
  72. modified_files = [d.a_path for d in diffs]
  73. untracked_files = repo.untracked_files
  74. staged_entries = repo.index.entries
  75. staged = [path[0] for path, _ in staged_entries.items()]
  76. tracking = active_branch.tracking_branch().name
  77. status = {"ahead": ahead, "behind": behind, "conflicted": conflicted, "created": created_files,
  78. "current": current, "deleted": deleted, "detached": detached, "files": all_files,
  79. "ignored": ignored_files,
  80. "modified": modified_files, "not_added": untracked_files, "staged": staged, "tracking": tracking}
  81. return status
  82. @gitrouter.post("/change")
  83. async def change(request: CommitHash):
  84. repo = get_repo(request.uuid, request.repo_url)
  85. if not repo:
  86. return {"status": "404", "msg": "仓库不存在", "uuid": request.uuid, "repo_url": request.repo_url}
  87. commit = repo.commit(request.commit_hash)
  88. if not commit.parents:
  89. print("首次提交,无父提交对比")
  90. return
  91. parent = commit.parents[0]
  92. diffs = parent.diff(commit,create_patch=True, no_renames=True)
  93. for diff in diffs:
  94. print(f"文件 {diff.a_path} ({diff.change_type}):")
  95. print(diff.diff.decode('utf-8'))