本文介绍了GitPython检查git pull是否更改了本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GitPython,我只想在拉取后对本地文件进行更改的情况下才调用函数.例如,如果我在另一台计算机上进行推送.然后拉上第一台计算机,该计算机将按预期工作,但不提供任何输出.理想的输出是已更改文件的列表.或者,也有一些东西告诉我是否拉取出错,因为分支是最新的,或者布尔值发生了更改,所以没有拉出任何东西.我相信我可以抓取repo.git.status(),但看起来很粗糙.环顾四周,似乎我也可以比较分支的更改,但是似乎有很多额外的代码和远程调用.仅使用pull调用是否有正确的方法?

Using GitPython and I want to call a function only if there is a change to local files after a pull. For example if I make a push on a separate computer. Then pull on the first computer it works as expected but does not provide any output. An ideal output is a list of files changed. Or alternatively just something that told me if the pull had an error, nothing pulled because the branch was up to date or a boolean that changes had happened. I believe I could scrape repo.git.status() but it seems crude. Looking around it looks like I could also compare branches for changes but it seems like a lot of extra code and remote calls. Is there a correct way using just the pull call?

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: This does work for checking if changes were made but does not give the files changes without extra remote calls

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)

推荐答案

我很感谢这已经晚了两年,但是,我希望它可能仍然有用.

I appreciate that this is two years late, however, I hope it may still be of some use.

我刚刚编写了 pullcheck ,这是一个简单的脚本,可以从存储库中提取信息,然后检查更改如果发现更改,请重新启动目标.我发现这种方法能够识别出拉动中的变化.

I have just written pullcheck, a simple script to pull from a repo, check for changes then restart a target if changes are found. I found that this method was able to identify changes from the 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

这篇关于GitPython检查git pull是否更改了本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 16:57
查看更多