0

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

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

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

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

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
4

1 回答 1

1

来自文档 ( 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_patchTrue

>>> 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)
[...]
于 2015-09-02T00:46:09.460 回答