Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push (with libgit2)

Tags:

c

qt

libgit2

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 :(

like image 715
pasbi Avatar asked Jan 20 '15 21:01

pasbi


1 Answers

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.

like image 127
pasbi Avatar answered Sep 18 '22 17:09

pasbi