我想仅获取git存储库中更改的文件的差异。目前,我正在使用gitpython来获取提交对象和git更改的文件,但我只想对更改部分进行依赖关系分析。是否有办法从git python获取git diff?还是必须逐行读取每个文件进行比较?
如果您想访问差异内容,请尝试以下方法:
repo = git.Repo(repo_root.as_posix())
commit_dev = repo.commit("dev")
commit_origin_dev = repo.commit("origin/dev")
diff_index = commit_origin_dev.diff(commit_dev)
for diff_item in diff_index.iter_change_type('M'):
print("A blob:\n{}".format(diff_item.a_blob.data_stream.read().decode('utf-8')))
print("B blob:\n{}".format(diff_item.b_blob.data_stream.read().decode('utf-8')))
这将打印每个文件的内容。
repo = Repo('/git/repository')
t = repo.head.commit.tree
repo.git.diff(t)
repo.git.diff('HEAD~1')
tree
和head~1
之间的diff
差异呢?它们有相似之处,但后者有更多的差异条目。看起来后者包含了与最后一次提交的差异。 - Timodifflib
模块来比较数据。repo.head.commit.diff(None)
。 - Addison Klinke我建议您使用PyDriller,它内部使用GitPython。使用起来非常简单:
for commit in Repository("path_to_repo").traverse_commits():
for modified_file in commit.modified_files: # here you have the list of modified files
print(modified_file.diff)
# etc...
您也可以通过以下方式分析单个提交:
for commit in RepositoryMining("path_to_repo", single="123213")
如果你想要重现类似于标准 git diff
的内容,可以尝试以下方法:
# cloned_repo = git.Repo.clone_from(
# url=ssh_url,
# to_path=repo_dir,
# env={"GIT_SSH_COMMAND": "ssh -i " + SSH_KEY},
# )
for diff_item in cloned_repo.index.diff(None, create_patch=True):
repo_diff += (
f"--- a/{diff_item.a_blob.name}\n+++ b/{diff_item.b_blob.name}\n"
f"{diff_item.diff.decode('utf-8')}\n\n"
)
import git
repo = git.Repo()
path_to_a_file = "diff_this_file_across_commits.txt"
commits_touching_path = list(repo.iter_commits(paths=path))
print repo.git.diff(commits_touching_path[0], commits_touching_path[1], path_to_a_file)
PyDriller +1
pip install pydriller
但是使用新的API:
Breaking API: ```
from pydriller import Repository
for commit in Repository('https://github.com/ishepard/pydriller').traverse_commits():
print(commit.hash)
print(commit.msg)
print(commit.author.name)
for file in commit.modified_files:
print(file.filename, ' has changed')
repo.git.diff("main", "head~5")
org.codehaus.mojo
findbugs-maven-plugin
3.0.5
低
中等
这是如何做到的
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' object has no attribute 'diff'
和 AttributeError: 'Repo' object has no attribute 'commits'
。 - firelynx