pythongitpull_GitPython检查git 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.

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


版权声明:本文为weixin_30047651原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。