我正在尝试 gitpython,而且我对它还很陌生。我正在尝试检测是否有任何需要提交的更改。
目前,我有一个如下所示的函数:
def commit(dir):
r = Repo(dir)
r.git.add(A=True)
r.git.commit(m='commit all')
但这只是提交目录的代码。我想做一些类似的事情,如果有更改,则显示一些消息,否则,显示另一条消息。
有人知道如何在 python 中做到这一点吗?
最佳答案
您可以像这样检查所有未暂存的更改:
for x in r.index.diff("HEAD"):
# Just print
print(x)
# Or for each entry you can find out information about it, e.g.
print(x.new_file)
print(x.b_path)
基本上,您正在将暂存区域(即索引
)与事件分支进行比较。
关于python - gitpython 使用 PYTHON 检查文件是否有任何更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42382487/