Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing 'git pull' with libgit2?

Tags:

git

c

libgit2

I have a relatively short Gist which is supposed to use libgit2 to emulate the functionality of the git pull command. Unfortunately, it's not quite working.

In summary, the snippet:

  • calls git_repository_open() to open the repository on disk
  • calls git_remote_load() to get a git_remote * to the remote named "origin"
  • calls git_remote_connect() with the GIT_DIRECTION_FETCH flag
  • calls git_remote_download() to fetch objects from the remote

According to git_remote_stats(), objects are indeed being fetched. But the working directory doesn't change to reflect the latest commit. I tried adding:

git_checkout_head(repo, NULL);

...but that made no difference.

Entering:

git checkout master

...in a terminal results in the following output:

Already on 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.

How do I fast-forward?

like image 325
Nathan Osman Avatar asked Mar 19 '13 05:03

Nathan Osman


People also ask

Does git use Libgit2?

Libgit2 is a dependency-free implementation of Git, with a focus on having a nice API for use within other programs. You can find it at https://libgit2.org.

Why git pull is not recommended?

it modifies your working directory in unpredictable ways. pausing what you are doing to review someone else's work is annoying with git pull. it makes it hard to correctly rebase onto the remote branch. it doesn't clean up branches that were deleted in the remote repo.

How pull with commit?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.


1 Answers

You should run git pull origin master

or

git fetch origin + git merge origin/master

Then means you need the equivalent libgit2 merge function.

merge function is available in libgit2 v0.20

like image 156
linquize Avatar answered Oct 01 '22 10:10

linquize