使用libgit2进行Git Pull
Doing a Git Pull with libgit2
我看过这个帖子的答案(libgit2 (fetch &合并,提交)),但我正在努力得到一个Git拉工作。我没有得到任何错误消息。"获取"似乎可以工作,但"合并"没有发生。执行Git状态显示我的分支落后1个提交…
On branch Branch_1_1.
Your branch is behind 'origin/Branch_1_1' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working directory clean
我的代码如下…
static int fetchhead_ref_cb(const char *name, const char *url,
const git_oid *oid, unsigned int is_merge, void *payload)
{
if ( is_merge )
{
strcpy_s( branchToMerge, 100, name );
memcpy( &branchOidToMerge, oid, sizeof( git_oid ) );
}
return 0;
}
void GitPull()
{
git_libgit2_init();
git_repository *repo = NULL;
git_remote *remote;
int error = git_repository_open( &repo, "C:\work\GitTestRepos\Repo1" );
CHECK_FOR_ERROR
error = git_remote_lookup( &remote, repo, "origin" );
CHECK_FOR_ERROR
git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
error = git_remote_fetch( remote, NULL, &options, NULL );
CHECK_FOR_ERROR
git_repository_fetchhead_foreach( repo, fetchhead_ref_cb, NULL );
git_merge_options merge_options = GIT_MERGE_OPTIONS_INIT;
git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
git_annotated_commit *heads[ 1 ];
git_reference *ref;
error = git_annotated_commit_lookup( &heads[ 0 ], repo, &branchOidToMerge );
CHECK_FOR_ERROR
error = git_merge( repo, (const git_annotated_commit **)heads, 1, &merge_options, &checkout_options );
CHECK_FOR_ERROR
git_annotated_commit_free( heads[ 0 ] );
git_repository_state_cleanup( repo );
git_libgit2_shutdown();
}
我做了什么导致合并不工作?
首先,git_checkout_options.checkout_strategy
有一个相当令人惊讶的默认值。默认为预演。所以你可能想把它改成GIT_CHECKOUT_SAFE
。
第二件要理解的事情是,git_merge实际上并没有提交合并。它仅为合并设置工作目录和索引。您仍然需要检查冲突并调用git_commit_create
来完成合并。
然而,在这个特定的情况下,看起来你并不真的需要合并。你的分支可以快进。调用git_merge_analysis
来确定要进行哪种合并。
git_reference_set_target
。
相关文章:
- 使用libgit2、c++进行pull
- 在已经使用Git的情况下减少编译时间
- 使用Qt C++计算类似Git的SHA1哈希
- 如何在c++中从git建立外部库
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- 从C++程序调用 git 克隆
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- 更改 git 分支名称后,在项目的 Visual Studio 代码中丢失智能感知(建议,转到定义C++
- 如何组织 git 工作流以修复错误并同时引入新功能
- 自动完成在git存储库中的Visual Studio项目中不起作用
- 在构建时执行execute_process以在构建时获取git提交哈希
- git 子模块的替代方案,用于使用多个存储库的项目
- 将 Git 提交哈希读取到C++时出错
- 使用 libgit2 从另一个分支创建新的 git 存储库?
- 如何在构建Qt应用程序时运行git命令
- 我被困在尝试克隆需要 cmake 文件和依赖项的 git 存储库中
- CMake Ninja Git 自签名证书颁发机构密码提示
- 如何从终端分离,以便 git 中的接收后钩子完成
- git-是否有互联网上某处所有可能错误的列表
- 使用libgit2进行Git Pull