2015-01-20 90 views
4

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

我想用Ç版本。克隆,打开,添加文件索引和提交工作就像一个魅力(见code)。

test-bare-repository是本地的。

不幸的是,参考和文档没有帮助我。例子非常少见,大多数已经过时(like thisgit_push_new()函数似乎不见了)。

我在猜测现在几个小时,我想我尝试了referenceexamples的代码片段的所有有意义的组合。

编辑:我担心根本不可能用libgit2来做到这一点。 任何人都可以建议我参考,非常/伪造我的恐惧?

有一些源([1][2])在互联网/邮件列表是说,这是不可能的libgit2 推动现在,但将有可能soonish。 但是,这些来源相当过时。

引用包含一些与推式相关的功能(至少按名称)。但没有一个似乎工作,我想:(

回答

3

,做的伎俩的代码是这样的:

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); 
    return true; 
} 

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