问题描述
我不了解git log -p
和gitpython
的输出之间的差异.
I don't understand the difference between the output of git log -p
and that of gitpython
in terms of diffs.
例如,对于某些合并提交f534e1...
,git log -p
给出:
For example, for some merge commit f534e1...
, git log -p
gives:
commit f534e1...
Merge: ....
Author: ....
Date: ...
Merge pull request ... from ...
具有无差异,我认为这是正确的(差异随下一个日志条目提供-f534e1...
的父级之一).
with no diff, which I assume to be correct (the diff comes with the next log entry - one of the parents of f534e1...
).
我想用gitpython
达到相同的效果.我正在尝试:
I would like to achieve the same effect with gitpython
. I'm trying:
repo = Repo("...")
for c in repo.iter_commits():
print c.hexsha
print c.summary
print c.diff()
我得到:
f534e1...
Merge pull request ... from ...
[<git.diff.Diff object at 0x102cd3490>]
带有一些差异.
这是什么区别?我为什么在这里买到它?如何模仿git log -p
的行为?
What diff is this? Why do I get it here? How can I mimic the behavior of git log -p
?
推荐答案
根据文档,c.diff()
会将提交与索引(即分阶段的更改)进行比较.
According to the documentation, c.diff()
will compare the commit against the index, i.e. staged changes.
看来,git log -p
将对所有给定提交的父级产生特殊格式的差异.以下代码(根据您的示例)可以实现大致可比的目标.
It appears that git log -p
will produce a special-format diff against all of the given commit's parents. Something roughly comparable can be achieved with the following code (based on your example).
repo = Repo("...")
for c in repo.iter_commits():
print c.hexsha
print c.summary
for p in c.parents:
handle_diff(c.diff(p))
后者会产生一个 Diff 对象,所有相关信息.
The latter would yield a Diff object with all relevant information.
如果您真正想要的是git log -p
生成的确切格式,则也可以调用repo.git.log(p=True)
并自己解析输出.
If what you really want is the exact format produced by git log -p
, you may also call repo.git.log(p=True)
and parse the output yourself.
这篇关于gitpython的diff和git log的diff有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!