c++ - 如何在 C++ 中使用 libgit2 检查是否需要 pull?

标签 c++ bitbucket commit git-pull libgit2

<分区>

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

最佳答案

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

然后将您的本地分支与其上游进行比较。使用 libgit2 执行此操作的一种方法是使用 revwalk 功能。如果你 git_revwalk_push_ref 上游和 git_revwalk_hide_ref 本地分支然后遍历范围,你可以计算本地分支后面有多少提交。反其道而行之以获得提前提交的数量。

例子:

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++ - 如何在 C++ 中使用 libgit2 检查是否需要 pull?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42347428/

相关文章:

c++ - 我可以检测成员函数是否存在并且可以被 C++ 中的 friend 访问吗?

c++ - 为什么左值转换有效?

c++ - 如何从预处理程序#if指令调用constexpr函数?

git - 运行 "git log"未出现提交历史记录

macos - OSX 上的 SourceTree : Clicking "Always Allow" bitbucket to use my password keychain has no effect

postgresql - "ERROR: invalid transaction termination"尝试使用嵌套事务控制执行过程时

c++ - 迭代超过 10GB+ 的二进制文件

Git 智能 merge

mysql - SQL 提交不起作用

Git push 与 git push heroku master