]> git.proxmox.com Git - libgit2.git/blob - examples/network/clone.c
Adding credentials callback to ls-remote and fetch too.
[libgit2.git] / examples / network / clone.c
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 #ifndef _WIN32
8 # include <pthread.h>
9 # include <unistd.h>
10 #endif
11
12 typedef struct progress_data {
13 git_transfer_progress fetch_progress;
14 size_t completed_steps;
15 size_t total_steps;
16 const char *path;
17 } progress_data;
18
19 static void print_progress(const progress_data *pd)
20 {
21 int network_percent = (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects;
22 int index_percent = (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects;
23 int checkout_percent = pd->total_steps > 0
24 ? (100 * pd->completed_steps) / pd->total_steps
25 : 0.f;
26 int kbytes = pd->fetch_progress.received_bytes / 1024;
27
28 printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
29 network_percent, kbytes,
30 pd->fetch_progress.received_objects, pd->fetch_progress.total_objects,
31 index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects,
32 checkout_percent,
33 pd->completed_steps, pd->total_steps,
34 pd->path);
35 }
36
37 static int fetch_progress(const git_transfer_progress *stats, void *payload)
38 {
39 progress_data *pd = (progress_data*)payload;
40 pd->fetch_progress = *stats;
41 print_progress(pd);
42 return 0;
43 }
44 static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
45 {
46 progress_data *pd = (progress_data*)payload;
47 pd->completed_steps = cur;
48 pd->total_steps = tot;
49 pd->path = path;
50 print_progress(pd);
51 }
52
53
54 int do_clone(git_repository *repo, int argc, char **argv)
55 {
56 progress_data pd = {{0}};
57 git_repository *cloned_repo = NULL;
58 git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
59 git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT;
60 const char *url = argv[1];
61 const char *path = argv[2];
62 int error;
63
64 (void)repo; // unused
65
66 // Validate args
67 if (argc < 3) {
68 printf ("USAGE: %s <url> <path>\n", argv[0]);
69 return -1;
70 }
71
72 // Set up options
73 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
74 checkout_opts.progress_cb = checkout_progress;
75 checkout_opts.progress_payload = &pd;
76 clone_opts.checkout_opts = checkout_opts;
77 clone_opts.fetch_progress_cb = &fetch_progress;
78 clone_opts.fetch_progress_payload = &pd;
79 clone_opts.cred_acquire_cb = cred_acquire_cb;
80
81 // Do the clone
82 error = git_clone(&cloned_repo, url, path, &clone_opts);
83 printf("\n");
84 if (error != 0) {
85 const git_error *err = giterr_last();
86 if (err) printf("ERROR %d: %s\n", err->klass, err->message);
87 else printf("ERROR %d: no detailed info\n", error);
88 }
89 else if (cloned_repo) git_repository_free(cloned_repo);
90 return error;
91 }