如何在python中打印pygit对象的内容

时间:2015-02-03 16:17:41

标签: python git pygit2

我正在尝试打印"差异"对象如下图所示。我期待一个类似于git show的输出,但我没有得到相同的结果。我该如何实现这一目标?感谢。

import pygit2
repo=pygit2.Repository('/home/repository')

t0=repo.revparse_single('HEAD')
t1=repo.revparse_single('HEAD^')


>>> repo.diff(t0,t1)
<_pygit2.Diff object at 0x7fc46eeb0470>
>>> out=repo.diff(t0,t1)
>>> print out
<_pygit2.Diff object at 0x7fc46eeb0410>
>>> 

1 个答案:

答案 0 :(得分:3)

让我们查看pygit2的文档:

$ pydoc pygit2.Diff
 |  ----------------------------------
 |  Data descriptors defined here:
 |  
 |  patch
 |      Patch diff string.

好的,让我们尝试一下:

>>> out=repo.diff(t0,t1)
>>> print out
<_pygit2.Diff object at 0x7fc46eeb0410>
>>> print out.patch
diff --git a/file1 b/file1
index 10952f3..66ed2b8 100644
--- a/file1
+++ b/file1
@@ -1,5 +1,6 @@
 DIR_COLORS
 DIR_COLORS.256color
+DIR_COLORS.lightbgcolor
 GREP_COLORS
 X11
 adjtime

似乎工作。