GIT 0.99.3
[git/git-svn.git] / http-pull.c
blobbd6195537ff38375aae3b16391b33c9e4fa23368
1 #include "cache.h"
2 #include "commit.h"
4 #include "pull.h"
6 #include <curl/curl.h>
7 #include <curl/easy.h>
9 #if LIBCURL_VERSION_NUM < 0x070704
10 #define curl_global_cleanup() do { /* nothing */ } while(0)
11 #endif
12 #if LIBCURL_VERSION_NUM < 0x070800
13 #define curl_global_init(a) do { /* nothing */ } while(0)
14 #endif
16 static CURL *curl;
18 static char *base;
20 static SHA_CTX c;
21 static z_stream stream;
23 static int local;
24 static int zret;
26 static int curl_ssl_verify;
28 struct buffer
30 size_t posn;
31 size_t size;
32 void *buffer;
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);
42 buffer->posn += size;
43 return size;
46 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
47 void *data)
49 unsigned char expn[4096];
50 size_t size = eltsize * nmemb;
51 int posn = 0;
52 do {
53 ssize_t retval = write(local, ptr + posn, size - posn);
54 if (retval < 0)
55 return posn;
56 posn += retval;
57 } while (posn < size);
59 stream.avail_in = size;
60 stream.next_in = ptr;
61 do {
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);
67 return size;
70 static int got_indices = 0;
72 static struct packed_git *packs = NULL;
74 static int fetch_index(unsigned char *sha1)
76 char *filename;
77 char *url;
79 FILE *indexfile;
81 if (has_pack_index(sha1))
82 return 0;
84 if (get_verbosely)
85 fprintf(stderr, "Getting index for pack %s\n",
86 sha1_to_hex(sha1));
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");
94 if (!indexfile)
95 return error("Unable to open local file %s for pack index",
96 filename);
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)) {
103 fclose(indexfile);
104 return error("Unable to get pack index %s", url);
107 fclose(indexfile);
108 return 0;
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))
118 return -1;
120 new_pack = parse_pack_index(sha1);
121 new_pack->next = packs;
122 packs = new_pack;
123 return 0;
126 static int fetch_indices(void)
128 unsigned char sha1[20];
129 char *url;
130 struct buffer buffer;
131 char *data;
132 int i = 0;
134 if (got_indices)
135 return 0;
137 data = xmalloc(4096);
138 buffer.size = 4096;
139 buffer.posn = 0;
140 buffer.buffer = data;
142 if (get_verbosely)
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);
156 do {
157 switch (data[i]) {
158 case 'P':
159 i++;
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);
164 setup_index(sha1);
165 i += 51;
166 break;
168 default:
169 while (data[i] != '\n')
170 i++;
172 i++;
173 } while (i < buffer.posn);
175 got_indices = 1;
176 return 0;
179 static int fetch_pack(unsigned char *sha1)
181 char *url;
182 struct packed_git *target;
183 struct packed_git **lst;
184 FILE *packfile;
185 char *filename;
187 if (fetch_indices())
188 return -1;
189 target = find_sha1_pack(sha1, packs);
190 if (!target)
191 return error("Couldn't get %s: not separate or in any pack",
192 sha1_to_hex(sha1));
194 if (get_verbosely) {
195 fprintf(stderr, "Getting pack %s\n",
196 sha1_to_hex(target->sha1));
197 fprintf(stderr, " which contains %s\n",
198 sha1_to_hex(sha1));
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");
207 if (!packfile)
208 return error("Unable to open local file %s for pack",
209 filename);
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)) {
216 fclose(packfile);
217 return error("Unable to get pack file %s", url);
220 fclose(packfile);
222 lst = &packs;
223 while (*lst != target)
224 lst = &((*lst)->next);
225 *lst = (*lst)->next;
227 install_packed_git(target);
229 return 0;
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];
237 char *url;
238 char *posn;
240 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
242 if (local < 0)
243 return error("Couldn't open local object %s\n", filename);
245 memset(&stream, 0, sizeof(stream));
247 inflateInit(&stream);
249 SHA1_Init(&c);
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);
256 strcpy(url, base);
257 posn = url + strlen(base);
258 strcpy(posn, "objects/");
259 posn += 8;
260 memcpy(posn, hex, 2);
261 posn += 2;
262 *(posn++) = '/';
263 strcpy(posn, hex + 2);
265 curl_easy_setopt(curl, CURLOPT_URL, url);
267 if (curl_easy_perform(curl)) {
268 unlink(filename);
269 if (fetch_pack(sha1))
270 return error("Tried %s", url);
271 return 0;
274 close(local);
275 inflateEnd(&stream);
276 SHA1_Final(real_sha1, &c);
277 if (zret != Z_STREAM_END) {
278 unlink(filename);
279 return error("File %s (%s) corrupt\n", hex, url);
281 if (memcmp(sha1, real_sha1, 20)) {
282 unlink(filename);
283 return error("File %s has bad hash\n", hex);
286 pull_say("got %s\n", hex);
287 return 0;
290 int fetch_ref(char *ref, unsigned char *sha1)
292 char *url, *posn;
293 char hex[42];
294 struct buffer buffer;
295 buffer.size = 41;
296 buffer.posn = 0;
297 buffer.buffer = hex;
298 hex[41] = '\0';
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));
304 strcpy(url, base);
305 posn = url + strlen(base);
306 strcpy(posn, "refs/");
307 posn += 5;
308 strcpy(posn, ref);
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);
315 hex[40] = '\0';
316 get_sha1_hex(hex, sha1);
317 return 0;
320 int main(int argc, char **argv)
322 char *commit_id;
323 char *url;
324 int arg = 1;
326 while (arg < argc && argv[arg][0] == '-') {
327 if (argv[arg][1] == 't') {
328 get_tree = 1;
329 } else if (argv[arg][1] == 'c') {
330 get_history = 1;
331 } else if (argv[arg][1] == 'a') {
332 get_all = 1;
333 get_tree = 1;
334 get_history = 1;
335 } else if (argv[arg][1] == 'v') {
336 get_verbosely = 1;
337 } else if (argv[arg][1] == 'w') {
338 write_ref = argv[arg + 1];
339 arg++;
341 arg++;
343 if (argc < arg + 2) {
344 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
345 return 1;
347 commit_id = argv[arg];
348 url = argv[arg + 1];
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);
358 #endif
360 base = url;
362 if (pull(commit_id))
363 return 1;
365 curl_global_cleanup();
366 return 0;