dec*_*bot 3 python git gitpython
我正在尝试 gitpython,而且我对它很陌生。我正在尝试检测是否有任何需要提交的更改。
目前,我有一个如下所示的函数:
def commit(dir):
r = Repo(dir)
r.git.add(A=True)
r.git.commit(m='commit all')
Run Code Online (Sandbox Code Playgroud)
但这只是提交目录的代码。我想做一些事情,如果有变化,则显示一些消息,否则,显示另一条消息。
有人知道我如何在 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)
Run Code Online (Sandbox Code Playgroud)
基本上,您正在将暂存区域(即index)与活动分支进行比较。