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