我试图把握Ø模块gitpython,如何使用gitpython做最后一次提交的当前提交的git diff?
hcommit = repo.head.commit
tdiff = hcommit.diff('HEAD~1')
但tdiff = hcommit.diff('HEAD^ HEAD')
不起作用! ('HEAD~ HEAD')
。,
我想获得差异输出!
我试图把握Ø模块gitpython,如何使用gitpython做最后一次提交的当前提交的git diff?
hcommit = repo.head.commit
tdiff = hcommit.diff('HEAD~1')
但tdiff = hcommit.diff('HEAD^ HEAD')
不起作用! ('HEAD~ HEAD')
。,
我想获得差异输出!
我想出了如何使用gitPython获取git diff。
import git
repo = git.Repo("path/of/repo/")
# the below gives us all commits
repo.commits()
# take the first and last commit
a_commit = repo.commits()[0]
b_commit = repo.commits()[1]
# now get the diff
repo.diff(a_commit,b_commit)
瞧!
我得到'AttributeError:'Repo'对象没有属性'diff'',而'Repo.diff'没有在[API doc]中提到(https://gitpython.readthedocs.io/en/stable/reference。 HTML)。这个答案是否需要更新? – phihag
import git
repo = git.Repo('repo_path')
commits_list = list(repo.iter_commits())
# --- To compare the current HEAD against the bare init commit
a_commit = commits_list[0]
b_commit = commits_list[-1]
a_commit.diff(b_commit)
这将返回提交的diff对象。还有其他方法可以实现这一点。例如(这是复制/ http://gitpython.readthedocs.io/en/stable/tutorial.html#obtaining-diff-information粘贴):
```
hcommit = repo.head.commit
hcommit.diff() # diff tree against index
hcommit.diff('HEAD~1') # diff tree against previous tree
hcommit.diff(None) # diff tree against working tree
index = repo.index
index.diff() # diff index against itself yielding empty diff
index.diff(None) # diff index against working copy
index.diff('HEAD') # diff index against current HEAD tree
```
,为妥善解决(不使用Git命令回调),您必须使用create_patch选项。
比较当前指数与以前的承诺:
diff_as_patch = repo.index.diff(repo.commit('HEAD~1'), create_patch=True)
print(diff_as_patch)
要获得差异的内容:
import git
repo = git.Repo("path/of/repo/")
# define a new git object of the desired repo
gitt = repo.git
diff_st = gitt.diff("commitID_A", "commitID_B")
我承认从未使用的gitpython代码,但它似乎很明显,如果hcommit是'repo.head.commit',它被绑定到*特殊*提交,因此'hcommit.diff'的意思是“不同于其他东西”。为了得到两个任意提交的差异,你必须选择其他的起点。 – torek