]>
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 | 27 | |
ab46b1d8 CMN |
28 | if (pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) { |
29 | printf("Resolving deltas %d/%d\r", | |
30 | pd->fetch_progress.indexed_deltas, | |
31 | pd->fetch_progress.total_deltas); | |
32 | } else { | |
33 | printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n", | |
0f3def71 RB |
34 | network_percent, kbytes, |
35 | pd->fetch_progress.received_objects, pd->fetch_progress.total_objects, | |
36 | index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects, | |
37 | checkout_percent, | |
38 | pd->completed_steps, pd->total_steps, | |
39 | pd->path); | |
ab46b1d8 | 40 | } |
aa1e8674 | 41 | } |
84595a30 | 42 | |
fe95ac1b | 43 | static int fetch_progress(const git_transfer_progress *stats, void *payload) |
aa1e8674 BS |
44 | { |
45 | progress_data *pd = (progress_data*)payload; | |
46 | pd->fetch_progress = *stats; | |
47 | print_progress(pd); | |
fe95ac1b | 48 | return 0; |
aa1e8674 | 49 | } |
9c05c17b | 50 | static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload) |
aa1e8674 BS |
51 | { |
52 | progress_data *pd = (progress_data*)payload; | |
9c05c17b BS |
53 | pd->completed_steps = cur; |
54 | pd->total_steps = tot; | |
aa1e8674 BS |
55 | pd->path = path; |
56 | print_progress(pd); | |
84595a30 BS |
57 | } |
58 | ||
2b10a2b0 | 59 | |
383fb799 | 60 | int do_clone(git_repository *repo, int argc, char **argv) |
84595a30 | 61 | { |
3de22567 | 62 | progress_data pd = {{0}}; |
aa1e8674 | 63 | git_repository *cloned_repo = NULL; |
0015b587 | 64 | git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; |
b81aa2f1 | 65 | git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; |
aa1e8674 BS |
66 | const char *url = argv[1]; |
67 | const char *path = argv[2]; | |
68 | int error; | |
84595a30 | 69 | |
0f3def71 RB |
70 | (void)repo; // unused |
71 | ||
84595a30 | 72 | // Validate args |
84595a30 | 73 | if (argc < 3) { |
aa1e8674 | 74 | printf ("USAGE: %s <url> <path>\n", argv[0]); |
84595a30 BS |
75 | return -1; |
76 | } | |
77 | ||
aa1e8674 | 78 | // Set up options |
c27e2112 | 79 | checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; |
aa1e8674 BS |
80 | checkout_opts.progress_cb = checkout_progress; |
81 | checkout_opts.progress_payload = &pd; | |
730df6d0 | 82 | clone_opts.checkout_opts = checkout_opts; |
0e0cf787 CMN |
83 | clone_opts.remote_callbacks.transfer_progress = &fetch_progress; |
84 | clone_opts.remote_callbacks.credentials = cred_acquire_cb; | |
85 | clone_opts.remote_callbacks.payload = &pd; | |
316bca69 BS |
86 | |
87 | // Do the clone | |
88 | error = git_clone(&cloned_repo, url, path, &clone_opts); | |
aa1e8674 BS |
89 | printf("\n"); |
90 | if (error != 0) { | |
91 | const git_error *err = giterr_last(); | |
92 | if (err) printf("ERROR %d: %s\n", err->klass, err->message); | |
93 | else printf("ERROR %d: no detailed info\n", error); | |
94 | } | |
95 | else if (cloned_repo) git_repository_free(cloned_repo); | |
96 | return error; | |
84595a30 | 97 | } |