How to clone/fetch a git repository with libgit2?

…衆ロ難τιáo~ 提交于 2020-01-02 07:14:10

问题


I need to initialize a git repository and fetch the latest version of a branch into it. In bash, the commands are:

git init
git remote add -t $BRANCH -f origin $REMOTE

I'm trying to do the same programmatically with libgit2, but am having trouble finding the equivalent for the second line. The calls to create a remote are apparent, but I'm not seeing any to add it to a repository or handle the branch.

Is it possible to do this with libgit2? If not, is there a library capable of doing this?


回答1:


High level steps describing how to implement a fetching process in libgit2 can be found here.

You can find example of codes performing such task here and here. Beware these examples might get a little out of sync as the API is moving on.

Please note that those pieces of code will retrieve all newer commits (a branch is only a pointer to a specific commit) from the upstream defined remote.

As of today, there's no way to perform a checkout in libgit2 yet.

Provided you need some more help, I'd recommend those two places:

  • the libgit2 mailing list (via libgit2@librelist.com)
  • the libgit2 issue tracker

Update

The clone feature has just been merged into the libgit2 repository.

  • clone.h header
  • sample code usage in examples/network/clone.c

As part of the pull request, the author took care of providing the users with a checkout implementation as well.

  • checkout.h header
  • checkout related unit tests


来源:https://stackoverflow.com/questions/8551169/how-to-clone-fetch-a-git-repository-with-libgit2

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