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 :(
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With