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