Implementing 'git pull' with libgit2?

2019-04-27 17:12发布

问题:

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?

回答1:

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



标签: c git libgit2