]> git.proxmox.com Git - libgit2.git/blame - examples/network/clone.c
Checkout: save index on checkout.
[libgit2.git] / examples / network / clone.c
CommitLineData
84595a30
BS
1#include "common.h"
2#include <git2.h>
3#include <git2/clone.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <pthread.h>
8#include <unistd.h>
9
10struct dl_data {
11 git_indexer_stats fetch_stats;
12 git_indexer_stats checkout_stats;
13 git_checkout_opts opts;
14 int ret;
15 int finished;
16 const char *url;
17 const char *path;
18};
19
20static void *clone_thread(void *ptr)
21{
22 struct dl_data *data = (struct dl_data *)ptr;
23 git_repository *repo = NULL;
24
25 // Kick off the clone
26 data->ret = git_clone(&repo, data->url, data->path,
27 &data->fetch_stats, &data->checkout_stats,
28 &data->opts);
29 if (repo) git_repository_free(repo);
30 data->finished = 1;
31
32 pthread_exit(&data->ret);
33}
34
35int clone(git_repository *repo, int argc, char **argv)
36{
37 struct dl_data data = {0};
38 pthread_t worker;
39
40 // Validate args
84595a30
BS
41 if (argc < 3) {
42 printf("USAGE: %s <url> <path>\n", argv[0]);
43 return -1;
44 }
45
46 // Data for background thread
47 data.url = argv[1];
48 data.path = argv[2];
49 data.opts.disable_filters = 1;
50 printf("Cloning '%s' to '%s'\n", data.url, data.path);
51
52 // Create the worker thread
53 pthread_create(&worker, NULL, clone_thread, &data);
54
55 // Watch for progress information
56 do {
57 usleep(10000);
58 printf("Fetch %d/%d – Checkout %d/%d\n",
59 data.fetch_stats.processed, data.fetch_stats.total,
60 data.checkout_stats.processed, data.checkout_stats.total);
61 } while (!data.finished);
62 printf("Fetch %d/%d – Checkout %d/%d\n",
63 data.fetch_stats.processed, data.fetch_stats.total,
64 data.checkout_stats.processed, data.checkout_stats.total);
65
66 return data.ret;
67}
68