]> git.proxmox.com Git - libgit2.git/blob - examples/fetch.c
Set upstream metadata fields: Bug-Database, Bug-Submit, Repository, Repository-Browse.
[libgit2.git] / examples / fetch.c
1 #include "common.h"
2
3 static int progress_cb(const char *str, int len, void *data)
4 {
5 (void)data;
6 printf("remote: %.*s", len, str);
7 fflush(stdout); /* We don't have the \n to force the flush */
8 return 0;
9 }
10
11 /**
12 * This function gets called for each remote-tracking branch that gets
13 * updated. The message we output depends on whether it's a new one or
14 * an update.
15 */
16 static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
17 {
18 char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
19 (void)data;
20
21 git_oid_fmt(b_str, b);
22 b_str[GIT_OID_HEXSZ] = '\0';
23
24 if (git_oid_is_zero(a)) {
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
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 */
41 static int transfer_progress_cb(const git_indexer_progress *stats, void *payload)
42 {
43 (void)payload;
44
45 if (stats->received_objects == stats->total_objects) {
46 printf("Resolving deltas %u/%u\r",
47 stats->indexed_deltas, stats->total_deltas);
48 } else if (stats->total_objects > 0) {
49 printf("Received %u/%u objects (%u) in %" PRIuZ " bytes\r",
50 stats->received_objects, stats->total_objects,
51 stats->indexed_objects, stats->received_bytes);
52 }
53 return 0;
54 }
55
56 /** Entry point for this command */
57 int lg2_fetch(git_repository *repo, int argc, char **argv)
58 {
59 git_remote *remote = NULL;
60 const git_indexer_progress *stats;
61 git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
62
63 if (argc < 2) {
64 fprintf(stderr, "usage: %s fetch <repo>\n", argv[-1]);
65 return EXIT_FAILURE;
66 }
67
68 /* Figure out whether it's a named remote or a URL */
69 printf("Fetching %s for repo %p\n", argv[1], repo);
70 if (git_remote_lookup(&remote, repo, argv[1]) < 0)
71 if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0)
72 goto on_error;
73
74 /* Set up the callbacks (only update_tips for now) */
75 fetch_opts.callbacks.update_tips = &update_cb;
76 fetch_opts.callbacks.sideband_progress = &progress_cb;
77 fetch_opts.callbacks.transfer_progress = transfer_progress_cb;
78 fetch_opts.callbacks.credentials = cred_acquire_cb;
79
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)
86 goto on_error;
87
88 /**
89 * If there are local objects (we got a thin pack), then tell
90 * the user how many objects we saved from having to cross the
91 * network.
92 */
93 stats = git_remote_stats(remote);
94 if (stats->local_objects > 0) {
95 printf("\rReceived %u/%u objects in %" PRIuZ " bytes (used %u local objects)\n",
96 stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
97 } else{
98 printf("\rReceived %u/%u objects in %" PRIuZ "bytes\n",
99 stats->indexed_objects, stats->total_objects, stats->received_bytes);
100 }
101
102 git_remote_free(remote);
103
104 return 0;
105
106 on_error:
107 git_remote_free(remote);
108 return -1;
109 }