diff options
author | Ben Straub <bstraub@github.com> | 2012-07-30 14:38:32 -0700 |
---|---|---|
committer | Ben Straub <bstraub@github.com> | 2012-07-30 14:38:32 -0700 |
commit | 84595a30c01d5808ff71fda8ab63603214d665bf (patch) | |
tree | 7b188596f6ebeed24a15062d187407534235964e | |
parent | f1587b97a11e3a7283b32f5af46b7d057b8be4c5 (diff) | |
download | libgit2-84595a30c01d5808ff71fda8ab63603214d665bf.tar.gz |
Add clone to the network example.
-rw-r--r-- | examples/network/Makefile | 1 | ||||
-rw-r--r-- | examples/network/clone.c | 69 | ||||
-rw-r--r-- | examples/network/common.h | 1 | ||||
-rw-r--r-- | examples/network/git2.c | 1 |
4 files changed, 72 insertions, 0 deletions
diff --git a/examples/network/Makefile b/examples/network/Makefile index 9afd49e5d..835be24cc 100644 --- a/examples/network/Makefile +++ b/examples/network/Makefile @@ -8,6 +8,7 @@ OBJECTS = \ git2.o \ ls-remote.o \ fetch.o \ + clone.o \ index-pack.o all: $(OBJECTS) diff --git a/examples/network/clone.c b/examples/network/clone.c new file mode 100644 index 000000000..177a4c246 --- /dev/null +++ b/examples/network/clone.c @@ -0,0 +1,69 @@ +#include "common.h" +#include <git2.h> +#include <git2/clone.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <pthread.h> +#include <unistd.h> + +struct dl_data { + git_indexer_stats fetch_stats; + git_indexer_stats checkout_stats; + git_checkout_opts opts; + int ret; + int finished; + const char *url; + const char *path; +}; + +static void *clone_thread(void *ptr) +{ + struct dl_data *data = (struct dl_data *)ptr; + git_repository *repo = NULL; + + // Kick off the clone + data->ret = git_clone(&repo, data->url, data->path, + &data->fetch_stats, &data->checkout_stats, + &data->opts); + if (repo) git_repository_free(repo); + data->finished = 1; + + pthread_exit(&data->ret); +} + +int clone(git_repository *repo, int argc, char **argv) +{ + struct dl_data data = {0}; + pthread_t worker; + + // Validate args + printf("argc %d\n"); + if (argc < 3) { + printf("USAGE: %s <url> <path>\n", argv[0]); + return -1; + } + + // Data for background thread + data.url = argv[1]; + data.path = argv[2]; + data.opts.disable_filters = 1; + printf("Cloning '%s' to '%s'\n", data.url, data.path); + + // Create the worker thread + pthread_create(&worker, NULL, clone_thread, &data); + + // Watch for progress information + do { + usleep(10000); + printf("Fetch %d/%d – Checkout %d/%d\n", + data.fetch_stats.processed, data.fetch_stats.total, + data.checkout_stats.processed, data.checkout_stats.total); + } while (!data.finished); + printf("Fetch %d/%d – Checkout %d/%d\n", + data.fetch_stats.processed, data.fetch_stats.total, + data.checkout_stats.processed, data.checkout_stats.total); + + return data.ret; +} + diff --git a/examples/network/common.h b/examples/network/common.h index 29460bb36..d4b63e77c 100644 --- a/examples/network/common.h +++ b/examples/network/common.h @@ -10,5 +10,6 @@ int parse_pkt_line(git_repository *repo, int argc, char **argv); int show_remote(git_repository *repo, int argc, char **argv); int fetch(git_repository *repo, int argc, char **argv); int index_pack(git_repository *repo, int argc, char **argv); +int clone(git_repository *repo, int argc, char **argv); #endif /* __COMMON_H__ */ diff --git a/examples/network/git2.c b/examples/network/git2.c index 7c02305c4..21c8ec9b0 100644 --- a/examples/network/git2.c +++ b/examples/network/git2.c @@ -12,6 +12,7 @@ struct { } commands[] = { {"ls-remote", ls_remote}, {"fetch", fetch}, + {"clone", clone}, {"index-pack", index_pack}, { NULL, NULL} }; |