[PATCH] Object library enhancements
[git/git.git] / http-pull.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "commit.h"
6eb7ed54 3
4250a5e5
DB
4#include "pull.h"
5
6eb7ed54
DB
6#include <curl/curl.h>
7#include <curl/easy.h>
8
4a30976e
JS
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
4a30976e 15
6eb7ed54
DB
16static CURL *curl;
17
18static char *base;
19
6eb7ed54
DB
20static SHA_CTX c;
21static z_stream stream;
22
23static int local;
24static int zret;
25
3dcb90f5
DT
26static int curl_ssl_verify;
27
fa3e0655
DB
28struct buffer
29{
30 size_t posn;
31 size_t size;
32 void *buffer;
33};
34
35static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
182005b9
DB
36 struct buffer *buffer)
37{
fa3e0655
DB
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;
44}
45
182005b9
DB
46static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
47 void *data)
48{
bf0f910d 49 unsigned char expn[4096];
6eb7ed54
DB
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);
58
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;
68}
69
182005b9
DB
70static int got_indices = 0;
71
72static struct packed_git *packs = NULL;
73
74static int fetch_index(unsigned char *sha1)
75{
76 char *filename;
77 char *url;
78
79 FILE *indexfile;
80
81 if (has_pack_index(sha1))
82 return 0;
83
84 if (get_verbosely)
85 fprintf(stderr, "Getting index for pack %s\n",
86 sha1_to_hex(sha1));
87
88 url = xmalloc(strlen(base) + 64);
89 sprintf(url, "%s/objects/pack/pack-%s.idx",
90 base, sha1_to_hex(sha1));
91
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);
97
98 curl_easy_setopt(curl, CURLOPT_FILE, indexfile);
99 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
100 curl_easy_setopt(curl, CURLOPT_URL, url);
101
102 if (curl_easy_perform(curl)) {
103 fclose(indexfile);
104 return error("Unable to get pack index %s", url);
105 }
106
107 fclose(indexfile);
108 return 0;
109}
110
111static int setup_index(unsigned char *sha1)
112{
113 struct packed_git *new_pack;
114 if (has_pack_file(sha1))
115 return 0; // don't list this as something we can get
116
117 if (fetch_index(sha1))
118 return -1;
119
120 new_pack = parse_pack_index(sha1);
121 new_pack->next = packs;
122 packs = new_pack;
123 return 0;
124}
125
126static int fetch_indices(void)
127{
128 unsigned char sha1[20];
129 char *url;
130 struct buffer buffer;
131 char *data;
132 int i = 0;
133
134 if (got_indices)
135 return 0;
136
137 data = xmalloc(4096);
138 buffer.size = 4096;
139 buffer.posn = 0;
140 buffer.buffer = data;
141
142 if (get_verbosely)
143 fprintf(stderr, "Getting pack list\n");
144
145 url = xmalloc(strlen(base) + 21);
146 sprintf(url, "%s/objects/info/packs", base);
147
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);
151
152 if (curl_easy_perform(curl)) {
153 return error("Unable to get pack index %s", url);
154 }
155
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;
167 }
168 default:
169 while (data[i] != '\n')
170 i++;
171 }
172 i++;
173 } while (i < buffer.posn);
174
175 got_indices = 1;
176 return 0;
177}
178
179static int fetch_pack(unsigned char *sha1)
180{
181 char *url;
182 struct packed_git *target;
183 struct packed_git **lst;
184 FILE *packfile;
185 char *filename;
186
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));
193
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));
199 }
200
201 url = xmalloc(strlen(base) + 65);
202 sprintf(url, "%s/objects/pack/pack-%s.pack",
203 base, sha1_to_hex(target->sha1));
204
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);
210
211 curl_easy_setopt(curl, CURLOPT_FILE, packfile);
212 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
213 curl_easy_setopt(curl, CURLOPT_URL, url);
214
215 if (curl_easy_perform(curl)) {
216 fclose(packfile);
217 return error("Unable to get pack file %s", url);
218 }
219
220 fclose(packfile);
221
222 lst = &packs;
223 while (*lst != target)
224 lst = &((*lst)->next);
225 *lst = (*lst)->next;
226
227 install_packed_git(target);
228
229 return 0;
230}
231
4250a5e5 232int fetch(unsigned char *sha1)
6eb7ed54
DB
233{
234 char *hex = sha1_to_hex(sha1);
235 char *filename = sha1_file_name(sha1);
bf0f910d 236 unsigned char real_sha1[20];
6eb7ed54
DB
237 char *url;
238 char *posn;
239
6eb7ed54
DB
240 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
241
242 if (local < 0)
182005b9 243 return error("Couldn't open local object %s\n", filename);
6eb7ed54
DB
244
245 memset(&stream, 0, sizeof(stream));
246
247 inflateInit(&stream);
248
249 SHA1_Init(&c);
250
182005b9 251 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
6eb7ed54
DB
252 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
253 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
254
812666c8 255 url = xmalloc(strlen(base) + 50);
6eb7ed54
DB
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);
264
265 curl_easy_setopt(curl, CURLOPT_URL, url);
266
182005b9
DB
267 if (curl_easy_perform(curl)) {
268 unlink(filename);
269 if (fetch_pack(sha1))
270 return error("Tried %s", url);
271 return 0;
272 }
6eb7ed54
DB
273
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);
280 }
281 if (memcmp(sha1, real_sha1, 20)) {
282 unlink(filename);
283 return error("File %s has bad hash\n", hex);
284 }
285
e78d9772 286 pull_say("got %s\n", hex);
6eb7ed54
DB
287 return 0;
288}
289
cd541a68
DB
290int fetch_ref(char *ref, unsigned char *sha1)
291{
fa3e0655
DB
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';
299
300 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
301 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
302
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);
309
310 curl_easy_setopt(curl, CURLOPT_URL, url);
311
312 if (curl_easy_perform(curl))
313 return error("Couldn't get %s for %s\n", url, ref);
314
315 hex[40] = '\0';
316 get_sha1_hex(hex, sha1);
317 return 0;
cd541a68
DB
318}
319
6eb7ed54
DB
320int main(int argc, char **argv)
321{
322 char *commit_id;
323 char *url;
324 int arg = 1;
6eb7ed54
DB
325
326 while (arg < argc && argv[arg][0] == '-') {
327 if (argv[arg][1] == 't') {
4250a5e5 328 get_tree = 1;
6eb7ed54 329 } else if (argv[arg][1] == 'c') {
4250a5e5 330 get_history = 1;
6eb7ed54 331 } else if (argv[arg][1] == 'a') {
4250a5e5
DB
332 get_all = 1;
333 get_tree = 1;
334 get_history = 1;
e78d9772
JH
335 } else if (argv[arg][1] == 'v') {
336 get_verbosely = 1;
fa3e0655
DB
337 } else if (argv[arg][1] == 'w') {
338 write_ref = argv[arg + 1];
339 arg++;
6eb7ed54
DB
340 }
341 arg++;
342 }
343 if (argc < arg + 2) {
fa3e0655 344 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
6eb7ed54
DB
345 return 1;
346 }
347 commit_id = argv[arg];
348 url = argv[arg + 1];
349
6eb7ed54
DB
350 curl_global_init(CURL_GLOBAL_ALL);
351
352 curl = curl_easy_init();
353
3dcb90f5
DT
354 curl_ssl_verify = gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
355 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
9f6cf65e 356#if LIBCURL_VERSION_NUM >= 0x070907
3dcb90f5 357 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
9f6cf65e 358#endif
3dcb90f5 359
6eb7ed54
DB
360 base = url;
361
4250a5e5 362 if (pull(commit_id))
6eb7ed54
DB
363 return 1;
364
365 curl_global_cleanup();
366 return 0;
367}