gittest.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from git import Repo
  2. import os
  3. # 配置参数
  4. repo_url_https = "https://github.com/fulian23/Maxtool" # HTTPS克隆地址
  5. repo_url_ssh = "git@github.com:fulian23/Maxtool.git" # SSH推送地址
  6. local_path = r"C:\Users\32965\Maxtool" # 本地存储路径
  7. ssh_key_path = r"C:\Users\32965\.ssh\id_rsa" # SSH私钥路径
  8. # 1. 克隆仓库(使用HTTPS)
  9. # Repo.clone_from(repo_url_https, local_path)
  10. # # 2. 修改远程仓库地址为SSH格式
  11. # repo = Repo(local_path)
  12. # origin = repo.remotes.origin
  13. # origin.set_url(repo_url_ssh)
  14. #
  15. # # 3. 创建测试文件
  16. # test_file = os.path.join(local_path, "test.txt")
  17. # with open(test_file, "w") as f:
  18. # f.write("This is a test file added via GitPython")
  19. #
  20. # # 4. 提交更改
  21. # repo.git.add("test.txt")
  22. # repo.git.commit("-m", "Add test file")
  23. #
  24. # # 5. 配置SSH密钥并推送
  25. # ssh_cmd = f"ssh -i {ssh_key_path}"
  26. # with repo.git.custom_environment(GIT_SSH_COMMAND=ssh_cmd):
  27. # origin.push()
  28. # local_path = os.path.join('test', 't1')
  29. from git import Repo
  30. repo = Repo("../9992cddb-b7d1-99ec-1bd2-35fdc177e623/test")
  31. # branches = repo.remote().refs
  32. # for item in branches:
  33. # print(item.remote_head)
  34. # 1. 基础状态
  35. # 2. 本地与远程差异
  36. active_branch = repo.active_branch
  37. tracking_branch = active_branch.tracking_branch()
  38. ahead = sum(1 for _ in repo.iter_commits(f"{active_branch}..{tracking_branch}"))
  39. behind = sum(1 for _ in repo.iter_commits(f"{tracking_branch}..{active_branch}"))
  40. conflicts = repo.index.unmerged_blobs()
  41. conflicted = [path for path, entries in conflicts.items()]
  42. created_files = repo.untracked_files
  43. current = repo.active_branch.name
  44. head_commit = repo.head.commit
  45. tree = head_commit.tree
  46. all_files = [item.path for item in tree.traverse() if item.type == 'blob']
  47. diffs = repo.index.diff(None)
  48. deleted = [d.a_path for d in diffs if d.change_type == 'D']
  49. detached = repo.head.is_detached
  50. ignored_files = repo.git.execute(["git", "ls-files", "--others", "--ignored", "--exclude-standard"]).split("\n")
  51. modified_files = [d.a_path for d in diffs]
  52. untracked_files = repo.untracked_files
  53. staged_entries = repo.index.entries
  54. staged = [path[0] for path, _ in staged_entries.items()]
  55. tracking = active_branch.tracking_branch().name
  56. status = {"ahead": ahead, "behind": behind, "condlicted": conflicted, "created": created_files,
  57. "current": current,
  58. "deleted": deleted, "detached": detached, "files": all_files, "ignored": ignored_files,
  59. "modified": modified_files,
  60. "not_added": untracked_files, "staged": staged, "tracking": tracking}
  61. print(status)