如何推送(使用 libgit2)

How to push (with libgit2)

如何使用 libgit2 进行推送? (就像控制台上的 git push origin master

我想使用 C 版本。克隆、打开、将文件添加到索引并像魅力一样提交工作(参见 code)。

test-bare-repository 是本地的。

不幸的是,参考资料和文档对我没有帮助。示例非常少见,而且大多已过时(like thisgit_push_new() 功能似乎已经消失)。

我猜了几个小时,我想我尝试了 reference and examples.

中所有有意义的代码片段组合

编辑:恐怕根本不可能用 libgit2 做到这一点。 谁能建议我参考 veryfiy/falsify 我的恐惧?

internet/mailing 列表中有一些来源 ([1], [2]) 说现在 无法使用 libgit2 推送 ,但它会可能很快。 然而,这些来源已经过时了。

参考包含一些与推送相关的函数(至少按名称)。但是 none 似乎按照我想要的方式工作:(

成功的代码是:

 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; 
 }

当然,你应该做一些错误检查,为了简洁我省略了。