9 #if LIBCURL_VERSION_NUM < 0x070704
10 #define curl_global_cleanup() do { /* nothing */ } while(0)
12 #if LIBCURL_VERSION_NUM < 0x070800
13 #define curl_global_init(a) do { /* nothing */ } while(0)
21 static z_stream stream;
26 static int curl_ssl_verify;
35 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
36 struct buffer *buffer)
38 size_t size = eltsize * nmemb;
39 if (size > buffer->size - buffer->posn)
40 size = buffer->size - buffer->posn;
41 memcpy(buffer->buffer + buffer->posn, ptr, size);
46 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
49 unsigned char expn[4096];
50 size_t size = eltsize * nmemb;
53 ssize_t retval = write(local, ptr + posn, size - posn);
57 } while (posn < size);
59 stream.avail_in = size;
62 stream.next_out = expn;
63 stream.avail_out = sizeof(expn);
64 zret = inflate(&stream, Z_SYNC_FLUSH);
65 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
66 } while (stream.avail_in && zret == Z_OK);
70 static int got_indices = 0;
72 static struct packed_git *packs = NULL;
74 static int fetch_index(unsigned char *sha1)
81 if (has_pack_index(sha1))
85 fprintf(stderr, "Getting index for pack %s\n",
88 url = xmalloc(strlen(base) + 64);
89 sprintf(url, "%s/objects/pack/pack-%s.idx",
90 base, sha1_to_hex(sha1));
92 filename = sha1_pack_index_name(sha1);
93 indexfile = fopen(filename, "w");
95 return error("Unable to open local file %s for pack index",
98 curl_easy_setopt(curl, CURLOPT_FILE, indexfile);
99 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
100 curl_easy_setopt(curl, CURLOPT_URL, url);
102 if (curl_easy_perform(curl)) {
104 return error("Unable to get pack index %s", url);
111 static int setup_index(unsigned char *sha1)
113 struct packed_git *new_pack;
114 if (has_pack_file(sha1))
115 return 0; // don't list this as something we can get
117 if (fetch_index(sha1))
120 new_pack = parse_pack_index(sha1);
121 new_pack->next = packs;
126 static int fetch_indices(void)
128 unsigned char sha1[20];
130 struct buffer buffer;
137 data = xmalloc(4096);
140 buffer.buffer = data;
143 fprintf(stderr, "Getting pack list\n");
145 url = xmalloc(strlen(base) + 21);
146 sprintf(url, "%s/objects/info/packs", base);
148 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
149 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
150 curl_easy_setopt(curl, CURLOPT_URL, url);
152 if (curl_easy_perform(curl)) {
153 return error("Unable to get pack index %s", url);
160 if (i + 52 < buffer.posn &&
161 !strncmp(data + i, " pack-", 6) &&
162 !strncmp(data + i + 46, ".pack\n", 6)) {
163 get_sha1_hex(data + i + 6, sha1);
169 while (data[i] != '\n')
173 } while (i < buffer.posn);
179 static int fetch_pack(unsigned char *sha1)
182 struct packed_git *target;
183 struct packed_git **lst;
189 target = find_sha1_pack(sha1, packs);
191 return error("Couldn't get %s: not separate or in any pack",
195 fprintf(stderr, "Getting pack %s\n",
196 sha1_to_hex(target->sha1));
197 fprintf(stderr, " which contains %s\n",
201 url = xmalloc(strlen(base) + 65);
202 sprintf(url, "%s/objects/pack/pack-%s.pack",
203 base, sha1_to_hex(target->sha1));
205 filename = sha1_pack_name(target->sha1);
206 packfile = fopen(filename, "w");
208 return error("Unable to open local file %s for pack",
211 curl_easy_setopt(curl, CURLOPT_FILE, packfile);
212 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
213 curl_easy_setopt(curl, CURLOPT_URL, url);
215 if (curl_easy_perform(curl)) {
217 return error("Unable to get pack file %s", url);
223 while (*lst != target)
224 lst = &((*lst)->next);
227 install_packed_git(target);
232 int fetch(unsigned char *sha1)
234 char *hex = sha1_to_hex(sha1);
235 char *filename = sha1_file_name(sha1);
236 unsigned char real_sha1[20];
240 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
243 return error("Couldn't open local object %s\n", filename);
245 memset(&stream, 0, sizeof(stream));
247 inflateInit(&stream);
251 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
252 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
253 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
255 url = xmalloc(strlen(base) + 50);
257 posn = url + strlen(base);
258 strcpy(posn, "objects/");
260 memcpy(posn, hex, 2);
263 strcpy(posn, hex + 2);
265 curl_easy_setopt(curl, CURLOPT_URL, url);
267 if (curl_easy_perform(curl)) {
269 if (fetch_pack(sha1))
270 return error("Tried %s", url);
276 SHA1_Final(real_sha1, &c);
277 if (zret != Z_STREAM_END) {
279 return error("File %s (%s) corrupt\n", hex, url);
281 if (memcmp(sha1, real_sha1, 20)) {
283 return error("File %s has bad hash\n", hex);
286 pull_say("got %s\n", hex);
290 int fetch_ref(char *ref, unsigned char *sha1)
294 struct buffer buffer;
300 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
301 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
303 url = xmalloc(strlen(base) + 6 + strlen(ref));
305 posn = url + strlen(base);
306 strcpy(posn, "refs/");
310 curl_easy_setopt(curl, CURLOPT_URL, url);
312 if (curl_easy_perform(curl))
313 return error("Couldn't get %s for %s\n", url, ref);
316 get_sha1_hex(hex, sha1);
320 int main(int argc, char **argv)
326 while (arg < argc && argv[arg][0] == '-') {
327 if (argv[arg][1] == 't') {
329 } else if (argv[arg][1] == 'c') {
331 } else if (argv[arg][1] == 'a') {
335 } else if (argv[arg][1] == 'v') {
337 } else if (argv[arg][1] == 'w') {
338 write_ref = argv[arg + 1];
343 if (argc < arg + 2) {
344 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
347 commit_id = argv[arg];
350 curl_global_init(CURL_GLOBAL_ALL);
352 curl = curl_easy_init();
354 curl_ssl_verify = gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
355 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
356 #if LIBCURL_VERSION_NUM >= 0x070907
357 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
365 curl_global_cleanup();