ci: run `hdr-check` as part of the `Static Analysis` job
[git/git.git] / http-push.c
CommitLineData
58e60dd2 1#include "cache.h"
109cd76d 2#include "repository.h"
58e60dd2 3#include "commit.h"
58e60dd2
NH
4#include "tag.h"
5#include "blob.h"
29508e1e 6#include "http.h"
aa1dbc98 7#include "refs.h"
c4e05b1a 8#include "diff.h"
aa1dbc98 9#include "revision.h"
d807c4a0 10#include "exec-cmd.h"
6b62816c 11#include "remote.h"
d633c882 12#include "list-objects.h"
4a16d072 13#include "sigchain.h"
a0355f6b 14#include "argv-array.h"
d6fe0036 15#include "packfile.h"
a80d72db 16#include "object-store.h"
64043556 17#include "commit-reach.h"
109cd76d 18
081fd8d0
MK
19#ifdef EXPAT_NEEDS_XMLPARSE_H
20#include <xmlparse.h>
21#else
bee8e79d 22#include <expat.h>
081fd8d0 23#endif
58e60dd2
NH
24
25static const char http_push_usage[] =
1b1dd23f 26"git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
58e60dd2 27
92e2eb9c
JS
28#ifndef XML_STATUS_OK
29enum XML_Status {
30 XML_STATUS_OK = 1,
31 XML_STATUS_ERROR = 0
32};
33#define XML_STATUS_OK 1
34#define XML_STATUS_ERROR 0
35#endif
36
197e8951 37#define PREV_BUF_SIZE 4096
58e60dd2 38
acf59575 39/* DAV methods */
58e60dd2
NH
40#define DAV_LOCK "LOCK"
41#define DAV_MKCOL "MKCOL"
42#define DAV_MOVE "MOVE"
43#define DAV_PROPFIND "PROPFIND"
44#define DAV_PUT "PUT"
45#define DAV_UNLOCK "UNLOCK"
3dfaf7bc 46#define DAV_DELETE "DELETE"
acf59575
NH
47
48/* DAV lock flags */
49#define DAV_PROP_LOCKWR (1u << 0)
50#define DAV_PROP_LOCKEX (1u << 1)
51#define DAV_LOCK_OK (1u << 2)
52
53/* DAV XML properties */
54#define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
55#define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
56#define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
57#define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
58#define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
59#define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
aa1dbc98
NH
60#define DAV_PROPFIND_RESP ".multistatus.response"
61#define DAV_PROPFIND_NAME ".multistatus.response.href"
62#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
acf59575
NH
63
64/* DAV request body templates */
aa1dbc98
NH
65#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>"
66#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
67#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>"
68
75187c9d
NH
69#define LOCK_TIME 600
70#define LOCK_REFRESH 30
71
208acbfb 72/* Remember to update object flag allocation in object.h */
1b65a5aa
JH
73#define LOCAL (1u<<16)
74#define REMOTE (1u<<17)
75#define FETCHING (1u<<18)
76#define PUSHING (1u<<19)
aa1dbc98 77
3dfaf7bc
NH
78/* We allow "recursive" symbolic refs. Only within reason, though */
79#define MAXDEPTH 5
80
96f1e58f
DR
81static int pushing;
82static int aborted;
a3c57c9a 83static signed char remote_dir_exists[256];
58e60dd2 84
96f1e58f 85static int push_verbosely;
28b9d6e5 86static int push_all = MATCH_REFS_NONE;
96f1e58f 87static int force_all;
fe5d1d3e 88static int dry_run;
ae4efe19 89static int helper_status;
58e60dd2 90
96f1e58f 91static struct object_list *objects;
aa1dbc98 92
9cba13ca 93struct repo {
58e60dd2 94 char *url;
e1f33efe 95 char *path;
aa1dbc98 96 int path_len;
197e8951
NH
97 int has_info_refs;
98 int can_update_info_refs;
99 int has_info_packs;
58e60dd2 100 struct packed_git *packs;
512d632c 101 struct remote_lock *locks;
58e60dd2
NH
102};
103
7b5201a6 104static struct repo *repo;
58e60dd2
NH
105
106enum transfer_state {
197e8951
NH
107 NEED_FETCH,
108 RUN_FETCH_LOOSE,
109 RUN_FETCH_PACKED,
58e60dd2
NH
110 NEED_PUSH,
111 RUN_MKCOL,
112 RUN_PUT,
113 RUN_MOVE,
114 ABORTED,
4b05548f 115 COMPLETE
58e60dd2
NH
116};
117
9cba13ca 118struct transfer_request {
aa1dbc98 119 struct object *obj;
58e60dd2
NH
120 char *url;
121 char *dest;
aa1dbc98 122 struct remote_lock *lock;
58e60dd2
NH
123 struct curl_slist *headers;
124 struct buffer buffer;
58e60dd2
NH
125 enum transfer_state state;
126 CURLcode curl_result;
127 char errorstr[CURL_ERROR_SIZE];
128 long http_code;
197e8951 129 void *userData;
58e60dd2
NH
130 struct active_request_slot *slot;
131 struct transfer_request *next;
132};
133
96f1e58f 134static struct transfer_request *request_queue_head;
58e60dd2 135
9cba13ca 136struct xml_ctx {
acf59575
NH
137 char *name;
138 int len;
139 char *cdata;
140 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
141 void *userData;
142};
143
9cba13ca 144struct remote_lock {
75187c9d 145 char *url;
26349b2e 146 char *owner;
75187c9d 147 char *token;
f024b87a 148 char tmpfile_suffix[GIT_MAX_HEXSZ + 1];
26349b2e
NH
149 time_t start_time;
150 long timeout;
75187c9d 151 int refreshing;
aa1dbc98
NH
152 struct remote_lock *next;
153};
154
3030baa7
NH
155/* Flags that control remote_ls processing */
156#define PROCESS_FILES (1u << 0)
157#define PROCESS_DIRS (1u << 1)
158#define RECURSIVE (1u << 2)
159
160/* Flags that remote_ls passes to callback functions */
161#define IS_DIR (1u << 0)
162
9cba13ca 163struct remote_ls_ctx {
3030baa7
NH
164 char *path;
165 void (*userFunc)(struct remote_ls_ctx *ls);
166 void *userData;
167 int flags;
168 char *dentry_name;
169 int dentry_flags;
170 struct remote_ls_ctx *parent;
26349b2e
NH
171};
172
b1c7d4aa
TRC
173/* get_dav_token_headers options */
174enum dav_header_flag {
175 DAV_HEADER_IF = (1u << 0),
176 DAV_HEADER_LOCK = (1u << 1),
177 DAV_HEADER_TIMEOUT = (1u << 2)
178};
179
2aab167a 180static char *xml_entities(const char *s)
519d05be
MH
181{
182 struct strbuf buf = STRBUF_INIT;
37141f27 183 strbuf_addstr_xml_quoted(&buf, s);
519d05be
MH
184 return strbuf_detach(&buf, NULL);
185}
186
ebaaf316
DM
187static void curl_setup_http_get(CURL *curl, const char *url,
188 const char *custom_req)
189{
190 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
191 curl_easy_setopt(curl, CURLOPT_URL, url);
192 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
193 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
194}
195
196static void curl_setup_http(CURL *curl, const char *url,
197 const char *custom_req, struct buffer *buffer,
198 curl_write_callback write_fn)
199{
200 curl_easy_setopt(curl, CURLOPT_PUT, 1);
201 curl_easy_setopt(curl, CURLOPT_URL, url);
202 curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
203 curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
204 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
205#ifndef NO_CURL_IOCTL
206 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
479eaa8e 207 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, buffer);
ebaaf316
DM
208#endif
209 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
210 curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
211 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
212 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
213}
214
d456c9fd
JH
215static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
216{
b1c7d4aa 217 struct strbuf buf = STRBUF_INIT;
8cb01e2f 218 struct curl_slist *dav_headers = http_copy_default_headers();
b1c7d4aa 219
d456c9fd 220 if (options & DAV_HEADER_IF) {
b1c7d4aa
TRC
221 strbuf_addf(&buf, "If: (<%s>)", lock->token);
222 dav_headers = curl_slist_append(dav_headers, buf.buf);
223 strbuf_reset(&buf);
224 }
d456c9fd 225 if (options & DAV_HEADER_LOCK) {
b1c7d4aa
TRC
226 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
227 dav_headers = curl_slist_append(dav_headers, buf.buf);
228 strbuf_reset(&buf);
229 }
d456c9fd 230 if (options & DAV_HEADER_TIMEOUT) {
b1c7d4aa
TRC
231 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
232 dav_headers = curl_slist_append(dav_headers, buf.buf);
233 strbuf_reset(&buf);
234 }
235 strbuf_release(&buf);
236
237 return dav_headers;
238}
239
29508e1e 240static void finish_request(struct transfer_request *request);
197e8951 241static void release_request(struct transfer_request *request);
58e60dd2 242
29508e1e 243static void process_response(void *callback_data)
58e60dd2 244{
29508e1e
NH
245 struct transfer_request *request =
246 (struct transfer_request *)callback_data;
58e60dd2 247
29508e1e 248 finish_request(request);
58e60dd2
NH
249}
250
dd8239f9 251#ifdef USE_CURL_MULTI
dcdb3335 252
197e8951
NH
253static void start_fetch_loose(struct transfer_request *request)
254{
197e8951 255 struct active_request_slot *slot;
5424bc55 256 struct http_object_request *obj_req;
197e8951 257
f0be0db1 258 obj_req = new_http_object_request(repo->url, &request->obj->oid);
5424bc55 259 if (obj_req == NULL) {
197e8951 260 request->state = ABORTED;
197e8951
NH
261 return;
262 }
263
5424bc55 264 slot = obj_req->slot;
197e8951
NH
265 slot->callback_func = process_response;
266 slot->callback_data = request;
267 request->slot = slot;
5424bc55 268 request->userData = obj_req;
197e8951
NH
269
270 /* Try to get the request started, abort the request on error */
271 request->state = RUN_FETCH_LOOSE;
272 if (!start_active_slot(slot)) {
273 fprintf(stderr, "Unable to start GET request\n");
7b5201a6 274 repo->can_update_info_refs = 0;
5424bc55 275 release_http_object_request(obj_req);
197e8951
NH
276 release_request(request);
277 }
278}
279
dd8239f9
JH
280static void start_mkcol(struct transfer_request *request)
281{
f2fd0760 282 char *hex = oid_to_hex(&request->obj->oid);
dd8239f9 283 struct active_request_slot *slot;
dd8239f9 284
7b5201a6 285 request->url = get_remote_object_url(repo->url, hex, 1);
dd8239f9
JH
286
287 slot = get_active_slot();
288 slot->callback_func = process_response;
289 slot->callback_data = request;
ebaaf316 290 curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
dd8239f9 291 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
dd8239f9
JH
292
293 if (start_active_slot(slot)) {
294 request->slot = slot;
295 request->state = RUN_MKCOL;
296 } else {
297 request->state = ABORTED;
6a83d902 298 FREE_AND_NULL(request->url);
dd8239f9
JH
299 }
300}
301#endif
302
197e8951
NH
303static void start_fetch_packed(struct transfer_request *request)
304{
197e8951 305 struct packed_git *target;
197e8951
NH
306
307 struct transfer_request *check_request = request_queue_head;
2264dfa5 308 struct http_pack_request *preq;
197e8951 309
ed1c9977 310 target = find_sha1_pack(request->obj->oid.hash, repo->packs);
197e8951 311 if (!target) {
f2fd0760 312 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
7b5201a6 313 repo->can_update_info_refs = 0;
197e8951
NH
314 release_request(request);
315 return;
316 }
317
538b1523 318 fprintf(stderr, "Fetching pack %s\n",
1cb158b6 319 hash_to_hex(target->hash));
f2fd0760 320 fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
197e8951 321
2264dfa5
TRC
322 preq = new_http_pack_request(target, repo->url);
323 if (preq == NULL) {
2264dfa5
TRC
324 repo->can_update_info_refs = 0;
325 return;
326 }
327 preq->lst = &repo->packs;
197e8951
NH
328
329 /* Make sure there isn't another open request for this pack */
330 while (check_request) {
331 if (check_request->state == RUN_FETCH_PACKED &&
2264dfa5
TRC
332 !strcmp(check_request->url, preq->url)) {
333 release_http_pack_request(preq);
197e8951
NH
334 release_request(request);
335 return;
336 }
337 check_request = check_request->next;
338 }
339
2264dfa5
TRC
340 preq->slot->callback_func = process_response;
341 preq->slot->callback_data = request;
342 request->slot = preq->slot;
343 request->userData = preq;
197e8951
NH
344
345 /* Try to get the request started, abort the request on error */
346 request->state = RUN_FETCH_PACKED;
2264dfa5 347 if (!start_active_slot(preq->slot)) {
197e8951 348 fprintf(stderr, "Unable to start GET request\n");
2264dfa5 349 release_http_pack_request(preq);
7b5201a6 350 repo->can_update_info_refs = 0;
197e8951
NH
351 release_request(request);
352 }
353}
354
58e60dd2
NH
355static void start_put(struct transfer_request *request)
356{
f2fd0760 357 char *hex = oid_to_hex(&request->obj->oid);
58e60dd2 358 struct active_request_slot *slot;
817d14a8 359 struct strbuf buf = STRBUF_INIT;
21666f1a 360 enum object_type type;
58e60dd2
NH
361 char hdr[50];
362 void *unpacked;
363 unsigned long len;
364 int hdrlen;
365 ssize_t size;
ef49a7a0 366 git_zstream stream;
58e60dd2 367
b4f5aca4 368 unpacked = read_object_file(&request->obj->oid, &type, &len);
ca473cef 369 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
58e60dd2
NH
370
371 /* Set it up */
55bb5c91 372 git_deflate_init(&stream, zlib_compression_level);
225a6f10 373 size = git_deflate_bound(&stream, len + hdrlen);
028c2976
MH
374 strbuf_init(&request->buffer.buf, size);
375 request->buffer.posn = 0;
58e60dd2
NH
376
377 /* Compress it */
028c2976 378 stream.next_out = (unsigned char *)request->buffer.buf.buf;
58e60dd2
NH
379 stream.avail_out = size;
380
381 /* First header.. */
382 stream.next_in = (void *)hdr;
383 stream.avail_in = hdrlen;
55bb5c91
JH
384 while (git_deflate(&stream, 0) == Z_OK)
385 ; /* nothing */
58e60dd2
NH
386
387 /* Then the data itself.. */
388 stream.next_in = unpacked;
389 stream.avail_in = len;
55bb5c91
JH
390 while (git_deflate(&stream, Z_FINISH) == Z_OK)
391 ; /* nothing */
392 git_deflate_end(&stream);
58e60dd2
NH
393 free(unpacked);
394
028c2976 395 request->buffer.buf.len = stream.total_out;
58e60dd2 396
817d14a8 397 strbuf_addstr(&buf, "Destination: ");
7b5201a6 398 append_remote_object_url(&buf, repo->url, hex, 0);
817d14a8
TRC
399 request->dest = strbuf_detach(&buf, NULL);
400
7b5201a6 401 append_remote_object_url(&buf, repo->url, hex, 0);
f024b87a 402 strbuf_add(&buf, request->lock->tmpfile_suffix, the_hash_algo->hexsz + 1);
817d14a8 403 request->url = strbuf_detach(&buf, NULL);
58e60dd2
NH
404
405 slot = get_active_slot();
29508e1e
NH
406 slot->callback_func = process_response;
407 slot->callback_data = request;
ebaaf316
DM
408 curl_setup_http(slot->curl, request->url, DAV_PUT,
409 &request->buffer, fwrite_null);
58e60dd2
NH
410
411 if (start_active_slot(slot)) {
412 request->slot = slot;
413 request->state = RUN_PUT;
414 } else {
415 request->state = ABORTED;
6a83d902 416 FREE_AND_NULL(request->url);
58e60dd2
NH
417 }
418}
419
420static void start_move(struct transfer_request *request)
421{
422 struct active_request_slot *slot;
8cb01e2f 423 struct curl_slist *dav_headers = http_copy_default_headers();
58e60dd2
NH
424
425 slot = get_active_slot();
29508e1e
NH
426 slot->callback_func = process_response;
427 slot->callback_data = request;
ebaaf316 428 curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
58e60dd2
NH
429 dav_headers = curl_slist_append(dav_headers, request->dest);
430 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
431 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
58e60dd2
NH
432
433 if (start_active_slot(slot)) {
434 request->slot = slot;
435 request->state = RUN_MOVE;
436 } else {
437 request->state = ABORTED;
6a83d902 438 FREE_AND_NULL(request->url);
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 528
9dde06de
CMAB
529 /* URL is reused for MOVE after PUT and used during FETCH */
530 if (request->state != RUN_PUT && request->state != RUN_FETCH_PACKED) {
6a83d902 531 FREE_AND_NULL(request->url);
aa1dbc98 532 }
7b899967 533
aa1dbc98 534 if (request->state == RUN_MKCOL) {
58e60dd2
NH
535 if (request->curl_result == CURLE_OK ||
536 request->http_code == 405) {
ed1c9977 537 remote_dir_exists[request->obj->oid.hash[0]] = 1;
58e60dd2
NH
538 start_put(request);
539 } else {
540 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
f2fd0760 541 oid_to_hex(&request->obj->oid),
58e60dd2
NH
542 request->curl_result, request->http_code);
543 request->state = ABORTED;
544 aborted = 1;
545 }
546 } else if (request->state == RUN_PUT) {
547 if (request->curl_result == CURLE_OK) {
548 start_move(request);
549 } else {
550 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
f2fd0760 551 oid_to_hex(&request->obj->oid),
58e60dd2
NH
552 request->curl_result, request->http_code);
553 request->state = ABORTED;
554 aborted = 1;
555 }
556 } else if (request->state == RUN_MOVE) {
557 if (request->curl_result == CURLE_OK) {
1a703cba
NH
558 if (push_verbosely)
559 fprintf(stderr, " sent %s\n",
f2fd0760 560 oid_to_hex(&request->obj->oid));
aa1dbc98
NH
561 request->obj->flags |= REMOTE;
562 release_request(request);
58e60dd2
NH
563 } else {
564 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
f2fd0760 565 oid_to_hex(&request->obj->oid),
58e60dd2
NH
566 request->curl_result, request->http_code);
567 request->state = ABORTED;
568 aborted = 1;
569 }
197e8951 570 } else if (request->state == RUN_FETCH_LOOSE) {
5424bc55 571 obj_req = (struct http_object_request *)request->userData;
197e8951 572
5424bc55
TRC
573 if (finish_http_object_request(obj_req) == 0)
574 if (obj_req->rename == 0)
575 request->obj->flags |= (LOCAL | REMOTE);
197e8951
NH
576
577 /* Try fetching packed if necessary */
5424bc55
TRC
578 if (request->obj->flags & LOCAL) {
579 release_http_object_request(obj_req);
197e8951 580 release_request(request);
5424bc55 581 } else
197e8951
NH
582 start_fetch_packed(request);
583
584 } else if (request->state == RUN_FETCH_PACKED) {
2264dfa5 585 int fail = 1;
197e8951
NH
586 if (request->curl_result != CURLE_OK) {
587 fprintf(stderr, "Unable to get pack file %s\n%s",
588 request->url, curl_errorstr);
197e8951 589 } else {
2264dfa5
TRC
590 preq = (struct http_pack_request *)request->userData;
591
592 if (preq) {
609621a4 593 if (finish_http_pack_request(preq) == 0)
2264dfa5
TRC
594 fail = 0;
595 release_http_pack_request(preq);
197e8951
NH
596 }
597 }
2264dfa5
TRC
598 if (fail)
599 repo->can_update_info_refs = 0;
197e8951 600 release_request(request);
58e60dd2
NH
601 }
602}
603
b3ca4e4e 604#ifdef USE_CURL_MULTI
4f66250d 605static int is_running_queue;
fc57b6aa 606static int fill_active_slot(void *unused)
58e60dd2 607{
8e24cbae 608 struct transfer_request *request;
58e60dd2 609
4f66250d 610 if (aborted || !is_running_queue)
45c17412 611 return 0;
58e60dd2 612
45c17412 613 for (request = request_queue_head; request; request = request->next) {
197e8951
NH
614 if (request->state == NEED_FETCH) {
615 start_fetch_loose(request);
45c17412 616 return 1;
197e8951 617 } else if (pushing && request->state == NEED_PUSH) {
ed1c9977 618 if (remote_dir_exists[request->obj->oid.hash[0]] == 1) {
0dd276b8 619 start_put(request);
aa1dbc98 620 } else {
0dd276b8 621 start_mkcol(request);
aa1dbc98 622 }
45c17412 623 return 1;
58e60dd2 624 }
aa1dbc98 625 }
45c17412 626 return 0;
58e60dd2 627}
b3ca4e4e 628#endif
58e60dd2 629
aa1dbc98
NH
630static void get_remote_object_list(unsigned char parent);
631
197e8951
NH
632static void add_fetch_request(struct object *obj)
633{
634 struct transfer_request *request;
635
636 check_locks();
637
638 /*
639 * Don't fetch the object if it's known to exist locally
640 * or is already in the request queue
641 */
ed1c9977 642 if (remote_dir_exists[obj->oid.hash[0]] == -1)
643 get_remote_object_list(obj->oid.hash[0]);
197e8951
NH
644 if (obj->flags & (LOCAL | FETCHING))
645 return;
646
647 obj->flags |= FETCHING;
648 request = xmalloc(sizeof(*request));
649 request->obj = obj;
650 request->url = NULL;
651 request->lock = NULL;
652 request->headers = NULL;
197e8951
NH
653 request->state = NEED_FETCH;
654 request->next = request_queue_head;
655 request_queue_head = request;
656
b3ca4e4e 657#ifdef USE_CURL_MULTI
197e8951
NH
658 fill_active_slots();
659 step_active_slots();
b3ca4e4e 660#endif
197e8951
NH
661}
662
1a703cba 663static int add_send_request(struct object *obj, struct remote_lock *lock)
58e60dd2 664{
3def06e6 665 struct transfer_request *request;
58e60dd2 666 struct packed_git *target;
58e60dd2 667
512d632c
NH
668 /* Keep locks active */
669 check_locks();
670
aa1dbc98
NH
671 /*
672 * Don't push the object if it's known to exist on the remote
673 * or is already in the request queue
674 */
ed1c9977 675 if (remote_dir_exists[obj->oid.hash[0]] == -1)
676 get_remote_object_list(obj->oid.hash[0]);
aa1dbc98 677 if (obj->flags & (REMOTE | PUSHING))
1a703cba 678 return 0;
ed1c9977 679 target = find_sha1_pack(obj->oid.hash, repo->packs);
aa1dbc98
NH
680 if (target) {
681 obj->flags |= REMOTE;
1a703cba 682 return 0;
aa1dbc98 683 }
58e60dd2 684
aa1dbc98 685 obj->flags |= PUSHING;
58e60dd2 686 request = xmalloc(sizeof(*request));
aa1dbc98 687 request->obj = obj;
58e60dd2 688 request->url = NULL;
26349b2e 689 request->lock = lock;
58e60dd2 690 request->headers = NULL;
aa1dbc98 691 request->state = NEED_PUSH;
c17fb6ee
NH
692 request->next = request_queue_head;
693 request_queue_head = request;
29508e1e 694
b3ca4e4e 695#ifdef USE_CURL_MULTI
29508e1e
NH
696 fill_active_slots();
697 step_active_slots();
b3ca4e4e 698#endif
1a703cba
NH
699
700 return 1;
58e60dd2
NH
701}
702
f4f440a0 703static int fetch_indices(void)
58e60dd2 704{
b8caac2b 705 int ret;
58e60dd2 706
58e60dd2
NH
707 if (push_verbosely)
708 fprintf(stderr, "Getting pack list\n");
1a703cba 709
b8caac2b
TRC
710 switch (http_get_info_packs(repo->url, &repo->packs)) {
711 case HTTP_OK:
712 case HTTP_MISSING_TARGET:
713 ret = 0;
714 break;
715 default:
716 ret = -1;
58e60dd2
NH
717 }
718
b8caac2b 719 return ret;
58e60dd2
NH
720}
721
1aa40df6 722static void one_remote_object(const struct object_id *oid)
aa1dbc98 723{
aa1dbc98
NH
724 struct object *obj;
725
d0229abd 726 obj = lookup_object(the_repository, oid);
aa1dbc98 727 if (!obj)
109cd76d 728 obj = parse_object(the_repository, oid);
aa1dbc98
NH
729
730 /* Ignore remote objects that don't exist locally */
731 if (!obj)
732 return;
733
734 obj->flags |= REMOTE;
735 if (!object_list_contains(objects, obj))
1f1e895f 736 object_list_insert(obj, &objects);
aa1dbc98
NH
737}
738
acf59575 739static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
26349b2e 740{
acf59575
NH
741 int *lock_flags = (int *)ctx->userData;
742
743 if (tag_closed) {
744 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
745 if ((*lock_flags & DAV_PROP_LOCKEX) &&
746 (*lock_flags & DAV_PROP_LOCKWR)) {
747 *lock_flags |= DAV_LOCK_OK;
748 }
749 *lock_flags &= DAV_LOCK_OK;
750 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
751 *lock_flags |= DAV_PROP_LOCKWR;
752 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
753 *lock_flags |= DAV_PROP_LOCKEX;
754 }
755 }
26349b2e
NH
756}
757
acf59575 758static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
26349b2e 759{
aa1dbc98 760 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
f024b87a 761 git_hash_ctx hash_ctx;
762 unsigned char lock_token_hash[GIT_MAX_RAWSZ];
acf59575
NH
763
764 if (tag_closed && ctx->cdata) {
765 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
95244ae3 766 lock->owner = xstrdup(ctx->cdata);
acf59575 767 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
ae021d87
JK
768 const char *arg;
769 if (skip_prefix(ctx->cdata, "Second-", &arg))
770 lock->timeout = strtol(arg, NULL, 10);
acf59575 771 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
95244ae3 772 lock->token = xstrdup(ctx->cdata);
dfab7c14 773
f024b87a 774 the_hash_algo->init_fn(&hash_ctx);
775 the_hash_algo->update_fn(&hash_ctx, lock->token, strlen(lock->token));
776 the_hash_algo->final_fn(lock_token_hash, &hash_ctx);
dfab7c14
TRC
777
778 lock->tmpfile_suffix[0] = '_';
f024b87a 779 memcpy(lock->tmpfile_suffix + 1, hash_to_hex(lock_token_hash), the_hash_algo->hexsz);
acf59575 780 }
26349b2e
NH
781 }
782}
783
2aab167a 784static void one_remote_ref(const char *refname);
aa1dbc98 785
26349b2e 786static void
acf59575 787xml_start_tag(void *userData, const char *name, const char **atts)
26349b2e 788{
acf59575 789 struct xml_ctx *ctx = (struct xml_ctx *)userData;
ef9e58c8 790 const char *c = strchr(name, ':');
0cc41428 791 int old_namelen, new_len;
acf59575
NH
792
793 if (c == NULL)
794 c = name;
795 else
796 c++;
797
0cc41428
JK
798 old_namelen = strlen(ctx->name);
799 new_len = old_namelen + strlen(c) + 2;
acf59575
NH
800
801 if (new_len > ctx->len) {
802 ctx->name = xrealloc(ctx->name, new_len);
803 ctx->len = new_len;
26349b2e 804 }
0cc41428 805 xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
26349b2e 806
6a83d902 807 FREE_AND_NULL(ctx->cdata);
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);
a2b9820c
PO
922 } else {
923 fprintf(stderr,
924 "error: curl result=%d, HTTP code=%ld\n",
925 results.curl_result, results.http_code);
58e60dd2
NH
926 }
927 } else {
1a703cba 928 fprintf(stderr, "Unable to start LOCK request\n");
58e60dd2
NH
929 }
930
acf59575 931 curl_slist_free_all(dav_headers);
028c2976
MH
932 strbuf_release(&out_buffer.buf);
933 strbuf_release(&in_buffer);
26349b2e 934
aa1dbc98 935 if (lock->token == NULL || lock->timeout <= 0) {
8e0f7003
JM
936 free(lock->token);
937 free(lock->owner);
75187c9d 938 free(url);
6a83d902 939 FREE_AND_NULL(lock);
acf59575 940 } else {
aa1dbc98 941 lock->url = url;
aa1dbc98 942 lock->start_time = time(NULL);
7b5201a6
AK
943 lock->next = repo->locks;
944 repo->locks = lock;
26349b2e
NH
945 }
946
aa1dbc98 947 return lock;
58e60dd2
NH
948}
949
aa1dbc98 950static int unlock_remote(struct remote_lock *lock)
58e60dd2
NH
951{
952 struct active_request_slot *slot;
baa7b67d 953 struct slot_results results;
7b5201a6 954 struct remote_lock *prev = repo->locks;
b1c7d4aa 955 struct curl_slist *dav_headers;
58e60dd2
NH
956 int rc = 0;
957
b1c7d4aa 958 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
58e60dd2
NH
959
960 slot = get_active_slot();
baa7b67d 961 slot->results = &results;
ebaaf316 962 curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
58e60dd2
NH
963 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
964
965 if (start_active_slot(slot)) {
966 run_active_slot(slot);
baa7b67d 967 if (results.curl_result == CURLE_OK)
58e60dd2
NH
968 rc = 1;
969 else
512d632c 970 fprintf(stderr, "UNLOCK HTTP error %ld\n",
baa7b67d 971 results.http_code);
58e60dd2 972 } else {
512d632c 973 fprintf(stderr, "Unable to start UNLOCK request\n");
58e60dd2
NH
974 }
975
976 curl_slist_free_all(dav_headers);
75187c9d 977
7b5201a6
AK
978 if (repo->locks == lock) {
979 repo->locks = lock->next;
512d632c
NH
980 } else {
981 while (prev && prev->next != lock)
982 prev = prev->next;
983 if (prev)
984 prev->next = prev->next->next;
985 }
986
8e0f7003 987 free(lock->owner);
512d632c
NH
988 free(lock->url);
989 free(lock->token);
990 free(lock);
58e60dd2
NH
991
992 return rc;
993}
994
6a491a17
CB
995static void remove_locks(void)
996{
7b5201a6 997 struct remote_lock *lock = repo->locks;
6a491a17
CB
998
999 fprintf(stderr, "Removing remote locks...\n");
1000 while (lock) {
6589ebf1 1001 struct remote_lock *next = lock->next;
6a491a17 1002 unlock_remote(lock);
6589ebf1 1003 lock = next;
6a491a17
CB
1004 }
1005}
1006
1007static void remove_locks_on_signal(int signo)
1008{
1009 remove_locks();
4a16d072 1010 sigchain_pop(signo);
6a491a17
CB
1011 raise(signo);
1012}
1013
3030baa7
NH
1014static void remote_ls(const char *path, int flags,
1015 void (*userFunc)(struct remote_ls_ctx *ls),
1016 void *userData);
aa1dbc98 1017
c3bdc4e7 1018/* extract hex from sharded "xx/x{38}" filename */
1aa40df6 1019static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
67a31f61 1020{
f024b87a 1021 if (strlen(path) != the_hash_algo->hexsz + 1)
67a31f61
JK
1022 return -1;
1023
c3bdc4e7
RS
1024 if (hex_to_bytes(oid->hash, path, 1))
1025 return -1;
67a31f61
JK
1026 path += 2;
1027 path++; /* skip '/' */
67a31f61 1028
f024b87a 1029 return hex_to_bytes(oid->hash + 1, path, the_hash_algo->rawsz - 1);
67a31f61
JK
1030}
1031
3030baa7
NH
1032static void process_ls_object(struct remote_ls_ctx *ls)
1033{
1034 unsigned int *parent = (unsigned int *)ls->userData;
67a31f61 1035 const char *path = ls->dentry_name;
1aa40df6 1036 struct object_id oid;
aa1dbc98 1037
3030baa7
NH
1038 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1039 remote_dir_exists[*parent] = 1;
1040 return;
1041 }
aa1dbc98 1042
67a31f61 1043 if (!skip_prefix(path, "objects/", &path) ||
1aa40df6 1044 get_oid_hex_from_objpath(path, &oid))
3030baa7 1045 return;
67a31f61 1046
1aa40df6 1047 one_remote_object(&oid);
3030baa7 1048}
aa1dbc98 1049
3030baa7
NH
1050static void process_ls_ref(struct remote_ls_ctx *ls)
1051{
1052 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1053 fprintf(stderr, " %s\n", ls->dentry_name);
1054 return;
1055 }
aa1dbc98 1056
3030baa7
NH
1057 if (!(ls->dentry_flags & IS_DIR))
1058 one_remote_ref(ls->dentry_name);
1059}
aa1dbc98 1060
3030baa7
NH
1061static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1062{
1063 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
aa1dbc98 1064
3030baa7
NH
1065 if (tag_closed) {
1066 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1067 if (ls->dentry_flags & IS_DIR) {
0fdadc50
TRC
1068
1069 /* ensure collection names end with slash */
1070 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1071
3030baa7
NH
1072 if (ls->flags & PROCESS_DIRS) {
1073 ls->userFunc(ls);
1074 }
1075 if (strcmp(ls->dentry_name, ls->path) &&
1076 ls->flags & RECURSIVE) {
1077 remote_ls(ls->dentry_name,
1078 ls->flags,
1079 ls->userFunc,
1080 ls->userData);
1081 }
1082 } else if (ls->flags & PROCESS_FILES) {
1083 ls->userFunc(ls);
aa1dbc98 1084 }
3030baa7 1085 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
e1f33efe
KK
1086 char *path = ctx->cdata;
1087 if (*ctx->cdata == 'h') {
1088 path = strstr(path, "//");
1089 if (path) {
1090 path = strchr(path+2, '/');
1091 }
1092 }
1093 if (path) {
dfc2dcd9
TRC
1094 const char *url = repo->url;
1095 if (repo->path)
1096 url = repo->path;
1097 if (strncmp(path, url, repo->path_len))
82247e9b 1098 error("Parsed path '%s' does not match url: '%s'",
dfc2dcd9
TRC
1099 path, url);
1100 else {
1101 path += repo->path_len;
1102 ls->dentry_name = xstrdup(path);
1103 }
e1f33efe 1104 }
3030baa7
NH
1105 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1106 ls->dentry_flags |= IS_DIR;
aa1dbc98 1107 }
3030baa7 1108 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
6a83d902 1109 FREE_AND_NULL(ls->dentry_name);
3030baa7 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:
ea82b2a0 1315 p = process_tree(lookup_tree(the_repository, &entry.oid),
f86bcc7b 1316 p);
1bbeb4c6
JS
1317 break;
1318 case OBJ_BLOB:
ea82b2a0 1319 p = process_blob(lookup_blob(the_repository, &entry.oid),
da14a7ff 1320 p);
1bbeb4c6
JS
1321 break;
1322 default:
1323 /* Subproject commit - not in this repository */
1324 break;
1325 }
1326
6e454b9a 1327 free_tree_buffer(tree);
aa1dbc98 1328 return p;
58e60dd2
NH
1329}
1330
1a703cba 1331static int get_delta(struct rev_info *revs, struct remote_lock *lock)
58e60dd2 1332{
1f1e895f 1333 int i;
58e60dd2 1334 struct commit *commit;
1f1e895f 1335 struct object_list **p = &objects;
1a703cba 1336 int count = 0;
58e60dd2 1337
aa1dbc98 1338 while ((commit = get_revision(revs)) != NULL) {
2e27bd77 1339 p = process_tree(get_commit_tree(commit), p);
aa1dbc98
NH
1340 commit->object.flags |= LOCAL;
1341 if (!(commit->object.flags & UNINTERESTING))
1a703cba 1342 count += add_send_request(&commit->object, lock);
aa1dbc98 1343 }
58e60dd2 1344
1f1e895f
LT
1345 for (i = 0; i < revs->pending.nr; i++) {
1346 struct object_array_entry *entry = revs->pending.objects + i;
1347 struct object *obj = entry->item;
1348 const char *name = entry->name;
58e60dd2 1349
aa1dbc98
NH
1350 if (obj->flags & (UNINTERESTING | SEEN))
1351 continue;
1974632c 1352 if (obj->type == OBJ_TAG) {
aa1dbc98 1353 obj->flags |= SEEN;
1f1e895f 1354 p = add_one_object(obj, p);
aa1dbc98 1355 continue;
58e60dd2 1356 }
1974632c 1357 if (obj->type == OBJ_TREE) {
41595938 1358 p = process_tree((struct tree *)obj, p);
aa1dbc98 1359 continue;
58e60dd2 1360 }
1974632c 1361 if (obj->type == OBJ_BLOB) {
41595938 1362 p = process_blob((struct blob *)obj, p);
aa1dbc98 1363 continue;
58e60dd2 1364 }
f2fd0760 1365 die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);
aa1dbc98
NH
1366 }
1367
1368 while (objects) {
1369 if (!(objects->item->flags & UNINTERESTING))
1a703cba 1370 count += add_send_request(objects->item, lock);
aa1dbc98 1371 objects = objects->next;
58e60dd2 1372 }
1a703cba
NH
1373
1374 return count;
58e60dd2
NH
1375}
1376
1cb158b6 1377static int update_remote(const struct object_id *oid, struct remote_lock *lock)
58e60dd2
NH
1378{
1379 struct active_request_slot *slot;
baa7b67d 1380 struct slot_results results;
028c2976 1381 struct buffer out_buffer = { STRBUF_INIT, 0 };
b1c7d4aa 1382 struct curl_slist *dav_headers;
58e60dd2 1383
b1c7d4aa 1384 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
58e60dd2 1385
1cb158b6 1386 strbuf_addf(&out_buffer.buf, "%s\n", oid_to_hex(oid));
58e60dd2
NH
1387
1388 slot = get_active_slot();
baa7b67d 1389 slot->results = &results;
ebaaf316
DM
1390 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1391 &out_buffer, fwrite_null);
58e60dd2 1392 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
58e60dd2
NH
1393
1394 if (start_active_slot(slot)) {
1395 run_active_slot(slot);
028c2976 1396 strbuf_release(&out_buffer.buf);
baa7b67d 1397 if (results.curl_result != CURLE_OK) {
58e60dd2
NH
1398 fprintf(stderr,
1399 "PUT error: curl result=%d, HTTP code=%ld\n",
baa7b67d 1400 results.curl_result, results.http_code);
58e60dd2
NH
1401 /* We should attempt recovery? */
1402 return 0;
1403 }
1404 } else {
028c2976 1405 strbuf_release(&out_buffer.buf);
58e60dd2
NH
1406 fprintf(stderr, "Unable to start PUT request\n");
1407 return 0;
1408 }
1409
1410 return 1;
1411}
1412
6d2bf96e 1413static struct ref *remote_refs;
aa1dbc98 1414
2aab167a 1415static void one_remote_ref(const char *refname)
aa1dbc98
NH
1416{
1417 struct ref *ref;
197e8951 1418 struct object *obj;
aa1dbc98 1419
59c69c0c 1420 ref = alloc_ref(refname);
c13b2633 1421
7b5201a6 1422 if (http_fetch_ref(repo->url, ref) != 0) {
aa1dbc98
NH
1423 fprintf(stderr,
1424 "Unable to fetch ref %s from %s\n",
7b5201a6 1425 refname, repo->url);
c13b2633 1426 free(ref);
aa1dbc98
NH
1427 return;
1428 }
1429
197e8951
NH
1430 /*
1431 * Fetch a copy of the object if it doesn't exist locally - it
1432 * may be required for updating server info later.
1433 */
f4e54d02 1434 if (repo->can_update_info_refs && !has_object_file(&ref->old_oid)) {
0ebbcf70 1435 obj = lookup_unknown_object(&ref->old_oid);
e94eac49
RS
1436 fprintf(stderr, " fetch %s for %s\n",
1437 oid_to_hex(&ref->old_oid), refname);
1438 add_fetch_request(obj);
197e8951
NH
1439 }
1440
6d2bf96e
CB
1441 ref->next = remote_refs;
1442 remote_refs = ref;
aa1dbc98
NH
1443}
1444
aa1dbc98
NH
1445static void get_dav_remote_heads(void)
1446{
3030baa7 1447 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
aa1dbc98
NH
1448}
1449
197e8951
NH
1450static void add_remote_info_ref(struct remote_ls_ctx *ls)
1451{
028c2976 1452 struct strbuf *buf = (struct strbuf *)ls->userData;
197e8951 1453 struct object *o;
c13b2633
DB
1454 struct ref *ref;
1455
59c69c0c 1456 ref = alloc_ref(ls->dentry_name);
197e8951 1457
7b5201a6 1458 if (http_fetch_ref(repo->url, ref) != 0) {
197e8951
NH
1459 fprintf(stderr,
1460 "Unable to fetch ref %s from %s\n",
7b5201a6 1461 ls->dentry_name, repo->url);
197e8951 1462 aborted = 1;
c13b2633 1463 free(ref);
197e8951
NH
1464 return;
1465 }
1466
109cd76d 1467 o = parse_object(the_repository, &ref->old_oid);
197e8951
NH
1468 if (!o) {
1469 fprintf(stderr,
1470 "Unable to parse object %s for remote ref %s\n",
f4e54d02 1471 oid_to_hex(&ref->old_oid), ls->dentry_name);
197e8951 1472 aborted = 1;
c13b2633 1473 free(ref);
197e8951
NH
1474 return;
1475 }
1476
7d0581a9 1477 strbuf_addf(buf, "%s\t%s\n",
f4e54d02 1478 oid_to_hex(&ref->old_oid), ls->dentry_name);
197e8951 1479
1974632c 1480 if (o->type == OBJ_TAG) {
a74093da 1481 o = deref_tag(the_repository, o, ls->dentry_name, 0);
7d0581a9
JK
1482 if (o)
1483 strbuf_addf(buf, "%s\t%s^{}\n",
f2fd0760 1484 oid_to_hex(&o->oid), ls->dentry_name);
197e8951 1485 }
c13b2633 1486 free(ref);
197e8951
NH
1487}
1488
1489static void update_remote_info_refs(struct remote_lock *lock)
1490{
028c2976 1491 struct buffer buffer = { STRBUF_INIT, 0 };
197e8951
NH
1492 struct active_request_slot *slot;
1493 struct slot_results results;
b1c7d4aa 1494 struct curl_slist *dav_headers;
197e8951 1495
197e8951 1496 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
028c2976 1497 add_remote_info_ref, &buffer.buf);
197e8951 1498 if (!aborted) {
b1c7d4aa 1499 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
197e8951
NH
1500
1501 slot = get_active_slot();
1502 slot->results = &results;
ebaaf316
DM
1503 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1504 &buffer, fwrite_null);
197e8951 1505 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
197e8951 1506
197e8951
NH
1507 if (start_active_slot(slot)) {
1508 run_active_slot(slot);
1509 if (results.curl_result != CURLE_OK) {
1510 fprintf(stderr,
1511 "PUT error: curl result=%d, HTTP code=%ld\n",
1512 results.curl_result, results.http_code);
1513 }
1514 }
197e8951 1515 }
028c2976 1516 strbuf_release(&buffer.buf);
197e8951
NH
1517}
1518
1519static int remote_exists(const char *path)
1520{
28310186 1521 char *url = xstrfmt("%s%s", repo->url, path);
446b941a 1522 int ret;
197e8951 1523
197e8951 1524
1bbcc224 1525 switch (http_get_strbuf(url, NULL, NULL)) {
446b941a
MH
1526 case HTTP_OK:
1527 ret = 1;
1528 break;
1529 case HTTP_MISSING_TARGET:
1530 ret = 0;
1531 break;
1532 case HTTP_ERROR:
4df13f69 1533 error("unable to access '%s': %s", url, curl_errorstr);
1cf01a34 1534 /* fallthrough */
446b941a
MH
1535 default:
1536 ret = -1;
197e8951 1537 }
3a462bc9
MH
1538 free(url);
1539 return ret;
197e8951
NH
1540}
1541
8eb94600 1542static void fetch_symref(const char *path, char **symref, struct object_id *oid)
3dfaf7bc 1543{
28310186 1544 char *url = xstrfmt("%s%s", repo->url, path);
028c2976 1545 struct strbuf buffer = STRBUF_INIT;
ae021d87 1546 const char *name;
3dfaf7bc 1547
1bbcc224 1548 if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
9af5abd9
MH
1549 die("Couldn't get %s for remote symref\n%s", url,
1550 curl_errorstr);
3dfaf7bc
NH
1551 free(url);
1552
6a83d902 1553 FREE_AND_NULL(*symref);
8eb94600 1554 oidclr(oid);
3dfaf7bc 1555
028c2976 1556 if (buffer.len == 0)
3dfaf7bc
NH
1557 return;
1558
f6786c8d
JK
1559 /* Cut off trailing newline. */
1560 strbuf_rtrim(&buffer);
1561
3dfaf7bc 1562 /* If it's a symref, set the refname; otherwise try for a sha1 */
ae021d87
JK
1563 if (skip_prefix(buffer.buf, "ref: ", &name)) {
1564 *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
3dfaf7bc 1565 } else {
8eb94600 1566 get_oid_hex(buffer.buf, oid);
3dfaf7bc
NH
1567 }
1568
028c2976 1569 strbuf_release(&buffer);
3dfaf7bc
NH
1570}
1571
8eb94600 1572static int verify_merge_base(struct object_id *head_oid, struct ref *remote)
3dfaf7bc 1573{
bc83266a 1574 struct commit *head = lookup_commit_or_die(head_oid, "HEAD");
1575 struct commit *branch = lookup_commit_or_die(&remote->old_oid,
1576 remote->name);
3dfaf7bc 1577
65712251 1578 return in_merge_bases(branch, head);
3dfaf7bc
NH
1579}
1580
2aab167a 1581static int delete_remote_branch(const char *pattern, int force)
3dfaf7bc
NH
1582{
1583 struct ref *refs = remote_refs;
1584 struct ref *remote_ref = NULL;
8eb94600 1585 struct object_id head_oid;
3dfaf7bc
NH
1586 char *symref = NULL;
1587 int match;
1588 int patlen = strlen(pattern);
1589 int i;
1590 struct active_request_slot *slot;
1591 struct slot_results results;
1592 char *url;
1593
1594 /* Find the remote branch(es) matching the specified branch name */
1595 for (match = 0; refs; refs = refs->next) {
1596 char *name = refs->name;
1597 int namelen = strlen(name);
1598 if (namelen < patlen ||
1599 memcmp(name + namelen - patlen, pattern, patlen))
1600 continue;
1601 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1602 continue;
1603 match++;
1604 remote_ref = refs;
1605 }
1606 if (match == 0)
1607 return error("No remote branch matches %s", pattern);
1608 if (match != 1)
1609 return error("More than one remote branch matches %s",
1610 pattern);
1611
1612 /*
1613 * Remote HEAD must be a symref (not exactly foolproof; a remote
1614 * symlink to a symref will look like a symref)
1615 */
8eb94600 1616 fetch_symref("HEAD", &symref, &head_oid);
3dfaf7bc
NH
1617 if (!symref)
1618 return error("Remote HEAD is not a symref");
1619
1620 /* Remote branch must not be the remote HEAD */
cd2b8ae9 1621 for (i = 0; symref && i < MAXDEPTH; i++) {
3dfaf7bc
NH
1622 if (!strcmp(remote_ref->name, symref))
1623 return error("Remote branch %s is the current HEAD",
1624 remote_ref->name);
8eb94600 1625 fetch_symref(symref, &symref, &head_oid);
3dfaf7bc
NH
1626 }
1627
1628 /* Run extra sanity checks if delete is not forced */
1629 if (!force) {
1630 /* Remote HEAD must resolve to a known object */
1631 if (symref)
1632 return error("Remote HEAD symrefs too deep");
8eb94600 1633 if (is_null_oid(&head_oid))
3dfaf7bc 1634 return error("Unable to resolve remote HEAD");
8eb94600 1635 if (!has_object_file(&head_oid))
1636 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
1637
1638 /* Remote branch must resolve to a known object */
f4e54d02 1639 if (is_null_oid(&remote_ref->old_oid))
3dfaf7bc
NH
1640 return error("Unable to resolve remote branch %s",
1641 remote_ref->name);
f4e54d02 1642 if (!has_object_file(&remote_ref->old_oid))
1643 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
1644
1645 /* Remote branch must be an ancestor of remote HEAD */
8eb94600 1646 if (!verify_merge_base(&head_oid, remote_ref)) {
00ae8289
BF
1647 return error("The branch '%s' is not an ancestor "
1648 "of your current HEAD.\n"
1649 "If you are sure you want to delete it,"
1650 " run:\n\t'git http-push -D %s %s'",
7b5201a6 1651 remote_ref->name, repo->url, pattern);
3dfaf7bc
NH
1652 }
1653 }
1654
1655 /* Send delete request */
1656 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
6eaf4060
CB
1657 if (dry_run)
1658 return 0;
28310186 1659 url = xstrfmt("%s%s", repo->url, remote_ref->name);
3dfaf7bc
NH
1660 slot = get_active_slot();
1661 slot->results = &results;
ebaaf316 1662 curl_setup_http_get(slot->curl, url, DAV_DELETE);
3dfaf7bc
NH
1663 if (start_active_slot(slot)) {
1664 run_active_slot(slot);
1665 free(url);
1666 if (results.curl_result != CURLE_OK)
82247e9b 1667 return error("DELETE request failed (%d/%ld)",
3dfaf7bc
NH
1668 results.curl_result, results.http_code);
1669 } else {
1670 free(url);
1671 return error("Unable to start DELETE request");
1672 }
1673
1674 return 0;
1675}
1676
2af202be 1677static void run_request_queue(void)
4f66250d
TRC
1678{
1679#ifdef USE_CURL_MULTI
1680 is_running_queue = 1;
1681 fill_active_slots();
1682 add_fill_function(NULL, fill_active_slot);
1683#endif
1684 do {
1685 finish_all_active_slots();
1686#ifdef USE_CURL_MULTI
1687 fill_active_slots();
1688#endif
1689 } while (request_queue_head && !aborted);
1690
1691#ifdef USE_CURL_MULTI
1692 is_running_queue = 0;
1693#endif
1694}
1695
3f2e2297 1696int cmd_main(int argc, const char **argv)
58e60dd2 1697{
58e60dd2
NH
1698 struct transfer_request *request;
1699 struct transfer_request *next_request;
38490dd4 1700 struct refspec rs = REFSPEC_INIT_PUSH;
512d632c 1701 struct remote_lock *ref_lock = NULL;
197e8951 1702 struct remote_lock *info_ref_lock = NULL;
aa1dbc98 1703 struct rev_info revs;
3dfaf7bc
NH
1704 int delete_branch = 0;
1705 int force_delete = 0;
1a703cba 1706 int objects_to_send;
58e60dd2
NH
1707 int rc = 0;
1708 int i;
8c9e7947 1709 int new_refs;
454e2025 1710 struct ref *ref, *local_refs;
58e60dd2 1711
f3d51ffd 1712 repo = xcalloc(1, sizeof(*repo));
58e60dd2
NH
1713
1714 argv++;
1715 for (i = 1; i < argc; i++, argv++) {
3f2e2297 1716 const char *arg = *argv;
58e60dd2
NH
1717
1718 if (*arg == '-') {
aa1dbc98 1719 if (!strcmp(arg, "--all")) {
28b9d6e5 1720 push_all = MATCH_REFS_ALL;
58e60dd2
NH
1721 continue;
1722 }
1723 if (!strcmp(arg, "--force")) {
1724 force_all = 1;
1725 continue;
1726 }
fe5d1d3e
SP
1727 if (!strcmp(arg, "--dry-run")) {
1728 dry_run = 1;
1729 continue;
1730 }
ae4efe19
SP
1731 if (!strcmp(arg, "--helper-status")) {
1732 helper_status = 1;
1733 continue;
1734 }
58e60dd2
NH
1735 if (!strcmp(arg, "--verbose")) {
1736 push_verbosely = 1;
e9176745 1737 http_is_verbose = 1;
58e60dd2
NH
1738 continue;
1739 }
3dfaf7bc
NH
1740 if (!strcmp(arg, "-d")) {
1741 delete_branch = 1;
1742 continue;
1743 }
1744 if (!strcmp(arg, "-D")) {
1745 delete_branch = 1;
1746 force_delete = 1;
1747 continue;
1748 }
548d3464
JN
1749 if (!strcmp(arg, "-h"))
1750 usage(http_push_usage);
58e60dd2 1751 }
7b5201a6 1752 if (!repo->url) {
aa1dbc98 1753 char *path = strstr(arg, "//");
1462d1af
TRC
1754 str_end_url_with_slash(arg, &repo->url);
1755 repo->path_len = strlen(repo->url);
aa1dbc98 1756 if (path) {
7b5201a6
AK
1757 repo->path = strchr(path+2, '/');
1758 if (repo->path)
1759 repo->path_len = strlen(repo->path);
aa1dbc98 1760 }
58e60dd2
NH
1761 continue;
1762 }
38490dd4 1763 refspec_appendn(&rs, argv, argc - i);
58e60dd2
NH
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
38490dd4 1774 if (delete_branch && rs.nr != 1)
3dfaf7bc
NH
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) {
38490dd4
BW
1820 const char *branch = rs.items[i].src;
1821 if (delete_remote_branch(branch, force_delete) == -1) {
3dfaf7bc 1822 fprintf(stderr, "Unable to delete remote branch %s\n",
38490dd4 1823 branch);
ae4efe19 1824 if (helper_status)
38490dd4 1825 printf("error %s cannot remove\n", branch);
ae4efe19 1826 }
3dfaf7bc
NH
1827 goto cleanup;
1828 }
1829
aa1dbc98 1830 /* match them up */
5c7ec846 1831 if (match_push_refs(local_refs, &remote_refs, &rs, 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
4a7e27e9 1863 if (oideq(&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));
2abf3503 1929 repo_init_revisions(the_repository, &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");
4f6d26b1 1937 mark_edges_uninteresting(&revs, NULL, 0);
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 */
1cb158b6 1951 if (aborted || !update_remote(&ref->new_oid, 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}