GitPython:git.diff(commit_a, commit_b) 总是 returns 空字符串

GitPython: git.diff(commit_a, commit_b) always returns empty string

当我使用 GitPython 尝试以下代码时:

repo.head.commit.diff('HEAD~1')[0].diff

它总是returns一个空字符串。我在不同的提交中也尝试过多次更改文件。

我还尝试了以下代码,它会列出第一次和最后一次提交之间所有更改的文件。

changed_files = []

for x in commits_list[0].diff(commits_list[-1]):
    if x.a_blob.path not in changed_files:
        changed_files.append(x.a_blob.path)

    if x.b_blob is not None and x.b_blob.path not in changed_files:
        changed_files.append(x.b_blob.path)

print changed_files

来自文档 (pydoc git.Commit)。

 |  Methods inherited from git.diff.Diffable:
 |  
 |  diff(self, other=<class 'git.diff.Index'>, paths=None, create_patch=False, **kwargs)
 [...]
 |      :param create_patch:
 |              If True, the returned Diff contains a detailed patch that if applied
 |              makes the self to other. Patches are somwhat costly as blobs have to be read
 |              and diffed.

因此,如果我们复制您的代码,我们会得到一个空的 diff 属性:

>>> import git
>>> r = git.Repo('.')
>>> c1 = r.head.commit
>>> c2 = r.commit('HEAD~1')
>>> print c1.diff(c2)[0].diff

但是如果我们将create_patch设置为True

>>> print c1.diff(c2, create_patch=True)[0].diff

--- a/nova/compute/manager.py
+++ b/nova/compute/manager.py
@@ -4593,19 +4593,11 @@ class ComputeManager(manager.Manager):
                 LOG.debug("Updating volume usage cache with totals",
                           instance=instance)
[...]