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