Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Last active June 30, 2018 06:11
Show Gist options
  • Save ryanwoodsmall/6a16f5c4aa5fff7b9a42ee5fc3bdf197 to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/6a16f5c4aa5fff7b9a42ee5fc3bdf197 to your computer and use it in GitHub Desktop.
clone only git client using libgit2
//
// gclone.c
// clone only git client using libgit2
// ignores all cert errors - unsafe!
// usage: glone <url> <path>
// links:
// sample clone: https://libgit2.github.com/docs/guides/101-samples/#repositories_clone_simple
// based on examples/network/clone.c: https://github.com/libgit2/libgit2/blob/master/examples/network/clone.c
// license: public domain
//
#include <git2.h>
#include <git2/clone.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
static int accept_cert();
int main(int argc, char **argv)
{
if(argc != 3){
printf("usage: gclone <url> <path>\n");
return(1);
}
git_libgit2_init();
git_repository *repo;
int cloneerr = 0;
char *url = argv[1];
char *path = argv[2];
git_clone_options cloneopts = GIT_CLONE_OPTIONS_INIT;
git_checkout_options checkoutopts = GIT_CHECKOUT_OPTIONS_INIT;
checkoutopts.checkout_strategy = GIT_CHECKOUT_SAFE;
cloneopts.checkout_opts = checkoutopts;
cloneopts.fetch_opts.callbacks.certificate_check = accept_cert;
printf("cloning %s into %s... ", url, path);
cloneerr = git_clone(&repo, url, path, &cloneopts);
if(cloneerr != 0){
const git_error *gerr = giterr_last();
if(gerr){
printf("ERROR %d: %s\n", gerr->klass, gerr->message);
}else{
printf("ERROR %d: no detailed info\n", cloneerr);
}
}else{
printf("done\n");
if(repo){
git_repository_free(repo);
}
}
git_libgit2_shutdown();
return(0);
}
static int accept_cert(git_cert *cert, int valid, const char* host, void *payload)
{
return(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment