Add --signoff and --verify option to git commit.
[git/dkf.git] / http-pull.c
blobe70ab39c03dc82e7f90afa43f8271ca4e3ae8206
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 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)
80 char *filename;
81 char *url;
83 FILE *indexfile;
85 if (has_pack_index(sha1))
86 return 0;
88 if (get_verbosely)
89 fprintf(stderr, "Getting index for pack %s\n",
90 sha1_to_hex(sha1));
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");
98 if (!indexfile)
99 return error("Unable to open local file %s for pack index",
100 filename);
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)) {
107 fclose(indexfile);
108 return error("Unable to get pack index %s", url);
111 fclose(indexfile);
112 return 0;
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))
122 return -1;
124 new_pack = parse_pack_index(sha1);
125 new_pack->next = packs;
126 packs = new_pack;
127 return 0;
130 static int fetch_indices(void)
132 unsigned char sha1[20];
133 char *url;
134 struct buffer buffer;
135 char *data;
136 int i = 0;
138 if (got_indices)
139 return 0;
141 data = xmalloc(4096);
142 buffer.size = 4096;
143 buffer.posn = 0;
144 buffer.buffer = data;
146 if (get_verbosely)
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);
160 do {
161 switch (data[i]) {
162 case 'P':
163 i++;
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);
168 setup_index(sha1);
169 i += 51;
170 break;
172 default:
173 while (data[i] != '\n')
174 i++;
176 i++;
177 } while (i < buffer.posn);
179 got_indices = 1;
180 return 0;
183 static int fetch_pack(unsigned char *sha1)
185 char *url;
186 struct packed_git *target;
187 struct packed_git **lst;
188 FILE *packfile;
189 char *filename;
191 if (fetch_indices())
192 return -1;
193 target = find_sha1_pack(sha1, packs);
194 if (!target)
195 return error("Couldn't get %s: not separate or in any pack",
196 sha1_to_hex(sha1));
198 if (get_verbosely) {
199 fprintf(stderr, "Getting pack %s\n",
200 sha1_to_hex(target->sha1));
201 fprintf(stderr, " which contains %s\n",
202 sha1_to_hex(sha1));
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");
211 if (!packfile)
212 return error("Unable to open local file %s for pack",
213 filename);
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)) {
220 fclose(packfile);
221 return error("Unable to get pack file %s", url);
224 fclose(packfile);
226 lst = &packs;
227 while (*lst != target)
228 lst = &((*lst)->next);
229 *lst = (*lst)->next;
231 install_packed_git(target);
233 return 0;
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];
241 char *url;
242 char *posn;
244 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
246 if (local < 0)
247 return error("Couldn't open local object %s\n", filename);
249 memset(&stream, 0, sizeof(stream));
251 inflateInit(&stream);
253 SHA1_Init(&c);
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);
260 strcpy(url, base);
261 posn = url + strlen(base);
262 strcpy(posn, "objects/");
263 posn += 8;
264 memcpy(posn, hex, 2);
265 posn += 2;
266 *(posn++) = '/';
267 strcpy(posn, hex + 2);
269 curl_easy_setopt(curl, CURLOPT_URL, url);
271 if (curl_easy_perform(curl)) {
272 unlink(filename);
273 if (fetch_pack(sha1))
274 return error("Tried %s", url);
275 return 0;
278 close(local);
279 inflateEnd(&stream);
280 SHA1_Final(real_sha1, &c);
281 if (zret != Z_STREAM_END) {
282 unlink(filename);
283 return error("File %s (%s) corrupt\n", hex, url);
285 if (memcmp(sha1, real_sha1, 20)) {
286 unlink(filename);
287 return error("File %s has bad hash\n", hex);
290 pull_say("got %s\n", hex);
291 return 0;
294 int fetch_ref(char *ref, unsigned char *sha1)
296 char *url, *posn;
297 char hex[42];
298 struct buffer buffer;
299 buffer.size = 41;
300 buffer.posn = 0;
301 buffer.buffer = hex;
302 hex[41] = '\0';
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));
308 strcpy(url, base);
309 posn = url + strlen(base);
310 strcpy(posn, "refs/");
311 posn += 5;
312 strcpy(posn, ref);
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);
319 hex[40] = '\0';
320 get_sha1_hex(hex, sha1);
321 return 0;
324 int main(int argc, char **argv)
326 char *commit_id;
327 char *url;
328 int arg = 1;
330 while (arg < argc && argv[arg][0] == '-') {
331 if (argv[arg][1] == 't') {
332 get_tree = 1;
333 } else if (argv[arg][1] == 'c') {
334 get_history = 1;
335 } else if (argv[arg][1] == 'a') {
336 get_all = 1;
337 get_tree = 1;
338 get_history = 1;
339 } else if (argv[arg][1] == 'v') {
340 get_verbosely = 1;
341 } else if (argv[arg][1] == 'w') {
342 write_ref = argv[arg + 1];
343 arg++;
345 arg++;
347 if (argc < arg + 2) {
348 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
349 return 1;
351 commit_id = argv[arg];
352 url = argv[arg + 1];
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);
362 #endif
364 base = url;
366 if (pull(commit_id))
367 return 1;
369 curl_global_cleanup();
370 return 0;