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 void prefetch(unsigned char *sha1)
74 static int got_indices = 0;
76 static struct packed_git *packs = NULL;
78 static int fetch_index(unsigned char *sha1)
85 if (has_pack_index(sha1))
89 fprintf(stderr, "Getting index for pack %s\n",
92 url = xmalloc(strlen(base) + 64);
93 sprintf(url, "%s/objects/pack/pack-%s.idx",
94 base, sha1_to_hex(sha1));
96 filename = sha1_pack_index_name(sha1);
97 indexfile = fopen(filename, "w");
99 return error("Unable to open local file %s for pack index",
102 curl_easy_setopt(curl, CURLOPT_FILE, indexfile);
103 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
104 curl_easy_setopt(curl, CURLOPT_URL, url);
106 if (curl_easy_perform(curl)) {
108 return error("Unable to get pack index %s", url);
115 static int setup_index(unsigned char *sha1)
117 struct packed_git *new_pack;
118 if (has_pack_file(sha1))
119 return 0; // don't list this as something we can get
121 if (fetch_index(sha1))
124 new_pack = parse_pack_index(sha1);
125 new_pack->next = packs;
130 static int fetch_indices(void)
132 unsigned char sha1[20];
134 struct buffer buffer;
141 data = xmalloc(4096);
144 buffer.buffer = data;
147 fprintf(stderr, "Getting pack list\n");
149 url = xmalloc(strlen(base) + 21);
150 sprintf(url, "%s/objects/info/packs", base);
152 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
153 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
154 curl_easy_setopt(curl, CURLOPT_URL, url);
156 if (curl_easy_perform(curl)) {
157 return error("Unable to get pack index %s", url);
164 if (i + 52 < buffer.posn &&
165 !strncmp(data + i, " pack-", 6) &&
166 !strncmp(data + i + 46, ".pack\n", 6)) {
167 get_sha1_hex(data + i + 6, sha1);
173 while (data[i] != '\n')
177 } while (i < buffer.posn);
183 static int fetch_pack(unsigned char *sha1)
186 struct packed_git *target;
187 struct packed_git **lst;
193 target = find_sha1_pack(sha1, packs);
195 return error("Couldn't get %s: not separate or in any pack",
199 fprintf(stderr, "Getting pack %s\n",
200 sha1_to_hex(target->sha1));
201 fprintf(stderr, " which contains %s\n",
205 url = xmalloc(strlen(base) + 65);
206 sprintf(url, "%s/objects/pack/pack-%s.pack",
207 base, sha1_to_hex(target->sha1));
209 filename = sha1_pack_name(target->sha1);
210 packfile = fopen(filename, "w");
212 return error("Unable to open local file %s for pack",
215 curl_easy_setopt(curl, CURLOPT_FILE, packfile);
216 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
217 curl_easy_setopt(curl, CURLOPT_URL, url);
219 if (curl_easy_perform(curl)) {
221 return error("Unable to get pack file %s", url);
227 while (*lst != target)
228 lst = &((*lst)->next);
231 install_packed_git(target);
236 int fetch(unsigned char *sha1)
238 char *hex = sha1_to_hex(sha1);
239 char *filename = sha1_file_name(sha1);
240 unsigned char real_sha1[20];
244 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
247 return error("Couldn't open local object %s\n", filename);
249 memset(&stream, 0, sizeof(stream));
251 inflateInit(&stream);
255 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
256 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
257 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
259 url = xmalloc(strlen(base) + 50);
261 posn = url + strlen(base);
262 strcpy(posn, "objects/");
264 memcpy(posn, hex, 2);
267 strcpy(posn, hex + 2);
269 curl_easy_setopt(curl, CURLOPT_URL, url);
271 if (curl_easy_perform(curl)) {
273 if (fetch_pack(sha1))
274 return error("Tried %s", url);
280 SHA1_Final(real_sha1, &c);
281 if (zret != Z_STREAM_END) {
283 return error("File %s (%s) corrupt\n", hex, url);
285 if (memcmp(sha1, real_sha1, 20)) {
287 return error("File %s has bad hash\n", hex);
290 pull_say("got %s\n", hex);
294 int fetch_ref(char *ref, unsigned char *sha1)
298 struct buffer buffer;
304 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
305 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
307 url = xmalloc(strlen(base) + 6 + strlen(ref));
309 posn = url + strlen(base);
310 strcpy(posn, "refs/");
314 curl_easy_setopt(curl, CURLOPT_URL, url);
316 if (curl_easy_perform(curl))
317 return error("Couldn't get %s for %s\n", url, ref);
320 get_sha1_hex(hex, sha1);
324 int main(int argc, char **argv)
330 while (arg < argc && argv[arg][0] == '-') {
331 if (argv[arg][1] == 't') {
333 } else if (argv[arg][1] == 'c') {
335 } else if (argv[arg][1] == 'a') {
339 } else if (argv[arg][1] == 'v') {
341 } else if (argv[arg][1] == 'w') {
342 write_ref = argv[arg + 1];
347 if (argc < arg + 2) {
348 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
351 commit_id = argv[arg];
354 curl_global_init(CURL_GLOBAL_ALL);
356 curl = curl_easy_init();
358 curl_ssl_verify = gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
359 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
360 #if LIBCURL_VERSION_NUM >= 0x070907
361 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
369 curl_global_cleanup();