问题
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