]> git.ipfire.org Git - thirdparty/git.git/blob - http-push.c
Merge branch 'rc/maint-http-local-slot-fix'
[thirdparty/git.git] / http-push.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 #include "tag.h"
5 #include "blob.h"
6 #include "http.h"
7 #include "refs.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "exec_cmd.h"
11 #include "remote.h"
12 #include "list-objects.h"
13 #include "sigchain.h"
14
15 #include <expat.h>
16
17 static const char http_push_usage[] =
18 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
19
20 #ifndef XML_STATUS_OK
21 enum XML_Status {
22 XML_STATUS_OK = 1,
23 XML_STATUS_ERROR = 0
24 };
25 #define XML_STATUS_OK 1
26 #define XML_STATUS_ERROR 0
27 #endif
28
29 #define PREV_BUF_SIZE 4096
30 #define RANGE_HEADER_SIZE 30
31
32 /* DAV methods */
33 #define DAV_LOCK "LOCK"
34 #define DAV_MKCOL "MKCOL"
35 #define DAV_MOVE "MOVE"
36 #define DAV_PROPFIND "PROPFIND"
37 #define DAV_PUT "PUT"
38 #define DAV_UNLOCK "UNLOCK"
39 #define DAV_DELETE "DELETE"
40
41 /* DAV lock flags */
42 #define DAV_PROP_LOCKWR (1u << 0)
43 #define DAV_PROP_LOCKEX (1u << 1)
44 #define DAV_LOCK_OK (1u << 2)
45
46 /* DAV XML properties */
47 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
48 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
49 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
50 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
51 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
52 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
53 #define DAV_PROPFIND_RESP ".multistatus.response"
54 #define DAV_PROPFIND_NAME ".multistatus.response.href"
55 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
56
57 /* DAV request body templates */
58 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
59 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
60 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
61
62 #define LOCK_TIME 600
63 #define LOCK_REFRESH 30
64
65 /* bits #0-15 in revision.h */
66
67 #define LOCAL (1u<<16)
68 #define REMOTE (1u<<17)
69 #define FETCHING (1u<<18)
70 #define PUSHING (1u<<19)
71
72 /* We allow "recursive" symbolic refs. Only within reason, though */
73 #define MAXDEPTH 5
74
75 static int pushing;
76 static int aborted;
77 static signed char remote_dir_exists[256];
78
79 static struct curl_slist *no_pragma_header;
80
81 static int push_verbosely;
82 static int push_all = MATCH_REFS_NONE;
83 static int force_all;
84 static int dry_run;
85
86 static struct object_list *objects;
87
88 struct repo
89 {
90 char *url;
91 char *path;
92 int path_len;
93 int has_info_refs;
94 int can_update_info_refs;
95 int has_info_packs;
96 struct packed_git *packs;
97 struct remote_lock *locks;
98 };
99
100 static struct repo *repo;
101
102 enum transfer_state {
103 NEED_FETCH,
104 RUN_FETCH_LOOSE,
105 RUN_FETCH_PACKED,
106 NEED_PUSH,
107 RUN_MKCOL,
108 RUN_PUT,
109 RUN_MOVE,
110 ABORTED,
111 COMPLETE,
112 };
113
114 struct transfer_request
115 {
116 struct object *obj;
117 char *url;
118 char *dest;
119 struct remote_lock *lock;
120 struct curl_slist *headers;
121 struct buffer buffer;
122 char filename[PATH_MAX];
123 char tmpfile[PATH_MAX];
124 int local_fileno;
125 FILE *local_stream;
126 enum transfer_state state;
127 CURLcode curl_result;
128 char errorstr[CURL_ERROR_SIZE];
129 long http_code;
130 unsigned char real_sha1[20];
131 git_SHA_CTX c;
132 z_stream stream;
133 int zret;
134 int rename;
135 void *userData;
136 struct active_request_slot *slot;
137 struct transfer_request *next;
138 };
139
140 static struct transfer_request *request_queue_head;
141
142 struct xml_ctx
143 {
144 char *name;
145 int len;
146 char *cdata;
147 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
148 void *userData;
149 };
150
151 struct remote_lock
152 {
153 char *url;
154 char *owner;
155 char *token;
156 char tmpfile_suffix[41];
157 time_t start_time;
158 long timeout;
159 int refreshing;
160 struct remote_lock *next;
161 };
162
163 /* Flags that control remote_ls processing */
164 #define PROCESS_FILES (1u << 0)
165 #define PROCESS_DIRS (1u << 1)
166 #define RECURSIVE (1u << 2)
167
168 /* Flags that remote_ls passes to callback functions */
169 #define IS_DIR (1u << 0)
170
171 struct remote_ls_ctx
172 {
173 char *path;
174 void (*userFunc)(struct remote_ls_ctx *ls);
175 void *userData;
176 int flags;
177 char *dentry_name;
178 int dentry_flags;
179 struct remote_ls_ctx *parent;
180 };
181
182 /* get_dav_token_headers options */
183 enum dav_header_flag {
184 DAV_HEADER_IF = (1u << 0),
185 DAV_HEADER_LOCK = (1u << 1),
186 DAV_HEADER_TIMEOUT = (1u << 2)
187 };
188
189 static char *xml_entities(char *s)
190 {
191 struct strbuf buf = STRBUF_INIT;
192 while (*s) {
193 size_t len = strcspn(s, "\"<>&");
194 strbuf_add(&buf, s, len);
195 s += len;
196 switch (*s) {
197 case '"':
198 strbuf_addstr(&buf, "&quot;");
199 break;
200 case '<':
201 strbuf_addstr(&buf, "&lt;");
202 break;
203 case '>':
204 strbuf_addstr(&buf, "&gt;");
205 break;
206 case '&':
207 strbuf_addstr(&buf, "&amp;");
208 break;
209 }
210 s++;
211 }
212 return strbuf_detach(&buf, NULL);
213 }
214
215 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
216 {
217 struct strbuf buf = STRBUF_INIT;
218 struct curl_slist *dav_headers = NULL;
219
220 if (options & DAV_HEADER_IF) {
221 strbuf_addf(&buf, "If: (<%s>)", lock->token);
222 dav_headers = curl_slist_append(dav_headers, buf.buf);
223 strbuf_reset(&buf);
224 }
225 if (options & DAV_HEADER_LOCK) {
226 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
227 dav_headers = curl_slist_append(dav_headers, buf.buf);
228 strbuf_reset(&buf);
229 }
230 if (options & DAV_HEADER_TIMEOUT) {
231 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
232 dav_headers = curl_slist_append(dav_headers, buf.buf);
233 strbuf_reset(&buf);
234 }
235 strbuf_release(&buf);
236
237 return dav_headers;
238 }
239
240 static void append_remote_object_url(struct strbuf *buf, const char *url,
241 const char *hex,
242 int only_two_digit_prefix)
243 {
244 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
245 if (!only_two_digit_prefix)
246 strbuf_addf(buf, "%s", hex+2);
247 }
248
249 static void finish_request(struct transfer_request *request);
250 static void release_request(struct transfer_request *request);
251
252 static void process_response(void *callback_data)
253 {
254 struct transfer_request *request =
255 (struct transfer_request *)callback_data;
256
257 finish_request(request);
258 }
259
260 #ifdef USE_CURL_MULTI
261
262 static char *get_remote_object_url(const char *url, const char *hex,
263 int only_two_digit_prefix)
264 {
265 struct strbuf buf = STRBUF_INIT;
266 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
267 return strbuf_detach(&buf, NULL);
268 }
269
270 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
271 void *data)
272 {
273 unsigned char expn[4096];
274 size_t size = eltsize * nmemb;
275 int posn = 0;
276 struct transfer_request *request = (struct transfer_request *)data;
277 do {
278 ssize_t retval = xwrite(request->local_fileno,
279 (char *) ptr + posn, size - posn);
280 if (retval < 0)
281 return posn;
282 posn += retval;
283 } while (posn < size);
284
285 request->stream.avail_in = size;
286 request->stream.next_in = ptr;
287 do {
288 request->stream.next_out = expn;
289 request->stream.avail_out = sizeof(expn);
290 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
291 git_SHA1_Update(&request->c, expn,
292 sizeof(expn) - request->stream.avail_out);
293 } while (request->stream.avail_in && request->zret == Z_OK);
294 data_received++;
295 return size;
296 }
297
298 static void start_fetch_loose(struct transfer_request *request)
299 {
300 char *hex = sha1_to_hex(request->obj->sha1);
301 char *filename;
302 char prevfile[PATH_MAX];
303 char *url;
304 int prevlocal;
305 unsigned char prev_buf[PREV_BUF_SIZE];
306 ssize_t prev_read = 0;
307 long prev_posn = 0;
308 char range[RANGE_HEADER_SIZE];
309 struct curl_slist *range_header = NULL;
310 struct active_request_slot *slot;
311
312 filename = sha1_file_name(request->obj->sha1);
313 snprintf(request->filename, sizeof(request->filename), "%s", filename);
314 snprintf(request->tmpfile, sizeof(request->tmpfile),
315 "%s.temp", filename);
316
317 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
318 unlink_or_warn(prevfile);
319 rename(request->tmpfile, prevfile);
320 unlink_or_warn(request->tmpfile);
321
322 if (request->local_fileno != -1)
323 error("fd leakage in start: %d", request->local_fileno);
324 request->local_fileno = open(request->tmpfile,
325 O_WRONLY | O_CREAT | O_EXCL, 0666);
326 /* This could have failed due to the "lazy directory creation";
327 * try to mkdir the last path component.
328 */
329 if (request->local_fileno < 0 && errno == ENOENT) {
330 char *dir = strrchr(request->tmpfile, '/');
331 if (dir) {
332 *dir = 0;
333 mkdir(request->tmpfile, 0777);
334 *dir = '/';
335 }
336 request->local_fileno = open(request->tmpfile,
337 O_WRONLY | O_CREAT | O_EXCL, 0666);
338 }
339
340 if (request->local_fileno < 0) {
341 request->state = ABORTED;
342 error("Couldn't create temporary file %s for %s: %s",
343 request->tmpfile, request->filename, strerror(errno));
344 return;
345 }
346
347 memset(&request->stream, 0, sizeof(request->stream));
348
349 git_inflate_init(&request->stream);
350
351 git_SHA1_Init(&request->c);
352
353 url = get_remote_object_url(repo->url, hex, 0);
354 request->url = xstrdup(url);
355
356 /* If a previous temp file is present, process what was already
357 fetched. */
358 prevlocal = open(prevfile, O_RDONLY);
359 if (prevlocal != -1) {
360 do {
361 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
362 if (prev_read>0) {
363 if (fwrite_sha1_file(prev_buf,
364 1,
365 prev_read,
366 request) == prev_read) {
367 prev_posn += prev_read;
368 } else {
369 prev_read = -1;
370 }
371 }
372 } while (prev_read > 0);
373 close(prevlocal);
374 }
375 unlink_or_warn(prevfile);
376
377 /* Reset inflate/SHA1 if there was an error reading the previous temp
378 file; also rewind to the beginning of the local file. */
379 if (prev_read == -1) {
380 memset(&request->stream, 0, sizeof(request->stream));
381 git_inflate_init(&request->stream);
382 git_SHA1_Init(&request->c);
383 if (prev_posn>0) {
384 prev_posn = 0;
385 lseek(request->local_fileno, 0, SEEK_SET);
386 ftruncate(request->local_fileno, 0);
387 }
388 }
389
390 slot = get_active_slot();
391 slot->callback_func = process_response;
392 slot->callback_data = request;
393 request->slot = slot;
394
395 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
396 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
397 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
398 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
399 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
400
401 /* If we have successfully processed data from a previous fetch
402 attempt, only fetch the data we don't already have. */
403 if (prev_posn>0) {
404 if (push_verbosely)
405 fprintf(stderr,
406 "Resuming fetch of object %s at byte %ld\n",
407 hex, prev_posn);
408 sprintf(range, "Range: bytes=%ld-", prev_posn);
409 range_header = curl_slist_append(range_header, range);
410 curl_easy_setopt(slot->curl,
411 CURLOPT_HTTPHEADER, range_header);
412 }
413
414 /* Try to get the request started, abort the request on error */
415 request->state = RUN_FETCH_LOOSE;
416 if (!start_active_slot(slot)) {
417 fprintf(stderr, "Unable to start GET request\n");
418 repo->can_update_info_refs = 0;
419 release_request(request);
420 }
421 }
422
423 static void start_mkcol(struct transfer_request *request)
424 {
425 char *hex = sha1_to_hex(request->obj->sha1);
426 struct active_request_slot *slot;
427
428 request->url = get_remote_object_url(repo->url, hex, 1);
429
430 slot = get_active_slot();
431 slot->callback_func = process_response;
432 slot->callback_data = request;
433 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
434 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
435 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
436 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
437 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
438
439 if (start_active_slot(slot)) {
440 request->slot = slot;
441 request->state = RUN_MKCOL;
442 } else {
443 request->state = ABORTED;
444 free(request->url);
445 request->url = NULL;
446 }
447 }
448 #endif
449
450 static void start_fetch_packed(struct transfer_request *request)
451 {
452 char *url;
453 struct packed_git *target;
454 FILE *packfile;
455 char *filename;
456 long prev_posn = 0;
457 char range[RANGE_HEADER_SIZE];
458 struct curl_slist *range_header = NULL;
459
460 struct transfer_request *check_request = request_queue_head;
461 struct active_request_slot *slot;
462
463 target = find_sha1_pack(request->obj->sha1, repo->packs);
464 if (!target) {
465 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
466 repo->can_update_info_refs = 0;
467 release_request(request);
468 return;
469 }
470
471 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
472 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
473
474 filename = sha1_pack_name(target->sha1);
475 snprintf(request->filename, sizeof(request->filename), "%s", filename);
476 snprintf(request->tmpfile, sizeof(request->tmpfile),
477 "%s.temp", filename);
478
479 url = xmalloc(strlen(repo->url) + 64);
480 sprintf(url, "%sobjects/pack/pack-%s.pack",
481 repo->url, sha1_to_hex(target->sha1));
482
483 /* Make sure there isn't another open request for this pack */
484 while (check_request) {
485 if (check_request->state == RUN_FETCH_PACKED &&
486 !strcmp(check_request->url, url)) {
487 free(url);
488 release_request(request);
489 return;
490 }
491 check_request = check_request->next;
492 }
493
494 packfile = fopen(request->tmpfile, "a");
495 if (!packfile) {
496 fprintf(stderr, "Unable to open local file %s for pack",
497 request->tmpfile);
498 repo->can_update_info_refs = 0;
499 free(url);
500 return;
501 }
502
503 slot = get_active_slot();
504 slot->callback_func = process_response;
505 slot->callback_data = request;
506 request->slot = slot;
507 request->local_stream = packfile;
508 request->userData = target;
509
510 request->url = url;
511 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
512 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
513 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
514 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
515 slot->local = packfile;
516
517 /* If there is data present from a previous transfer attempt,
518 resume where it left off */
519 prev_posn = ftell(packfile);
520 if (prev_posn>0) {
521 if (push_verbosely)
522 fprintf(stderr,
523 "Resuming fetch of pack %s at byte %ld\n",
524 sha1_to_hex(target->sha1), prev_posn);
525 sprintf(range, "Range: bytes=%ld-", prev_posn);
526 range_header = curl_slist_append(range_header, range);
527 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
528 }
529
530 /* Try to get the request started, abort the request on error */
531 request->state = RUN_FETCH_PACKED;
532 if (!start_active_slot(slot)) {
533 fprintf(stderr, "Unable to start GET request\n");
534 repo->can_update_info_refs = 0;
535 release_request(request);
536 }
537 }
538
539 static void start_put(struct transfer_request *request)
540 {
541 char *hex = sha1_to_hex(request->obj->sha1);
542 struct active_request_slot *slot;
543 struct strbuf buf = STRBUF_INIT;
544 enum object_type type;
545 char hdr[50];
546 void *unpacked;
547 unsigned long len;
548 int hdrlen;
549 ssize_t size;
550 z_stream stream;
551
552 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
553 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
554
555 /* Set it up */
556 memset(&stream, 0, sizeof(stream));
557 deflateInit(&stream, zlib_compression_level);
558 size = deflateBound(&stream, len + hdrlen);
559 strbuf_init(&request->buffer.buf, size);
560 request->buffer.posn = 0;
561
562 /* Compress it */
563 stream.next_out = (unsigned char *)request->buffer.buf.buf;
564 stream.avail_out = size;
565
566 /* First header.. */
567 stream.next_in = (void *)hdr;
568 stream.avail_in = hdrlen;
569 while (deflate(&stream, 0) == Z_OK)
570 /* nothing */;
571
572 /* Then the data itself.. */
573 stream.next_in = unpacked;
574 stream.avail_in = len;
575 while (deflate(&stream, Z_FINISH) == Z_OK)
576 /* nothing */;
577 deflateEnd(&stream);
578 free(unpacked);
579
580 request->buffer.buf.len = stream.total_out;
581
582 strbuf_addstr(&buf, "Destination: ");
583 append_remote_object_url(&buf, repo->url, hex, 0);
584 request->dest = strbuf_detach(&buf, NULL);
585
586 append_remote_object_url(&buf, repo->url, hex, 0);
587 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
588 request->url = strbuf_detach(&buf, NULL);
589
590 slot = get_active_slot();
591 slot->callback_func = process_response;
592 slot->callback_data = request;
593 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
594 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
595 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
596 #ifndef NO_CURL_IOCTL
597 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
598 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
599 #endif
600 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
601 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
602 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
603 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
604 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
605 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
606
607 if (start_active_slot(slot)) {
608 request->slot = slot;
609 request->state = RUN_PUT;
610 } else {
611 request->state = ABORTED;
612 free(request->url);
613 request->url = NULL;
614 }
615 }
616
617 static void start_move(struct transfer_request *request)
618 {
619 struct active_request_slot *slot;
620 struct curl_slist *dav_headers = NULL;
621
622 slot = get_active_slot();
623 slot->callback_func = process_response;
624 slot->callback_data = request;
625 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
626 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
627 dav_headers = curl_slist_append(dav_headers, request->dest);
628 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
629 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
630 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
631 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
632
633 if (start_active_slot(slot)) {
634 request->slot = slot;
635 request->state = RUN_MOVE;
636 } else {
637 request->state = ABORTED;
638 free(request->url);
639 request->url = NULL;
640 }
641 }
642
643 static int refresh_lock(struct remote_lock *lock)
644 {
645 struct active_request_slot *slot;
646 struct slot_results results;
647 struct curl_slist *dav_headers;
648 int rc = 0;
649
650 lock->refreshing = 1;
651
652 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
653
654 slot = get_active_slot();
655 slot->results = &results;
656 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
657 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
658 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
659 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
660 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
661
662 if (start_active_slot(slot)) {
663 run_active_slot(slot);
664 if (results.curl_result != CURLE_OK) {
665 fprintf(stderr, "LOCK HTTP error %ld\n",
666 results.http_code);
667 } else {
668 lock->start_time = time(NULL);
669 rc = 1;
670 }
671 }
672
673 lock->refreshing = 0;
674 curl_slist_free_all(dav_headers);
675
676 return rc;
677 }
678
679 static void check_locks(void)
680 {
681 struct remote_lock *lock = repo->locks;
682 time_t current_time = time(NULL);
683 int time_remaining;
684
685 while (lock) {
686 time_remaining = lock->start_time + lock->timeout -
687 current_time;
688 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
689 if (!refresh_lock(lock)) {
690 fprintf(stderr,
691 "Unable to refresh lock for %s\n",
692 lock->url);
693 aborted = 1;
694 return;
695 }
696 }
697 lock = lock->next;
698 }
699 }
700
701 static void release_request(struct transfer_request *request)
702 {
703 struct transfer_request *entry = request_queue_head;
704
705 if (request == request_queue_head) {
706 request_queue_head = request->next;
707 } else {
708 while (entry->next != NULL && entry->next != request)
709 entry = entry->next;
710 if (entry->next == request)
711 entry->next = entry->next->next;
712 }
713
714 if (request->local_fileno != -1)
715 close(request->local_fileno);
716 if (request->local_stream)
717 fclose(request->local_stream);
718 free(request->url);
719 free(request);
720 }
721
722 static void finish_request(struct transfer_request *request)
723 {
724 struct stat st;
725 struct packed_git *target;
726 struct packed_git **lst;
727 struct active_request_slot *slot;
728
729 request->curl_result = request->slot->curl_result;
730 request->http_code = request->slot->http_code;
731 slot = request->slot;
732 request->slot = NULL;
733
734 /* Keep locks active */
735 check_locks();
736
737 if (request->headers != NULL)
738 curl_slist_free_all(request->headers);
739
740 /* URL is reused for MOVE after PUT */
741 if (request->state != RUN_PUT) {
742 free(request->url);
743 request->url = NULL;
744 }
745
746 if (request->state == RUN_MKCOL) {
747 if (request->curl_result == CURLE_OK ||
748 request->http_code == 405) {
749 remote_dir_exists[request->obj->sha1[0]] = 1;
750 start_put(request);
751 } else {
752 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
753 sha1_to_hex(request->obj->sha1),
754 request->curl_result, request->http_code);
755 request->state = ABORTED;
756 aborted = 1;
757 }
758 } else if (request->state == RUN_PUT) {
759 if (request->curl_result == CURLE_OK) {
760 start_move(request);
761 } else {
762 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
763 sha1_to_hex(request->obj->sha1),
764 request->curl_result, request->http_code);
765 request->state = ABORTED;
766 aborted = 1;
767 }
768 } else if (request->state == RUN_MOVE) {
769 if (request->curl_result == CURLE_OK) {
770 if (push_verbosely)
771 fprintf(stderr, " sent %s\n",
772 sha1_to_hex(request->obj->sha1));
773 request->obj->flags |= REMOTE;
774 release_request(request);
775 } else {
776 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
777 sha1_to_hex(request->obj->sha1),
778 request->curl_result, request->http_code);
779 request->state = ABORTED;
780 aborted = 1;
781 }
782 } else if (request->state == RUN_FETCH_LOOSE) {
783 close(request->local_fileno); request->local_fileno = -1;
784
785 if (request->curl_result != CURLE_OK &&
786 request->http_code != 416) {
787 if (stat(request->tmpfile, &st) == 0) {
788 if (st.st_size == 0)
789 unlink_or_warn(request->tmpfile);
790 }
791 } else {
792 if (request->http_code == 416)
793 warning("requested range invalid; we may already have all the data.");
794
795 git_inflate_end(&request->stream);
796 git_SHA1_Final(request->real_sha1, &request->c);
797 if (request->zret != Z_STREAM_END) {
798 unlink_or_warn(request->tmpfile);
799 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
800 unlink_or_warn(request->tmpfile);
801 } else {
802 request->rename =
803 move_temp_to_file(
804 request->tmpfile,
805 request->filename);
806 if (request->rename == 0) {
807 request->obj->flags |= (LOCAL | REMOTE);
808 }
809 }
810 }
811
812 /* Try fetching packed if necessary */
813 if (request->obj->flags & LOCAL)
814 release_request(request);
815 else
816 start_fetch_packed(request);
817
818 } else if (request->state == RUN_FETCH_PACKED) {
819 if (request->curl_result != CURLE_OK) {
820 fprintf(stderr, "Unable to get pack file %s\n%s",
821 request->url, curl_errorstr);
822 repo->can_update_info_refs = 0;
823 } else {
824 off_t pack_size = ftell(request->local_stream);
825
826 fclose(request->local_stream);
827 request->local_stream = NULL;
828 slot->local = NULL;
829 if (!move_temp_to_file(request->tmpfile,
830 request->filename)) {
831 target = (struct packed_git *)request->userData;
832 target->pack_size = pack_size;
833 lst = &repo->packs;
834 while (*lst != target)
835 lst = &((*lst)->next);
836 *lst = (*lst)->next;
837
838 if (!verify_pack(target))
839 install_packed_git(target);
840 else
841 repo->can_update_info_refs = 0;
842 }
843 }
844 release_request(request);
845 }
846 }
847
848 #ifdef USE_CURL_MULTI
849 static int fill_active_slot(void *unused)
850 {
851 struct transfer_request *request;
852
853 if (aborted)
854 return 0;
855
856 for (request = request_queue_head; request; request = request->next) {
857 if (request->state == NEED_FETCH) {
858 start_fetch_loose(request);
859 return 1;
860 } else if (pushing && request->state == NEED_PUSH) {
861 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
862 start_put(request);
863 } else {
864 start_mkcol(request);
865 }
866 return 1;
867 }
868 }
869 return 0;
870 }
871 #endif
872
873 static void get_remote_object_list(unsigned char parent);
874
875 static void add_fetch_request(struct object *obj)
876 {
877 struct transfer_request *request;
878
879 check_locks();
880
881 /*
882 * Don't fetch the object if it's known to exist locally
883 * or is already in the request queue
884 */
885 if (remote_dir_exists[obj->sha1[0]] == -1)
886 get_remote_object_list(obj->sha1[0]);
887 if (obj->flags & (LOCAL | FETCHING))
888 return;
889
890 obj->flags |= FETCHING;
891 request = xmalloc(sizeof(*request));
892 request->obj = obj;
893 request->url = NULL;
894 request->lock = NULL;
895 request->headers = NULL;
896 request->local_fileno = -1;
897 request->local_stream = NULL;
898 request->state = NEED_FETCH;
899 request->next = request_queue_head;
900 request_queue_head = request;
901
902 #ifdef USE_CURL_MULTI
903 fill_active_slots();
904 step_active_slots();
905 #endif
906 }
907
908 static int add_send_request(struct object *obj, struct remote_lock *lock)
909 {
910 struct transfer_request *request = request_queue_head;
911 struct packed_git *target;
912
913 /* Keep locks active */
914 check_locks();
915
916 /*
917 * Don't push the object if it's known to exist on the remote
918 * or is already in the request queue
919 */
920 if (remote_dir_exists[obj->sha1[0]] == -1)
921 get_remote_object_list(obj->sha1[0]);
922 if (obj->flags & (REMOTE | PUSHING))
923 return 0;
924 target = find_sha1_pack(obj->sha1, repo->packs);
925 if (target) {
926 obj->flags |= REMOTE;
927 return 0;
928 }
929
930 obj->flags |= PUSHING;
931 request = xmalloc(sizeof(*request));
932 request->obj = obj;
933 request->url = NULL;
934 request->lock = lock;
935 request->headers = NULL;
936 request->local_fileno = -1;
937 request->local_stream = NULL;
938 request->state = NEED_PUSH;
939 request->next = request_queue_head;
940 request_queue_head = request;
941
942 #ifdef USE_CURL_MULTI
943 fill_active_slots();
944 step_active_slots();
945 #endif
946
947 return 1;
948 }
949
950 static int fetch_index(unsigned char *sha1)
951 {
952 char *hex = sha1_to_hex(sha1);
953 char *filename;
954 char *url;
955 char tmpfile[PATH_MAX];
956 long prev_posn = 0;
957 char range[RANGE_HEADER_SIZE];
958 struct curl_slist *range_header = NULL;
959
960 FILE *indexfile;
961 struct active_request_slot *slot;
962 struct slot_results results;
963
964 /* Don't use the index if the pack isn't there */
965 url = xmalloc(strlen(repo->url) + 64);
966 sprintf(url, "%sobjects/pack/pack-%s.pack", repo->url, hex);
967 slot = get_active_slot();
968 slot->results = &results;
969 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
970 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
971 if (start_active_slot(slot)) {
972 run_active_slot(slot);
973 if (results.curl_result != CURLE_OK) {
974 free(url);
975 return error("Unable to verify pack %s is available",
976 hex);
977 }
978 } else {
979 free(url);
980 return error("Unable to start request");
981 }
982
983 if (has_pack_index(sha1)) {
984 free(url);
985 return 0;
986 }
987
988 if (push_verbosely)
989 fprintf(stderr, "Getting index for pack %s\n", hex);
990
991 sprintf(url, "%sobjects/pack/pack-%s.idx", repo->url, hex);
992
993 filename = sha1_pack_index_name(sha1);
994 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
995 indexfile = fopen(tmpfile, "a");
996 if (!indexfile) {
997 free(url);
998 return error("Unable to open local file %s for pack index",
999 tmpfile);
1000 }
1001
1002 slot = get_active_slot();
1003 slot->results = &results;
1004 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1005 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1006 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
1007 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1008 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1009 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1010 slot->local = indexfile;
1011
1012 /* If there is data present from a previous transfer attempt,
1013 resume where it left off */
1014 prev_posn = ftell(indexfile);
1015 if (prev_posn>0) {
1016 if (push_verbosely)
1017 fprintf(stderr,
1018 "Resuming fetch of index for pack %s at byte %ld\n",
1019 hex, prev_posn);
1020 sprintf(range, "Range: bytes=%ld-", prev_posn);
1021 range_header = curl_slist_append(range_header, range);
1022 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
1023 }
1024
1025 if (start_active_slot(slot)) {
1026 run_active_slot(slot);
1027 if (results.curl_result != CURLE_OK) {
1028 free(url);
1029 fclose(indexfile);
1030 slot->local = NULL;
1031 return error("Unable to get pack index %s\n%s", url,
1032 curl_errorstr);
1033 }
1034 } else {
1035 free(url);
1036 fclose(indexfile);
1037 slot->local = NULL;
1038 return error("Unable to start request");
1039 }
1040
1041 free(url);
1042 fclose(indexfile);
1043 slot->local = NULL;
1044
1045 return move_temp_to_file(tmpfile, filename);
1046 }
1047
1048 static int setup_index(unsigned char *sha1)
1049 {
1050 struct packed_git *new_pack;
1051
1052 if (fetch_index(sha1))
1053 return -1;
1054
1055 new_pack = parse_pack_index(sha1);
1056 new_pack->next = repo->packs;
1057 repo->packs = new_pack;
1058 return 0;
1059 }
1060
1061 static int fetch_indices(void)
1062 {
1063 unsigned char sha1[20];
1064 char *url;
1065 struct strbuf buffer = STRBUF_INIT;
1066 char *data;
1067 int i = 0;
1068
1069 struct active_request_slot *slot;
1070 struct slot_results results;
1071
1072 if (push_verbosely)
1073 fprintf(stderr, "Getting pack list\n");
1074
1075 url = xmalloc(strlen(repo->url) + 20);
1076 sprintf(url, "%sobjects/info/packs", repo->url);
1077
1078 slot = get_active_slot();
1079 slot->results = &results;
1080 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1081 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1082 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1083 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1084 if (start_active_slot(slot)) {
1085 run_active_slot(slot);
1086 if (results.curl_result != CURLE_OK) {
1087 strbuf_release(&buffer);
1088 free(url);
1089 if (results.http_code == 404)
1090 return 0;
1091 else
1092 return error("%s", curl_errorstr);
1093 }
1094 } else {
1095 strbuf_release(&buffer);
1096 free(url);
1097 return error("Unable to start request");
1098 }
1099 free(url);
1100
1101 data = buffer.buf;
1102 while (i < buffer.len) {
1103 switch (data[i]) {
1104 case 'P':
1105 i++;
1106 if (i + 52 < buffer.len &&
1107 !prefixcmp(data + i, " pack-") &&
1108 !prefixcmp(data + i + 46, ".pack\n")) {
1109 get_sha1_hex(data + i + 6, sha1);
1110 setup_index(sha1);
1111 i += 51;
1112 break;
1113 }
1114 default:
1115 while (data[i] != '\n')
1116 i++;
1117 }
1118 i++;
1119 }
1120
1121 strbuf_release(&buffer);
1122 return 0;
1123 }
1124
1125 static void one_remote_object(const char *hex)
1126 {
1127 unsigned char sha1[20];
1128 struct object *obj;
1129
1130 if (get_sha1_hex(hex, sha1) != 0)
1131 return;
1132
1133 obj = lookup_object(sha1);
1134 if (!obj)
1135 obj = parse_object(sha1);
1136
1137 /* Ignore remote objects that don't exist locally */
1138 if (!obj)
1139 return;
1140
1141 obj->flags |= REMOTE;
1142 if (!object_list_contains(objects, obj))
1143 object_list_insert(obj, &objects);
1144 }
1145
1146 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1147 {
1148 int *lock_flags = (int *)ctx->userData;
1149
1150 if (tag_closed) {
1151 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1152 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1153 (*lock_flags & DAV_PROP_LOCKWR)) {
1154 *lock_flags |= DAV_LOCK_OK;
1155 }
1156 *lock_flags &= DAV_LOCK_OK;
1157 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1158 *lock_flags |= DAV_PROP_LOCKWR;
1159 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1160 *lock_flags |= DAV_PROP_LOCKEX;
1161 }
1162 }
1163 }
1164
1165 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1166 {
1167 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1168 git_SHA_CTX sha_ctx;
1169 unsigned char lock_token_sha1[20];
1170
1171 if (tag_closed && ctx->cdata) {
1172 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1173 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1174 strcpy(lock->owner, ctx->cdata);
1175 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1176 if (!prefixcmp(ctx->cdata, "Second-"))
1177 lock->timeout =
1178 strtol(ctx->cdata + 7, NULL, 10);
1179 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1180 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1181 strcpy(lock->token, ctx->cdata);
1182
1183 git_SHA1_Init(&sha_ctx);
1184 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
1185 git_SHA1_Final(lock_token_sha1, &sha_ctx);
1186
1187 lock->tmpfile_suffix[0] = '_';
1188 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
1189 }
1190 }
1191 }
1192
1193 static void one_remote_ref(char *refname);
1194
1195 static void
1196 xml_start_tag(void *userData, const char *name, const char **atts)
1197 {
1198 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1199 const char *c = strchr(name, ':');
1200 int new_len;
1201
1202 if (c == NULL)
1203 c = name;
1204 else
1205 c++;
1206
1207 new_len = strlen(ctx->name) + strlen(c) + 2;
1208
1209 if (new_len > ctx->len) {
1210 ctx->name = xrealloc(ctx->name, new_len);
1211 ctx->len = new_len;
1212 }
1213 strcat(ctx->name, ".");
1214 strcat(ctx->name, c);
1215
1216 free(ctx->cdata);
1217 ctx->cdata = NULL;
1218
1219 ctx->userFunc(ctx, 0);
1220 }
1221
1222 static void
1223 xml_end_tag(void *userData, const char *name)
1224 {
1225 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1226 const char *c = strchr(name, ':');
1227 char *ep;
1228
1229 ctx->userFunc(ctx, 1);
1230
1231 if (c == NULL)
1232 c = name;
1233 else
1234 c++;
1235
1236 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1237 *ep = 0;
1238 }
1239
1240 static void
1241 xml_cdata(void *userData, const XML_Char *s, int len)
1242 {
1243 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1244 free(ctx->cdata);
1245 ctx->cdata = xmemdupz(s, len);
1246 }
1247
1248 static struct remote_lock *lock_remote(const char *path, long timeout)
1249 {
1250 struct active_request_slot *slot;
1251 struct slot_results results;
1252 struct buffer out_buffer = { STRBUF_INIT, 0 };
1253 struct strbuf in_buffer = STRBUF_INIT;
1254 char *url;
1255 char *ep;
1256 char timeout_header[25];
1257 struct remote_lock *lock = NULL;
1258 struct curl_slist *dav_headers = NULL;
1259 struct xml_ctx ctx;
1260 char *escaped;
1261
1262 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1263 sprintf(url, "%s%s", repo->url, path);
1264
1265 /* Make sure leading directories exist for the remote ref */
1266 ep = strchr(url + strlen(repo->url) + 1, '/');
1267 while (ep) {
1268 char saved_character = ep[1];
1269 ep[1] = '\0';
1270 slot = get_active_slot();
1271 slot->results = &results;
1272 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1273 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1274 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1275 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1276 if (start_active_slot(slot)) {
1277 run_active_slot(slot);
1278 if (results.curl_result != CURLE_OK &&
1279 results.http_code != 405) {
1280 fprintf(stderr,
1281 "Unable to create branch path %s\n",
1282 url);
1283 free(url);
1284 return NULL;
1285 }
1286 } else {
1287 fprintf(stderr, "Unable to start MKCOL request\n");
1288 free(url);
1289 return NULL;
1290 }
1291 ep[1] = saved_character;
1292 ep = strchr(ep + 1, '/');
1293 }
1294
1295 escaped = xml_entities(git_default_email);
1296 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
1297 free(escaped);
1298
1299 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1300 dav_headers = curl_slist_append(dav_headers, timeout_header);
1301 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1302
1303 slot = get_active_slot();
1304 slot->results = &results;
1305 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1306 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1307 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1308 #ifndef NO_CURL_IOCTL
1309 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1310 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1311 #endif
1312 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1313 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1314 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1315 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1316 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1317 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1318
1319 lock = xcalloc(1, sizeof(*lock));
1320 lock->timeout = -1;
1321
1322 if (start_active_slot(slot)) {
1323 run_active_slot(slot);
1324 if (results.curl_result == CURLE_OK) {
1325 XML_Parser parser = XML_ParserCreate(NULL);
1326 enum XML_Status result;
1327 ctx.name = xcalloc(10, 1);
1328 ctx.len = 0;
1329 ctx.cdata = NULL;
1330 ctx.userFunc = handle_new_lock_ctx;
1331 ctx.userData = lock;
1332 XML_SetUserData(parser, &ctx);
1333 XML_SetElementHandler(parser, xml_start_tag,
1334 xml_end_tag);
1335 XML_SetCharacterDataHandler(parser, xml_cdata);
1336 result = XML_Parse(parser, in_buffer.buf,
1337 in_buffer.len, 1);
1338 free(ctx.name);
1339 if (result != XML_STATUS_OK) {
1340 fprintf(stderr, "XML error: %s\n",
1341 XML_ErrorString(
1342 XML_GetErrorCode(parser)));
1343 lock->timeout = -1;
1344 }
1345 XML_ParserFree(parser);
1346 }
1347 } else {
1348 fprintf(stderr, "Unable to start LOCK request\n");
1349 }
1350
1351 curl_slist_free_all(dav_headers);
1352 strbuf_release(&out_buffer.buf);
1353 strbuf_release(&in_buffer);
1354
1355 if (lock->token == NULL || lock->timeout <= 0) {
1356 free(lock->token);
1357 free(lock->owner);
1358 free(url);
1359 free(lock);
1360 lock = NULL;
1361 } else {
1362 lock->url = url;
1363 lock->start_time = time(NULL);
1364 lock->next = repo->locks;
1365 repo->locks = lock;
1366 }
1367
1368 return lock;
1369 }
1370
1371 static int unlock_remote(struct remote_lock *lock)
1372 {
1373 struct active_request_slot *slot;
1374 struct slot_results results;
1375 struct remote_lock *prev = repo->locks;
1376 struct curl_slist *dav_headers;
1377 int rc = 0;
1378
1379 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1380
1381 slot = get_active_slot();
1382 slot->results = &results;
1383 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1384 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1385 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1386 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1387
1388 if (start_active_slot(slot)) {
1389 run_active_slot(slot);
1390 if (results.curl_result == CURLE_OK)
1391 rc = 1;
1392 else
1393 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1394 results.http_code);
1395 } else {
1396 fprintf(stderr, "Unable to start UNLOCK request\n");
1397 }
1398
1399 curl_slist_free_all(dav_headers);
1400
1401 if (repo->locks == lock) {
1402 repo->locks = lock->next;
1403 } else {
1404 while (prev && prev->next != lock)
1405 prev = prev->next;
1406 if (prev)
1407 prev->next = prev->next->next;
1408 }
1409
1410 free(lock->owner);
1411 free(lock->url);
1412 free(lock->token);
1413 free(lock);
1414
1415 return rc;
1416 }
1417
1418 static void remove_locks(void)
1419 {
1420 struct remote_lock *lock = repo->locks;
1421
1422 fprintf(stderr, "Removing remote locks...\n");
1423 while (lock) {
1424 struct remote_lock *next = lock->next;
1425 unlock_remote(lock);
1426 lock = next;
1427 }
1428 }
1429
1430 static void remove_locks_on_signal(int signo)
1431 {
1432 remove_locks();
1433 sigchain_pop(signo);
1434 raise(signo);
1435 }
1436
1437 static void remote_ls(const char *path, int flags,
1438 void (*userFunc)(struct remote_ls_ctx *ls),
1439 void *userData);
1440
1441 static void process_ls_object(struct remote_ls_ctx *ls)
1442 {
1443 unsigned int *parent = (unsigned int *)ls->userData;
1444 char *path = ls->dentry_name;
1445 char *obj_hex;
1446
1447 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1448 remote_dir_exists[*parent] = 1;
1449 return;
1450 }
1451
1452 if (strlen(path) != 49)
1453 return;
1454 path += 8;
1455 obj_hex = xmalloc(strlen(path));
1456 /* NB: path is not null-terminated, can not use strlcpy here */
1457 memcpy(obj_hex, path, 2);
1458 strcpy(obj_hex + 2, path + 3);
1459 one_remote_object(obj_hex);
1460 free(obj_hex);
1461 }
1462
1463 static void process_ls_ref(struct remote_ls_ctx *ls)
1464 {
1465 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1466 fprintf(stderr, " %s\n", ls->dentry_name);
1467 return;
1468 }
1469
1470 if (!(ls->dentry_flags & IS_DIR))
1471 one_remote_ref(ls->dentry_name);
1472 }
1473
1474 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1475 {
1476 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1477
1478 if (tag_closed) {
1479 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1480 if (ls->dentry_flags & IS_DIR) {
1481 if (ls->flags & PROCESS_DIRS) {
1482 ls->userFunc(ls);
1483 }
1484 if (strcmp(ls->dentry_name, ls->path) &&
1485 ls->flags & RECURSIVE) {
1486 remote_ls(ls->dentry_name,
1487 ls->flags,
1488 ls->userFunc,
1489 ls->userData);
1490 }
1491 } else if (ls->flags & PROCESS_FILES) {
1492 ls->userFunc(ls);
1493 }
1494 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1495 char *path = ctx->cdata;
1496 if (*ctx->cdata == 'h') {
1497 path = strstr(path, "//");
1498 if (path) {
1499 path = strchr(path+2, '/');
1500 }
1501 }
1502 if (path) {
1503 path += repo->path_len;
1504 ls->dentry_name = xstrdup(path);
1505 }
1506 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1507 ls->dentry_flags |= IS_DIR;
1508 }
1509 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1510 free(ls->dentry_name);
1511 ls->dentry_name = NULL;
1512 ls->dentry_flags = 0;
1513 }
1514 }
1515
1516 /*
1517 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1518 * should _only_ heed the information from that file, instead of trying to
1519 * determine the refs from the remote file system (badly: it does not even
1520 * know about packed-refs).
1521 */
1522 static void remote_ls(const char *path, int flags,
1523 void (*userFunc)(struct remote_ls_ctx *ls),
1524 void *userData)
1525 {
1526 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1527 struct active_request_slot *slot;
1528 struct slot_results results;
1529 struct strbuf in_buffer = STRBUF_INIT;
1530 struct buffer out_buffer = { STRBUF_INIT, 0 };
1531 struct curl_slist *dav_headers = NULL;
1532 struct xml_ctx ctx;
1533 struct remote_ls_ctx ls;
1534
1535 ls.flags = flags;
1536 ls.path = xstrdup(path);
1537 ls.dentry_name = NULL;
1538 ls.dentry_flags = 0;
1539 ls.userData = userData;
1540 ls.userFunc = userFunc;
1541
1542 sprintf(url, "%s%s", repo->url, path);
1543
1544 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1545
1546 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1547 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1548
1549 slot = get_active_slot();
1550 slot->results = &results;
1551 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1552 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1553 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1554 #ifndef NO_CURL_IOCTL
1555 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1556 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1557 #endif
1558 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1559 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1560 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1561 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1562 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1563 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1564
1565 if (start_active_slot(slot)) {
1566 run_active_slot(slot);
1567 if (results.curl_result == CURLE_OK) {
1568 XML_Parser parser = XML_ParserCreate(NULL);
1569 enum XML_Status result;
1570 ctx.name = xcalloc(10, 1);
1571 ctx.len = 0;
1572 ctx.cdata = NULL;
1573 ctx.userFunc = handle_remote_ls_ctx;
1574 ctx.userData = &ls;
1575 XML_SetUserData(parser, &ctx);
1576 XML_SetElementHandler(parser, xml_start_tag,
1577 xml_end_tag);
1578 XML_SetCharacterDataHandler(parser, xml_cdata);
1579 result = XML_Parse(parser, in_buffer.buf,
1580 in_buffer.len, 1);
1581 free(ctx.name);
1582
1583 if (result != XML_STATUS_OK) {
1584 fprintf(stderr, "XML error: %s\n",
1585 XML_ErrorString(
1586 XML_GetErrorCode(parser)));
1587 }
1588 XML_ParserFree(parser);
1589 }
1590 } else {
1591 fprintf(stderr, "Unable to start PROPFIND request\n");
1592 }
1593
1594 free(ls.path);
1595 free(url);
1596 strbuf_release(&out_buffer.buf);
1597 strbuf_release(&in_buffer);
1598 curl_slist_free_all(dav_headers);
1599 }
1600
1601 static void get_remote_object_list(unsigned char parent)
1602 {
1603 char path[] = "objects/XX/";
1604 static const char hex[] = "0123456789abcdef";
1605 unsigned int val = parent;
1606
1607 path[8] = hex[val >> 4];
1608 path[9] = hex[val & 0xf];
1609 remote_dir_exists[val] = 0;
1610 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1611 process_ls_object, &val);
1612 }
1613
1614 static int locking_available(void)
1615 {
1616 struct active_request_slot *slot;
1617 struct slot_results results;
1618 struct strbuf in_buffer = STRBUF_INIT;
1619 struct buffer out_buffer = { STRBUF_INIT, 0 };
1620 struct curl_slist *dav_headers = NULL;
1621 struct xml_ctx ctx;
1622 int lock_flags = 0;
1623 char *escaped;
1624
1625 escaped = xml_entities(repo->url);
1626 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1627 free(escaped);
1628
1629 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1630 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1631
1632 slot = get_active_slot();
1633 slot->results = &results;
1634 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1635 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1636 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1637 #ifndef NO_CURL_IOCTL
1638 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1639 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1640 #endif
1641 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1642 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1643 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1644 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1645 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1646 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1647
1648 if (start_active_slot(slot)) {
1649 run_active_slot(slot);
1650 if (results.curl_result == CURLE_OK) {
1651 XML_Parser parser = XML_ParserCreate(NULL);
1652 enum XML_Status result;
1653 ctx.name = xcalloc(10, 1);
1654 ctx.len = 0;
1655 ctx.cdata = NULL;
1656 ctx.userFunc = handle_lockprop_ctx;
1657 ctx.userData = &lock_flags;
1658 XML_SetUserData(parser, &ctx);
1659 XML_SetElementHandler(parser, xml_start_tag,
1660 xml_end_tag);
1661 result = XML_Parse(parser, in_buffer.buf,
1662 in_buffer.len, 1);
1663 free(ctx.name);
1664
1665 if (result != XML_STATUS_OK) {
1666 fprintf(stderr, "XML error: %s\n",
1667 XML_ErrorString(
1668 XML_GetErrorCode(parser)));
1669 lock_flags = 0;
1670 }
1671 XML_ParserFree(parser);
1672 if (!lock_flags)
1673 error("no DAV locking support on %s",
1674 repo->url);
1675
1676 } else {
1677 error("Cannot access URL %s, return code %d",
1678 repo->url, results.curl_result);
1679 lock_flags = 0;
1680 }
1681 } else {
1682 error("Unable to start PROPFIND request on %s", repo->url);
1683 }
1684
1685 strbuf_release(&out_buffer.buf);
1686 strbuf_release(&in_buffer);
1687 curl_slist_free_all(dav_headers);
1688
1689 return lock_flags;
1690 }
1691
1692 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1693 {
1694 struct object_list *entry = xmalloc(sizeof(struct object_list));
1695 entry->item = obj;
1696 entry->next = *p;
1697 *p = entry;
1698 return &entry->next;
1699 }
1700
1701 static struct object_list **process_blob(struct blob *blob,
1702 struct object_list **p,
1703 struct name_path *path,
1704 const char *name)
1705 {
1706 struct object *obj = &blob->object;
1707
1708 obj->flags |= LOCAL;
1709
1710 if (obj->flags & (UNINTERESTING | SEEN))
1711 return p;
1712
1713 obj->flags |= SEEN;
1714 return add_one_object(obj, p);
1715 }
1716
1717 static struct object_list **process_tree(struct tree *tree,
1718 struct object_list **p,
1719 struct name_path *path,
1720 const char *name)
1721 {
1722 struct object *obj = &tree->object;
1723 struct tree_desc desc;
1724 struct name_entry entry;
1725 struct name_path me;
1726
1727 obj->flags |= LOCAL;
1728
1729 if (obj->flags & (UNINTERESTING | SEEN))
1730 return p;
1731 if (parse_tree(tree) < 0)
1732 die("bad tree object %s", sha1_to_hex(obj->sha1));
1733
1734 obj->flags |= SEEN;
1735 name = xstrdup(name);
1736 p = add_one_object(obj, p);
1737 me.up = path;
1738 me.elem = name;
1739 me.elem_len = strlen(name);
1740
1741 init_tree_desc(&desc, tree->buffer, tree->size);
1742
1743 while (tree_entry(&desc, &entry))
1744 switch (object_type(entry.mode)) {
1745 case OBJ_TREE:
1746 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1747 break;
1748 case OBJ_BLOB:
1749 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1750 break;
1751 default:
1752 /* Subproject commit - not in this repository */
1753 break;
1754 }
1755
1756 free(tree->buffer);
1757 tree->buffer = NULL;
1758 return p;
1759 }
1760
1761 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1762 {
1763 int i;
1764 struct commit *commit;
1765 struct object_list **p = &objects;
1766 int count = 0;
1767
1768 while ((commit = get_revision(revs)) != NULL) {
1769 p = process_tree(commit->tree, p, NULL, "");
1770 commit->object.flags |= LOCAL;
1771 if (!(commit->object.flags & UNINTERESTING))
1772 count += add_send_request(&commit->object, lock);
1773 }
1774
1775 for (i = 0; i < revs->pending.nr; i++) {
1776 struct object_array_entry *entry = revs->pending.objects + i;
1777 struct object *obj = entry->item;
1778 const char *name = entry->name;
1779
1780 if (obj->flags & (UNINTERESTING | SEEN))
1781 continue;
1782 if (obj->type == OBJ_TAG) {
1783 obj->flags |= SEEN;
1784 p = add_one_object(obj, p);
1785 continue;
1786 }
1787 if (obj->type == OBJ_TREE) {
1788 p = process_tree((struct tree *)obj, p, NULL, name);
1789 continue;
1790 }
1791 if (obj->type == OBJ_BLOB) {
1792 p = process_blob((struct blob *)obj, p, NULL, name);
1793 continue;
1794 }
1795 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1796 }
1797
1798 while (objects) {
1799 if (!(objects->item->flags & UNINTERESTING))
1800 count += add_send_request(objects->item, lock);
1801 objects = objects->next;
1802 }
1803
1804 return count;
1805 }
1806
1807 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1808 {
1809 struct active_request_slot *slot;
1810 struct slot_results results;
1811 struct buffer out_buffer = { STRBUF_INIT, 0 };
1812 struct curl_slist *dav_headers;
1813
1814 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1815
1816 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1817
1818 slot = get_active_slot();
1819 slot->results = &results;
1820 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1821 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1822 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1823 #ifndef NO_CURL_IOCTL
1824 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1825 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1826 #endif
1827 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1828 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1829 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1830 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1831 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1832 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1833
1834 if (start_active_slot(slot)) {
1835 run_active_slot(slot);
1836 strbuf_release(&out_buffer.buf);
1837 if (results.curl_result != CURLE_OK) {
1838 fprintf(stderr,
1839 "PUT error: curl result=%d, HTTP code=%ld\n",
1840 results.curl_result, results.http_code);
1841 /* We should attempt recovery? */
1842 return 0;
1843 }
1844 } else {
1845 strbuf_release(&out_buffer.buf);
1846 fprintf(stderr, "Unable to start PUT request\n");
1847 return 0;
1848 }
1849
1850 return 1;
1851 }
1852
1853 static struct ref *remote_refs;
1854
1855 static void one_remote_ref(char *refname)
1856 {
1857 struct ref *ref;
1858 struct object *obj;
1859
1860 ref = alloc_ref(refname);
1861
1862 if (http_fetch_ref(repo->url, ref) != 0) {
1863 fprintf(stderr,
1864 "Unable to fetch ref %s from %s\n",
1865 refname, repo->url);
1866 free(ref);
1867 return;
1868 }
1869
1870 /*
1871 * Fetch a copy of the object if it doesn't exist locally - it
1872 * may be required for updating server info later.
1873 */
1874 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1875 obj = lookup_unknown_object(ref->old_sha1);
1876 if (obj) {
1877 fprintf(stderr, " fetch %s for %s\n",
1878 sha1_to_hex(ref->old_sha1), refname);
1879 add_fetch_request(obj);
1880 }
1881 }
1882
1883 ref->next = remote_refs;
1884 remote_refs = ref;
1885 }
1886
1887 static void get_dav_remote_heads(void)
1888 {
1889 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1890 }
1891
1892 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1893 {
1894 struct strbuf *buf = (struct strbuf *)ls->userData;
1895 struct object *o;
1896 int len;
1897 char *ref_info;
1898 struct ref *ref;
1899
1900 ref = alloc_ref(ls->dentry_name);
1901
1902 if (http_fetch_ref(repo->url, ref) != 0) {
1903 fprintf(stderr,
1904 "Unable to fetch ref %s from %s\n",
1905 ls->dentry_name, repo->url);
1906 aborted = 1;
1907 free(ref);
1908 return;
1909 }
1910
1911 o = parse_object(ref->old_sha1);
1912 if (!o) {
1913 fprintf(stderr,
1914 "Unable to parse object %s for remote ref %s\n",
1915 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1916 aborted = 1;
1917 free(ref);
1918 return;
1919 }
1920
1921 len = strlen(ls->dentry_name) + 42;
1922 ref_info = xcalloc(len + 1, 1);
1923 sprintf(ref_info, "%s %s\n",
1924 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1925 fwrite_buffer(ref_info, 1, len, buf);
1926 free(ref_info);
1927
1928 if (o->type == OBJ_TAG) {
1929 o = deref_tag(o, ls->dentry_name, 0);
1930 if (o) {
1931 len = strlen(ls->dentry_name) + 45;
1932 ref_info = xcalloc(len + 1, 1);
1933 sprintf(ref_info, "%s %s^{}\n",
1934 sha1_to_hex(o->sha1), ls->dentry_name);
1935 fwrite_buffer(ref_info, 1, len, buf);
1936 free(ref_info);
1937 }
1938 }
1939 free(ref);
1940 }
1941
1942 static void update_remote_info_refs(struct remote_lock *lock)
1943 {
1944 struct buffer buffer = { STRBUF_INIT, 0 };
1945 struct active_request_slot *slot;
1946 struct slot_results results;
1947 struct curl_slist *dav_headers;
1948
1949 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1950 add_remote_info_ref, &buffer.buf);
1951 if (!aborted) {
1952 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1953
1954 slot = get_active_slot();
1955 slot->results = &results;
1956 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1957 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1958 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1959 #ifndef NO_CURL_IOCTL
1960 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1961 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1962 #endif
1963 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1964 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1965 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1966 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1967 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1968 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1969
1970 if (start_active_slot(slot)) {
1971 run_active_slot(slot);
1972 if (results.curl_result != CURLE_OK) {
1973 fprintf(stderr,
1974 "PUT error: curl result=%d, HTTP code=%ld\n",
1975 results.curl_result, results.http_code);
1976 }
1977 }
1978 }
1979 strbuf_release(&buffer.buf);
1980 }
1981
1982 static int remote_exists(const char *path)
1983 {
1984 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1985 struct active_request_slot *slot;
1986 struct slot_results results;
1987 int ret = -1;
1988
1989 sprintf(url, "%s%s", repo->url, path);
1990
1991 slot = get_active_slot();
1992 slot->results = &results;
1993 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1994 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1995
1996 if (start_active_slot(slot)) {
1997 run_active_slot(slot);
1998 if (results.http_code == 404)
1999 ret = 0;
2000 else if (results.curl_result == CURLE_OK)
2001 ret = 1;
2002 else
2003 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2004 } else {
2005 fprintf(stderr, "Unable to start HEAD request\n");
2006 }
2007
2008 free(url);
2009 return ret;
2010 }
2011
2012 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2013 {
2014 char *url;
2015 struct strbuf buffer = STRBUF_INIT;
2016 struct active_request_slot *slot;
2017 struct slot_results results;
2018
2019 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
2020 sprintf(url, "%s%s", repo->url, path);
2021
2022 slot = get_active_slot();
2023 slot->results = &results;
2024 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2025 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2026 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2027 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2028 if (start_active_slot(slot)) {
2029 run_active_slot(slot);
2030 if (results.curl_result != CURLE_OK) {
2031 die("Couldn't get %s for remote symref\n%s",
2032 url, curl_errorstr);
2033 }
2034 } else {
2035 die("Unable to start remote symref request");
2036 }
2037 free(url);
2038
2039 free(*symref);
2040 *symref = NULL;
2041 hashclr(sha1);
2042
2043 if (buffer.len == 0)
2044 return;
2045
2046 /* If it's a symref, set the refname; otherwise try for a sha1 */
2047 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2048 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2049 } else {
2050 get_sha1_hex(buffer.buf, sha1);
2051 }
2052
2053 strbuf_release(&buffer);
2054 }
2055
2056 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2057 {
2058 struct commit *head = lookup_commit(head_sha1);
2059 struct commit *branch = lookup_commit(branch_sha1);
2060 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2061
2062 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2063 }
2064
2065 static int delete_remote_branch(char *pattern, int force)
2066 {
2067 struct ref *refs = remote_refs;
2068 struct ref *remote_ref = NULL;
2069 unsigned char head_sha1[20];
2070 char *symref = NULL;
2071 int match;
2072 int patlen = strlen(pattern);
2073 int i;
2074 struct active_request_slot *slot;
2075 struct slot_results results;
2076 char *url;
2077
2078 /* Find the remote branch(es) matching the specified branch name */
2079 for (match = 0; refs; refs = refs->next) {
2080 char *name = refs->name;
2081 int namelen = strlen(name);
2082 if (namelen < patlen ||
2083 memcmp(name + namelen - patlen, pattern, patlen))
2084 continue;
2085 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2086 continue;
2087 match++;
2088 remote_ref = refs;
2089 }
2090 if (match == 0)
2091 return error("No remote branch matches %s", pattern);
2092 if (match != 1)
2093 return error("More than one remote branch matches %s",
2094 pattern);
2095
2096 /*
2097 * Remote HEAD must be a symref (not exactly foolproof; a remote
2098 * symlink to a symref will look like a symref)
2099 */
2100 fetch_symref("HEAD", &symref, head_sha1);
2101 if (!symref)
2102 return error("Remote HEAD is not a symref");
2103
2104 /* Remote branch must not be the remote HEAD */
2105 for (i=0; symref && i<MAXDEPTH; i++) {
2106 if (!strcmp(remote_ref->name, symref))
2107 return error("Remote branch %s is the current HEAD",
2108 remote_ref->name);
2109 fetch_symref(symref, &symref, head_sha1);
2110 }
2111
2112 /* Run extra sanity checks if delete is not forced */
2113 if (!force) {
2114 /* Remote HEAD must resolve to a known object */
2115 if (symref)
2116 return error("Remote HEAD symrefs too deep");
2117 if (is_null_sha1(head_sha1))
2118 return error("Unable to resolve remote HEAD");
2119 if (!has_sha1_file(head_sha1))
2120 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2121
2122 /* Remote branch must resolve to a known object */
2123 if (is_null_sha1(remote_ref->old_sha1))
2124 return error("Unable to resolve remote branch %s",
2125 remote_ref->name);
2126 if (!has_sha1_file(remote_ref->old_sha1))
2127 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
2128
2129 /* Remote branch must be an ancestor of remote HEAD */
2130 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2131 return error("The branch '%s' is not an ancestor "
2132 "of your current HEAD.\n"
2133 "If you are sure you want to delete it,"
2134 " run:\n\t'git http-push -D %s %s'",
2135 remote_ref->name, repo->url, pattern);
2136 }
2137 }
2138
2139 /* Send delete request */
2140 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2141 if (dry_run)
2142 return 0;
2143 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
2144 sprintf(url, "%s%s", repo->url, remote_ref->name);
2145 slot = get_active_slot();
2146 slot->results = &results;
2147 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2148 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2149 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2150 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2151 if (start_active_slot(slot)) {
2152 run_active_slot(slot);
2153 free(url);
2154 if (results.curl_result != CURLE_OK)
2155 return error("DELETE request failed (%d/%ld)\n",
2156 results.curl_result, results.http_code);
2157 } else {
2158 free(url);
2159 return error("Unable to start DELETE request");
2160 }
2161
2162 return 0;
2163 }
2164
2165 int main(int argc, char **argv)
2166 {
2167 struct transfer_request *request;
2168 struct transfer_request *next_request;
2169 int nr_refspec = 0;
2170 char **refspec = NULL;
2171 struct remote_lock *ref_lock = NULL;
2172 struct remote_lock *info_ref_lock = NULL;
2173 struct rev_info revs;
2174 int delete_branch = 0;
2175 int force_delete = 0;
2176 int objects_to_send;
2177 int rc = 0;
2178 int i;
2179 int new_refs;
2180 struct ref *ref, *local_refs;
2181 struct remote *remote;
2182 char *rewritten_url = NULL;
2183
2184 git_extract_argv0_path(argv[0]);
2185
2186 setup_git_directory();
2187
2188 repo = xcalloc(sizeof(*repo), 1);
2189
2190 argv++;
2191 for (i = 1; i < argc; i++, argv++) {
2192 char *arg = *argv;
2193
2194 if (*arg == '-') {
2195 if (!strcmp(arg, "--all")) {
2196 push_all = MATCH_REFS_ALL;
2197 continue;
2198 }
2199 if (!strcmp(arg, "--force")) {
2200 force_all = 1;
2201 continue;
2202 }
2203 if (!strcmp(arg, "--dry-run")) {
2204 dry_run = 1;
2205 continue;
2206 }
2207 if (!strcmp(arg, "--verbose")) {
2208 push_verbosely = 1;
2209 continue;
2210 }
2211 if (!strcmp(arg, "-d")) {
2212 delete_branch = 1;
2213 continue;
2214 }
2215 if (!strcmp(arg, "-D")) {
2216 delete_branch = 1;
2217 force_delete = 1;
2218 continue;
2219 }
2220 }
2221 if (!repo->url) {
2222 char *path = strstr(arg, "//");
2223 repo->url = arg;
2224 repo->path_len = strlen(arg);
2225 if (path) {
2226 repo->path = strchr(path+2, '/');
2227 if (repo->path)
2228 repo->path_len = strlen(repo->path);
2229 }
2230 continue;
2231 }
2232 refspec = argv;
2233 nr_refspec = argc - i;
2234 break;
2235 }
2236
2237 #ifndef USE_CURL_MULTI
2238 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2239 #endif
2240
2241 if (!repo->url)
2242 usage(http_push_usage);
2243
2244 if (delete_branch && nr_refspec != 1)
2245 die("You must specify only one branch name when deleting a remote branch");
2246
2247 memset(remote_dir_exists, -1, 256);
2248
2249 /*
2250 * Create a minimum remote by hand to give to http_init(),
2251 * primarily to allow it to look at the URL.
2252 */
2253 remote = xcalloc(sizeof(*remote), 1);
2254 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
2255 remote->url[remote->url_nr++] = repo->url;
2256 http_init(remote);
2257
2258 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2259
2260 if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
2261 rewritten_url = xmalloc(strlen(repo->url)+2);
2262 strcpy(rewritten_url, repo->url);
2263 strcat(rewritten_url, "/");
2264 repo->path = rewritten_url + (repo->path - repo->url);
2265 repo->path_len++;
2266 repo->url = rewritten_url;
2267 }
2268
2269 /* Verify DAV compliance/lock support */
2270 if (!locking_available()) {
2271 rc = 1;
2272 goto cleanup;
2273 }
2274
2275 sigchain_push_common(remove_locks_on_signal);
2276
2277 /* Check whether the remote has server info files */
2278 repo->can_update_info_refs = 0;
2279 repo->has_info_refs = remote_exists("info/refs");
2280 repo->has_info_packs = remote_exists("objects/info/packs");
2281 if (repo->has_info_refs) {
2282 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2283 if (info_ref_lock)
2284 repo->can_update_info_refs = 1;
2285 else {
2286 error("cannot lock existing info/refs");
2287 rc = 1;
2288 goto cleanup;
2289 }
2290 }
2291 if (repo->has_info_packs)
2292 fetch_indices();
2293
2294 /* Get a list of all local and remote heads to validate refspecs */
2295 local_refs = get_local_heads();
2296 fprintf(stderr, "Fetching remote heads...\n");
2297 get_dav_remote_heads();
2298
2299 /* Remove a remote branch if -d or -D was specified */
2300 if (delete_branch) {
2301 if (delete_remote_branch(refspec[0], force_delete) == -1)
2302 fprintf(stderr, "Unable to delete remote branch %s\n",
2303 refspec[0]);
2304 goto cleanup;
2305 }
2306
2307 /* match them up */
2308 if (match_refs(local_refs, &remote_refs,
2309 nr_refspec, (const char **) refspec, push_all)) {
2310 rc = -1;
2311 goto cleanup;
2312 }
2313 if (!remote_refs) {
2314 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2315 rc = 0;
2316 goto cleanup;
2317 }
2318
2319 new_refs = 0;
2320 for (ref = remote_refs; ref; ref = ref->next) {
2321 char old_hex[60], *new_hex;
2322 const char *commit_argv[5];
2323 int commit_argc;
2324 char *new_sha1_hex, *old_sha1_hex;
2325
2326 if (!ref->peer_ref)
2327 continue;
2328
2329 if (is_null_sha1(ref->peer_ref->new_sha1)) {
2330 if (delete_remote_branch(ref->name, 1) == -1) {
2331 error("Could not remove %s", ref->name);
2332 rc = -4;
2333 }
2334 new_refs++;
2335 continue;
2336 }
2337
2338 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2339 if (push_verbosely || 1)
2340 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2341 continue;
2342 }
2343
2344 if (!force_all &&
2345 !is_null_sha1(ref->old_sha1) &&
2346 !ref->force) {
2347 if (!has_sha1_file(ref->old_sha1) ||
2348 !ref_newer(ref->peer_ref->new_sha1,
2349 ref->old_sha1)) {
2350 /*
2351 * We do not have the remote ref, or
2352 * we know that the remote ref is not
2353 * an ancestor of what we are trying to
2354 * push. Either way this can be losing
2355 * commits at the remote end and likely
2356 * we were not up to date to begin with.
2357 */
2358 error("remote '%s' is not an ancestor of\n"
2359 "local '%s'.\n"
2360 "Maybe you are not up-to-date and "
2361 "need to pull first?",
2362 ref->name,
2363 ref->peer_ref->name);
2364 rc = -2;
2365 continue;
2366 }
2367 }
2368 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2369 new_refs++;
2370 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2371 new_hex = sha1_to_hex(ref->new_sha1);
2372
2373 fprintf(stderr, "updating '%s'", ref->name);
2374 if (strcmp(ref->name, ref->peer_ref->name))
2375 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2376 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2377 if (dry_run)
2378 continue;
2379
2380 /* Lock remote branch ref */
2381 ref_lock = lock_remote(ref->name, LOCK_TIME);
2382 if (ref_lock == NULL) {
2383 fprintf(stderr, "Unable to lock remote branch %s\n",
2384 ref->name);
2385 rc = 1;
2386 continue;
2387 }
2388
2389 /* Set up revision info for this refspec */
2390 commit_argc = 3;
2391 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2392 old_sha1_hex = NULL;
2393 commit_argv[1] = "--objects";
2394 commit_argv[2] = new_sha1_hex;
2395 if (!push_all && !is_null_sha1(ref->old_sha1)) {
2396 old_sha1_hex = xmalloc(42);
2397 sprintf(old_sha1_hex, "^%s",
2398 sha1_to_hex(ref->old_sha1));
2399 commit_argv[3] = old_sha1_hex;
2400 commit_argc++;
2401 }
2402 commit_argv[commit_argc] = NULL;
2403 init_revisions(&revs, setup_git_directory());
2404 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2405 revs.edge_hint = 0; /* just in case */
2406 free(new_sha1_hex);
2407 if (old_sha1_hex) {
2408 free(old_sha1_hex);
2409 commit_argv[1] = NULL;
2410 }
2411
2412 /* Generate a list of objects that need to be pushed */
2413 pushing = 0;
2414 if (prepare_revision_walk(&revs))
2415 die("revision walk setup failed");
2416 mark_edges_uninteresting(revs.commits, &revs, NULL);
2417 objects_to_send = get_delta(&revs, ref_lock);
2418 finish_all_active_slots();
2419
2420 /* Push missing objects to remote, this would be a
2421 convenient time to pack them first if appropriate. */
2422 pushing = 1;
2423 if (objects_to_send)
2424 fprintf(stderr, " sending %d objects\n",
2425 objects_to_send);
2426 #ifdef USE_CURL_MULTI
2427 fill_active_slots();
2428 add_fill_function(NULL, fill_active_slot);
2429 #endif
2430 do {
2431 finish_all_active_slots();
2432 #ifdef USE_CURL_MULTI
2433 fill_active_slots();
2434 #endif
2435 } while (request_queue_head && !aborted);
2436
2437 /* Update the remote branch if all went well */
2438 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2439 rc = 1;
2440
2441 if (!rc)
2442 fprintf(stderr, " done\n");
2443 unlock_remote(ref_lock);
2444 check_locks();
2445 }
2446
2447 /* Update remote server info if appropriate */
2448 if (repo->has_info_refs && new_refs) {
2449 if (info_ref_lock && repo->can_update_info_refs) {
2450 fprintf(stderr, "Updating remote server info\n");
2451 if (!dry_run)
2452 update_remote_info_refs(info_ref_lock);
2453 } else {
2454 fprintf(stderr, "Unable to update server info\n");
2455 }
2456 }
2457
2458 cleanup:
2459 free(rewritten_url);
2460 if (info_ref_lock)
2461 unlock_remote(info_ref_lock);
2462 free(repo);
2463
2464 curl_slist_free_all(no_pragma_header);
2465
2466 http_cleanup();
2467
2468 request = request_queue_head;
2469 while (request != NULL) {
2470 next_request = request->next;
2471 release_request(request);
2472 request = next_request;
2473 }
2474
2475 return rc;
2476 }