]> git.ipfire.org Git - thirdparty/git.git/blob - http-push.c
trace2/tr2_tls.h: remove unnecessary include
[thirdparty/git.git] / http-push.c
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "hex.h"
4 #include "repository.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "http.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "remote.h"
12 #include "list-objects.h"
13 #include "setup.h"
14 #include "sigchain.h"
15 #include "strvec.h"
16 #include "tree.h"
17 #include "tree-walk.h"
18 #include "url.h"
19 #include "packfile.h"
20 #include "object-store-ll.h"
21 #include "commit-reach.h"
22
23 #ifdef EXPAT_NEEDS_XMLPARSE_H
24 #include <xmlparse.h>
25 #else
26 #include <expat.h>
27 #endif
28
29 static const char http_push_usage[] =
30 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
31
32 #ifndef XML_STATUS_OK
33 enum XML_Status {
34 XML_STATUS_OK = 1,
35 XML_STATUS_ERROR = 0
36 };
37 #define XML_STATUS_OK 1
38 #define XML_STATUS_ERROR 0
39 #endif
40
41 #define PREV_BUF_SIZE 4096
42
43 /* DAV methods */
44 #define DAV_LOCK "LOCK"
45 #define DAV_MKCOL "MKCOL"
46 #define DAV_MOVE "MOVE"
47 #define DAV_PROPFIND "PROPFIND"
48 #define DAV_PUT "PUT"
49 #define DAV_UNLOCK "UNLOCK"
50 #define DAV_DELETE "DELETE"
51
52 /* DAV lock flags */
53 #define DAV_PROP_LOCKWR (1u << 0)
54 #define DAV_PROP_LOCKEX (1u << 1)
55 #define DAV_LOCK_OK (1u << 2)
56
57 /* DAV XML properties */
58 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
59 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
60 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
61 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
62 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
63 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
64 #define DAV_PROPFIND_RESP ".multistatus.response"
65 #define DAV_PROPFIND_NAME ".multistatus.response.href"
66 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
67
68 /* DAV request body templates */
69 #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>"
70 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
71 #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>"
72
73 #define LOCK_TIME 600
74 #define LOCK_REFRESH 30
75
76 /* Remember to update object flag allocation in object.h */
77 #define LOCAL (1u<<11)
78 #define REMOTE (1u<<12)
79 #define FETCHING (1u<<13)
80 #define PUSHING (1u<<14)
81
82 /* We allow "recursive" symbolic refs. Only within reason, though */
83 #define MAXDEPTH 5
84
85 static int pushing;
86 static int aborted;
87 static signed char remote_dir_exists[256];
88
89 static int push_verbosely;
90 static int push_all = MATCH_REFS_NONE;
91 static int force_all;
92 static int dry_run;
93 static int helper_status;
94
95 static struct object_list *objects;
96
97 struct repo {
98 char *url;
99 char *path;
100 int path_len;
101 int has_info_refs;
102 int can_update_info_refs;
103 int has_info_packs;
104 struct packed_git *packs;
105 struct remote_lock *locks;
106 };
107
108 static struct repo *repo;
109
110 enum transfer_state {
111 NEED_FETCH,
112 RUN_FETCH_LOOSE,
113 RUN_FETCH_PACKED,
114 NEED_PUSH,
115 RUN_MKCOL,
116 RUN_PUT,
117 RUN_MOVE,
118 ABORTED,
119 COMPLETE
120 };
121
122 struct transfer_request {
123 struct object *obj;
124 struct packed_git *target;
125 char *url;
126 char *dest;
127 struct remote_lock *lock;
128 struct curl_slist *headers;
129 struct buffer buffer;
130 enum transfer_state state;
131 CURLcode curl_result;
132 char errorstr[CURL_ERROR_SIZE];
133 long http_code;
134 void *userData;
135 struct active_request_slot *slot;
136 struct transfer_request *next;
137 };
138
139 static struct transfer_request *request_queue_head;
140
141 struct xml_ctx {
142 char *name;
143 int len;
144 char *cdata;
145 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
146 void *userData;
147 };
148
149 struct remote_lock {
150 char *url;
151 char *owner;
152 char *token;
153 char tmpfile_suffix[GIT_MAX_HEXSZ + 1];
154 time_t start_time;
155 long timeout;
156 int refreshing;
157 struct remote_lock *next;
158 };
159
160 /* Flags that control remote_ls processing */
161 #define PROCESS_FILES (1u << 0)
162 #define PROCESS_DIRS (1u << 1)
163 #define RECURSIVE (1u << 2)
164
165 /* Flags that remote_ls passes to callback functions */
166 #define IS_DIR (1u << 0)
167
168 struct remote_ls_ctx {
169 char *path;
170 void (*userFunc)(struct remote_ls_ctx *ls);
171 void *userData;
172 int flags;
173 char *dentry_name;
174 int dentry_flags;
175 struct remote_ls_ctx *parent;
176 };
177
178 /* get_dav_token_headers options */
179 enum dav_header_flag {
180 DAV_HEADER_IF = (1u << 0),
181 DAV_HEADER_LOCK = (1u << 1),
182 DAV_HEADER_TIMEOUT = (1u << 2)
183 };
184
185 static char *xml_entities(const char *s)
186 {
187 struct strbuf buf = STRBUF_INIT;
188 strbuf_addstr_xml_quoted(&buf, s);
189 return strbuf_detach(&buf, NULL);
190 }
191
192 static void curl_setup_http_get(CURL *curl, const char *url,
193 const char *custom_req)
194 {
195 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
196 curl_easy_setopt(curl, CURLOPT_URL, url);
197 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
198 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
199 }
200
201 static void curl_setup_http(CURL *curl, const char *url,
202 const char *custom_req, struct buffer *buffer,
203 curl_write_callback write_fn)
204 {
205 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
206 curl_easy_setopt(curl, CURLOPT_URL, url);
207 curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
208 curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
209 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
210 curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, seek_buffer);
211 curl_easy_setopt(curl, CURLOPT_SEEKDATA, buffer);
212 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
213 curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
214 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
215 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
216 }
217
218 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
219 {
220 struct strbuf buf = STRBUF_INIT;
221 struct curl_slist *dav_headers = http_copy_default_headers();
222
223 if (options & DAV_HEADER_IF) {
224 strbuf_addf(&buf, "If: (<%s>)", lock->token);
225 dav_headers = curl_slist_append(dav_headers, buf.buf);
226 strbuf_reset(&buf);
227 }
228 if (options & DAV_HEADER_LOCK) {
229 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
230 dav_headers = curl_slist_append(dav_headers, buf.buf);
231 strbuf_reset(&buf);
232 }
233 if (options & DAV_HEADER_TIMEOUT) {
234 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
235 dav_headers = curl_slist_append(dav_headers, buf.buf);
236 strbuf_reset(&buf);
237 }
238 strbuf_release(&buf);
239
240 return dav_headers;
241 }
242
243 static void finish_request(struct transfer_request *request);
244 static void release_request(struct transfer_request *request);
245
246 static void process_response(void *callback_data)
247 {
248 struct transfer_request *request =
249 (struct transfer_request *)callback_data;
250
251 finish_request(request);
252 }
253
254 static void start_fetch_loose(struct transfer_request *request)
255 {
256 struct active_request_slot *slot;
257 struct http_object_request *obj_req;
258
259 obj_req = new_http_object_request(repo->url, &request->obj->oid);
260 if (!obj_req) {
261 request->state = ABORTED;
262 return;
263 }
264
265 slot = obj_req->slot;
266 slot->callback_func = process_response;
267 slot->callback_data = request;
268 request->slot = slot;
269 request->userData = obj_req;
270
271 /* Try to get the request started, abort the request on error */
272 request->state = RUN_FETCH_LOOSE;
273 if (!start_active_slot(slot)) {
274 fprintf(stderr, "Unable to start GET request\n");
275 repo->can_update_info_refs = 0;
276 release_http_object_request(obj_req);
277 release_request(request);
278 }
279 }
280
281 static void start_mkcol(struct transfer_request *request)
282 {
283 char *hex = oid_to_hex(&request->obj->oid);
284 struct active_request_slot *slot;
285
286 request->url = get_remote_object_url(repo->url, hex, 1);
287
288 slot = get_active_slot();
289 slot->callback_func = process_response;
290 slot->callback_data = request;
291 curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
292 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
293
294 if (start_active_slot(slot)) {
295 request->slot = slot;
296 request->state = RUN_MKCOL;
297 } else {
298 request->state = ABORTED;
299 FREE_AND_NULL(request->url);
300 }
301 }
302
303 static void start_fetch_packed(struct transfer_request *request)
304 {
305 struct packed_git *target;
306
307 struct transfer_request *check_request = request_queue_head;
308 struct http_pack_request *preq;
309
310 target = find_sha1_pack(request->obj->oid.hash, repo->packs);
311 if (!target) {
312 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
313 repo->can_update_info_refs = 0;
314 release_request(request);
315 return;
316 }
317 close_pack_index(target);
318 request->target = target;
319
320 fprintf(stderr, "Fetching pack %s\n",
321 hash_to_hex(target->hash));
322 fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
323
324 preq = new_http_pack_request(target->hash, repo->url);
325 if (!preq) {
326 repo->can_update_info_refs = 0;
327 return;
328 }
329
330 /* Make sure there isn't another open request for this pack */
331 while (check_request) {
332 if (check_request->state == RUN_FETCH_PACKED &&
333 !strcmp(check_request->url, preq->url)) {
334 release_http_pack_request(preq);
335 release_request(request);
336 return;
337 }
338 check_request = check_request->next;
339 }
340
341 preq->slot->callback_func = process_response;
342 preq->slot->callback_data = request;
343 request->slot = preq->slot;
344 request->userData = preq;
345
346 /* Try to get the request started, abort the request on error */
347 request->state = RUN_FETCH_PACKED;
348 if (!start_active_slot(preq->slot)) {
349 fprintf(stderr, "Unable to start GET request\n");
350 release_http_pack_request(preq);
351 repo->can_update_info_refs = 0;
352 release_request(request);
353 }
354 }
355
356 static void start_put(struct transfer_request *request)
357 {
358 char *hex = oid_to_hex(&request->obj->oid);
359 struct active_request_slot *slot;
360 struct strbuf buf = STRBUF_INIT;
361 enum object_type type;
362 char hdr[50];
363 void *unpacked;
364 unsigned long len;
365 int hdrlen;
366 ssize_t size;
367 git_zstream stream;
368
369 unpacked = repo_read_object_file(the_repository, &request->obj->oid,
370 &type, &len);
371 hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
372
373 /* Set it up */
374 git_deflate_init(&stream, zlib_compression_level);
375 size = git_deflate_bound(&stream, len + hdrlen);
376 strbuf_init(&request->buffer.buf, size);
377 request->buffer.posn = 0;
378
379 /* Compress it */
380 stream.next_out = (unsigned char *)request->buffer.buf.buf;
381 stream.avail_out = size;
382
383 /* First header.. */
384 stream.next_in = (void *)hdr;
385 stream.avail_in = hdrlen;
386 while (git_deflate(&stream, 0) == Z_OK)
387 ; /* nothing */
388
389 /* Then the data itself.. */
390 stream.next_in = unpacked;
391 stream.avail_in = len;
392 while (git_deflate(&stream, Z_FINISH) == Z_OK)
393 ; /* nothing */
394 git_deflate_end(&stream);
395 free(unpacked);
396
397 request->buffer.buf.len = stream.total_out;
398
399 strbuf_addstr(&buf, "Destination: ");
400 append_remote_object_url(&buf, repo->url, hex, 0);
401 request->dest = strbuf_detach(&buf, NULL);
402
403 append_remote_object_url(&buf, repo->url, hex, 0);
404 strbuf_add(&buf, request->lock->tmpfile_suffix, the_hash_algo->hexsz + 1);
405 request->url = strbuf_detach(&buf, NULL);
406
407 slot = get_active_slot();
408 slot->callback_func = process_response;
409 slot->callback_data = request;
410 curl_setup_http(slot->curl, request->url, DAV_PUT,
411 &request->buffer, fwrite_null);
412
413 if (start_active_slot(slot)) {
414 request->slot = slot;
415 request->state = RUN_PUT;
416 } else {
417 request->state = ABORTED;
418 FREE_AND_NULL(request->url);
419 }
420 }
421
422 static void start_move(struct transfer_request *request)
423 {
424 struct active_request_slot *slot;
425 struct curl_slist *dav_headers = http_copy_default_headers();
426
427 slot = get_active_slot();
428 slot->callback_func = process_response;
429 slot->callback_data = request;
430 curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
431 dav_headers = curl_slist_append(dav_headers, request->dest);
432 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
433 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
434
435 if (start_active_slot(slot)) {
436 request->slot = slot;
437 request->state = RUN_MOVE;
438 } else {
439 request->state = ABORTED;
440 FREE_AND_NULL(request->url);
441 }
442 }
443
444 static int refresh_lock(struct remote_lock *lock)
445 {
446 struct active_request_slot *slot;
447 struct slot_results results;
448 struct curl_slist *dav_headers;
449 int rc = 0;
450
451 lock->refreshing = 1;
452
453 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
454
455 slot = get_active_slot();
456 slot->results = &results;
457 curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
458 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
459
460 if (start_active_slot(slot)) {
461 run_active_slot(slot);
462 if (results.curl_result != CURLE_OK) {
463 fprintf(stderr, "LOCK HTTP error %ld\n",
464 results.http_code);
465 } else {
466 lock->start_time = time(NULL);
467 rc = 1;
468 }
469 }
470
471 lock->refreshing = 0;
472 curl_slist_free_all(dav_headers);
473
474 return rc;
475 }
476
477 static void check_locks(void)
478 {
479 struct remote_lock *lock = repo->locks;
480 time_t current_time = time(NULL);
481 int time_remaining;
482
483 while (lock) {
484 time_remaining = lock->start_time + lock->timeout -
485 current_time;
486 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
487 if (!refresh_lock(lock)) {
488 fprintf(stderr,
489 "Unable to refresh lock for %s\n",
490 lock->url);
491 aborted = 1;
492 return;
493 }
494 }
495 lock = lock->next;
496 }
497 }
498
499 static void release_request(struct transfer_request *request)
500 {
501 struct transfer_request *entry = request_queue_head;
502
503 if (request == request_queue_head) {
504 request_queue_head = request->next;
505 } else {
506 while (entry && entry->next != request)
507 entry = entry->next;
508 if (entry)
509 entry->next = request->next;
510 }
511
512 free(request->url);
513 free(request);
514 }
515
516 static void finish_request(struct transfer_request *request)
517 {
518 struct http_pack_request *preq;
519 struct http_object_request *obj_req;
520
521 request->curl_result = request->slot->curl_result;
522 request->http_code = request->slot->http_code;
523 request->slot = NULL;
524
525 /* Keep locks active */
526 check_locks();
527
528 if (request->headers)
529 curl_slist_free_all(request->headers);
530
531 /* URL is reused for MOVE after PUT and used during FETCH */
532 if (request->state != RUN_PUT && request->state != RUN_FETCH_PACKED) {
533 FREE_AND_NULL(request->url);
534 }
535
536 if (request->state == RUN_MKCOL) {
537 if (request->curl_result == CURLE_OK ||
538 request->http_code == 405) {
539 remote_dir_exists[request->obj->oid.hash[0]] = 1;
540 start_put(request);
541 } else {
542 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
543 oid_to_hex(&request->obj->oid),
544 request->curl_result, request->http_code);
545 request->state = ABORTED;
546 aborted = 1;
547 }
548 } else if (request->state == RUN_PUT) {
549 if (request->curl_result == CURLE_OK) {
550 start_move(request);
551 } else {
552 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
553 oid_to_hex(&request->obj->oid),
554 request->curl_result, request->http_code);
555 request->state = ABORTED;
556 aborted = 1;
557 }
558 } else if (request->state == RUN_MOVE) {
559 if (request->curl_result == CURLE_OK) {
560 if (push_verbosely)
561 fprintf(stderr, " sent %s\n",
562 oid_to_hex(&request->obj->oid));
563 request->obj->flags |= REMOTE;
564 release_request(request);
565 } else {
566 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
567 oid_to_hex(&request->obj->oid),
568 request->curl_result, request->http_code);
569 request->state = ABORTED;
570 aborted = 1;
571 }
572 } else if (request->state == RUN_FETCH_LOOSE) {
573 obj_req = (struct http_object_request *)request->userData;
574
575 if (finish_http_object_request(obj_req) == 0)
576 if (obj_req->rename == 0)
577 request->obj->flags |= (LOCAL | REMOTE);
578
579 /* Try fetching packed if necessary */
580 if (request->obj->flags & LOCAL) {
581 release_http_object_request(obj_req);
582 release_request(request);
583 } else
584 start_fetch_packed(request);
585
586 } else if (request->state == RUN_FETCH_PACKED) {
587 int fail = 1;
588 if (request->curl_result != CURLE_OK) {
589 fprintf(stderr, "Unable to get pack file %s\n%s",
590 request->url, curl_errorstr);
591 } else {
592 preq = (struct http_pack_request *)request->userData;
593
594 if (preq) {
595 if (finish_http_pack_request(preq) == 0)
596 fail = 0;
597 release_http_pack_request(preq);
598 }
599 }
600 if (fail)
601 repo->can_update_info_refs = 0;
602 else
603 http_install_packfile(request->target, &repo->packs);
604 release_request(request);
605 }
606 }
607
608 static int is_running_queue;
609 static int fill_active_slot(void *data UNUSED)
610 {
611 struct transfer_request *request;
612
613 if (aborted || !is_running_queue)
614 return 0;
615
616 for (request = request_queue_head; request; request = request->next) {
617 if (request->state == NEED_FETCH) {
618 start_fetch_loose(request);
619 return 1;
620 } else if (pushing && request->state == NEED_PUSH) {
621 if (remote_dir_exists[request->obj->oid.hash[0]] == 1) {
622 start_put(request);
623 } else {
624 start_mkcol(request);
625 }
626 return 1;
627 }
628 }
629 return 0;
630 }
631
632 static void get_remote_object_list(unsigned char parent);
633
634 static void add_fetch_request(struct object *obj)
635 {
636 struct transfer_request *request;
637
638 check_locks();
639
640 /*
641 * Don't fetch the object if it's known to exist locally
642 * or is already in the request queue
643 */
644 if (remote_dir_exists[obj->oid.hash[0]] == -1)
645 get_remote_object_list(obj->oid.hash[0]);
646 if (obj->flags & (LOCAL | FETCHING))
647 return;
648
649 obj->flags |= FETCHING;
650 request = xmalloc(sizeof(*request));
651 request->obj = obj;
652 request->url = NULL;
653 request->lock = NULL;
654 request->headers = NULL;
655 request->state = NEED_FETCH;
656 request->next = request_queue_head;
657 request_queue_head = request;
658
659 fill_active_slots();
660 step_active_slots();
661 }
662
663 static int add_send_request(struct object *obj, struct remote_lock *lock)
664 {
665 struct transfer_request *request;
666 struct packed_git *target;
667
668 /* Keep locks active */
669 check_locks();
670
671 /*
672 * Don't push the object if it's known to exist on the remote
673 * or is already in the request queue
674 */
675 if (remote_dir_exists[obj->oid.hash[0]] == -1)
676 get_remote_object_list(obj->oid.hash[0]);
677 if (obj->flags & (REMOTE | PUSHING))
678 return 0;
679 target = find_sha1_pack(obj->oid.hash, repo->packs);
680 if (target) {
681 obj->flags |= REMOTE;
682 return 0;
683 }
684
685 obj->flags |= PUSHING;
686 request = xmalloc(sizeof(*request));
687 request->obj = obj;
688 request->url = NULL;
689 request->lock = lock;
690 request->headers = NULL;
691 request->state = NEED_PUSH;
692 request->next = request_queue_head;
693 request_queue_head = request;
694
695 fill_active_slots();
696 step_active_slots();
697
698 return 1;
699 }
700
701 static int fetch_indices(void)
702 {
703 int ret;
704
705 if (push_verbosely)
706 fprintf(stderr, "Getting pack list\n");
707
708 switch (http_get_info_packs(repo->url, &repo->packs)) {
709 case HTTP_OK:
710 case HTTP_MISSING_TARGET:
711 ret = 0;
712 break;
713 default:
714 ret = -1;
715 }
716
717 return ret;
718 }
719
720 static void one_remote_object(const struct object_id *oid)
721 {
722 struct object *obj;
723
724 obj = lookup_object(the_repository, oid);
725 if (!obj)
726 obj = parse_object(the_repository, oid);
727
728 /* Ignore remote objects that don't exist locally */
729 if (!obj)
730 return;
731
732 obj->flags |= REMOTE;
733 if (!object_list_contains(objects, obj))
734 object_list_insert(obj, &objects);
735 }
736
737 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
738 {
739 int *lock_flags = (int *)ctx->userData;
740
741 if (tag_closed) {
742 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
743 if ((*lock_flags & DAV_PROP_LOCKEX) &&
744 (*lock_flags & DAV_PROP_LOCKWR)) {
745 *lock_flags |= DAV_LOCK_OK;
746 }
747 *lock_flags &= DAV_LOCK_OK;
748 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
749 *lock_flags |= DAV_PROP_LOCKWR;
750 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
751 *lock_flags |= DAV_PROP_LOCKEX;
752 }
753 }
754 }
755
756 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
757 {
758 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
759 git_hash_ctx hash_ctx;
760 unsigned char lock_token_hash[GIT_MAX_RAWSZ];
761
762 if (tag_closed && ctx->cdata) {
763 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
764 lock->owner = xstrdup(ctx->cdata);
765 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
766 const char *arg;
767 if (skip_prefix(ctx->cdata, "Second-", &arg))
768 lock->timeout = strtol(arg, NULL, 10);
769 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
770 lock->token = xstrdup(ctx->cdata);
771
772 the_hash_algo->init_fn(&hash_ctx);
773 the_hash_algo->update_fn(&hash_ctx, lock->token, strlen(lock->token));
774 the_hash_algo->final_fn(lock_token_hash, &hash_ctx);
775
776 lock->tmpfile_suffix[0] = '_';
777 memcpy(lock->tmpfile_suffix + 1, hash_to_hex(lock_token_hash), the_hash_algo->hexsz);
778 }
779 }
780 }
781
782 static void one_remote_ref(const char *refname);
783
784 static void
785 xml_start_tag(void *userData, const char *name, const char **atts UNUSED)
786 {
787 struct xml_ctx *ctx = (struct xml_ctx *)userData;
788 const char *c = strchr(name, ':');
789 int old_namelen, new_len;
790
791 if (!c)
792 c = name;
793 else
794 c++;
795
796 old_namelen = strlen(ctx->name);
797 new_len = old_namelen + strlen(c) + 2;
798
799 if (new_len > ctx->len) {
800 ctx->name = xrealloc(ctx->name, new_len);
801 ctx->len = new_len;
802 }
803 xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
804
805 FREE_AND_NULL(ctx->cdata);
806
807 ctx->userFunc(ctx, 0);
808 }
809
810 static void
811 xml_end_tag(void *userData, const char *name)
812 {
813 struct xml_ctx *ctx = (struct xml_ctx *)userData;
814 const char *c = strchr(name, ':');
815 char *ep;
816
817 ctx->userFunc(ctx, 1);
818
819 if (!c)
820 c = name;
821 else
822 c++;
823
824 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
825 *ep = 0;
826 }
827
828 static void
829 xml_cdata(void *userData, const XML_Char *s, int len)
830 {
831 struct xml_ctx *ctx = (struct xml_ctx *)userData;
832 free(ctx->cdata);
833 ctx->cdata = xmemdupz(s, len);
834 }
835
836 static struct remote_lock *lock_remote(const char *path, long timeout)
837 {
838 struct active_request_slot *slot;
839 struct slot_results results;
840 struct buffer out_buffer = { STRBUF_INIT, 0 };
841 struct strbuf in_buffer = STRBUF_INIT;
842 char *url;
843 char *ep;
844 char timeout_header[25];
845 struct remote_lock *lock = NULL;
846 struct curl_slist *dav_headers = http_copy_default_headers();
847 struct xml_ctx ctx;
848 char *escaped;
849
850 url = xstrfmt("%s%s", repo->url, path);
851
852 /* Make sure leading directories exist for the remote ref */
853 ep = strchr(url + strlen(repo->url) + 1, '/');
854 while (ep) {
855 char saved_character = ep[1];
856 ep[1] = '\0';
857 slot = get_active_slot();
858 slot->results = &results;
859 curl_setup_http_get(slot->curl, url, DAV_MKCOL);
860 if (start_active_slot(slot)) {
861 run_active_slot(slot);
862 if (results.curl_result != CURLE_OK &&
863 results.http_code != 405) {
864 fprintf(stderr,
865 "Unable to create branch path %s\n",
866 url);
867 free(url);
868 return NULL;
869 }
870 } else {
871 fprintf(stderr, "Unable to start MKCOL request\n");
872 free(url);
873 return NULL;
874 }
875 ep[1] = saved_character;
876 ep = strchr(ep + 1, '/');
877 }
878
879 escaped = xml_entities(ident_default_email());
880 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
881 free(escaped);
882
883 xsnprintf(timeout_header, sizeof(timeout_header), "Timeout: Second-%ld", timeout);
884 dav_headers = curl_slist_append(dav_headers, timeout_header);
885 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
886
887 slot = get_active_slot();
888 slot->results = &results;
889 curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
890 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
891 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &in_buffer);
892
893 CALLOC_ARRAY(lock, 1);
894 lock->timeout = -1;
895
896 if (start_active_slot(slot)) {
897 run_active_slot(slot);
898 if (results.curl_result == CURLE_OK) {
899 XML_Parser parser = XML_ParserCreate(NULL);
900 enum XML_Status result;
901 ctx.name = xcalloc(10, 1);
902 ctx.len = 0;
903 ctx.cdata = NULL;
904 ctx.userFunc = handle_new_lock_ctx;
905 ctx.userData = lock;
906 XML_SetUserData(parser, &ctx);
907 XML_SetElementHandler(parser, xml_start_tag,
908 xml_end_tag);
909 XML_SetCharacterDataHandler(parser, xml_cdata);
910 result = XML_Parse(parser, in_buffer.buf,
911 in_buffer.len, 1);
912 free(ctx.name);
913 if (result != XML_STATUS_OK) {
914 fprintf(stderr, "XML error: %s\n",
915 XML_ErrorString(
916 XML_GetErrorCode(parser)));
917 lock->timeout = -1;
918 }
919 XML_ParserFree(parser);
920 } else {
921 fprintf(stderr,
922 "error: curl result=%d, HTTP code=%ld\n",
923 results.curl_result, results.http_code);
924 }
925 } else {
926 fprintf(stderr, "Unable to start LOCK request\n");
927 }
928
929 curl_slist_free_all(dav_headers);
930 strbuf_release(&out_buffer.buf);
931 strbuf_release(&in_buffer);
932
933 if (lock->token == NULL || lock->timeout <= 0) {
934 free(lock->token);
935 free(lock->owner);
936 free(url);
937 FREE_AND_NULL(lock);
938 } else {
939 lock->url = url;
940 lock->start_time = time(NULL);
941 lock->next = repo->locks;
942 repo->locks = lock;
943 }
944
945 return lock;
946 }
947
948 static int unlock_remote(struct remote_lock *lock)
949 {
950 struct active_request_slot *slot;
951 struct slot_results results;
952 struct remote_lock *prev = repo->locks;
953 struct curl_slist *dav_headers;
954 int rc = 0;
955
956 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
957
958 slot = get_active_slot();
959 slot->results = &results;
960 curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
961 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
962
963 if (start_active_slot(slot)) {
964 run_active_slot(slot);
965 if (results.curl_result == CURLE_OK)
966 rc = 1;
967 else
968 fprintf(stderr, "UNLOCK HTTP error %ld\n",
969 results.http_code);
970 } else {
971 fprintf(stderr, "Unable to start UNLOCK request\n");
972 }
973
974 curl_slist_free_all(dav_headers);
975
976 if (repo->locks == lock) {
977 repo->locks = lock->next;
978 } else {
979 while (prev && prev->next != lock)
980 prev = prev->next;
981 if (prev)
982 prev->next = lock->next;
983 }
984
985 free(lock->owner);
986 free(lock->url);
987 free(lock->token);
988 free(lock);
989
990 return rc;
991 }
992
993 static void remove_locks(void)
994 {
995 struct remote_lock *lock = repo->locks;
996
997 fprintf(stderr, "Removing remote locks...\n");
998 while (lock) {
999 struct remote_lock *next = lock->next;
1000 unlock_remote(lock);
1001 lock = next;
1002 }
1003 }
1004
1005 static void remove_locks_on_signal(int signo)
1006 {
1007 remove_locks();
1008 sigchain_pop(signo);
1009 raise(signo);
1010 }
1011
1012 static void remote_ls(const char *path, int flags,
1013 void (*userFunc)(struct remote_ls_ctx *ls),
1014 void *userData);
1015
1016 /* extract hex from sharded "xx/x{38}" filename */
1017 static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
1018 {
1019 oid->algo = hash_algo_by_ptr(the_hash_algo);
1020
1021 if (strlen(path) != the_hash_algo->hexsz + 1)
1022 return -1;
1023
1024 if (hex_to_bytes(oid->hash, path, 1))
1025 return -1;
1026 path += 2;
1027 path++; /* skip '/' */
1028
1029 return hex_to_bytes(oid->hash + 1, path, the_hash_algo->rawsz - 1);
1030 }
1031
1032 static void process_ls_object(struct remote_ls_ctx *ls)
1033 {
1034 unsigned int *parent = (unsigned int *)ls->userData;
1035 const char *path = ls->dentry_name;
1036 struct object_id oid;
1037
1038 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1039 remote_dir_exists[*parent] = 1;
1040 return;
1041 }
1042
1043 if (!skip_prefix(path, "objects/", &path) ||
1044 get_oid_hex_from_objpath(path, &oid))
1045 return;
1046
1047 one_remote_object(&oid);
1048 }
1049
1050 static void process_ls_ref(struct remote_ls_ctx *ls)
1051 {
1052 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1053 fprintf(stderr, " %s\n", ls->dentry_name);
1054 return;
1055 }
1056
1057 if (!(ls->dentry_flags & IS_DIR))
1058 one_remote_ref(ls->dentry_name);
1059 }
1060
1061 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1062 {
1063 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1064
1065 if (tag_closed) {
1066 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1067 if (ls->dentry_flags & IS_DIR) {
1068
1069 /* ensure collection names end with slash */
1070 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1071
1072 if (ls->flags & PROCESS_DIRS) {
1073 ls->userFunc(ls);
1074 }
1075 if (strcmp(ls->dentry_name, ls->path) &&
1076 ls->flags & RECURSIVE) {
1077 remote_ls(ls->dentry_name,
1078 ls->flags,
1079 ls->userFunc,
1080 ls->userData);
1081 }
1082 } else if (ls->flags & PROCESS_FILES) {
1083 ls->userFunc(ls);
1084 }
1085 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1086 char *path = ctx->cdata;
1087 if (*ctx->cdata == 'h') {
1088 path = strstr(path, "//");
1089 if (path) {
1090 path = strchr(path+2, '/');
1091 }
1092 }
1093 if (path) {
1094 const char *url = repo->url;
1095 if (repo->path)
1096 url = repo->path;
1097 if (strncmp(path, url, repo->path_len))
1098 error("Parsed path '%s' does not match url: '%s'",
1099 path, url);
1100 else {
1101 path += repo->path_len;
1102 ls->dentry_name = xstrdup(path);
1103 }
1104 }
1105 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1106 ls->dentry_flags |= IS_DIR;
1107 }
1108 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1109 FREE_AND_NULL(ls->dentry_name);
1110 ls->dentry_flags = 0;
1111 }
1112 }
1113
1114 /*
1115 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1116 * should _only_ heed the information from that file, instead of trying to
1117 * determine the refs from the remote file system (badly: it does not even
1118 * know about packed-refs).
1119 */
1120 static void remote_ls(const char *path, int flags,
1121 void (*userFunc)(struct remote_ls_ctx *ls),
1122 void *userData)
1123 {
1124 char *url = xstrfmt("%s%s", repo->url, path);
1125 struct active_request_slot *slot;
1126 struct slot_results results;
1127 struct strbuf in_buffer = STRBUF_INIT;
1128 struct buffer out_buffer = { STRBUF_INIT, 0 };
1129 struct curl_slist *dav_headers = http_copy_default_headers();
1130 struct xml_ctx ctx;
1131 struct remote_ls_ctx ls;
1132
1133 ls.flags = flags;
1134 ls.path = xstrdup(path);
1135 ls.dentry_name = NULL;
1136 ls.dentry_flags = 0;
1137 ls.userData = userData;
1138 ls.userFunc = userFunc;
1139
1140 strbuf_addstr(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1141
1142 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1143 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1144
1145 slot = get_active_slot();
1146 slot->results = &results;
1147 curl_setup_http(slot->curl, url, DAV_PROPFIND,
1148 &out_buffer, fwrite_buffer);
1149 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1150 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &in_buffer);
1151
1152 if (start_active_slot(slot)) {
1153 run_active_slot(slot);
1154 if (results.curl_result == CURLE_OK) {
1155 XML_Parser parser = XML_ParserCreate(NULL);
1156 enum XML_Status result;
1157 ctx.name = xcalloc(10, 1);
1158 ctx.len = 0;
1159 ctx.cdata = NULL;
1160 ctx.userFunc = handle_remote_ls_ctx;
1161 ctx.userData = &ls;
1162 XML_SetUserData(parser, &ctx);
1163 XML_SetElementHandler(parser, xml_start_tag,
1164 xml_end_tag);
1165 XML_SetCharacterDataHandler(parser, xml_cdata);
1166 result = XML_Parse(parser, in_buffer.buf,
1167 in_buffer.len, 1);
1168 free(ctx.name);
1169
1170 if (result != XML_STATUS_OK) {
1171 fprintf(stderr, "XML error: %s\n",
1172 XML_ErrorString(
1173 XML_GetErrorCode(parser)));
1174 }
1175 XML_ParserFree(parser);
1176 }
1177 } else {
1178 fprintf(stderr, "Unable to start PROPFIND request\n");
1179 }
1180
1181 free(ls.path);
1182 free(url);
1183 strbuf_release(&out_buffer.buf);
1184 strbuf_release(&in_buffer);
1185 curl_slist_free_all(dav_headers);
1186 }
1187
1188 static void get_remote_object_list(unsigned char parent)
1189 {
1190 char path[] = "objects/XX/";
1191 static const char hex[] = "0123456789abcdef";
1192 unsigned int val = parent;
1193
1194 path[8] = hex[val >> 4];
1195 path[9] = hex[val & 0xf];
1196 remote_dir_exists[val] = 0;
1197 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1198 process_ls_object, &val);
1199 }
1200
1201 static int locking_available(void)
1202 {
1203 struct active_request_slot *slot;
1204 struct slot_results results;
1205 struct strbuf in_buffer = STRBUF_INIT;
1206 struct buffer out_buffer = { STRBUF_INIT, 0 };
1207 struct curl_slist *dav_headers = http_copy_default_headers();
1208 struct xml_ctx ctx;
1209 int lock_flags = 0;
1210 char *escaped;
1211
1212 escaped = xml_entities(repo->url);
1213 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1214 free(escaped);
1215
1216 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1217 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1218
1219 slot = get_active_slot();
1220 slot->results = &results;
1221 curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
1222 &out_buffer, fwrite_buffer);
1223 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1224 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &in_buffer);
1225
1226 if (start_active_slot(slot)) {
1227 run_active_slot(slot);
1228 if (results.curl_result == CURLE_OK) {
1229 XML_Parser parser = XML_ParserCreate(NULL);
1230 enum XML_Status result;
1231 ctx.name = xcalloc(10, 1);
1232 ctx.len = 0;
1233 ctx.cdata = NULL;
1234 ctx.userFunc = handle_lockprop_ctx;
1235 ctx.userData = &lock_flags;
1236 XML_SetUserData(parser, &ctx);
1237 XML_SetElementHandler(parser, xml_start_tag,
1238 xml_end_tag);
1239 result = XML_Parse(parser, in_buffer.buf,
1240 in_buffer.len, 1);
1241 free(ctx.name);
1242
1243 if (result != XML_STATUS_OK) {
1244 fprintf(stderr, "XML error: %s\n",
1245 XML_ErrorString(
1246 XML_GetErrorCode(parser)));
1247 lock_flags = 0;
1248 }
1249 XML_ParserFree(parser);
1250 if (!lock_flags)
1251 error("no DAV locking support on %s",
1252 repo->url);
1253
1254 } else {
1255 error("Cannot access URL %s, return code %d",
1256 repo->url, results.curl_result);
1257 lock_flags = 0;
1258 }
1259 } else {
1260 error("Unable to start PROPFIND request on %s", repo->url);
1261 }
1262
1263 strbuf_release(&out_buffer.buf);
1264 strbuf_release(&in_buffer);
1265 curl_slist_free_all(dav_headers);
1266
1267 return lock_flags;
1268 }
1269
1270 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1271 {
1272 struct object_list *entry = xmalloc(sizeof(struct object_list));
1273 entry->item = obj;
1274 entry->next = *p;
1275 *p = entry;
1276 return &entry->next;
1277 }
1278
1279 static struct object_list **process_blob(struct blob *blob,
1280 struct object_list **p)
1281 {
1282 struct object *obj = &blob->object;
1283
1284 obj->flags |= LOCAL;
1285
1286 if (obj->flags & (UNINTERESTING | SEEN))
1287 return p;
1288
1289 obj->flags |= SEEN;
1290 return add_one_object(obj, p);
1291 }
1292
1293 static struct object_list **process_tree(struct tree *tree,
1294 struct object_list **p)
1295 {
1296 struct object *obj = &tree->object;
1297 struct tree_desc desc;
1298 struct name_entry entry;
1299
1300 obj->flags |= LOCAL;
1301
1302 if (obj->flags & (UNINTERESTING | SEEN))
1303 return p;
1304 if (parse_tree(tree) < 0)
1305 die("bad tree object %s", oid_to_hex(&obj->oid));
1306
1307 obj->flags |= SEEN;
1308 p = add_one_object(obj, p);
1309
1310 init_tree_desc(&desc, tree->buffer, tree->size);
1311
1312 while (tree_entry(&desc, &entry))
1313 switch (object_type(entry.mode)) {
1314 case OBJ_TREE:
1315 p = process_tree(lookup_tree(the_repository, &entry.oid),
1316 p);
1317 break;
1318 case OBJ_BLOB:
1319 p = process_blob(lookup_blob(the_repository, &entry.oid),
1320 p);
1321 break;
1322 default:
1323 /* Subproject commit - not in this repository */
1324 break;
1325 }
1326
1327 free_tree_buffer(tree);
1328 return p;
1329 }
1330
1331 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1332 {
1333 int i;
1334 struct commit *commit;
1335 struct object_list **p = &objects;
1336 int count = 0;
1337
1338 while ((commit = get_revision(revs)) != NULL) {
1339 p = process_tree(repo_get_commit_tree(the_repository, commit),
1340 p);
1341 commit->object.flags |= LOCAL;
1342 if (!(commit->object.flags & UNINTERESTING))
1343 count += add_send_request(&commit->object, lock);
1344 }
1345
1346 for (i = 0; i < revs->pending.nr; i++) {
1347 struct object_array_entry *entry = revs->pending.objects + i;
1348 struct object *obj = entry->item;
1349 const char *name = entry->name;
1350
1351 if (obj->flags & (UNINTERESTING | SEEN))
1352 continue;
1353 if (obj->type == OBJ_TAG) {
1354 obj->flags |= SEEN;
1355 p = add_one_object(obj, p);
1356 continue;
1357 }
1358 if (obj->type == OBJ_TREE) {
1359 p = process_tree((struct tree *)obj, p);
1360 continue;
1361 }
1362 if (obj->type == OBJ_BLOB) {
1363 p = process_blob((struct blob *)obj, p);
1364 continue;
1365 }
1366 die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);
1367 }
1368
1369 while (objects) {
1370 if (!(objects->item->flags & UNINTERESTING))
1371 count += add_send_request(objects->item, lock);
1372 objects = objects->next;
1373 }
1374
1375 return count;
1376 }
1377
1378 static int update_remote(const struct object_id *oid, struct remote_lock *lock)
1379 {
1380 struct active_request_slot *slot;
1381 struct slot_results results;
1382 struct buffer out_buffer = { STRBUF_INIT, 0 };
1383 struct curl_slist *dav_headers;
1384
1385 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1386
1387 strbuf_addf(&out_buffer.buf, "%s\n", oid_to_hex(oid));
1388
1389 slot = get_active_slot();
1390 slot->results = &results;
1391 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1392 &out_buffer, fwrite_null);
1393 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1394
1395 if (start_active_slot(slot)) {
1396 run_active_slot(slot);
1397 strbuf_release(&out_buffer.buf);
1398 if (results.curl_result != CURLE_OK) {
1399 fprintf(stderr,
1400 "PUT error: curl result=%d, HTTP code=%ld\n",
1401 results.curl_result, results.http_code);
1402 /* We should attempt recovery? */
1403 return 0;
1404 }
1405 } else {
1406 strbuf_release(&out_buffer.buf);
1407 fprintf(stderr, "Unable to start PUT request\n");
1408 return 0;
1409 }
1410
1411 return 1;
1412 }
1413
1414 static struct ref *remote_refs;
1415
1416 static void one_remote_ref(const char *refname)
1417 {
1418 struct ref *ref;
1419 struct object *obj;
1420
1421 ref = alloc_ref(refname);
1422
1423 if (http_fetch_ref(repo->url, ref) != 0) {
1424 fprintf(stderr,
1425 "Unable to fetch ref %s from %s\n",
1426 refname, repo->url);
1427 free(ref);
1428 return;
1429 }
1430
1431 /*
1432 * Fetch a copy of the object if it doesn't exist locally - it
1433 * may be required for updating server info later.
1434 */
1435 if (repo->can_update_info_refs && !repo_has_object_file(the_repository, &ref->old_oid)) {
1436 obj = lookup_unknown_object(the_repository, &ref->old_oid);
1437 fprintf(stderr, " fetch %s for %s\n",
1438 oid_to_hex(&ref->old_oid), refname);
1439 add_fetch_request(obj);
1440 }
1441
1442 ref->next = remote_refs;
1443 remote_refs = ref;
1444 }
1445
1446 static void get_dav_remote_heads(void)
1447 {
1448 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1449 }
1450
1451 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1452 {
1453 struct strbuf *buf = (struct strbuf *)ls->userData;
1454 struct object *o;
1455 struct ref *ref;
1456
1457 ref = alloc_ref(ls->dentry_name);
1458
1459 if (http_fetch_ref(repo->url, ref) != 0) {
1460 fprintf(stderr,
1461 "Unable to fetch ref %s from %s\n",
1462 ls->dentry_name, repo->url);
1463 aborted = 1;
1464 free(ref);
1465 return;
1466 }
1467
1468 o = parse_object(the_repository, &ref->old_oid);
1469 if (!o) {
1470 fprintf(stderr,
1471 "Unable to parse object %s for remote ref %s\n",
1472 oid_to_hex(&ref->old_oid), ls->dentry_name);
1473 aborted = 1;
1474 free(ref);
1475 return;
1476 }
1477
1478 strbuf_addf(buf, "%s\t%s\n",
1479 oid_to_hex(&ref->old_oid), ls->dentry_name);
1480
1481 if (o->type == OBJ_TAG) {
1482 o = deref_tag(the_repository, o, ls->dentry_name, 0);
1483 if (o)
1484 strbuf_addf(buf, "%s\t%s^{}\n",
1485 oid_to_hex(&o->oid), ls->dentry_name);
1486 }
1487 free(ref);
1488 }
1489
1490 static void update_remote_info_refs(struct remote_lock *lock)
1491 {
1492 struct buffer buffer = { STRBUF_INIT, 0 };
1493 struct active_request_slot *slot;
1494 struct slot_results results;
1495 struct curl_slist *dav_headers;
1496
1497 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1498 add_remote_info_ref, &buffer.buf);
1499 if (!aborted) {
1500 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1501
1502 slot = get_active_slot();
1503 slot->results = &results;
1504 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1505 &buffer, fwrite_null);
1506 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1507
1508 if (start_active_slot(slot)) {
1509 run_active_slot(slot);
1510 if (results.curl_result != CURLE_OK) {
1511 fprintf(stderr,
1512 "PUT error: curl result=%d, HTTP code=%ld\n",
1513 results.curl_result, results.http_code);
1514 }
1515 }
1516 }
1517 strbuf_release(&buffer.buf);
1518 }
1519
1520 static int remote_exists(const char *path)
1521 {
1522 char *url = xstrfmt("%s%s", repo->url, path);
1523 int ret;
1524
1525
1526 switch (http_get_strbuf(url, NULL, NULL)) {
1527 case HTTP_OK:
1528 ret = 1;
1529 break;
1530 case HTTP_MISSING_TARGET:
1531 ret = 0;
1532 break;
1533 case HTTP_ERROR:
1534 error("unable to access '%s': %s", url, curl_errorstr);
1535 /* fallthrough */
1536 default:
1537 ret = -1;
1538 }
1539 free(url);
1540 return ret;
1541 }
1542
1543 static void fetch_symref(const char *path, char **symref, struct object_id *oid)
1544 {
1545 char *url = xstrfmt("%s%s", repo->url, path);
1546 struct strbuf buffer = STRBUF_INIT;
1547 const char *name;
1548
1549 if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
1550 die("Couldn't get %s for remote symref\n%s", url,
1551 curl_errorstr);
1552 free(url);
1553
1554 FREE_AND_NULL(*symref);
1555 oidclr(oid);
1556
1557 if (buffer.len == 0)
1558 return;
1559
1560 /* Cut off trailing newline. */
1561 strbuf_rtrim(&buffer);
1562
1563 /* If it's a symref, set the refname; otherwise try for a sha1 */
1564 if (skip_prefix(buffer.buf, "ref: ", &name)) {
1565 *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
1566 } else {
1567 get_oid_hex(buffer.buf, oid);
1568 }
1569
1570 strbuf_release(&buffer);
1571 }
1572
1573 static int verify_merge_base(struct object_id *head_oid, struct ref *remote)
1574 {
1575 struct commit *head = lookup_commit_or_die(head_oid, "HEAD");
1576 struct commit *branch = lookup_commit_or_die(&remote->old_oid,
1577 remote->name);
1578
1579 return repo_in_merge_bases(the_repository, branch, head);
1580 }
1581
1582 static int delete_remote_branch(const char *pattern, int force)
1583 {
1584 struct ref *refs = remote_refs;
1585 struct ref *remote_ref = NULL;
1586 struct object_id head_oid;
1587 char *symref = NULL;
1588 int match;
1589 int patlen = strlen(pattern);
1590 int i;
1591 struct active_request_slot *slot;
1592 struct slot_results results;
1593 char *url;
1594
1595 /* Find the remote branch(es) matching the specified branch name */
1596 for (match = 0; refs; refs = refs->next) {
1597 char *name = refs->name;
1598 int namelen = strlen(name);
1599 if (namelen < patlen ||
1600 memcmp(name + namelen - patlen, pattern, patlen))
1601 continue;
1602 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1603 continue;
1604 match++;
1605 remote_ref = refs;
1606 }
1607 if (match == 0)
1608 return error("No remote branch matches %s", pattern);
1609 if (match != 1)
1610 return error("More than one remote branch matches %s",
1611 pattern);
1612
1613 /*
1614 * Remote HEAD must be a symref (not exactly foolproof; a remote
1615 * symlink to a symref will look like a symref)
1616 */
1617 fetch_symref("HEAD", &symref, &head_oid);
1618 if (!symref)
1619 return error("Remote HEAD is not a symref");
1620
1621 /* Remote branch must not be the remote HEAD */
1622 for (i = 0; symref && i < MAXDEPTH; i++) {
1623 if (!strcmp(remote_ref->name, symref))
1624 return error("Remote branch %s is the current HEAD",
1625 remote_ref->name);
1626 fetch_symref(symref, &symref, &head_oid);
1627 }
1628
1629 /* Run extra sanity checks if delete is not forced */
1630 if (!force) {
1631 /* Remote HEAD must resolve to a known object */
1632 if (symref)
1633 return error("Remote HEAD symrefs too deep");
1634 if (is_null_oid(&head_oid))
1635 return error("Unable to resolve remote HEAD");
1636 if (!repo_has_object_file(the_repository, &head_oid))
1637 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid));
1638
1639 /* Remote branch must resolve to a known object */
1640 if (is_null_oid(&remote_ref->old_oid))
1641 return error("Unable to resolve remote branch %s",
1642 remote_ref->name);
1643 if (!repo_has_object_file(the_repository, &remote_ref->old_oid))
1644 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, oid_to_hex(&remote_ref->old_oid));
1645
1646 /* Remote branch must be an ancestor of remote HEAD */
1647 if (!verify_merge_base(&head_oid, remote_ref)) {
1648 return error("The branch '%s' is not an ancestor "
1649 "of your current HEAD.\n"
1650 "If you are sure you want to delete it,"
1651 " run:\n\t'git http-push -D %s %s'",
1652 remote_ref->name, repo->url, pattern);
1653 }
1654 }
1655
1656 /* Send delete request */
1657 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1658 if (dry_run)
1659 return 0;
1660 url = xstrfmt("%s%s", repo->url, remote_ref->name);
1661 slot = get_active_slot();
1662 slot->results = &results;
1663 curl_setup_http_get(slot->curl, url, DAV_DELETE);
1664 if (start_active_slot(slot)) {
1665 run_active_slot(slot);
1666 free(url);
1667 if (results.curl_result != CURLE_OK)
1668 return error("DELETE request failed (%d/%ld)",
1669 results.curl_result, results.http_code);
1670 } else {
1671 free(url);
1672 return error("Unable to start DELETE request");
1673 }
1674
1675 return 0;
1676 }
1677
1678 static void run_request_queue(void)
1679 {
1680 is_running_queue = 1;
1681 fill_active_slots();
1682 add_fill_function(NULL, fill_active_slot);
1683 do {
1684 finish_all_active_slots();
1685 fill_active_slots();
1686 } while (request_queue_head && !aborted);
1687
1688 is_running_queue = 0;
1689 }
1690
1691 int cmd_main(int argc, const char **argv)
1692 {
1693 struct transfer_request *request;
1694 struct transfer_request *next_request;
1695 struct refspec rs = REFSPEC_INIT_PUSH;
1696 struct remote_lock *ref_lock = NULL;
1697 struct remote_lock *info_ref_lock = NULL;
1698 int delete_branch = 0;
1699 int force_delete = 0;
1700 int objects_to_send;
1701 int rc = 0;
1702 int i;
1703 int new_refs;
1704 struct ref *ref, *local_refs;
1705
1706 CALLOC_ARRAY(repo, 1);
1707
1708 argv++;
1709 for (i = 1; i < argc; i++, argv++) {
1710 const char *arg = *argv;
1711
1712 if (*arg == '-') {
1713 if (!strcmp(arg, "--all")) {
1714 push_all = MATCH_REFS_ALL;
1715 continue;
1716 }
1717 if (!strcmp(arg, "--force")) {
1718 force_all = 1;
1719 continue;
1720 }
1721 if (!strcmp(arg, "--dry-run")) {
1722 dry_run = 1;
1723 continue;
1724 }
1725 if (!strcmp(arg, "--helper-status")) {
1726 helper_status = 1;
1727 continue;
1728 }
1729 if (!strcmp(arg, "--verbose")) {
1730 push_verbosely = 1;
1731 http_is_verbose = 1;
1732 continue;
1733 }
1734 if (!strcmp(arg, "-d")) {
1735 delete_branch = 1;
1736 continue;
1737 }
1738 if (!strcmp(arg, "-D")) {
1739 delete_branch = 1;
1740 force_delete = 1;
1741 continue;
1742 }
1743 if (!strcmp(arg, "-h"))
1744 usage(http_push_usage);
1745 }
1746 if (!repo->url) {
1747 char *path = strstr(arg, "//");
1748 str_end_url_with_slash(arg, &repo->url);
1749 repo->path_len = strlen(repo->url);
1750 if (path) {
1751 repo->path = strchr(path+2, '/');
1752 if (repo->path)
1753 repo->path_len = strlen(repo->path);
1754 }
1755 continue;
1756 }
1757 refspec_appendn(&rs, argv, argc - i);
1758 break;
1759 }
1760
1761 if (!repo->url)
1762 usage(http_push_usage);
1763
1764 if (delete_branch && rs.nr != 1)
1765 die("You must specify only one branch name when deleting a remote branch");
1766
1767 setup_git_directory();
1768
1769 memset(remote_dir_exists, -1, 256);
1770
1771 http_init(NULL, repo->url, 1);
1772
1773 is_running_queue = 0;
1774
1775 /* Verify DAV compliance/lock support */
1776 if (!locking_available()) {
1777 rc = 1;
1778 goto cleanup;
1779 }
1780
1781 sigchain_push_common(remove_locks_on_signal);
1782
1783 /* Check whether the remote has server info files */
1784 repo->can_update_info_refs = 0;
1785 repo->has_info_refs = remote_exists("info/refs");
1786 repo->has_info_packs = remote_exists("objects/info/packs");
1787 if (repo->has_info_refs) {
1788 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1789 if (info_ref_lock)
1790 repo->can_update_info_refs = 1;
1791 else {
1792 error("cannot lock existing info/refs");
1793 rc = 1;
1794 goto cleanup;
1795 }
1796 }
1797 if (repo->has_info_packs)
1798 fetch_indices();
1799
1800 /* Get a list of all local and remote heads to validate refspecs */
1801 local_refs = get_local_heads();
1802 fprintf(stderr, "Fetching remote heads...\n");
1803 get_dav_remote_heads();
1804 run_request_queue();
1805
1806 /* Remove a remote branch if -d or -D was specified */
1807 if (delete_branch) {
1808 const char *branch = rs.items[i].src;
1809 if (delete_remote_branch(branch, force_delete) == -1) {
1810 fprintf(stderr, "Unable to delete remote branch %s\n",
1811 branch);
1812 if (helper_status)
1813 printf("error %s cannot remove\n", branch);
1814 }
1815 goto cleanup;
1816 }
1817
1818 /* match them up */
1819 if (match_push_refs(local_refs, &remote_refs, &rs, push_all)) {
1820 rc = -1;
1821 goto cleanup;
1822 }
1823 if (!remote_refs) {
1824 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1825 if (helper_status)
1826 printf("error null no match\n");
1827 rc = 0;
1828 goto cleanup;
1829 }
1830
1831 new_refs = 0;
1832 for (ref = remote_refs; ref; ref = ref->next) {
1833 struct rev_info revs;
1834 struct strvec commit_argv = STRVEC_INIT;
1835
1836 if (!ref->peer_ref)
1837 continue;
1838
1839 if (is_null_oid(&ref->peer_ref->new_oid)) {
1840 if (delete_remote_branch(ref->name, 1) == -1) {
1841 error("Could not remove %s", ref->name);
1842 if (helper_status)
1843 printf("error %s cannot remove\n", ref->name);
1844 rc = -4;
1845 }
1846 else if (helper_status)
1847 printf("ok %s\n", ref->name);
1848 new_refs++;
1849 continue;
1850 }
1851
1852 if (oideq(&ref->old_oid, &ref->peer_ref->new_oid)) {
1853 if (push_verbosely)
1854 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1855 if (helper_status)
1856 printf("ok %s up to date\n", ref->name);
1857 continue;
1858 }
1859
1860 if (!force_all &&
1861 !is_null_oid(&ref->old_oid) &&
1862 !ref->force) {
1863 if (!repo_has_object_file(the_repository, &ref->old_oid) ||
1864 !ref_newer(&ref->peer_ref->new_oid,
1865 &ref->old_oid)) {
1866 /*
1867 * We do not have the remote ref, or
1868 * we know that the remote ref is not
1869 * an ancestor of what we are trying to
1870 * push. Either way this can be losing
1871 * commits at the remote end and likely
1872 * we were not up to date to begin with.
1873 */
1874 error("remote '%s' is not an ancestor of\n"
1875 "local '%s'.\n"
1876 "Maybe you are not up-to-date and "
1877 "need to pull first?",
1878 ref->name,
1879 ref->peer_ref->name);
1880 if (helper_status)
1881 printf("error %s non-fast forward\n", ref->name);
1882 rc = -2;
1883 continue;
1884 }
1885 }
1886 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
1887 new_refs++;
1888
1889 fprintf(stderr, "updating '%s'", ref->name);
1890 if (strcmp(ref->name, ref->peer_ref->name))
1891 fprintf(stderr, " using '%s'", ref->peer_ref->name);
1892 fprintf(stderr, "\n from %s\n to %s\n",
1893 oid_to_hex(&ref->old_oid), oid_to_hex(&ref->new_oid));
1894 if (dry_run) {
1895 if (helper_status)
1896 printf("ok %s\n", ref->name);
1897 continue;
1898 }
1899
1900 /* Lock remote branch ref */
1901 ref_lock = lock_remote(ref->name, LOCK_TIME);
1902 if (!ref_lock) {
1903 fprintf(stderr, "Unable to lock remote branch %s\n",
1904 ref->name);
1905 if (helper_status)
1906 printf("error %s lock error\n", ref->name);
1907 rc = 1;
1908 continue;
1909 }
1910
1911 /* Set up revision info for this refspec */
1912 strvec_push(&commit_argv, ""); /* ignored */
1913 strvec_push(&commit_argv, "--objects");
1914 strvec_push(&commit_argv, oid_to_hex(&ref->new_oid));
1915 if (!push_all && !is_null_oid(&ref->old_oid))
1916 strvec_pushf(&commit_argv, "^%s",
1917 oid_to_hex(&ref->old_oid));
1918 repo_init_revisions(the_repository, &revs, setup_git_directory());
1919 setup_revisions(commit_argv.nr, commit_argv.v, &revs, NULL);
1920 revs.edge_hint = 0; /* just in case */
1921
1922 /* Generate a list of objects that need to be pushed */
1923 pushing = 0;
1924 if (prepare_revision_walk(&revs))
1925 die("revision walk setup failed");
1926 mark_edges_uninteresting(&revs, NULL, 0);
1927 objects_to_send = get_delta(&revs, ref_lock);
1928 finish_all_active_slots();
1929
1930 /* Push missing objects to remote, this would be a
1931 convenient time to pack them first if appropriate. */
1932 pushing = 1;
1933 if (objects_to_send)
1934 fprintf(stderr, " sending %d objects\n",
1935 objects_to_send);
1936
1937 run_request_queue();
1938
1939 /* Update the remote branch if all went well */
1940 if (aborted || !update_remote(&ref->new_oid, ref_lock))
1941 rc = 1;
1942
1943 if (!rc)
1944 fprintf(stderr, " done\n");
1945 if (helper_status)
1946 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
1947 unlock_remote(ref_lock);
1948 check_locks();
1949 strvec_clear(&commit_argv);
1950 release_revisions(&revs);
1951 }
1952
1953 /* Update remote server info if appropriate */
1954 if (repo->has_info_refs && new_refs) {
1955 if (info_ref_lock && repo->can_update_info_refs) {
1956 fprintf(stderr, "Updating remote server info\n");
1957 if (!dry_run)
1958 update_remote_info_refs(info_ref_lock);
1959 } else {
1960 fprintf(stderr, "Unable to update server info\n");
1961 }
1962 }
1963
1964 cleanup:
1965 if (info_ref_lock)
1966 unlock_remote(info_ref_lock);
1967 free(repo);
1968
1969 http_cleanup();
1970
1971 request = request_queue_head;
1972 while (request != NULL) {
1973 next_request = request->next;
1974 release_request(request);
1975 request = next_request;
1976 }
1977
1978 return rc;
1979 }