3 #include <git2/clone.h>
12 typedef struct progress_data
{
13 git_transfer_progress fetch_progress
;
14 size_t completed_steps
;
19 static void print_progress(const progress_data
*pd
)
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
26 int kbytes
= pd
->fetch_progress
.received_bytes
/ 1024;
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
,
33 pd
->completed_steps
, pd
->total_steps
,
37 static int fetch_progress(const git_transfer_progress
*stats
, void *payload
)
39 progress_data
*pd
= (progress_data
*)payload
;
40 pd
->fetch_progress
= *stats
;
44 static void checkout_progress(const char *path
, size_t cur
, size_t tot
, void *payload
)
46 progress_data
*pd
= (progress_data
*)payload
;
47 pd
->completed_steps
= cur
;
48 pd
->total_steps
= tot
;
54 int do_clone(git_repository
*repo
, int argc
, char **argv
)
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];
68 printf ("USAGE: %s <url> <path>\n", argv
[0]);
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
;
82 error
= git_clone(&cloned_repo
, url
, path
, &clone_opts
);
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
);
89 else if (cloned_repo
) git_repository_free(cloned_repo
);