]>
Commit | Line | Data |
---|---|---|
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> | |
c27e2112 ET |
7 | #ifndef _WIN32 |
8 | # include <pthread.h> | |
9 | # include <unistd.h> | |
10 | #endif | |
84595a30 | 11 | |
aa1e8674 | 12 | typedef struct progress_data { |
9762ad99 | 13 | git_transfer_progress fetch_progress; |
9c05c17b BS |
14 | size_t completed_steps; |
15 | size_t total_steps; | |
84595a30 | 16 | const char *path; |
aa1e8674 | 17 | } progress_data; |
84595a30 | 18 | |
aa1e8674 | 19 | static void print_progress(const progress_data *pd) |
84595a30 | 20 | { |
9762ad99 BS |
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; | |
9c05c17b | 23 | int checkout_percent = pd->total_steps > 0 |
2dae54a9 | 24 | ? (100 * pd->completed_steps) / pd->total_steps |
9c05c17b | 25 | : 0.f; |
9762ad99 | 26 | int kbytes = pd->fetch_progress.received_bytes / 1024; |
0f3def71 RB |
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); | |
aa1e8674 | 35 | } |
84595a30 | 36 | |
fe95ac1b | 37 | static int fetch_progress(const git_transfer_progress *stats, void *payload) |
aa1e8674 BS |
38 | { |
39 | progress_data *pd = (progress_data*)payload; | |
40 | pd->fetch_progress = *stats; | |
41 | print_progress(pd); | |
fe95ac1b | 42 | return 0; |
aa1e8674 | 43 | } |
9c05c17b | 44 | static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload) |
aa1e8674 BS |
45 | { |
46 | progress_data *pd = (progress_data*)payload; | |
9c05c17b BS |
47 | pd->completed_steps = cur; |
48 | pd->total_steps = tot; | |
aa1e8674 BS |
49 | pd->path = path; |
50 | print_progress(pd); | |
84595a30 BS |
51 | } |
52 | ||
2b10a2b0 | 53 | |
383fb799 | 54 | int do_clone(git_repository *repo, int argc, char **argv) |
84595a30 | 55 | { |
3de22567 | 56 | progress_data pd = {{0}}; |
aa1e8674 | 57 | git_repository *cloned_repo = NULL; |
0015b587 | 58 | git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; |
b81aa2f1 | 59 | git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; |
aa1e8674 BS |
60 | const char *url = argv[1]; |
61 | const char *path = argv[2]; | |
62 | int error; | |
84595a30 | 63 | |
0f3def71 RB |
64 | (void)repo; // unused |
65 | ||
84595a30 | 66 | // Validate args |
84595a30 | 67 | if (argc < 3) { |
aa1e8674 | 68 | printf ("USAGE: %s <url> <path>\n", argv[0]); |
84595a30 BS |
69 | return -1; |
70 | } | |
71 | ||
aa1e8674 | 72 | // Set up options |
c27e2112 | 73 | checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; |
aa1e8674 BS |
74 | checkout_opts.progress_cb = checkout_progress; |
75 | checkout_opts.progress_payload = &pd; | |
730df6d0 | 76 | clone_opts.checkout_opts = checkout_opts; |
0015b587 BS |
77 | clone_opts.fetch_progress_cb = &fetch_progress; |
78 | clone_opts.fetch_progress_payload = &pd; | |
255836dd | 79 | clone_opts.cred_acquire_cb = cred_acquire_cb; |
316bca69 BS |
80 | |
81 | // Do the clone | |
82 | error = git_clone(&cloned_repo, url, path, &clone_opts); | |
aa1e8674 BS |
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; | |
84595a30 | 91 | } |