首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GitPython: git.diff(commit_a,commit_b)总是返回空字符串

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

Stack Overflow用户
提问于 2015-09-02 00:00:38
回答 1查看 1.2K关注 0票数 0

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

代码语言:javascript
运行
复制
repo.head.commit.diff('HEAD~1')[0].diff

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

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

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-02 00:46:09

来自docs (pydoc git.Commit)。

代码语言:javascript
运行
复制
 |  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属性:

代码语言:javascript
运行
复制
>>> import git
>>> r = git.Repo('.')
>>> c1 = r.head.commit
>>> c2 = r.commit('HEAD~1')
>>> print c1.diff(c2)[0].diff

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

代码语言:javascript
运行
复制
>>> 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)
[...]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32342684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档