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