我正在尝试掌握 gitpython 模块,
hcommit = repo.head.commit
tdiff = hcommit.diff('HEAD~1')
但tdiff = hcommit.diff('HEAD^ HEAD')
不起作用!也没有('HEAD~ HEAD')
.,
我正在尝试获取差异输出!
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)
这将为提交返回一个差异对象。还有其他方法可以实现这一点。例如(这是从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
```
我想出了如何使用 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)
瞧!
要获取差异的内容:
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")
对于正确的解决方案(不使用 git 命令回调),您必须使用 create_patch 选项。
要将当前索引与之前的提交进行比较:
diff_as_patch = repo.index.diff(repo.commit('HEAD~1'), create_patch=True)
print(diff_as_patch)
抱歉挖掘了一个旧线程......如果您需要找出哪些文件已更改,您可以使用 diff 对象的.a_path
or.b_path
属性,具体取决于您首先提供的对象。在下面的示例中,我使用的是头部提交,a
所以我查看a
.
例如:
# this will compare the most recent commit to the one prior to find out what changed.
from git import repo
repo = git.Repo("path/to/.git directory")
repoDiffs = repo.head.commit.diff('HEAD~1')
for item in repoDiffs:
print(item.a_path)