0

鉴于hexshadirectory并且file已知,我怎样才能获得 2 个特定文件之间的差异,例如以下将返回 2 个修订之间的差异:

irepo.git.diff("93ba8ae12f79e7f90e5ec5217e44ce28624a66d8..d144da4b5f0dff89b918bc88629cb7902150d77c")

但是,我怎样才能产生<directory>/<file>包含在上述两个版本中的差异?

4

1 回答 1

0

你可以使用 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

于 2015-02-24T13:39:18.010 回答