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