[英]how to confirm that current git diff is one with current and last and not with current commit and any other
[英]how to do a git diff of current commit with last commit using gitpython?
我正在嘗試掌握 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)
這將為提交返回一個 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
``
我想出了如何使用 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
或.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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.