Libgit2 aims to make it easy to do interesting things with git. What’s the first thing you always do when learning git? That’s right, you clone something from GitHub. Let’s get started, shall we? Let’s get some of the boilerplate out of the way:
What does the do_clone method look like? Let’s start simple:
git_clone takes some information, and fills in a pointer for us with
git_repository object we can use to do all manner of unholy things. For
now, let’s ignore the repository itself, except to be good citizens and release
the memory associated with it.
NULL parameter? That’s for a
git_clone_options structure, which
defaults to some sensible stuff. The way our code is written right now, these
two commands will have the same results:
… except that
git tells you what it’s doing. Let’s fix that.
One of the things you can do with
git_clone_options is have libgit2 call
a function when there is progress to report. A typical callback looks like
stats object gives you lots of useful stuff:
- The number of objects transferred over the network
- The number of objects that the indexer has processed
- The total number of objects expected
- The number of bytes transferred
So let’s rewrite our
do_clone function to plug that in:
If you run this now, the program will tell you what it’s doing! You can watch the network transfer happening, and notice that the indexer is doing its job at the same time.
If you try this with a large repository, you’ll notice a significant pause at the end. All the data has been moved, what’s going on? It turns out that doing a checkout can take a non-trivial amount of time. It also turns out that libgit2 will let you report that progress as well!
But that’s part of checkout, which warrants its own blog post. In the
meantime, check out the clone
to see what
git_clone can do. If you want to, you could even use the code
from this post as a starting point for your
I dunno. What are you trying to do? You could always check out my other libgit2 posts for some ideas. Or look for help everywhere else.