如何使用libgit2进行推送? (与控制台上的git push origin master
一样)
我想使用 C 版本。克隆,打开,添加文件到索引并像魅力一样提交工作(参见code)。
test-bare-repository是本地的。
不幸的是,参考和文档对我没有帮助。示例非常罕见且大部分已过时(like this,git_push_new()
函数似乎已消失。)
我现在猜了几个小时,我想我尝试了reference和examples的所有有意义的代码段组合。
编辑:我担心根本不可能用libgit2做到这一点。 任何人都可以建议我参考那些非常/伪造我的恐惧吗?
互联网/邮件列表中有一些来源([1],[2])表示目前无法推出libgit2 ,但它有可能soonish。 然而,这些来源已经过时了。
参考包含一些推送相关功能(至少按名称)。但似乎没有一个按我想要的方式工作:(
答案 0 :(得分:6)
实现这一诀窍的代码是:
bool push(git_repository *repository)
{
// get the remote.
git_remote* remote = NULL;
git_remote_lookup( &remote, repository, "origin" );
// connect to remote
git_remote_connect( remote, GIT_DIRECTION_PUSH )
// add a push refspec
git_remote_add_push( remote, "refs/heads/master:refs/heads/master" );
// configure options
git_push_options options;
git_push_init_options( &options, GIT_PUSH_OPTIONS_VERSION );
// do the push
git_remote_upload( remote, NULL, &options );
git_remote_free( remote );
return true;
}
当然,你应该做一些错误检查,我为了简洁而省略了。