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