如何在C ++中使用libgit2检查是否需要拉动? [英] How can I check if pull is needed using libgit2 in c++?

查看:117
本文介绍了如何在C ++中使用libgit2检查是否需要拉动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我是否具有最新版本的程序.我已将程序共享到bitbucket.org,并且如果需要拉最新版本,或者我已经拥有最新版本,我希望我的c ++代码能写给我.

I want to check if I have the latest version of program. I have my program shared to bitbucket.org , and I want my c++ code to write me if I need to pull the latest version, or I already have the latest version.

推荐答案

首先,您必须获取获取远程跟踪分支的状态.没有其他方法可以检查您的分支是否已在远程上更新.为此,许多工具会定期(例如每10分钟一次)自动获取.

First, you have to fetch to get the state of the remote tracking branches. There isn't any other way to check if your branch has been updated on the remote. Many tools automatically fetch periodically (like every 10 minutes) for this purpose.

然后将您的本地分支机构与其上游分支机构进行比较.使用libgit2做到这一点的一种方法是使用revwalk功能.如果您git_revwalk_push_ref上游分支和git_revwalk_hide_ref本地分支遍历范围,则可以计算出本地分支后面有多少个提交.相反,请提前进行提交次数.

Then compare your local branch with its upstream. One way to do that with libgit2 is to use the revwalk functionality. If you git_revwalk_push_ref the upstream and git_revwalk_hide_ref the local branch then walk over the range, you can count how many commits behind your local branch is. Do the opposite to get the number of commits ahead.

示例:

git_revwalk *walker;
git_revwalk_new(&walker, repo);
git_revwalk_push_ref(walker, "refs/remotes/origin/master");
git_revwalk_hide_ref(walker, "refs/heads/master");

git_oid id;
int count = 0;
while (!git_revwalk_next(&id, walker))
    ++count;

// 'count' is the difference between remote and local

这篇关于如何在C ++中使用libgit2检查是否需要拉动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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