python - GitPython 检查 git pull 是否更改了本地文件

标签 python python-3.x git gitpython

使用 GitPython 并且我只想在 pull 后对本地文件进行更改时才调用函数。例如,如果我在单独的计算机上进行推送。然后 pull 上第一台计算机,它按预期工作,但不提供任何输出。理想的输出是更改的文件列表。或者只是告诉我 pull 是否有错误,没有 pull 因为分支是最新的或发生了变化的 bool 值。我相信我可以刮掉 repo.git.status() 但它看起来很粗糙。环顾四周,我似乎还可以比较分支的变化,但似乎有很多额外的代码和远程调用。是否有仅使用 pull 调用的正确方法?

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)

更新:这确实适用于检查是否进行了更改,但不会在没有额外远程调用的情况下更改文件
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)

最佳答案

我很感激这晚了两年,但是,我希望它仍然有用。
我刚刚写了pullcheck ,一个从 repo 中提取的简单脚本,检查更改,如果发现更改则重新启动目标。我发现这种方法能够从 pull 中识别变化。

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

关于python - GitPython 检查 git pull 是否更改了本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50633456/

相关文章:

python OrderedDict 获取关键索引的

python - 可能替换的所有版本

git - 是否有等同于 gits no-fast-forward merge 的 mercurial?

javascript - Grunt 碰撞并提示提交消息

python - 如何删除重复的字母,但添加一些异常(exception)字母

c# - 将变量从 C# - IronPython 传递到 Python 脚本

python - 迷惑Gothon练习游戏Python

python - Mac OSX 魔术函数 %hist 未找到

在 Windows 上使用 SSH 私钥进行 Git 推送

python - 我如何要求用户输入 2 个数字,然后它会检查哪个数字更大并打印更大的数字