如何推(使用libgit2)
如何使用libgit2进行推送? (就像控制台上的git push origin master
)
我想使用C版本。 克隆,打开,添加文件到索引和提交工作就像魅力(见代码 )。
测试裸存储库是本地的。
不幸的是,参考和文档对我没有帮助。 示例是非常罕见的并且大部分已过时( 像这样 , git_push_new()
函数似乎已经消失)。
我现在猜几个小时了,我想我尝试了参考和示例中所有有意义的代码片段组合。
编辑:我担心libgit2根本不可能做到这一点。 任何人都可以建议我参考,非常/伪造我的恐惧?
互联网/邮件列表中有一些消息来源( [1] , [2] )表示目前不可能推出libgit2,但很快就可能实现。 然而,这些来源已经过时了。
引用包含一些与推送相关的函数(至少按名称)。 但似乎没有一个像我想要的那样工作:(
实现这一诀窍的代码是:
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; }
当然,你应该做一些错误检查,我为了简洁而省略了。