使用 GitPython,我只想在拉取后本地文件发生更改时才调用函数。例如,如果我在一台单独的计算机上进行推送。然后拉第一台计算机,它按预期工作,但不提供任何输出。理想的输出是已更改的文件列表。或者只是告诉我拉动是否有错误,没有拉动,因为分支是最新的或者发生了变化的布尔值。我相信我可以抓取 repo.git.status() 但它看起来很粗糙。环顾四周,我似乎还可以比较分支的更改,但似乎有很多额外的代码和远程调用。是否有仅使用拉取调用的正确方法?
while True:
repo = git.Repo()
o = repo.remotes.origin
o.pull()
changed = NOT_SURE
if changed:
do_something()
print(repo.git.status())
time.sleep(POLLING_RATE)
Update:这确实可以检查是否进行了更改,但在没有额外远程调用的情况下不会对文件进行更改
while True:
print(str(time.ctime())+": Checking for updates")
repo = git.Repo()
current_hash = repo.head.object.hexsha
o = repo.remotes.origin
o.pull()
pull_hash = repo.head.object.hexsha
if current_hash != pull_hash:
print("files have changed")
else:
print("no changes")
time.sleep(config.GIT_POLL_RATE)
我知道这已经晚了两年,但是,我希望它仍然有用。
我刚刚写过拉检查 https://github.com/jamesgeddes/pullcheck,一个从存储库中提取的简单脚本,检查更改,如果发现更改则重新启动目标。我发现这个方法能够识别拉动中的变化。
def git_pull_change(path):
repo = git.Repo(path)
current = repo.head.commit
repo.remotes.origin.pull()
if current == repo.head.commit:
print("Repo not changed. Sleep mode activated.")
return False
else:
print("Repo changed! Activated.")
return True
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)