gittest.py 776 B

123456789101112131415161718192021222324
  1. from git import Repo
  2. def debug_commit_changes(repo_path, commit_hash, target_files=None):
  3. repo = Repo(repo_path)
  4. commit = repo.commit(commit_hash)
  5. if not commit.parents:
  6. print("Initial commit, no parent to compare")
  7. return
  8. parent = commit.parents[0]
  9. # 强制生成文本差异,忽略重命名
  10. diffs = parent.diff(commit, create_patch=True, no_renames=True)
  11. for diff in diffs:
  12. path = diff.a_path if diff.a_path else diff.b_path
  13. print(f"文件: {path} ({diff.change_type})")
  14. print(f"代码变更: {diff.diff.decode('utf-8')}")
  15. # 调用示例
  16. debug_commit_changes(r"C:\Users\32965\repo\9992cddb-b7d1-99ec-1bd2-35fdc177e623\test", "a0126f5620377c4894394c918786c6e98d218b6c", target_files=["README.md"])