使用GitPython检查本地Python相对于远程的状态 [英] Check status of local Python relative to remote with GitPython

查看:76
本文介绍了使用GitPython检查本地Python相对于远程的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 GitPython 确定是否:

  • 我的本地分支位于远程服务器的前面(我可以安全地推送)
  • 我的本地分支位于远程站点后面(我可以放心拉动)
  • 我的本地分支与远程分支不同吗?

要检查本地和远程是否相同,我正在这样做:

To check if the local and remote are the same, I'm doing this:

def local_and_remote_are_at_same_commit(repo, remote):
    local_commit = repo.commit()
    remote_commit = remote.fetch()[0].commit
    return local_commit.hexsha == remote_commit.hexsha

推荐答案

请参见 https://stackoverflow.com/a/15862203/197789

例如

commits_behind = repo.iter_commits('master..origin/master')

commits_behind = repo.iter_commits('master..origin/master')

commits_ahead = repo.iter_commits('origin/master..master')

commits_ahead = repo.iter_commits('origin/master..master')

然后,您可以使用类似以下内容的方法从迭代器转换为计数:

Then you can use something like the following to go from iterator to a count:

count = sum(对于commits_ahead中的c,为1)

count = sum(1 for c in commits_ahead)

最后一次使用GitPython 1.0.2进行了检查.

This was last checked with GitPython 1.0.2.

这篇关于使用GitPython检查本地Python相对于远程的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆