]> git.proxmox.com Git - libgit2.git/blame - examples/fetch.c
Enable reproducible builds by default
[libgit2.git] / examples / fetch.c
CommitLineData
24012627 1#include "common.h"
bf4ef0c5 2
ea8ce3d1 3static int progress_cb(const char *str, int len, void *data)
0a1db746 4{
9da187e8 5 (void)data;
0a1db746
CMN
6 printf("remote: %.*s", len, str);
7 fflush(stdout); /* We don't have the \n to force the flush */
ea8ce3d1 8 return 0;
0a1db746
CMN
9}
10
c44820c6 11/**
4f62d559 12 * This function gets called for each remote-tracking branch that gets
c44820c6
CMN
13 * updated. The message we output depends on whether it's a new one or
14 * an update.
15 */
ae789622 16static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
f184836b 17{
f184836b 18 char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
9da187e8 19 (void)data;
f184836b
CMN
20
21 git_oid_fmt(b_str, b);
22 b_str[GIT_OID_HEXSZ] = '\0';
23
0c9c969a 24 if (git_oid_is_zero(a)) {
f184836b
CMN
25 printf("[new] %.20s %s\n", b_str, refname);
26 } else {
27 git_oid_fmt(a_str, a);
28 a_str[GIT_OID_HEXSZ] = '\0';
29 printf("[updated] %.10s..%.10s %s\n", a_str, b_str, refname);
30 }
31
32 return 0;
33}
34
6c7e86e1
CMN
35/**
36 * This gets called during the download and indexing. Here we show
37 * processed and total objects in the pack and the amount of received
38 * data. Most frontends will probably want to show a percentage and
39 * the download rate.
40 */
0c9c969a 41static int transfer_progress_cb(const git_indexer_progress *stats, void *payload)
6c7e86e1 42{
7314da10
PS
43 (void)payload;
44
6c7e86e1 45 if (stats->received_objects == stats->total_objects) {
0c9c969a 46 printf("Resolving deltas %u/%u\r",
6c7e86e1
CMN
47 stats->indexed_deltas, stats->total_deltas);
48 } else if (stats->total_objects > 0) {
0c9c969a 49 printf("Received %u/%u objects (%u) in %" PRIuZ " bytes\r",
6c7e86e1
CMN
50 stats->received_objects, stats->total_objects,
51 stats->indexed_objects, stats->received_bytes);
52 }
8b8f1f91 53 return 0;
6c7e86e1
CMN
54}
55
c44820c6 56/** Entry point for this command */
0c9c969a 57int lg2_fetch(git_repository *repo, int argc, char **argv)
24012627 58{
7eeec8f2 59 git_remote *remote = NULL;
0c9c969a 60 const git_indexer_progress *stats;
3fec548a 61 git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
7eeec8f2 62
9da187e8
RB
63 if (argc < 2) {
64 fprintf(stderr, "usage: %s fetch <repo>\n", argv[-1]);
65 return EXIT_FAILURE;
66 }
67
7314da10 68 /* Figure out whether it's a named remote or a URL */
d57c47dc 69 printf("Fetching %s for repo %p\n", argv[1], repo);
7314da10 70 if (git_remote_lookup(&remote, repo, argv[1]) < 0)
ae5b9362 71 if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0)
7314da10 72 goto on_error;
7eeec8f2 73
7314da10 74 /* Set up the callbacks (only update_tips for now) */
3fec548a
CMN
75 fetch_opts.callbacks.update_tips = &update_cb;
76 fetch_opts.callbacks.sideband_progress = &progress_cb;
6c7e86e1 77 fetch_opts.callbacks.transfer_progress = transfer_progress_cb;
3fec548a 78 fetch_opts.callbacks.credentials = cred_acquire_cb;
b3aaa7a7 79
6c7e86e1
CMN
80 /**
81 * Perform the fetch with the configured refspecs from the
82 * config. Update the reflog for the updated references with
83 * "fetch".
84 */
85 if (git_remote_fetch(remote, NULL, &fetch_opts, "fetch") < 0)
7314da10 86 goto on_error;
c27e2112 87
c44820c6
CMN
88 /**
89 * If there are local objects (we got a thin pack), then tell
4f62d559 90 * the user how many objects we saved from having to cross the
c44820c6
CMN
91 * network.
92 */
6c7e86e1 93 stats = git_remote_stats(remote);
ebbd48f0 94 if (stats->local_objects > 0) {
0c9c969a 95 printf("\rReceived %u/%u objects in %" PRIuZ " bytes (used %u local objects)\n",
ebbd48f0
CMN
96 stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
97 } else{
0c9c969a 98 printf("\rReceived %u/%u objects in %" PRIuZ "bytes\n",
1e3b8ed5 99 stats->indexed_objects, stats->total_objects, stats->received_bytes);
ebbd48f0 100 }
7eeec8f2 101
7eeec8f2
CMN
102 git_remote_free(remote);
103
104 return 0;
105
106 on_error:
107 git_remote_free(remote);
108 return -1;
24012627 109}