4b941f4d3e37e191688de64a99809909cbbed8ef
1#include "cache.h"
2#include "commit.h"
3#include "pack.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
14#include <expat.h>
15
16static const char http_push_usage[] =
17"git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
18
19#ifndef XML_STATUS_OK
20enum XML_Status {
21 XML_STATUS_OK = 1,
22 XML_STATUS_ERROR = 0
23};
24#define XML_STATUS_OK 1
25#define XML_STATUS_ERROR 0
26#endif
27
28#define PREV_BUF_SIZE 4096
29#define RANGE_HEADER_SIZE 30
30
31/* DAV methods */
32#define DAV_LOCK "LOCK"
33#define DAV_MKCOL "MKCOL"
34#define DAV_MOVE "MOVE"
35#define DAV_PROPFIND "PROPFIND"
36#define DAV_PUT "PUT"
37#define DAV_UNLOCK "UNLOCK"
38#define DAV_DELETE "DELETE"
39
40/* DAV lock flags */
41#define DAV_PROP_LOCKWR (1u << 0)
42#define DAV_PROP_LOCKEX (1u << 1)
43#define DAV_LOCK_OK (1u << 2)
44
45/* DAV XML properties */
46#define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
47#define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
48#define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
49#define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
50#define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
51#define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
52#define DAV_PROPFIND_RESP ".multistatus.response"
53#define DAV_PROPFIND_NAME ".multistatus.response.href"
54#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
55
56/* DAV request body templates */
57#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>"
58#define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
59#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>"
60
61#define LOCK_TIME 600
62#define LOCK_REFRESH 30
63
64/* bits #0-15 in revision.h */
65
66#define LOCAL (1u<<16)
67#define REMOTE (1u<<17)
68#define FETCHING (1u<<18)
69#define PUSHING (1u<<19)
70
71/* We allow "recursive" symbolic refs. Only within reason, though */
72#define MAXDEPTH 5
73
74static int pushing;
75static int aborted;
76static signed char remote_dir_exists[256];
77
78static struct curl_slist *no_pragma_header;
79
80static int push_verbosely;
81static int push_all = MATCH_REFS_NONE;
82static int force_all;
83static int dry_run;
84
85static struct object_list *objects;
86
87struct repo
88{
89 char *url;
90 char *path;
91 int path_len;
92 int has_info_refs;
93 int can_update_info_refs;
94 int has_info_packs;
95 struct packed_git *packs;
96 struct remote_lock *locks;
97};
98
99static struct repo *remote;
100
101enum transfer_state {
102 NEED_FETCH,
103 RUN_FETCH_LOOSE,
104 RUN_FETCH_PACKED,
105 NEED_PUSH,
106 RUN_MKCOL,
107 RUN_PUT,
108 RUN_MOVE,
109 ABORTED,
110 COMPLETE,
111};
112
113struct transfer_request
114{
115 struct object *obj;
116 char *url;
117 char *dest;
118 struct remote_lock *lock;
119 struct curl_slist *headers;
120 struct buffer buffer;
121 char filename[PATH_MAX];
122 char tmpfile[PATH_MAX];
123 int local_fileno;
124 FILE *local_stream;
125 enum transfer_state state;
126 CURLcode curl_result;
127 char errorstr[CURL_ERROR_SIZE];
128 long http_code;
129 unsigned char real_sha1[20];
130 git_SHA_CTX c;
131 z_stream stream;
132 int zret;
133 int rename;
134 void *userData;
135 struct active_request_slot *slot;
136 struct transfer_request *next;
137};
138
139static struct transfer_request *request_queue_head;
140
141struct xml_ctx
142{
143 char *name;
144 int len;
145 char *cdata;
146 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
147 void *userData;
148};
149
150struct remote_lock
151{
152 char *url;
153 char *owner;
154 char *token;
155 time_t start_time;
156 long timeout;
157 int refreshing;
158 struct remote_lock *next;
159};
160
161/* Flags that control remote_ls processing */
162#define PROCESS_FILES (1u << 0)
163#define PROCESS_DIRS (1u << 1)
164#define RECURSIVE (1u << 2)
165
166/* Flags that remote_ls passes to callback functions */
167#define IS_DIR (1u << 0)
168
169struct remote_ls_ctx
170{
171 char *path;
172 void (*userFunc)(struct remote_ls_ctx *ls);
173 void *userData;
174 int flags;
175 char *dentry_name;
176 int dentry_flags;
177 struct remote_ls_ctx *parent;
178};
179
180/* get_dav_token_headers options */
181enum dav_header_flag {
182 DAV_HEADER_IF = (1u << 0),
183 DAV_HEADER_LOCK = (1u << 1),
184 DAV_HEADER_TIMEOUT = (1u << 2)
185};
186
187static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
188{
189 struct strbuf buf = STRBUF_INIT;
190 struct curl_slist *dav_headers = NULL;
191
192 if (options & DAV_HEADER_IF) {
193 strbuf_addf(&buf, "If: (<%s>)", lock->token);
194 dav_headers = curl_slist_append(dav_headers, buf.buf);
195 strbuf_reset(&buf);
196 }
197 if (options & DAV_HEADER_LOCK) {
198 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
199 dav_headers = curl_slist_append(dav_headers, buf.buf);
200 strbuf_reset(&buf);
201 }
202 if (options & DAV_HEADER_TIMEOUT) {
203 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
204 dav_headers = curl_slist_append(dav_headers, buf.buf);
205 strbuf_reset(&buf);
206 }
207 strbuf_release(&buf);
208
209 return dav_headers;
210}
211
212static void append_remote_object_url(struct strbuf *buf, const char *url,
213 const char *hex,
214 int only_two_digit_prefix)
215{
216 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
217 if (!only_two_digit_prefix)
218 strbuf_addf(buf, "%s", hex+2);
219}
220
221static void finish_request(struct transfer_request *request);
222static void release_request(struct transfer_request *request);
223
224static void process_response(void *callback_data)
225{
226 struct transfer_request *request =
227 (struct transfer_request *)callback_data;
228
229 finish_request(request);
230}
231
232#ifdef USE_CURL_MULTI
233
234static char *get_remote_object_url(const char *url, const char *hex, int only_two_digit_prefix)
235{
236 struct strbuf buf = STRBUF_INIT;
237 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
238 return strbuf_detach(&buf, NULL);
239}
240
241static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
242 void *data)
243{
244 unsigned char expn[4096];
245 size_t size = eltsize * nmemb;
246 int posn = 0;
247 struct transfer_request *request = (struct transfer_request *)data;
248 do {
249 ssize_t retval = xwrite(request->local_fileno,
250 (char *) ptr + posn, size - posn);
251 if (retval < 0)
252 return posn;
253 posn += retval;
254 } while (posn < size);
255
256 request->stream.avail_in = size;
257 request->stream.next_in = ptr;
258 do {
259 request->stream.next_out = expn;
260 request->stream.avail_out = sizeof(expn);
261 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
262 git_SHA1_Update(&request->c, expn,
263 sizeof(expn) - request->stream.avail_out);
264 } while (request->stream.avail_in && request->zret == Z_OK);
265 data_received++;
266 return size;
267}
268
269static void start_fetch_loose(struct transfer_request *request)
270{
271 char *hex = sha1_to_hex(request->obj->sha1);
272 char *filename;
273 char prevfile[PATH_MAX];
274 char *url;
275 int prevlocal;
276 unsigned char prev_buf[PREV_BUF_SIZE];
277 ssize_t prev_read = 0;
278 long prev_posn = 0;
279 char range[RANGE_HEADER_SIZE];
280 struct curl_slist *range_header = NULL;
281 struct active_request_slot *slot;
282
283 filename = sha1_file_name(request->obj->sha1);
284 snprintf(request->filename, sizeof(request->filename), "%s", filename);
285 snprintf(request->tmpfile, sizeof(request->tmpfile),
286 "%s.temp", filename);
287
288 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
289 unlink(prevfile);
290 rename(request->tmpfile, prevfile);
291 unlink(request->tmpfile);
292
293 if (request->local_fileno != -1)
294 error("fd leakage in start: %d", request->local_fileno);
295 request->local_fileno = open(request->tmpfile,
296 O_WRONLY | O_CREAT | O_EXCL, 0666);
297 /* This could have failed due to the "lazy directory creation";
298 * try to mkdir the last path component.
299 */
300 if (request->local_fileno < 0 && errno == ENOENT) {
301 char *dir = strrchr(request->tmpfile, '/');
302 if (dir) {
303 *dir = 0;
304 mkdir(request->tmpfile, 0777);
305 *dir = '/';
306 }
307 request->local_fileno = open(request->tmpfile,
308 O_WRONLY | O_CREAT | O_EXCL, 0666);
309 }
310
311 if (request->local_fileno < 0) {
312 request->state = ABORTED;
313 error("Couldn't create temporary file %s for %s: %s",
314 request->tmpfile, request->filename, strerror(errno));
315 return;
316 }
317
318 memset(&request->stream, 0, sizeof(request->stream));
319
320 git_inflate_init(&request->stream);
321
322 git_SHA1_Init(&request->c);
323
324 url = get_remote_object_url(remote->url, hex, 0);
325 request->url = xstrdup(url);
326
327 /* If a previous temp file is present, process what was already
328 fetched. */
329 prevlocal = open(prevfile, O_RDONLY);
330 if (prevlocal != -1) {
331 do {
332 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
333 if (prev_read>0) {
334 if (fwrite_sha1_file(prev_buf,
335 1,
336 prev_read,
337 request) == prev_read) {
338 prev_posn += prev_read;
339 } else {
340 prev_read = -1;
341 }
342 }
343 } while (prev_read > 0);
344 close(prevlocal);
345 }
346 unlink(prevfile);
347
348 /* Reset inflate/SHA1 if there was an error reading the previous temp
349 file; also rewind to the beginning of the local file. */
350 if (prev_read == -1) {
351 memset(&request->stream, 0, sizeof(request->stream));
352 git_inflate_init(&request->stream);
353 git_SHA1_Init(&request->c);
354 if (prev_posn>0) {
355 prev_posn = 0;
356 lseek(request->local_fileno, 0, SEEK_SET);
357 ftruncate(request->local_fileno, 0);
358 }
359 }
360
361 slot = get_active_slot();
362 slot->callback_func = process_response;
363 slot->callback_data = request;
364 request->slot = slot;
365
366 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
367 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
368 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
369 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
370 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
371
372 /* If we have successfully processed data from a previous fetch
373 attempt, only fetch the data we don't already have. */
374 if (prev_posn>0) {
375 if (push_verbosely)
376 fprintf(stderr,
377 "Resuming fetch of object %s at byte %ld\n",
378 hex, prev_posn);
379 sprintf(range, "Range: bytes=%ld-", prev_posn);
380 range_header = curl_slist_append(range_header, range);
381 curl_easy_setopt(slot->curl,
382 CURLOPT_HTTPHEADER, range_header);
383 }
384
385 /* Try to get the request started, abort the request on error */
386 request->state = RUN_FETCH_LOOSE;
387 if (!start_active_slot(slot)) {
388 fprintf(stderr, "Unable to start GET request\n");
389 remote->can_update_info_refs = 0;
390 release_request(request);
391 }
392}
393
394static void start_mkcol(struct transfer_request *request)
395{
396 char *hex = sha1_to_hex(request->obj->sha1);
397 struct active_request_slot *slot;
398
399 request->url = get_remote_object_url(remote->url, hex, 1);
400
401 slot = get_active_slot();
402 slot->callback_func = process_response;
403 slot->callback_data = request;
404 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
405 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
406 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
407 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
408 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
409
410 if (start_active_slot(slot)) {
411 request->slot = slot;
412 request->state = RUN_MKCOL;
413 } else {
414 request->state = ABORTED;
415 free(request->url);
416 request->url = NULL;
417 }
418}
419#endif
420
421static void start_fetch_packed(struct transfer_request *request)
422{
423 char *url;
424 struct packed_git *target;
425 FILE *packfile;
426 char *filename;
427 long prev_posn = 0;
428 char range[RANGE_HEADER_SIZE];
429 struct curl_slist *range_header = NULL;
430
431 struct transfer_request *check_request = request_queue_head;
432 struct active_request_slot *slot;
433
434 target = find_sha1_pack(request->obj->sha1, remote->packs);
435 if (!target) {
436 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
437 remote->can_update_info_refs = 0;
438 release_request(request);
439 return;
440 }
441
442 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
443 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
444
445 filename = sha1_pack_name(target->sha1);
446 snprintf(request->filename, sizeof(request->filename), "%s", filename);
447 snprintf(request->tmpfile, sizeof(request->tmpfile),
448 "%s.temp", filename);
449
450 url = xmalloc(strlen(remote->url) + 64);
451 sprintf(url, "%sobjects/pack/pack-%s.pack",
452 remote->url, sha1_to_hex(target->sha1));
453
454 /* Make sure there isn't another open request for this pack */
455 while (check_request) {
456 if (check_request->state == RUN_FETCH_PACKED &&
457 !strcmp(check_request->url, url)) {
458 free(url);
459 release_request(request);
460 return;
461 }
462 check_request = check_request->next;
463 }
464
465 packfile = fopen(request->tmpfile, "a");
466 if (!packfile) {
467 fprintf(stderr, "Unable to open local file %s for pack",
468 request->tmpfile);
469 remote->can_update_info_refs = 0;
470 free(url);
471 return;
472 }
473
474 slot = get_active_slot();
475 slot->callback_func = process_response;
476 slot->callback_data = request;
477 request->slot = slot;
478 request->local_stream = packfile;
479 request->userData = target;
480
481 request->url = url;
482 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
483 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
484 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
485 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
486 slot->local = packfile;
487
488 /* If there is data present from a previous transfer attempt,
489 resume where it left off */
490 prev_posn = ftell(packfile);
491 if (prev_posn>0) {
492 if (push_verbosely)
493 fprintf(stderr,
494 "Resuming fetch of pack %s at byte %ld\n",
495 sha1_to_hex(target->sha1), prev_posn);
496 sprintf(range, "Range: bytes=%ld-", prev_posn);
497 range_header = curl_slist_append(range_header, range);
498 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
499 }
500
501 /* Try to get the request started, abort the request on error */
502 request->state = RUN_FETCH_PACKED;
503 if (!start_active_slot(slot)) {
504 fprintf(stderr, "Unable to start GET request\n");
505 remote->can_update_info_refs = 0;
506 release_request(request);
507 }
508}
509
510static void start_put(struct transfer_request *request)
511{
512 char *hex = sha1_to_hex(request->obj->sha1);
513 struct active_request_slot *slot;
514 struct strbuf buf = STRBUF_INIT;
515 enum object_type type;
516 char hdr[50];
517 void *unpacked;
518 unsigned long len;
519 int hdrlen;
520 ssize_t size;
521 z_stream stream;
522
523 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
524 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
525
526 /* Set it up */
527 memset(&stream, 0, sizeof(stream));
528 deflateInit(&stream, zlib_compression_level);
529 size = deflateBound(&stream, len + hdrlen);
530 strbuf_init(&request->buffer.buf, size);
531 request->buffer.posn = 0;
532
533 /* Compress it */
534 stream.next_out = (unsigned char *)request->buffer.buf.buf;
535 stream.avail_out = size;
536
537 /* First header.. */
538 stream.next_in = (void *)hdr;
539 stream.avail_in = hdrlen;
540 while (deflate(&stream, 0) == Z_OK)
541 /* nothing */;
542
543 /* Then the data itself.. */
544 stream.next_in = unpacked;
545 stream.avail_in = len;
546 while (deflate(&stream, Z_FINISH) == Z_OK)
547 /* nothing */;
548 deflateEnd(&stream);
549 free(unpacked);
550
551 request->buffer.buf.len = stream.total_out;
552
553 strbuf_addstr(&buf, "Destination: ");
554 append_remote_object_url(&buf, remote->url, hex, 0);
555 request->dest = strbuf_detach(&buf, NULL);
556
557 append_remote_object_url(&buf, remote->url, hex, 0);
558 strbuf_addstr(&buf, "_");
559 strbuf_addstr(&buf, request->lock->token);
560 request->url = strbuf_detach(&buf, NULL);
561
562 slot = get_active_slot();
563 slot->callback_func = process_response;
564 slot->callback_data = request;
565 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
566 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
567 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
568 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
569 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
570 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
571 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
572 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
573 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
574
575 if (start_active_slot(slot)) {
576 request->slot = slot;
577 request->state = RUN_PUT;
578 } else {
579 request->state = ABORTED;
580 free(request->url);
581 request->url = NULL;
582 }
583}
584
585static void start_move(struct transfer_request *request)
586{
587 struct active_request_slot *slot;
588 struct curl_slist *dav_headers = NULL;
589
590 slot = get_active_slot();
591 slot->callback_func = process_response;
592 slot->callback_data = request;
593 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
594 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
595 dav_headers = curl_slist_append(dav_headers, request->dest);
596 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
597 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
598 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
599 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
600
601 if (start_active_slot(slot)) {
602 request->slot = slot;
603 request->state = RUN_MOVE;
604 } else {
605 request->state = ABORTED;
606 free(request->url);
607 request->url = NULL;
608 }
609}
610
611static int refresh_lock(struct remote_lock *lock)
612{
613 struct active_request_slot *slot;
614 struct slot_results results;
615 struct curl_slist *dav_headers;
616 int rc = 0;
617
618 lock->refreshing = 1;
619
620 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
621
622 slot = get_active_slot();
623 slot->results = &results;
624 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
625 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
626 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
627 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
628 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
629
630 if (start_active_slot(slot)) {
631 run_active_slot(slot);
632 if (results.curl_result != CURLE_OK) {
633 fprintf(stderr, "LOCK HTTP error %ld\n",
634 results.http_code);
635 } else {
636 lock->start_time = time(NULL);
637 rc = 1;
638 }
639 }
640
641 lock->refreshing = 0;
642 curl_slist_free_all(dav_headers);
643
644 return rc;
645}
646
647static void check_locks(void)
648{
649 struct remote_lock *lock = remote->locks;
650 time_t current_time = time(NULL);
651 int time_remaining;
652
653 while (lock) {
654 time_remaining = lock->start_time + lock->timeout -
655 current_time;
656 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
657 if (!refresh_lock(lock)) {
658 fprintf(stderr,
659 "Unable to refresh lock for %s\n",
660 lock->url);
661 aborted = 1;
662 return;
663 }
664 }
665 lock = lock->next;
666 }
667}
668
669static void release_request(struct transfer_request *request)
670{
671 struct transfer_request *entry = request_queue_head;
672
673 if (request == request_queue_head) {
674 request_queue_head = request->next;
675 } else {
676 while (entry->next != NULL && entry->next != request)
677 entry = entry->next;
678 if (entry->next == request)
679 entry->next = entry->next->next;
680 }
681
682 if (request->local_fileno != -1)
683 close(request->local_fileno);
684 if (request->local_stream)
685 fclose(request->local_stream);
686 free(request->url);
687 free(request);
688}
689
690static void finish_request(struct transfer_request *request)
691{
692 struct stat st;
693 struct packed_git *target;
694 struct packed_git **lst;
695
696 request->curl_result = request->slot->curl_result;
697 request->http_code = request->slot->http_code;
698 request->slot = NULL;
699
700 /* Keep locks active */
701 check_locks();
702
703 if (request->headers != NULL)
704 curl_slist_free_all(request->headers);
705
706 /* URL is reused for MOVE after PUT */
707 if (request->state != RUN_PUT) {
708 free(request->url);
709 request->url = NULL;
710 }
711
712 if (request->state == RUN_MKCOL) {
713 if (request->curl_result == CURLE_OK ||
714 request->http_code == 405) {
715 remote_dir_exists[request->obj->sha1[0]] = 1;
716 start_put(request);
717 } else {
718 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
719 sha1_to_hex(request->obj->sha1),
720 request->curl_result, request->http_code);
721 request->state = ABORTED;
722 aborted = 1;
723 }
724 } else if (request->state == RUN_PUT) {
725 if (request->curl_result == CURLE_OK) {
726 start_move(request);
727 } else {
728 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
729 sha1_to_hex(request->obj->sha1),
730 request->curl_result, request->http_code);
731 request->state = ABORTED;
732 aborted = 1;
733 }
734 } else if (request->state == RUN_MOVE) {
735 if (request->curl_result == CURLE_OK) {
736 if (push_verbosely)
737 fprintf(stderr, " sent %s\n",
738 sha1_to_hex(request->obj->sha1));
739 request->obj->flags |= REMOTE;
740 release_request(request);
741 } else {
742 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
743 sha1_to_hex(request->obj->sha1),
744 request->curl_result, request->http_code);
745 request->state = ABORTED;
746 aborted = 1;
747 }
748 } else if (request->state == RUN_FETCH_LOOSE) {
749 fchmod(request->local_fileno, 0444);
750 close(request->local_fileno); request->local_fileno = -1;
751
752 if (request->curl_result != CURLE_OK &&
753 request->http_code != 416) {
754 if (stat(request->tmpfile, &st) == 0) {
755 if (st.st_size == 0)
756 unlink(request->tmpfile);
757 }
758 } else {
759 if (request->http_code == 416)
760 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
761
762 git_inflate_end(&request->stream);
763 git_SHA1_Final(request->real_sha1, &request->c);
764 if (request->zret != Z_STREAM_END) {
765 unlink(request->tmpfile);
766 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
767 unlink(request->tmpfile);
768 } else {
769 request->rename =
770 move_temp_to_file(
771 request->tmpfile,
772 request->filename);
773 if (request->rename == 0) {
774 request->obj->flags |= (LOCAL | REMOTE);
775 }
776 }
777 }
778
779 /* Try fetching packed if necessary */
780 if (request->obj->flags & LOCAL)
781 release_request(request);
782 else
783 start_fetch_packed(request);
784
785 } else if (request->state == RUN_FETCH_PACKED) {
786 if (request->curl_result != CURLE_OK) {
787 fprintf(stderr, "Unable to get pack file %s\n%s",
788 request->url, curl_errorstr);
789 remote->can_update_info_refs = 0;
790 } else {
791 off_t pack_size = ftell(request->local_stream);
792
793 fclose(request->local_stream);
794 request->local_stream = NULL;
795 if (!move_temp_to_file(request->tmpfile,
796 request->filename)) {
797 target = (struct packed_git *)request->userData;
798 target->pack_size = pack_size;
799 lst = &remote->packs;
800 while (*lst != target)
801 lst = &((*lst)->next);
802 *lst = (*lst)->next;
803
804 if (!verify_pack(target))
805 install_packed_git(target);
806 else
807 remote->can_update_info_refs = 0;
808 }
809 }
810 release_request(request);
811 }
812}
813
814#ifdef USE_CURL_MULTI
815static int fill_active_slot(void *unused)
816{
817 struct transfer_request *request = request_queue_head;
818
819 if (aborted)
820 return 0;
821
822 for (request = request_queue_head; request; request = request->next) {
823 if (request->state == NEED_FETCH) {
824 start_fetch_loose(request);
825 return 1;
826 } else if (pushing && request->state == NEED_PUSH) {
827 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
828 start_put(request);
829 } else {
830 start_mkcol(request);
831 }
832 return 1;
833 }
834 }
835 return 0;
836}
837#endif
838
839static void get_remote_object_list(unsigned char parent);
840
841static void add_fetch_request(struct object *obj)
842{
843 struct transfer_request *request;
844
845 check_locks();
846
847 /*
848 * Don't fetch the object if it's known to exist locally
849 * or is already in the request queue
850 */
851 if (remote_dir_exists[obj->sha1[0]] == -1)
852 get_remote_object_list(obj->sha1[0]);
853 if (obj->flags & (LOCAL | FETCHING))
854 return;
855
856 obj->flags |= FETCHING;
857 request = xmalloc(sizeof(*request));
858 request->obj = obj;
859 request->url = NULL;
860 request->lock = NULL;
861 request->headers = NULL;
862 request->local_fileno = -1;
863 request->local_stream = NULL;
864 request->state = NEED_FETCH;
865 request->next = request_queue_head;
866 request_queue_head = request;
867
868#ifdef USE_CURL_MULTI
869 fill_active_slots();
870 step_active_slots();
871#endif
872}
873
874static int add_send_request(struct object *obj, struct remote_lock *lock)
875{
876 struct transfer_request *request = request_queue_head;
877 struct packed_git *target;
878
879 /* Keep locks active */
880 check_locks();
881
882 /*
883 * Don't push the object if it's known to exist on the remote
884 * or is already in the request queue
885 */
886 if (remote_dir_exists[obj->sha1[0]] == -1)
887 get_remote_object_list(obj->sha1[0]);
888 if (obj->flags & (REMOTE | PUSHING))
889 return 0;
890 target = find_sha1_pack(obj->sha1, remote->packs);
891 if (target) {
892 obj->flags |= REMOTE;
893 return 0;
894 }
895
896 obj->flags |= PUSHING;
897 request = xmalloc(sizeof(*request));
898 request->obj = obj;
899 request->url = NULL;
900 request->lock = lock;
901 request->headers = NULL;
902 request->local_fileno = -1;
903 request->local_stream = NULL;
904 request->state = NEED_PUSH;
905 request->next = request_queue_head;
906 request_queue_head = request;
907
908#ifdef USE_CURL_MULTI
909 fill_active_slots();
910 step_active_slots();
911#endif
912
913 return 1;
914}
915
916static int fetch_index(unsigned char *sha1)
917{
918 char *hex = sha1_to_hex(sha1);
919 char *filename;
920 char *url;
921 char tmpfile[PATH_MAX];
922 long prev_posn = 0;
923 char range[RANGE_HEADER_SIZE];
924 struct curl_slist *range_header = NULL;
925
926 FILE *indexfile;
927 struct active_request_slot *slot;
928 struct slot_results results;
929
930 /* Don't use the index if the pack isn't there */
931 url = xmalloc(strlen(remote->url) + 64);
932 sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
933 slot = get_active_slot();
934 slot->results = &results;
935 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
936 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
937 if (start_active_slot(slot)) {
938 run_active_slot(slot);
939 if (results.curl_result != CURLE_OK) {
940 free(url);
941 return error("Unable to verify pack %s is available",
942 hex);
943 }
944 } else {
945 free(url);
946 return error("Unable to start request");
947 }
948
949 if (has_pack_index(sha1)) {
950 free(url);
951 return 0;
952 }
953
954 if (push_verbosely)
955 fprintf(stderr, "Getting index for pack %s\n", hex);
956
957 sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
958
959 filename = sha1_pack_index_name(sha1);
960 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
961 indexfile = fopen(tmpfile, "a");
962 if (!indexfile) {
963 free(url);
964 return error("Unable to open local file %s for pack index",
965 tmpfile);
966 }
967
968 slot = get_active_slot();
969 slot->results = &results;
970 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
971 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
972 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
973 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
974 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
975 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
976 slot->local = indexfile;
977
978 /* If there is data present from a previous transfer attempt,
979 resume where it left off */
980 prev_posn = ftell(indexfile);
981 if (prev_posn>0) {
982 if (push_verbosely)
983 fprintf(stderr,
984 "Resuming fetch of index for pack %s at byte %ld\n",
985 hex, prev_posn);
986 sprintf(range, "Range: bytes=%ld-", prev_posn);
987 range_header = curl_slist_append(range_header, range);
988 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
989 }
990
991 if (start_active_slot(slot)) {
992 run_active_slot(slot);
993 if (results.curl_result != CURLE_OK) {
994 free(url);
995 fclose(indexfile);
996 return error("Unable to get pack index %s\n%s", url,
997 curl_errorstr);
998 }
999 } else {
1000 free(url);
1001 fclose(indexfile);
1002 return error("Unable to start request");
1003 }
1004
1005 free(url);
1006 fclose(indexfile);
1007
1008 return move_temp_to_file(tmpfile, filename);
1009}
1010
1011static int setup_index(unsigned char *sha1)
1012{
1013 struct packed_git *new_pack;
1014
1015 if (fetch_index(sha1))
1016 return -1;
1017
1018 new_pack = parse_pack_index(sha1);
1019 new_pack->next = remote->packs;
1020 remote->packs = new_pack;
1021 return 0;
1022}
1023
1024static int fetch_indices(void)
1025{
1026 unsigned char sha1[20];
1027 char *url;
1028 struct strbuf buffer = STRBUF_INIT;
1029 char *data;
1030 int i = 0;
1031
1032 struct active_request_slot *slot;
1033 struct slot_results results;
1034
1035 if (push_verbosely)
1036 fprintf(stderr, "Getting pack list\n");
1037
1038 url = xmalloc(strlen(remote->url) + 20);
1039 sprintf(url, "%sobjects/info/packs", remote->url);
1040
1041 slot = get_active_slot();
1042 slot->results = &results;
1043 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1044 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1045 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1046 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1047 if (start_active_slot(slot)) {
1048 run_active_slot(slot);
1049 if (results.curl_result != CURLE_OK) {
1050 strbuf_release(&buffer);
1051 free(url);
1052 if (results.http_code == 404)
1053 return 0;
1054 else
1055 return error("%s", curl_errorstr);
1056 }
1057 } else {
1058 strbuf_release(&buffer);
1059 free(url);
1060 return error("Unable to start request");
1061 }
1062 free(url);
1063
1064 data = buffer.buf;
1065 while (i < buffer.len) {
1066 switch (data[i]) {
1067 case 'P':
1068 i++;
1069 if (i + 52 < buffer.len &&
1070 !prefixcmp(data + i, " pack-") &&
1071 !prefixcmp(data + i + 46, ".pack\n")) {
1072 get_sha1_hex(data + i + 6, sha1);
1073 setup_index(sha1);
1074 i += 51;
1075 break;
1076 }
1077 default:
1078 while (data[i] != '\n')
1079 i++;
1080 }
1081 i++;
1082 }
1083
1084 strbuf_release(&buffer);
1085 return 0;
1086}
1087
1088static void one_remote_object(const char *hex)
1089{
1090 unsigned char sha1[20];
1091 struct object *obj;
1092
1093 if (get_sha1_hex(hex, sha1) != 0)
1094 return;
1095
1096 obj = lookup_object(sha1);
1097 if (!obj)
1098 obj = parse_object(sha1);
1099
1100 /* Ignore remote objects that don't exist locally */
1101 if (!obj)
1102 return;
1103
1104 obj->flags |= REMOTE;
1105 if (!object_list_contains(objects, obj))
1106 object_list_insert(obj, &objects);
1107}
1108
1109static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1110{
1111 int *lock_flags = (int *)ctx->userData;
1112
1113 if (tag_closed) {
1114 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1115 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1116 (*lock_flags & DAV_PROP_LOCKWR)) {
1117 *lock_flags |= DAV_LOCK_OK;
1118 }
1119 *lock_flags &= DAV_LOCK_OK;
1120 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1121 *lock_flags |= DAV_PROP_LOCKWR;
1122 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1123 *lock_flags |= DAV_PROP_LOCKEX;
1124 }
1125 }
1126}
1127
1128static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1129{
1130 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1131
1132 if (tag_closed && ctx->cdata) {
1133 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1134 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1135 strcpy(lock->owner, ctx->cdata);
1136 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1137 if (!prefixcmp(ctx->cdata, "Second-"))
1138 lock->timeout =
1139 strtol(ctx->cdata + 7, NULL, 10);
1140 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1141 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1142 strcpy(lock->token, ctx->cdata);
1143 }
1144 }
1145}
1146
1147static void one_remote_ref(char *refname);
1148
1149static void
1150xml_start_tag(void *userData, const char *name, const char **atts)
1151{
1152 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1153 const char *c = strchr(name, ':');
1154 int new_len;
1155
1156 if (c == NULL)
1157 c = name;
1158 else
1159 c++;
1160
1161 new_len = strlen(ctx->name) + strlen(c) + 2;
1162
1163 if (new_len > ctx->len) {
1164 ctx->name = xrealloc(ctx->name, new_len);
1165 ctx->len = new_len;
1166 }
1167 strcat(ctx->name, ".");
1168 strcat(ctx->name, c);
1169
1170 free(ctx->cdata);
1171 ctx->cdata = NULL;
1172
1173 ctx->userFunc(ctx, 0);
1174}
1175
1176static void
1177xml_end_tag(void *userData, const char *name)
1178{
1179 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1180 const char *c = strchr(name, ':');
1181 char *ep;
1182
1183 ctx->userFunc(ctx, 1);
1184
1185 if (c == NULL)
1186 c = name;
1187 else
1188 c++;
1189
1190 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1191 *ep = 0;
1192}
1193
1194static void
1195xml_cdata(void *userData, const XML_Char *s, int len)
1196{
1197 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1198 free(ctx->cdata);
1199 ctx->cdata = xmemdupz(s, len);
1200}
1201
1202static struct remote_lock *lock_remote(const char *path, long timeout)
1203{
1204 struct active_request_slot *slot;
1205 struct slot_results results;
1206 struct buffer out_buffer = { STRBUF_INIT, 0 };
1207 struct strbuf in_buffer = STRBUF_INIT;
1208 char *url;
1209 char *ep;
1210 char timeout_header[25];
1211 struct remote_lock *lock = NULL;
1212 struct curl_slist *dav_headers = NULL;
1213 struct xml_ctx ctx;
1214
1215 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1216 sprintf(url, "%s%s", remote->url, path);
1217
1218 /* Make sure leading directories exist for the remote ref */
1219 ep = strchr(url + strlen(remote->url) + 1, '/');
1220 while (ep) {
1221 char saved_character = ep[1];
1222 ep[1] = '\0';
1223 slot = get_active_slot();
1224 slot->results = &results;
1225 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1226 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1227 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1228 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1229 if (start_active_slot(slot)) {
1230 run_active_slot(slot);
1231 if (results.curl_result != CURLE_OK &&
1232 results.http_code != 405) {
1233 fprintf(stderr,
1234 "Unable to create branch path %s\n",
1235 url);
1236 free(url);
1237 return NULL;
1238 }
1239 } else {
1240 fprintf(stderr, "Unable to start MKCOL request\n");
1241 free(url);
1242 return NULL;
1243 }
1244 ep[1] = saved_character;
1245 ep = strchr(ep + 1, '/');
1246 }
1247
1248 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
1249
1250 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1251 dav_headers = curl_slist_append(dav_headers, timeout_header);
1252 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1253
1254 slot = get_active_slot();
1255 slot->results = &results;
1256 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1257 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1258 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1259 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1260 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1261 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1262 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1263 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1264 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1265
1266 lock = xcalloc(1, sizeof(*lock));
1267 lock->timeout = -1;
1268
1269 if (start_active_slot(slot)) {
1270 run_active_slot(slot);
1271 if (results.curl_result == CURLE_OK) {
1272 XML_Parser parser = XML_ParserCreate(NULL);
1273 enum XML_Status result;
1274 ctx.name = xcalloc(10, 1);
1275 ctx.len = 0;
1276 ctx.cdata = NULL;
1277 ctx.userFunc = handle_new_lock_ctx;
1278 ctx.userData = lock;
1279 XML_SetUserData(parser, &ctx);
1280 XML_SetElementHandler(parser, xml_start_tag,
1281 xml_end_tag);
1282 XML_SetCharacterDataHandler(parser, xml_cdata);
1283 result = XML_Parse(parser, in_buffer.buf,
1284 in_buffer.len, 1);
1285 free(ctx.name);
1286 if (result != XML_STATUS_OK) {
1287 fprintf(stderr, "XML error: %s\n",
1288 XML_ErrorString(
1289 XML_GetErrorCode(parser)));
1290 lock->timeout = -1;
1291 }
1292 XML_ParserFree(parser);
1293 }
1294 } else {
1295 fprintf(stderr, "Unable to start LOCK request\n");
1296 }
1297
1298 curl_slist_free_all(dav_headers);
1299 strbuf_release(&out_buffer.buf);
1300 strbuf_release(&in_buffer);
1301
1302 if (lock->token == NULL || lock->timeout <= 0) {
1303 free(lock->token);
1304 free(lock->owner);
1305 free(url);
1306 free(lock);
1307 lock = NULL;
1308 } else {
1309 lock->url = url;
1310 lock->start_time = time(NULL);
1311 lock->next = remote->locks;
1312 remote->locks = lock;
1313 }
1314
1315 return lock;
1316}
1317
1318static int unlock_remote(struct remote_lock *lock)
1319{
1320 struct active_request_slot *slot;
1321 struct slot_results results;
1322 struct remote_lock *prev = remote->locks;
1323 struct curl_slist *dav_headers;
1324 int rc = 0;
1325
1326 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1327
1328 slot = get_active_slot();
1329 slot->results = &results;
1330 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1331 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1332 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1333 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1334
1335 if (start_active_slot(slot)) {
1336 run_active_slot(slot);
1337 if (results.curl_result == CURLE_OK)
1338 rc = 1;
1339 else
1340 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1341 results.http_code);
1342 } else {
1343 fprintf(stderr, "Unable to start UNLOCK request\n");
1344 }
1345
1346 curl_slist_free_all(dav_headers);
1347
1348 if (remote->locks == lock) {
1349 remote->locks = lock->next;
1350 } else {
1351 while (prev && prev->next != lock)
1352 prev = prev->next;
1353 if (prev)
1354 prev->next = prev->next->next;
1355 }
1356
1357 free(lock->owner);
1358 free(lock->url);
1359 free(lock->token);
1360 free(lock);
1361
1362 return rc;
1363}
1364
1365static void remove_locks(void)
1366{
1367 struct remote_lock *lock = remote->locks;
1368
1369 fprintf(stderr, "Removing remote locks...\n");
1370 while (lock) {
1371 unlock_remote(lock);
1372 lock = lock->next;
1373 }
1374}
1375
1376static void remove_locks_on_signal(int signo)
1377{
1378 remove_locks();
1379 signal(signo, SIG_DFL);
1380 raise(signo);
1381}
1382
1383static void remote_ls(const char *path, int flags,
1384 void (*userFunc)(struct remote_ls_ctx *ls),
1385 void *userData);
1386
1387static void process_ls_object(struct remote_ls_ctx *ls)
1388{
1389 unsigned int *parent = (unsigned int *)ls->userData;
1390 char *path = ls->dentry_name;
1391 char *obj_hex;
1392
1393 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1394 remote_dir_exists[*parent] = 1;
1395 return;
1396 }
1397
1398 if (strlen(path) != 49)
1399 return;
1400 path += 8;
1401 obj_hex = xmalloc(strlen(path));
1402 /* NB: path is not null-terminated, can not use strlcpy here */
1403 memcpy(obj_hex, path, 2);
1404 strcpy(obj_hex + 2, path + 3);
1405 one_remote_object(obj_hex);
1406 free(obj_hex);
1407}
1408
1409static void process_ls_ref(struct remote_ls_ctx *ls)
1410{
1411 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1412 fprintf(stderr, " %s\n", ls->dentry_name);
1413 return;
1414 }
1415
1416 if (!(ls->dentry_flags & IS_DIR))
1417 one_remote_ref(ls->dentry_name);
1418}
1419
1420static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1421{
1422 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1423
1424 if (tag_closed) {
1425 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1426 if (ls->dentry_flags & IS_DIR) {
1427 if (ls->flags & PROCESS_DIRS) {
1428 ls->userFunc(ls);
1429 }
1430 if (strcmp(ls->dentry_name, ls->path) &&
1431 ls->flags & RECURSIVE) {
1432 remote_ls(ls->dentry_name,
1433 ls->flags,
1434 ls->userFunc,
1435 ls->userData);
1436 }
1437 } else if (ls->flags & PROCESS_FILES) {
1438 ls->userFunc(ls);
1439 }
1440 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1441 char *path = ctx->cdata;
1442 if (*ctx->cdata == 'h') {
1443 path = strstr(path, "//");
1444 if (path) {
1445 path = strchr(path+2, '/');
1446 }
1447 }
1448 if (path) {
1449 path += remote->path_len;
1450 ls->dentry_name = xstrdup(path);
1451 }
1452 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1453 ls->dentry_flags |= IS_DIR;
1454 }
1455 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1456 free(ls->dentry_name);
1457 ls->dentry_name = NULL;
1458 ls->dentry_flags = 0;
1459 }
1460}
1461
1462/*
1463 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1464 * should _only_ heed the information from that file, instead of trying to
1465 * determine the refs from the remote file system (badly: it does not even
1466 * know about packed-refs).
1467 */
1468static void remote_ls(const char *path, int flags,
1469 void (*userFunc)(struct remote_ls_ctx *ls),
1470 void *userData)
1471{
1472 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1473 struct active_request_slot *slot;
1474 struct slot_results results;
1475 struct strbuf in_buffer = STRBUF_INIT;
1476 struct buffer out_buffer = { STRBUF_INIT, 0 };
1477 struct curl_slist *dav_headers = NULL;
1478 struct xml_ctx ctx;
1479 struct remote_ls_ctx ls;
1480
1481 ls.flags = flags;
1482 ls.path = xstrdup(path);
1483 ls.dentry_name = NULL;
1484 ls.dentry_flags = 0;
1485 ls.userData = userData;
1486 ls.userFunc = userFunc;
1487
1488 sprintf(url, "%s%s", remote->url, path);
1489
1490 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1491
1492 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1493 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1494
1495 slot = get_active_slot();
1496 slot->results = &results;
1497 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1498 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1499 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1500 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1501 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1502 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1503 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1504 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1505 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1506
1507 if (start_active_slot(slot)) {
1508 run_active_slot(slot);
1509 if (results.curl_result == CURLE_OK) {
1510 XML_Parser parser = XML_ParserCreate(NULL);
1511 enum XML_Status result;
1512 ctx.name = xcalloc(10, 1);
1513 ctx.len = 0;
1514 ctx.cdata = NULL;
1515 ctx.userFunc = handle_remote_ls_ctx;
1516 ctx.userData = &ls;
1517 XML_SetUserData(parser, &ctx);
1518 XML_SetElementHandler(parser, xml_start_tag,
1519 xml_end_tag);
1520 XML_SetCharacterDataHandler(parser, xml_cdata);
1521 result = XML_Parse(parser, in_buffer.buf,
1522 in_buffer.len, 1);
1523 free(ctx.name);
1524
1525 if (result != XML_STATUS_OK) {
1526 fprintf(stderr, "XML error: %s\n",
1527 XML_ErrorString(
1528 XML_GetErrorCode(parser)));
1529 }
1530 XML_ParserFree(parser);
1531 }
1532 } else {
1533 fprintf(stderr, "Unable to start PROPFIND request\n");
1534 }
1535
1536 free(ls.path);
1537 free(url);
1538 strbuf_release(&out_buffer.buf);
1539 strbuf_release(&in_buffer);
1540 curl_slist_free_all(dav_headers);
1541}
1542
1543static void get_remote_object_list(unsigned char parent)
1544{
1545 char path[] = "objects/XX/";
1546 static const char hex[] = "0123456789abcdef";
1547 unsigned int val = parent;
1548
1549 path[8] = hex[val >> 4];
1550 path[9] = hex[val & 0xf];
1551 remote_dir_exists[val] = 0;
1552 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1553 process_ls_object, &val);
1554}
1555
1556static int locking_available(void)
1557{
1558 struct active_request_slot *slot;
1559 struct slot_results results;
1560 struct strbuf in_buffer = STRBUF_INIT;
1561 struct buffer out_buffer = { STRBUF_INIT, 0 };
1562 struct curl_slist *dav_headers = NULL;
1563 struct xml_ctx ctx;
1564 int lock_flags = 0;
1565
1566 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1567
1568 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1569 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1570
1571 slot = get_active_slot();
1572 slot->results = &results;
1573 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1574 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1575 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1576 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1577 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1578 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1579 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1580 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1581 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1582
1583 if (start_active_slot(slot)) {
1584 run_active_slot(slot);
1585 if (results.curl_result == CURLE_OK) {
1586 XML_Parser parser = XML_ParserCreate(NULL);
1587 enum XML_Status result;
1588 ctx.name = xcalloc(10, 1);
1589 ctx.len = 0;
1590 ctx.cdata = NULL;
1591 ctx.userFunc = handle_lockprop_ctx;
1592 ctx.userData = &lock_flags;
1593 XML_SetUserData(parser, &ctx);
1594 XML_SetElementHandler(parser, xml_start_tag,
1595 xml_end_tag);
1596 result = XML_Parse(parser, in_buffer.buf,
1597 in_buffer.len, 1);
1598 free(ctx.name);
1599
1600 if (result != XML_STATUS_OK) {
1601 fprintf(stderr, "XML error: %s\n",
1602 XML_ErrorString(
1603 XML_GetErrorCode(parser)));
1604 lock_flags = 0;
1605 }
1606 XML_ParserFree(parser);
1607 if (!lock_flags)
1608 error("Error: no DAV locking support on %s",
1609 remote->url);
1610
1611 } else {
1612 error("Cannot access URL %s, return code %d",
1613 remote->url, results.curl_result);
1614 lock_flags = 0;
1615 }
1616 } else {
1617 error("Unable to start PROPFIND request on %s", remote->url);
1618 }
1619
1620 strbuf_release(&out_buffer.buf);
1621 strbuf_release(&in_buffer);
1622 curl_slist_free_all(dav_headers);
1623
1624 return lock_flags;
1625}
1626
1627static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1628{
1629 struct object_list *entry = xmalloc(sizeof(struct object_list));
1630 entry->item = obj;
1631 entry->next = *p;
1632 *p = entry;
1633 return &entry->next;
1634}
1635
1636static struct object_list **process_blob(struct blob *blob,
1637 struct object_list **p,
1638 struct name_path *path,
1639 const char *name)
1640{
1641 struct object *obj = &blob->object;
1642
1643 obj->flags |= LOCAL;
1644
1645 if (obj->flags & (UNINTERESTING | SEEN))
1646 return p;
1647
1648 obj->flags |= SEEN;
1649 return add_one_object(obj, p);
1650}
1651
1652static struct object_list **process_tree(struct tree *tree,
1653 struct object_list **p,
1654 struct name_path *path,
1655 const char *name)
1656{
1657 struct object *obj = &tree->object;
1658 struct tree_desc desc;
1659 struct name_entry entry;
1660 struct name_path me;
1661
1662 obj->flags |= LOCAL;
1663
1664 if (obj->flags & (UNINTERESTING | SEEN))
1665 return p;
1666 if (parse_tree(tree) < 0)
1667 die("bad tree object %s", sha1_to_hex(obj->sha1));
1668
1669 obj->flags |= SEEN;
1670 name = xstrdup(name);
1671 p = add_one_object(obj, p);
1672 me.up = path;
1673 me.elem = name;
1674 me.elem_len = strlen(name);
1675
1676 init_tree_desc(&desc, tree->buffer, tree->size);
1677
1678 while (tree_entry(&desc, &entry))
1679 switch (object_type(entry.mode)) {
1680 case OBJ_TREE:
1681 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1682 break;
1683 case OBJ_BLOB:
1684 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1685 break;
1686 default:
1687 /* Subproject commit - not in this repository */
1688 break;
1689 }
1690
1691 free(tree->buffer);
1692 tree->buffer = NULL;
1693 return p;
1694}
1695
1696static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1697{
1698 int i;
1699 struct commit *commit;
1700 struct object_list **p = &objects;
1701 int count = 0;
1702
1703 while ((commit = get_revision(revs)) != NULL) {
1704 p = process_tree(commit->tree, p, NULL, "");
1705 commit->object.flags |= LOCAL;
1706 if (!(commit->object.flags & UNINTERESTING))
1707 count += add_send_request(&commit->object, lock);
1708 }
1709
1710 for (i = 0; i < revs->pending.nr; i++) {
1711 struct object_array_entry *entry = revs->pending.objects + i;
1712 struct object *obj = entry->item;
1713 const char *name = entry->name;
1714
1715 if (obj->flags & (UNINTERESTING | SEEN))
1716 continue;
1717 if (obj->type == OBJ_TAG) {
1718 obj->flags |= SEEN;
1719 p = add_one_object(obj, p);
1720 continue;
1721 }
1722 if (obj->type == OBJ_TREE) {
1723 p = process_tree((struct tree *)obj, p, NULL, name);
1724 continue;
1725 }
1726 if (obj->type == OBJ_BLOB) {
1727 p = process_blob((struct blob *)obj, p, NULL, name);
1728 continue;
1729 }
1730 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1731 }
1732
1733 while (objects) {
1734 if (!(objects->item->flags & UNINTERESTING))
1735 count += add_send_request(objects->item, lock);
1736 objects = objects->next;
1737 }
1738
1739 return count;
1740}
1741
1742static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1743{
1744 struct active_request_slot *slot;
1745 struct slot_results results;
1746 struct buffer out_buffer = { STRBUF_INIT, 0 };
1747 struct curl_slist *dav_headers;
1748
1749 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1750
1751 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1752
1753 slot = get_active_slot();
1754 slot->results = &results;
1755 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1756 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1757 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1758 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1759 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1760 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1761 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1762 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1763 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1764
1765 if (start_active_slot(slot)) {
1766 run_active_slot(slot);
1767 strbuf_release(&out_buffer.buf);
1768 if (results.curl_result != CURLE_OK) {
1769 fprintf(stderr,
1770 "PUT error: curl result=%d, HTTP code=%ld\n",
1771 results.curl_result, results.http_code);
1772 /* We should attempt recovery? */
1773 return 0;
1774 }
1775 } else {
1776 strbuf_release(&out_buffer.buf);
1777 fprintf(stderr, "Unable to start PUT request\n");
1778 return 0;
1779 }
1780
1781 return 1;
1782}
1783
1784static struct ref *local_refs, **local_tail;
1785static struct ref *remote_refs, **remote_tail;
1786
1787static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1788{
1789 struct ref *ref;
1790 int len = strlen(refname) + 1;
1791 ref = xcalloc(1, sizeof(*ref) + len);
1792 hashcpy(ref->new_sha1, sha1);
1793 memcpy(ref->name, refname, len);
1794 *local_tail = ref;
1795 local_tail = &ref->next;
1796 return 0;
1797}
1798
1799static void one_remote_ref(char *refname)
1800{
1801 struct ref *ref;
1802 struct object *obj;
1803
1804 ref = alloc_ref(refname);
1805
1806 if (http_fetch_ref(remote->url, ref) != 0) {
1807 fprintf(stderr,
1808 "Unable to fetch ref %s from %s\n",
1809 refname, remote->url);
1810 free(ref);
1811 return;
1812 }
1813
1814 /*
1815 * Fetch a copy of the object if it doesn't exist locally - it
1816 * may be required for updating server info later.
1817 */
1818 if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1819 obj = lookup_unknown_object(ref->old_sha1);
1820 if (obj) {
1821 fprintf(stderr, " fetch %s for %s\n",
1822 sha1_to_hex(ref->old_sha1), refname);
1823 add_fetch_request(obj);
1824 }
1825 }
1826
1827 *remote_tail = ref;
1828 remote_tail = &ref->next;
1829}
1830
1831static void get_local_heads(void)
1832{
1833 local_tail = &local_refs;
1834 for_each_ref(one_local_ref, NULL);
1835}
1836
1837static void get_dav_remote_heads(void)
1838{
1839 remote_tail = &remote_refs;
1840 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1841}
1842
1843static int is_zero_sha1(const unsigned char *sha1)
1844{
1845 int i;
1846
1847 for (i = 0; i < 20; i++) {
1848 if (*sha1++)
1849 return 0;
1850 }
1851 return 1;
1852}
1853
1854static void unmark_and_free(struct commit_list *list, unsigned int mark)
1855{
1856 while (list) {
1857 struct commit_list *temp = list;
1858 temp->item->object.flags &= ~mark;
1859 list = temp->next;
1860 free(temp);
1861 }
1862}
1863
1864static int ref_newer(const unsigned char *new_sha1,
1865 const unsigned char *old_sha1)
1866{
1867 struct object *o;
1868 struct commit *old, *new;
1869 struct commit_list *list, *used;
1870 int found = 0;
1871
1872 /* Both new and old must be commit-ish and new is descendant of
1873 * old. Otherwise we require --force.
1874 */
1875 o = deref_tag(parse_object(old_sha1), NULL, 0);
1876 if (!o || o->type != OBJ_COMMIT)
1877 return 0;
1878 old = (struct commit *) o;
1879
1880 o = deref_tag(parse_object(new_sha1), NULL, 0);
1881 if (!o || o->type != OBJ_COMMIT)
1882 return 0;
1883 new = (struct commit *) o;
1884
1885 if (parse_commit(new) < 0)
1886 return 0;
1887
1888 used = list = NULL;
1889 commit_list_insert(new, &list);
1890 while (list) {
1891 new = pop_most_recent_commit(&list, TMP_MARK);
1892 commit_list_insert(new, &used);
1893 if (new == old) {
1894 found = 1;
1895 break;
1896 }
1897 }
1898 unmark_and_free(list, TMP_MARK);
1899 unmark_and_free(used, TMP_MARK);
1900 return found;
1901}
1902
1903static void add_remote_info_ref(struct remote_ls_ctx *ls)
1904{
1905 struct strbuf *buf = (struct strbuf *)ls->userData;
1906 struct object *o;
1907 int len;
1908 char *ref_info;
1909 struct ref *ref;
1910
1911 ref = alloc_ref(ls->dentry_name);
1912
1913 if (http_fetch_ref(remote->url, ref) != 0) {
1914 fprintf(stderr,
1915 "Unable to fetch ref %s from %s\n",
1916 ls->dentry_name, remote->url);
1917 aborted = 1;
1918 free(ref);
1919 return;
1920 }
1921
1922 o = parse_object(ref->old_sha1);
1923 if (!o) {
1924 fprintf(stderr,
1925 "Unable to parse object %s for remote ref %s\n",
1926 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1927 aborted = 1;
1928 free(ref);
1929 return;
1930 }
1931
1932 len = strlen(ls->dentry_name) + 42;
1933 ref_info = xcalloc(len + 1, 1);
1934 sprintf(ref_info, "%s %s\n",
1935 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1936 fwrite_buffer(ref_info, 1, len, buf);
1937 free(ref_info);
1938
1939 if (o->type == OBJ_TAG) {
1940 o = deref_tag(o, ls->dentry_name, 0);
1941 if (o) {
1942 len = strlen(ls->dentry_name) + 45;
1943 ref_info = xcalloc(len + 1, 1);
1944 sprintf(ref_info, "%s %s^{}\n",
1945 sha1_to_hex(o->sha1), ls->dentry_name);
1946 fwrite_buffer(ref_info, 1, len, buf);
1947 free(ref_info);
1948 }
1949 }
1950 free(ref);
1951}
1952
1953static void update_remote_info_refs(struct remote_lock *lock)
1954{
1955 struct buffer buffer = { STRBUF_INIT, 0 };
1956 struct active_request_slot *slot;
1957 struct slot_results results;
1958 struct curl_slist *dav_headers;
1959
1960 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1961 add_remote_info_ref, &buffer.buf);
1962 if (!aborted) {
1963 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1964
1965 slot = get_active_slot();
1966 slot->results = &results;
1967 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1968 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1969 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1970 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1971 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1972 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1973 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1974 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1975 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1976
1977 if (start_active_slot(slot)) {
1978 run_active_slot(slot);
1979 if (results.curl_result != CURLE_OK) {
1980 fprintf(stderr,
1981 "PUT error: curl result=%d, HTTP code=%ld\n",
1982 results.curl_result, results.http_code);
1983 }
1984 }
1985 }
1986 strbuf_release(&buffer.buf);
1987}
1988
1989static int remote_exists(const char *path)
1990{
1991 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1992 struct active_request_slot *slot;
1993 struct slot_results results;
1994 int ret = -1;
1995
1996 sprintf(url, "%s%s", remote->url, path);
1997
1998 slot = get_active_slot();
1999 slot->results = &results;
2000 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2001 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2002
2003 if (start_active_slot(slot)) {
2004 run_active_slot(slot);
2005 if (results.http_code == 404)
2006 ret = 0;
2007 else if (results.curl_result == CURLE_OK)
2008 ret = 1;
2009 else
2010 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2011 } else {
2012 fprintf(stderr, "Unable to start HEAD request\n");
2013 }
2014
2015 free(url);
2016 return ret;
2017}
2018
2019static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2020{
2021 char *url;
2022 struct strbuf buffer = STRBUF_INIT;
2023 struct active_request_slot *slot;
2024 struct slot_results results;
2025
2026 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2027 sprintf(url, "%s%s", remote->url, path);
2028
2029 slot = get_active_slot();
2030 slot->results = &results;
2031 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2032 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2033 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2034 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2035 if (start_active_slot(slot)) {
2036 run_active_slot(slot);
2037 if (results.curl_result != CURLE_OK) {
2038 die("Couldn't get %s for remote symref\n%s",
2039 url, curl_errorstr);
2040 }
2041 } else {
2042 die("Unable to start remote symref request");
2043 }
2044 free(url);
2045
2046 free(*symref);
2047 *symref = NULL;
2048 hashclr(sha1);
2049
2050 if (buffer.len == 0)
2051 return;
2052
2053 /* If it's a symref, set the refname; otherwise try for a sha1 */
2054 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2055 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2056 } else {
2057 get_sha1_hex(buffer.buf, sha1);
2058 }
2059
2060 strbuf_release(&buffer);
2061}
2062
2063static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2064{
2065 struct commit *head = lookup_commit(head_sha1);
2066 struct commit *branch = lookup_commit(branch_sha1);
2067 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2068
2069 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2070}
2071
2072static int delete_remote_branch(char *pattern, int force)
2073{
2074 struct ref *refs = remote_refs;
2075 struct ref *remote_ref = NULL;
2076 unsigned char head_sha1[20];
2077 char *symref = NULL;
2078 int match;
2079 int patlen = strlen(pattern);
2080 int i;
2081 struct active_request_slot *slot;
2082 struct slot_results results;
2083 char *url;
2084
2085 /* Find the remote branch(es) matching the specified branch name */
2086 for (match = 0; refs; refs = refs->next) {
2087 char *name = refs->name;
2088 int namelen = strlen(name);
2089 if (namelen < patlen ||
2090 memcmp(name + namelen - patlen, pattern, patlen))
2091 continue;
2092 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2093 continue;
2094 match++;
2095 remote_ref = refs;
2096 }
2097 if (match == 0)
2098 return error("No remote branch matches %s", pattern);
2099 if (match != 1)
2100 return error("More than one remote branch matches %s",
2101 pattern);
2102
2103 /*
2104 * Remote HEAD must be a symref (not exactly foolproof; a remote
2105 * symlink to a symref will look like a symref)
2106 */
2107 fetch_symref("HEAD", &symref, head_sha1);
2108 if (!symref)
2109 return error("Remote HEAD is not a symref");
2110
2111 /* Remote branch must not be the remote HEAD */
2112 for (i=0; symref && i<MAXDEPTH; i++) {
2113 if (!strcmp(remote_ref->name, symref))
2114 return error("Remote branch %s is the current HEAD",
2115 remote_ref->name);
2116 fetch_symref(symref, &symref, head_sha1);
2117 }
2118
2119 /* Run extra sanity checks if delete is not forced */
2120 if (!force) {
2121 /* Remote HEAD must resolve to a known object */
2122 if (symref)
2123 return error("Remote HEAD symrefs too deep");
2124 if (is_zero_sha1(head_sha1))
2125 return error("Unable to resolve remote HEAD");
2126 if (!has_sha1_file(head_sha1))
2127 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2128
2129 /* Remote branch must resolve to a known object */
2130 if (is_zero_sha1(remote_ref->old_sha1))
2131 return error("Unable to resolve remote branch %s",
2132 remote_ref->name);
2133 if (!has_sha1_file(remote_ref->old_sha1))
2134 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
2135
2136 /* Remote branch must be an ancestor of remote HEAD */
2137 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2138 return error("The branch '%s' is not an ancestor "
2139 "of your current HEAD.\n"
2140 "If you are sure you want to delete it,"
2141 " run:\n\t'git http-push -D %s %s'",
2142 remote_ref->name, remote->url, pattern);
2143 }
2144 }
2145
2146 /* Send delete request */
2147 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2148 if (dry_run)
2149 return 0;
2150 url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2151 sprintf(url, "%s%s", remote->url, remote_ref->name);
2152 slot = get_active_slot();
2153 slot->results = &results;
2154 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2155 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2156 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2157 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2158 if (start_active_slot(slot)) {
2159 run_active_slot(slot);
2160 free(url);
2161 if (results.curl_result != CURLE_OK)
2162 return error("DELETE request failed (%d/%ld)\n",
2163 results.curl_result, results.http_code);
2164 } else {
2165 free(url);
2166 return error("Unable to start DELETE request");
2167 }
2168
2169 return 0;
2170}
2171
2172int main(int argc, char **argv)
2173{
2174 struct transfer_request *request;
2175 struct transfer_request *next_request;
2176 int nr_refspec = 0;
2177 char **refspec = NULL;
2178 struct remote_lock *ref_lock = NULL;
2179 struct remote_lock *info_ref_lock = NULL;
2180 struct rev_info revs;
2181 int delete_branch = 0;
2182 int force_delete = 0;
2183 int objects_to_send;
2184 int rc = 0;
2185 int i;
2186 int new_refs;
2187 struct ref *ref;
2188 char *rewritten_url = NULL;
2189
2190 setup_git_directory();
2191
2192 remote = xcalloc(sizeof(*remote), 1);
2193
2194 argv++;
2195 for (i = 1; i < argc; i++, argv++) {
2196 char *arg = *argv;
2197
2198 if (*arg == '-') {
2199 if (!strcmp(arg, "--all")) {
2200 push_all = MATCH_REFS_ALL;
2201 continue;
2202 }
2203 if (!strcmp(arg, "--force")) {
2204 force_all = 1;
2205 continue;
2206 }
2207 if (!strcmp(arg, "--dry-run")) {
2208 dry_run = 1;
2209 continue;
2210 }
2211 if (!strcmp(arg, "--verbose")) {
2212 push_verbosely = 1;
2213 continue;
2214 }
2215 if (!strcmp(arg, "-d")) {
2216 delete_branch = 1;
2217 continue;
2218 }
2219 if (!strcmp(arg, "-D")) {
2220 delete_branch = 1;
2221 force_delete = 1;
2222 continue;
2223 }
2224 }
2225 if (!remote->url) {
2226 char *path = strstr(arg, "//");
2227 remote->url = arg;
2228 remote->path_len = strlen(arg);
2229 if (path) {
2230 remote->path = strchr(path+2, '/');
2231 if (remote->path)
2232 remote->path_len = strlen(remote->path);
2233 }
2234 continue;
2235 }
2236 refspec = argv;
2237 nr_refspec = argc - i;
2238 break;
2239 }
2240
2241#ifndef USE_CURL_MULTI
2242 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2243#endif
2244
2245 if (!remote->url)
2246 usage(http_push_usage);
2247
2248 if (delete_branch && nr_refspec != 1)
2249 die("You must specify only one branch name when deleting a remote branch");
2250
2251 memset(remote_dir_exists, -1, 256);
2252
2253 http_init(NULL);
2254
2255 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2256
2257 if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
2258 rewritten_url = xmalloc(strlen(remote->url)+2);
2259 strcpy(rewritten_url, remote->url);
2260 strcat(rewritten_url, "/");
2261 remote->path = rewritten_url + (remote->path - remote->url);
2262 remote->path_len++;
2263 remote->url = rewritten_url;
2264 }
2265
2266 /* Verify DAV compliance/lock support */
2267 if (!locking_available()) {
2268 rc = 1;
2269 goto cleanup;
2270 }
2271
2272 signal(SIGINT, remove_locks_on_signal);
2273 signal(SIGHUP, remove_locks_on_signal);
2274 signal(SIGQUIT, remove_locks_on_signal);
2275 signal(SIGTERM, remove_locks_on_signal);
2276
2277 /* Check whether the remote has server info files */
2278 remote->can_update_info_refs = 0;
2279 remote->has_info_refs = remote_exists("info/refs");
2280 remote->has_info_packs = remote_exists("objects/info/packs");
2281 if (remote->has_info_refs) {
2282 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2283 if (info_ref_lock)
2284 remote->can_update_info_refs = 1;
2285 else {
2286 fprintf(stderr, "Error: cannot lock existing info/refs\n");
2287 rc = 1;
2288 goto cleanup;
2289 }
2290 }
2291 if (remote->has_info_packs)
2292 fetch_indices();
2293
2294 /* Get a list of all local and remote heads to validate refspecs */
2295 get_local_heads();
2296 fprintf(stderr, "Fetching remote heads...\n");
2297 get_dav_remote_heads();
2298
2299 /* Remove a remote branch if -d or -D was specified */
2300 if (delete_branch) {
2301 if (delete_remote_branch(refspec[0], force_delete) == -1)
2302 fprintf(stderr, "Unable to delete remote branch %s\n",
2303 refspec[0]);
2304 goto cleanup;
2305 }
2306
2307 /* match them up */
2308 if (!remote_tail)
2309 remote_tail = &remote_refs;
2310 if (match_refs(local_refs, remote_refs, &remote_tail,
2311 nr_refspec, (const char **) refspec, push_all)) {
2312 rc = -1;
2313 goto cleanup;
2314 }
2315 if (!remote_refs) {
2316 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2317 rc = 0;
2318 goto cleanup;
2319 }
2320
2321 new_refs = 0;
2322 for (ref = remote_refs; ref; ref = ref->next) {
2323 char old_hex[60], *new_hex;
2324 const char *commit_argv[4];
2325 int commit_argc;
2326 char *new_sha1_hex, *old_sha1_hex;
2327
2328 if (!ref->peer_ref)
2329 continue;
2330
2331 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2332 if (delete_remote_branch(ref->name, 1) == -1) {
2333 error("Could not remove %s", ref->name);
2334 rc = -4;
2335 }
2336 new_refs++;
2337 continue;
2338 }
2339
2340 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2341 if (push_verbosely || 1)
2342 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2343 continue;
2344 }
2345
2346 if (!force_all &&
2347 !is_zero_sha1(ref->old_sha1) &&
2348 !ref->force) {
2349 if (!has_sha1_file(ref->old_sha1) ||
2350 !ref_newer(ref->peer_ref->new_sha1,
2351 ref->old_sha1)) {
2352 /*
2353 * We do not have the remote ref, or
2354 * we know that the remote ref is not
2355 * an ancestor of what we are trying to
2356 * push. Either way this can be losing
2357 * commits at the remote end and likely
2358 * we were not up to date to begin with.
2359 */
2360 error("remote '%s' is not an ancestor of\n"
2361 "local '%s'.\n"
2362 "Maybe you are not up-to-date and "
2363 "need to pull first?",
2364 ref->name,
2365 ref->peer_ref->name);
2366 rc = -2;
2367 continue;
2368 }
2369 }
2370 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2371 new_refs++;
2372 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2373 new_hex = sha1_to_hex(ref->new_sha1);
2374
2375 fprintf(stderr, "updating '%s'", ref->name);
2376 if (strcmp(ref->name, ref->peer_ref->name))
2377 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2378 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2379 if (dry_run)
2380 continue;
2381
2382 /* Lock remote branch ref */
2383 ref_lock = lock_remote(ref->name, LOCK_TIME);
2384 if (ref_lock == NULL) {
2385 fprintf(stderr, "Unable to lock remote branch %s\n",
2386 ref->name);
2387 rc = 1;
2388 continue;
2389 }
2390
2391 /* Set up revision info for this refspec */
2392 commit_argc = 3;
2393 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2394 old_sha1_hex = NULL;
2395 commit_argv[1] = "--objects";
2396 commit_argv[2] = new_sha1_hex;
2397 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2398 old_sha1_hex = xmalloc(42);
2399 sprintf(old_sha1_hex, "^%s",
2400 sha1_to_hex(ref->old_sha1));
2401 commit_argv[3] = old_sha1_hex;
2402 commit_argc++;
2403 }
2404 init_revisions(&revs, setup_git_directory());
2405 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2406 revs.edge_hint = 0; /* just in case */
2407 free(new_sha1_hex);
2408 if (old_sha1_hex) {
2409 free(old_sha1_hex);
2410 commit_argv[1] = NULL;
2411 }
2412
2413 /* Generate a list of objects that need to be pushed */
2414 pushing = 0;
2415 if (prepare_revision_walk(&revs))
2416 die("revision walk setup failed");
2417 mark_edges_uninteresting(revs.commits, &revs, NULL);
2418 objects_to_send = get_delta(&revs, ref_lock);
2419 finish_all_active_slots();
2420
2421 /* Push missing objects to remote, this would be a
2422 convenient time to pack them first if appropriate. */
2423 pushing = 1;
2424 if (objects_to_send)
2425 fprintf(stderr, " sending %d objects\n",
2426 objects_to_send);
2427#ifdef USE_CURL_MULTI
2428 fill_active_slots();
2429 add_fill_function(NULL, fill_active_slot);
2430#endif
2431 do {
2432 finish_all_active_slots();
2433#ifdef USE_CURL_MULTI
2434 fill_active_slots();
2435#endif
2436 } while (request_queue_head && !aborted);
2437
2438 /* Update the remote branch if all went well */
2439 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2440 rc = 1;
2441
2442 if (!rc)
2443 fprintf(stderr, " done\n");
2444 unlock_remote(ref_lock);
2445 check_locks();
2446 }
2447
2448 /* Update remote server info if appropriate */
2449 if (remote->has_info_refs && new_refs) {
2450 if (info_ref_lock && remote->can_update_info_refs) {
2451 fprintf(stderr, "Updating remote server info\n");
2452 if (!dry_run)
2453 update_remote_info_refs(info_ref_lock);
2454 } else {
2455 fprintf(stderr, "Unable to update server info\n");
2456 }
2457 }
2458
2459 cleanup:
2460 free(rewritten_url);
2461 if (info_ref_lock)
2462 unlock_remote(info_ref_lock);
2463 free(remote);
2464
2465 curl_slist_free_all(no_pragma_header);
2466
2467 http_cleanup();
2468
2469 request = request_queue_head;
2470 while (request != NULL) {
2471 next_request = request->next;
2472 release_request(request);
2473 request = next_request;
2474 }
2475
2476 return rc;
2477}