鉴于hexsha
,directory
并且file
已知,我怎样才能获得 2 个特定文件之间的差异,例如以下将返回 2 个修订之间的差异:
irepo.git.diff("93ba8ae12f79e7f90e5ec5217e44ce28624a66d8..d144da4b5f0dff89b918bc88629cb7902150d77c")
但是,我怎样才能产生<directory>/<file>
包含在上述两个版本中的差异?
你可以使用 GitPython 的内置差异工具来做到这一点。
import git
r = git.Repo(path_to_repo)
diff_index = r.commit(lhs_hexsha).diff(rhs_hexsha, create_patch=True)
# find all modified paths you are interested in
for diff_info in diff_index.iter_change_type('M'):
if diff_info.a_blob.path == my_path:
print(str(diff_info))
按照链接获取有关区分对象、diff()
调用、返回DiffIndex
对象或其中包含的Diff
对象的更多信息。
示例中a_blob
引用的是一个Blob
对象,它在提交时提供对比较文件的读取访问权限lhs_hexsha
。还有一个b_blob
代表文件在提交时的状态rhs_hexsha
。