Implementing 'git pull' with libgit2?

末鹿安然 提交于 2019-12-22 05:13:18

问题


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



来源:https://stackoverflow.com/questions/15492375/implementing-git-pull-with-libgit2

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