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