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