]>
Commit | Line | Data |
---|---|---|
84595a30 | 1 | #include "common.h" |
84595a30 | 2 | |
aa1e8674 | 3 | typedef struct progress_data { |
0c9c969a | 4 | git_indexer_progress fetch_progress; |
9c05c17b BS |
5 | size_t completed_steps; |
6 | size_t total_steps; | |
84595a30 | 7 | const char *path; |
aa1e8674 | 8 | } progress_data; |
84595a30 | 9 | |
aa1e8674 | 10 | static void print_progress(const progress_data *pd) |
84595a30 | 11 | { |
9af3c416 ET |
12 | int network_percent = pd->fetch_progress.total_objects > 0 ? |
13 | (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects : | |
14 | 0; | |
15 | int index_percent = pd->fetch_progress.total_objects > 0 ? | |
16 | (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects : | |
17 | 0; | |
18 | ||
9c05c17b | 19 | int checkout_percent = pd->total_steps > 0 |
0c9c969a | 20 | ? (int)((100 * pd->completed_steps) / pd->total_steps) |
300f4412 | 21 | : 0; |
0c9c969a | 22 | size_t kbytes = pd->fetch_progress.received_bytes / 1024; |
0f3def71 | 23 | |
9af3c416 ET |
24 | if (pd->fetch_progress.total_objects && |
25 | pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) { | |
0c9c969a | 26 | printf("Resolving deltas %u/%u\r", |
ab46b1d8 CMN |
27 | pd->fetch_progress.indexed_deltas, |
28 | pd->fetch_progress.total_deltas); | |
29 | } else { | |
0c9c969a | 30 | printf("net %3d%% (%4" PRIuZ " kb, %5u/%5u) / idx %3d%% (%5u/%5u) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ")%s\n", |
0f3def71 RB |
31 | network_percent, kbytes, |
32 | pd->fetch_progress.received_objects, pd->fetch_progress.total_objects, | |
33 | index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects, | |
34 | checkout_percent, | |
35 | pd->completed_steps, pd->total_steps, | |
36 | pd->path); | |
ab46b1d8 | 37 | } |
aa1e8674 | 38 | } |
84595a30 | 39 | |
cf66c474 CMN |
40 | static int sideband_progress(const char *str, int len, void *payload) |
41 | { | |
ac3d33df | 42 | (void)payload; /* unused */ |
d202bb7d | 43 | |
3fdba15c | 44 | printf("remote: %.*s", len, str); |
cf66c474 CMN |
45 | fflush(stdout); |
46 | return 0; | |
47 | } | |
48 | ||
0c9c969a | 49 | static int fetch_progress(const git_indexer_progress *stats, void *payload) |
aa1e8674 BS |
50 | { |
51 | progress_data *pd = (progress_data*)payload; | |
52 | pd->fetch_progress = *stats; | |
53 | print_progress(pd); | |
fe95ac1b | 54 | return 0; |
aa1e8674 | 55 | } |
9c05c17b | 56 | static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload) |
aa1e8674 BS |
57 | { |
58 | progress_data *pd = (progress_data*)payload; | |
9c05c17b BS |
59 | pd->completed_steps = cur; |
60 | pd->total_steps = tot; | |
aa1e8674 BS |
61 | pd->path = path; |
62 | print_progress(pd); | |
84595a30 BS |
63 | } |
64 | ||
2b10a2b0 | 65 | |
0c9c969a | 66 | int lg2_clone(git_repository *repo, int argc, char **argv) |
84595a30 | 67 | { |
3de22567 | 68 | progress_data pd = {{0}}; |
aa1e8674 | 69 | git_repository *cloned_repo = NULL; |
0015b587 | 70 | git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; |
6affd71f | 71 | git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; |
aa1e8674 BS |
72 | const char *url = argv[1]; |
73 | const char *path = argv[2]; | |
74 | int error; | |
84595a30 | 75 | |
ac3d33df | 76 | (void)repo; /* unused */ |
0f3def71 | 77 | |
ac3d33df | 78 | /* Validate args */ |
84595a30 | 79 | if (argc < 3) { |
aa1e8674 | 80 | printf ("USAGE: %s <url> <path>\n", argv[0]); |
84595a30 BS |
81 | return -1; |
82 | } | |
83 | ||
ac3d33df | 84 | /* Set up options */ |
14fec0ae | 85 | checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; |
aa1e8674 BS |
86 | checkout_opts.progress_cb = checkout_progress; |
87 | checkout_opts.progress_payload = &pd; | |
730df6d0 | 88 | clone_opts.checkout_opts = checkout_opts; |
cf66c474 | 89 | clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress; |
3fec548a CMN |
90 | clone_opts.fetch_opts.callbacks.transfer_progress = &fetch_progress; |
91 | clone_opts.fetch_opts.callbacks.credentials = cred_acquire_cb; | |
92 | clone_opts.fetch_opts.callbacks.payload = &pd; | |
316bca69 | 93 | |
ac3d33df | 94 | /* Do the clone */ |
316bca69 | 95 | error = git_clone(&cloned_repo, url, path, &clone_opts); |
aa1e8674 BS |
96 | printf("\n"); |
97 | if (error != 0) { | |
ac3d33df | 98 | const git_error *err = git_error_last(); |
aa1e8674 BS |
99 | if (err) printf("ERROR %d: %s\n", err->klass, err->message); |
100 | else printf("ERROR %d: no detailed info\n", error); | |
101 | } | |
102 | else if (cloned_repo) git_repository_free(cloned_repo); | |
103 | return error; | |
84595a30 | 104 | } |