首页 > 解决方案 > 在 gitpython 的最新提交中打印更改的文件路径

问题描述

我正在尝试在 git python 中获取最新提交和之前的提交之间的更改文件路径。问题是即使最新提交有 1 个更改的文件,它显示的内容也更多。以下是我的代码: -

repo = git.Repo(path)
commits_list = list(repo.iter_commits())
a_commit = commits_list[0]
b_commit = commits_list[-1]

itemDiff = a_commit.diff(b_commit)

for item in itemDiff
print(item.a_path)

我正在尝试针对本地克隆存储库进行此操作。我究竟做错了什么?

标签: pythongitgitpython

解决方案


如果您需要从 repo 中读取,请考虑使用 GitPython 的抽象Pydriller

for commit in RepositoryMining("repo").traverse_commits():
    for modified_file in commit.modifications:
        modified_file.new_path # here you have the path of all the files changed in the commit

推荐阅读