]> git.proxmox.com Git - libgit2.git/blob - examples/network/clone.c
Fix warnings in example
[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 #include <pthread.h>
8 #include <unistd.h>
9
10 /* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/ */
11 #ifdef UNUSED
12 #elif defined(__GNUC__)
13 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
14 #elif defined(__LCLINT__)
15 # define UNUSED(x) /*@unused@*/ x
16 #else
17 # define UNUSED(x) x
18 #endif
19
20 typedef struct progress_data {
21 git_transfer_progress fetch_progress;
22 size_t completed_steps;
23 size_t total_steps;
24 const char *path;
25 } progress_data;
26
27 static void print_progress(const progress_data *pd)
28 {
29 int network_percent = (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects;
30 int index_percent = (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects;
31 int checkout_percent = pd->total_steps > 0
32 ? (100 * pd->completed_steps) / pd->total_steps
33 : 0.f;
34 int kbytes = pd->fetch_progress.received_bytes / 1024;
35
36 printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
37 network_percent, kbytes,
38 pd->fetch_progress.received_objects, pd->fetch_progress.total_objects,
39 index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects,
40 checkout_percent,
41 pd->completed_steps, pd->total_steps,
42 pd->path);
43 }
44
45 static void fetch_progress(const git_transfer_progress *stats, void *payload)
46 {
47 progress_data *pd = (progress_data*)payload;
48 pd->fetch_progress = *stats;
49 print_progress(pd);
50 }
51 static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
52 {
53 progress_data *pd = (progress_data*)payload;
54 pd->completed_steps = cur;
55 pd->total_steps = tot;
56 pd->path = path;
57 print_progress(pd);
58 }
59
60 static int cred_acquire(git_cred **out,
61 const char * UNUSED(url),
62 unsigned int UNUSED(allowed_types),
63 void * UNUSED(payload))
64 {
65 char username[128] = {0};
66 char password[128] = {0};
67
68 printf("Username: ");
69 scanf("%s", username);
70
71 /* Yup. Right there on your terminal. Careful where you copy/paste output. */
72 printf("Password: ");
73 scanf("%s", password);
74
75 return git_cred_userpass_plaintext_new(out, username, password);
76 }
77
78 int do_clone(git_repository *repo, int argc, char **argv)
79 {
80 progress_data pd = {{0}};
81 git_repository *cloned_repo = NULL;
82 git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
83 git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT;
84 const char *url = argv[1];
85 const char *path = argv[2];
86 int error;
87
88 (void)repo; // unused
89
90 // Validate args
91 if (argc < 3) {
92 printf ("USAGE: %s <url> <path>\n", argv[0]);
93 return -1;
94 }
95
96 // Set up options
97 clone_opts.checkout_opts = &checkout_opts;
98 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
99 checkout_opts.progress_cb = checkout_progress;
100 checkout_opts.progress_payload = &pd;
101 clone_opts.fetch_progress_cb = &fetch_progress;
102 clone_opts.fetch_progress_payload = &pd;
103 clone_opts.cred_acquire_cb = cred_acquire;
104
105 // Do the clone
106 error = git_clone(&cloned_repo, url, path, &clone_opts);
107 printf("\n");
108 if (error != 0) {
109 const git_error *err = giterr_last();
110 if (err) printf("ERROR %d: %s\n", err->klass, err->message);
111 else printf("ERROR %d: no detailed info\n", error);
112 }
113 else if (cloned_repo) git_repository_free(cloned_repo);
114 return error;
115 }