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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值