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