简体   繁体   English

如何在python中获取Git repo的修改文件列表?

[英]How to get list of modified files of a Git repo in python?

I am trying to write a function using python to determine modified files in a git repository.我正在尝试使用 python 编写一个函数来确定 git 存储库中的修改文件。 I saw there are couple of packages:我看到有几个包:

How can I get a list of pathlib.Path instances of modified files ?如何获取已修改文件pathlib.Path实例列表?

Python version in use: 3.8使用的 Python 版本:3.8

Edit:编辑:

In order to check modified files in GitPython I have tried the following:为了检查 GitPython 中修改过的文件,我尝试了以下操作:

diff = repo.git.diff('HEAD~1..HEAD', name_only=True)

but this gives me the files that are different, comparing the latest commit with the one before, instead of comparing the latest commit with unstaged changes.但这给了我不同的文件,将最新提交与之前的提交进行比较,而不是将最新提交与未暂存的更改进行比较。

To get a definitive list of what's changed (but not yet staged):要获得已更改内容的明确列表(但尚未上演):

# Gives a list of the differing objects
diff_list = repo.head.commit.diff()

for diff in diff_list:
    print(diff.change_type) # Gives the change type. eg. 'A': added, 'M': modified etc.

    # Returns true if it is a new file
    print(diff.new_file) 

    # Print the old file path
    print(diff.a_path)

    # Print the new file path. If the filename (or path) was changed it will differ
    print(diff.b_path) 

# Too many options to show. This gives a comprehensive description of what is available
help(diff_list[0]) 

I found the diff object to be very useful and should give any info you require.我发现 diff 对象非常有用,应该提供您需要的任何信息。

For staged items, use repo.index对于暂存项目,使用repo.index

The other option is repo.git.diff(...) which I found less useful as it gives long text strings for output rather than objects that can be easily parsed.另一个选项是repo.git.diff(...)我发现它不太有用,因为它为输出提供了长文本字符串,而不是可以轻松解析的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM