python - 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. I saw there are couple of packages:
How can I get a list of pathlib.Path
instances of modified files?
Python version in use: 3.8
Edit:
In order to check modified files in GitPython I have tried the following:
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.
For staged items, use 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.
推荐阅读
- intellij-idea - 这是尾递归还是 Kotlin 编译器的问题?
- sql-server - 查询以获取具有限制的一对多记录
- javascript - HTML表格,固定列加载和水平滚动到左/右滚动箭头上的下一列单击
- ios - ARKit:以实际大小显示对象
- firebase - Firebase Firestore 和 Cloud Functions 显示错误的读取、写入和删除数字?
- listview - Xamarin Forms Listview 不更新 GUI
- python - 如何使用视频分析技术确定机器人汽车的速度?
- python - Python Keras 预测返回 nan
- node.js - 是否可以使用通用调试器(gdb、lldb 等)在电子应用程序中调试 node c++ 插件?
- ios - expo.io 的 BackgroundFetch 没有运行定义的任务