How to push (with libgit2)

◇◆丶佛笑我妖孽 提交于 2019-12-07 03:47:56

问题


How do I do a push with libgit2? (Like git push origin master on console)

I want to use the C version. Cloning, opening, adding files to index and committing work like a charm (see code).

The test-bare-repository is local.

Unfortunately, reference and documentation did not help me. Examples are very rare and mostly outdated (like this, the git_push_new() function seems to be gone).

I'm guessing for some hours now and I think I tried all meaningfull combinations of code snippets from reference and examples.

Edit: I fear there is no possibility to do that with libgit2 at all. Can anyone suggest me references that veryfiy/falsify my fears?

There are some sources ([1], [2]) in the internet/mailing lists that say it is impossible to push with libgit2 for now, but it will be possible soonish. However those sources are quite outdated.

Reference contains some push-related functions (at least by name). But none seems to work the way I want :(


回答1:


The code that does the trick is:

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

Of course, you should do some error checks which I omitted for conciseness.



来源:https://stackoverflow.com/questions/28055919/how-to-push-with-libgit2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!