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