]> git.ipfire.org Git - thirdparty/git.git/blame - http-push.c
exec_cmd: rename to use dash in file name
[thirdparty/git.git] / http-push.c
CommitLineData
58e60dd2
NH
1#include "cache.h"
2#include "commit.h"
58e60dd2
NH
3#include "tag.h"
4#include "blob.h"
29508e1e 5#include "http.h"
aa1dbc98 6#include "refs.h"
c4e05b1a 7#include "diff.h"
aa1dbc98 8#include "revision.h"
d807c4a0 9#include "exec-cmd.h"
6b62816c 10#include "remote.h"
d633c882 11#include "list-objects.h"
4a16d072 12#include "sigchain.h"
a0355f6b 13#include "argv-array.h"
d6fe0036 14#include "packfile.h"
a80d72db 15#include "object-store.h"
29508e1e 16
081fd8d0
MK
17#ifdef EXPAT_NEEDS_XMLPARSE_H
18#include <xmlparse.h>
19#else
bee8e79d 20#include <expat.h>
081fd8d0 21#endif
58e60dd2
NH
22
23static const char http_push_usage[] =
1b1dd23f 24"git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
58e60dd2 25
92e2eb9c
JS
26#ifndef XML_STATUS_OK
27enum XML_Status {
28 XML_STATUS_OK = 1,
29 XML_STATUS_ERROR = 0
30};
31#define XML_STATUS_OK 1
32#define XML_STATUS_ERROR 0
33#endif
34
197e8951 35#define PREV_BUF_SIZE 4096
58e60dd2 36
acf59575 37/* DAV methods */
58e60dd2
NH
38#define DAV_LOCK "LOCK"
39#define DAV_MKCOL "MKCOL"
40#define DAV_MOVE "MOVE"
41#define DAV_PROPFIND "PROPFIND"
42#define DAV_PUT "PUT"
43#define DAV_UNLOCK "UNLOCK"
3dfaf7bc 44#define DAV_DELETE "DELETE"
acf59575
NH
45
46/* DAV lock flags */
47#define DAV_PROP_LOCKWR (1u << 0)
48#define DAV_PROP_LOCKEX (1u << 1)
49#define DAV_LOCK_OK (1u << 2)
50
51/* DAV XML properties */
52#define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
53#define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
54#define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
55#define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
56#define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
57#define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
aa1dbc98
NH
58#define DAV_PROPFIND_RESP ".multistatus.response"
59#define DAV_PROPFIND_NAME ".multistatus.response.href"
60#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
acf59575
NH
61
62/* DAV request body templates */
aa1dbc98
NH
63#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>"
64#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
65#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>"
66
75187c9d
NH
67#define LOCK_TIME 600
68#define LOCK_REFRESH 30
69
208acbfb 70/* Remember to update object flag allocation in object.h */
1b65a5aa
JH
71#define LOCAL (1u<<16)
72#define REMOTE (1u<<17)
73#define FETCHING (1u<<18)
74#define PUSHING (1u<<19)
aa1dbc98 75
3dfaf7bc
NH
76/* We allow "recursive" symbolic refs. Only within reason, though */
77#define MAXDEPTH 5
78
96f1e58f
DR
79static int pushing;
80static int aborted;
a3c57c9a 81static signed char remote_dir_exists[256];
58e60dd2 82
96f1e58f 83static int push_verbosely;
28b9d6e5 84static int push_all = MATCH_REFS_NONE;
96f1e58f 85static int force_all;
fe5d1d3e 86static int dry_run;
ae4efe19 87static int helper_status;
58e60dd2 88
96f1e58f 89static struct object_list *objects;
aa1dbc98 90
9cba13ca 91struct repo {
58e60dd2 92 char *url;
e1f33efe 93 char *path;
aa1dbc98 94 int path_len;
197e8951
NH
95 int has_info_refs;
96 int can_update_info_refs;
97 int has_info_packs;
58e60dd2 98 struct packed_git *packs;
512d632c 99 struct remote_lock *locks;
58e60dd2
NH
100};
101
7b5201a6 102static struct repo *repo;
58e60dd2
NH
103
104enum transfer_state {
197e8951
NH
105 NEED_FETCH,
106 RUN_FETCH_LOOSE,
107 RUN_FETCH_PACKED,
58e60dd2
NH
108 NEED_PUSH,
109 RUN_MKCOL,
110 RUN_PUT,
111 RUN_MOVE,
112 ABORTED,
4b05548f 113 COMPLETE
58e60dd2
NH
114};
115
9cba13ca 116struct transfer_request {
aa1dbc98 117 struct object *obj;
58e60dd2
NH
118 char *url;
119 char *dest;
aa1dbc98 120 struct remote_lock *lock;
58e60dd2
NH
121 struct curl_slist *headers;
122 struct buffer buffer;
58e60dd2
NH
123 enum transfer_state state;
124 CURLcode curl_result;
125 char errorstr[CURL_ERROR_SIZE];
126 long http_code;
197e8951 127 void *userData;
58e60dd2
NH
128 struct active_request_slot *slot;
129 struct transfer_request *next;
130};
131
96f1e58f 132static struct transfer_request *request_queue_head;
58e60dd2 133
9cba13ca 134struct xml_ctx {
acf59575
NH
135 char *name;
136 int len;
137 char *cdata;
138 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
139 void *userData;
140};
141
9cba13ca 142struct remote_lock {
75187c9d 143 char *url;
26349b2e 144 char *owner;
75187c9d 145 char *token;
dfab7c14 146 char tmpfile_suffix[41];
26349b2e
NH
147 time_t start_time;
148 long timeout;
75187c9d 149 int refreshing;
aa1dbc98
NH
150 struct remote_lock *next;
151};
152
3030baa7
NH
153/* Flags that control remote_ls processing */
154#define PROCESS_FILES (1u << 0)
155#define PROCESS_DIRS (1u << 1)
156#define RECURSIVE (1u << 2)
157
158/* Flags that remote_ls passes to callback functions */
159#define IS_DIR (1u << 0)
160
9cba13ca 161struct remote_ls_ctx {
3030baa7
NH
162 char *path;
163 void (*userFunc)(struct remote_ls_ctx *ls);
164 void *userData;
165 int flags;
166 char *dentry_name;
167 int dentry_flags;
168 struct remote_ls_ctx *parent;
26349b2e
NH
169};
170
b1c7d4aa
TRC
171/* get_dav_token_headers options */
172enum dav_header_flag {
173 DAV_HEADER_IF = (1u << 0),
174 DAV_HEADER_LOCK = (1u << 1),
175 DAV_HEADER_TIMEOUT = (1u << 2)
176};
177
2aab167a 178static char *xml_entities(const char *s)
519d05be
MH
179{
180 struct strbuf buf = STRBUF_INIT;
37141f27 181 strbuf_addstr_xml_quoted(&buf, s);
519d05be
MH
182 return strbuf_detach(&buf, NULL);
183}
184
ebaaf316
DM
185static void curl_setup_http_get(CURL *curl, const char *url,
186 const char *custom_req)
187{
188 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
189 curl_easy_setopt(curl, CURLOPT_URL, url);
190 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
191 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
192}
193
194static void curl_setup_http(CURL *curl, const char *url,
195 const char *custom_req, struct buffer *buffer,
196 curl_write_callback write_fn)
197{
198 curl_easy_setopt(curl, CURLOPT_PUT, 1);
199 curl_easy_setopt(curl, CURLOPT_URL, url);
200 curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
201 curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
202 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
203#ifndef NO_CURL_IOCTL
204 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
479eaa8e 205 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, buffer);
ebaaf316
DM
206#endif
207 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
208 curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
209 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
210 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
211}
212
d456c9fd
JH
213static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
214{
b1c7d4aa 215 struct strbuf buf = STRBUF_INIT;
8cb01e2f 216 struct curl_slist *dav_headers = http_copy_default_headers();
b1c7d4aa 217
d456c9fd 218 if (options & DAV_HEADER_IF) {
b1c7d4aa
TRC
219 strbuf_addf(&buf, "If: (<%s>)", lock->token);
220 dav_headers = curl_slist_append(dav_headers, buf.buf);
221 strbuf_reset(&buf);
222 }
d456c9fd 223 if (options & DAV_HEADER_LOCK) {
b1c7d4aa
TRC
224 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
225 dav_headers = curl_slist_append(dav_headers, buf.buf);
226 strbuf_reset(&buf);
227 }
d456c9fd 228 if (options & DAV_HEADER_TIMEOUT) {
b1c7d4aa
TRC
229 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
230 dav_headers = curl_slist_append(dav_headers, buf.buf);
231 strbuf_reset(&buf);
232 }
233 strbuf_release(&buf);
234
235 return dav_headers;
236}
237
29508e1e 238static void finish_request(struct transfer_request *request);
197e8951 239static void release_request(struct transfer_request *request);
58e60dd2 240
29508e1e 241static void process_response(void *callback_data)
58e60dd2 242{
29508e1e
NH
243 struct transfer_request *request =
244 (struct transfer_request *)callback_data;
58e60dd2 245
29508e1e 246 finish_request(request);
58e60dd2
NH
247}
248
dd8239f9 249#ifdef USE_CURL_MULTI
dcdb3335 250
197e8951
NH
251static void start_fetch_loose(struct transfer_request *request)
252{
197e8951 253 struct active_request_slot *slot;
5424bc55 254 struct http_object_request *obj_req;
197e8951 255
ed1c9977 256 obj_req = new_http_object_request(repo->url, request->obj->oid.hash);
5424bc55 257 if (obj_req == NULL) {
197e8951 258 request->state = ABORTED;
197e8951
NH
259 return;
260 }
261
5424bc55 262 slot = obj_req->slot;
197e8951
NH
263 slot->callback_func = process_response;
264 slot->callback_data = request;
265 request->slot = slot;
5424bc55 266 request->userData = obj_req;
197e8951
NH
267
268 /* Try to get the request started, abort the request on error */
269 request->state = RUN_FETCH_LOOSE;
270 if (!start_active_slot(slot)) {
271 fprintf(stderr, "Unable to start GET request\n");
7b5201a6 272 repo->can_update_info_refs = 0;
5424bc55 273 release_http_object_request(obj_req);
197e8951
NH
274 release_request(request);
275 }
276}
277
dd8239f9
JH
278static void start_mkcol(struct transfer_request *request)
279{
f2fd0760 280 char *hex = oid_to_hex(&request->obj->oid);
dd8239f9 281 struct active_request_slot *slot;
dd8239f9 282
7b5201a6 283 request->url = get_remote_object_url(repo->url, hex, 1);
dd8239f9
JH
284
285 slot = get_active_slot();
286 slot->callback_func = process_response;
287 slot->callback_data = request;
ebaaf316 288 curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
dd8239f9 289 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
dd8239f9
JH
290
291 if (start_active_slot(slot)) {
292 request->slot = slot;
293 request->state = RUN_MKCOL;
294 } else {
295 request->state = ABORTED;
6a83d902 296 FREE_AND_NULL(request->url);
dd8239f9
JH
297 }
298}
299#endif
300
197e8951
NH
301static void start_fetch_packed(struct transfer_request *request)
302{
197e8951 303 struct packed_git *target;
197e8951
NH
304
305 struct transfer_request *check_request = request_queue_head;
2264dfa5 306 struct http_pack_request *preq;
197e8951 307
ed1c9977 308 target = find_sha1_pack(request->obj->oid.hash, repo->packs);
197e8951 309 if (!target) {
f2fd0760 310 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
7b5201a6 311 repo->can_update_info_refs = 0;
197e8951
NH
312 release_request(request);
313 return;
314 }
315
316 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
f2fd0760 317 fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
197e8951 318
2264dfa5
TRC
319 preq = new_http_pack_request(target, repo->url);
320 if (preq == NULL) {
2264dfa5
TRC
321 repo->can_update_info_refs = 0;
322 return;
323 }
324 preq->lst = &repo->packs;
197e8951
NH
325
326 /* Make sure there isn't another open request for this pack */
327 while (check_request) {
328 if (check_request->state == RUN_FETCH_PACKED &&
2264dfa5
TRC
329 !strcmp(check_request->url, preq->url)) {
330 release_http_pack_request(preq);
197e8951
NH
331 release_request(request);
332 return;
333 }
334 check_request = check_request->next;
335 }
336
2264dfa5
TRC
337 preq->slot->callback_func = process_response;
338 preq->slot->callback_data = request;
339 request->slot = preq->slot;
340 request->userData = preq;
197e8951
NH
341
342 /* Try to get the request started, abort the request on error */
343 request->state = RUN_FETCH_PACKED;
2264dfa5 344 if (!start_active_slot(preq->slot)) {
197e8951 345 fprintf(stderr, "Unable to start GET request\n");
2264dfa5 346 release_http_pack_request(preq);
7b5201a6 347 repo->can_update_info_refs = 0;
197e8951
NH
348 release_request(request);
349 }
350}
351
58e60dd2
NH
352static void start_put(struct transfer_request *request)
353{
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);
debca9d2 366 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), 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);
dfab7c14 399 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
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 {
501 while (entry->next != NULL && entry->next != request)
502 entry = entry->next;
503 if (entry->next == request)
504 entry->next = entry->next->next;
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
NH
525
526 /* URL is reused for MOVE after PUT */
527 if (request->state != RUN_PUT) {
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;
197e8951 597 release_request(request);
58e60dd2
NH
598 }
599}
600
b3ca4e4e 601#ifdef USE_CURL_MULTI
4f66250d 602static int is_running_queue;
fc57b6aa 603static int fill_active_slot(void *unused)
58e60dd2 604{
8e24cbae 605 struct transfer_request *request;
58e60dd2 606
4f66250d 607 if (aborted || !is_running_queue)
45c17412 608 return 0;
58e60dd2 609
45c17412 610 for (request = request_queue_head; request; request = request->next) {
197e8951
NH
611 if (request->state == NEED_FETCH) {
612 start_fetch_loose(request);
45c17412 613 return 1;
197e8951 614 } else if (pushing && request->state == NEED_PUSH) {
ed1c9977 615 if (remote_dir_exists[request->obj->oid.hash[0]] == 1) {
0dd276b8 616 start_put(request);
aa1dbc98 617 } else {
0dd276b8 618 start_mkcol(request);
aa1dbc98 619 }
45c17412 620 return 1;
58e60dd2 621 }
aa1dbc98 622 }
45c17412 623 return 0;
58e60dd2 624}
b3ca4e4e 625#endif
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
b3ca4e4e 654#ifdef USE_CURL_MULTI
197e8951
NH
655 fill_active_slots();
656 step_active_slots();
b3ca4e4e 657#endif
197e8951
NH
658}
659
1a703cba 660static int add_send_request(struct object *obj, struct remote_lock *lock)
58e60dd2 661{
3def06e6 662 struct transfer_request *request;
58e60dd2 663 struct packed_git *target;
58e60dd2 664
512d632c
NH
665 /* Keep locks active */
666 check_locks();
667
aa1dbc98
NH
668 /*
669 * Don't push the object if it's known to exist on the remote
670 * or is already in the request queue
671 */
ed1c9977 672 if (remote_dir_exists[obj->oid.hash[0]] == -1)
673 get_remote_object_list(obj->oid.hash[0]);
aa1dbc98 674 if (obj->flags & (REMOTE | PUSHING))
1a703cba 675 return 0;
ed1c9977 676 target = find_sha1_pack(obj->oid.hash, repo->packs);
aa1dbc98
NH
677 if (target) {
678 obj->flags |= REMOTE;
1a703cba 679 return 0;
aa1dbc98 680 }
58e60dd2 681
aa1dbc98 682 obj->flags |= PUSHING;
58e60dd2 683 request = xmalloc(sizeof(*request));
aa1dbc98 684 request->obj = obj;
58e60dd2 685 request->url = NULL;
26349b2e 686 request->lock = lock;
58e60dd2 687 request->headers = NULL;
aa1dbc98 688 request->state = NEED_PUSH;
c17fb6ee
NH
689 request->next = request_queue_head;
690 request_queue_head = request;
29508e1e 691
b3ca4e4e 692#ifdef USE_CURL_MULTI
29508e1e
NH
693 fill_active_slots();
694 step_active_slots();
b3ca4e4e 695#endif
1a703cba
NH
696
697 return 1;
58e60dd2
NH
698}
699
f4f440a0 700static int fetch_indices(void)
58e60dd2 701{
b8caac2b 702 int ret;
58e60dd2 703
58e60dd2
NH
704 if (push_verbosely)
705 fprintf(stderr, "Getting pack list\n");
1a703cba 706
b8caac2b
TRC
707 switch (http_get_info_packs(repo->url, &repo->packs)) {
708 case HTTP_OK:
709 case HTTP_MISSING_TARGET:
710 ret = 0;
711 break;
712 default:
713 ret = -1;
58e60dd2
NH
714 }
715
b8caac2b 716 return ret;
58e60dd2
NH
717}
718
1aa40df6 719static void one_remote_object(const struct object_id *oid)
aa1dbc98 720{
aa1dbc98
NH
721 struct object *obj;
722
1aa40df6 723 obj = lookup_object(oid->hash);
aa1dbc98 724 if (!obj)
c251c83d 725 obj = parse_object(oid);
aa1dbc98
NH
726
727 /* Ignore remote objects that don't exist locally */
728 if (!obj)
729 return;
730
731 obj->flags |= REMOTE;
732 if (!object_list_contains(objects, obj))
1f1e895f 733 object_list_insert(obj, &objects);
aa1dbc98
NH
734}
735
acf59575 736static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
26349b2e 737{
acf59575
NH
738 int *lock_flags = (int *)ctx->userData;
739
740 if (tag_closed) {
741 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
742 if ((*lock_flags & DAV_PROP_LOCKEX) &&
743 (*lock_flags & DAV_PROP_LOCKWR)) {
744 *lock_flags |= DAV_LOCK_OK;
745 }
746 *lock_flags &= DAV_LOCK_OK;
747 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
748 *lock_flags |= DAV_PROP_LOCKWR;
749 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
750 *lock_flags |= DAV_PROP_LOCKEX;
751 }
752 }
26349b2e
NH
753}
754
acf59575 755static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
26349b2e 756{
aa1dbc98 757 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
dfab7c14
TRC
758 git_SHA_CTX sha_ctx;
759 unsigned char lock_token_sha1[20];
acf59575
NH
760
761 if (tag_closed && ctx->cdata) {
762 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
95244ae3 763 lock->owner = xstrdup(ctx->cdata);
acf59575 764 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
ae021d87
JK
765 const char *arg;
766 if (skip_prefix(ctx->cdata, "Second-", &arg))
767 lock->timeout = strtol(arg, NULL, 10);
acf59575 768 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
95244ae3 769 lock->token = xstrdup(ctx->cdata);
dfab7c14
TRC
770
771 git_SHA1_Init(&sha_ctx);
772 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
773 git_SHA1_Final(lock_token_sha1, &sha_ctx);
774
775 lock->tmpfile_suffix[0] = '_';
776 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
acf59575 777 }
26349b2e
NH
778 }
779}
780
2aab167a 781static void one_remote_ref(const char *refname);
aa1dbc98 782
26349b2e 783static void
acf59575 784xml_start_tag(void *userData, const char *name, const char **atts)
26349b2e 785{
acf59575 786 struct xml_ctx *ctx = (struct xml_ctx *)userData;
ef9e58c8 787 const char *c = strchr(name, ':');
0cc41428 788 int old_namelen, new_len;
acf59575
NH
789
790 if (c == NULL)
791 c = name;
792 else
793 c++;
794
0cc41428
JK
795 old_namelen = strlen(ctx->name);
796 new_len = old_namelen + strlen(c) + 2;
acf59575
NH
797
798 if (new_len > ctx->len) {
799 ctx->name = xrealloc(ctx->name, new_len);
800 ctx->len = new_len;
26349b2e 801 }
0cc41428 802 xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
26349b2e 803
6a83d902 804 FREE_AND_NULL(ctx->cdata);
acf59575
NH
805
806 ctx->userFunc(ctx, 0);
26349b2e
NH
807}
808
58e60dd2 809static void
acf59575 810xml_end_tag(void *userData, const char *name)
58e60dd2 811{
acf59575 812 struct xml_ctx *ctx = (struct xml_ctx *)userData;
ef9e58c8 813 const char *c = strchr(name, ':');
acf59575 814 char *ep;
58e60dd2 815
acf59575
NH
816 ctx->userFunc(ctx, 1);
817
818 if (c == NULL)
819 c = name;
820 else
821 c++;
822
823 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
824 *ep = 0;
58e60dd2
NH
825}
826
827static void
acf59575 828xml_cdata(void *userData, const XML_Char *s, int len)
58e60dd2 829{
acf59575 830 struct xml_ctx *ctx = (struct xml_ctx *)userData;
4cac42b1 831 free(ctx->cdata);
182af834 832 ctx->cdata = xmemdupz(s, len);
58e60dd2
NH
833}
834
554fe20d 835static struct remote_lock *lock_remote(const char *path, long timeout)
58e60dd2
NH
836{
837 struct active_request_slot *slot;
baa7b67d 838 struct slot_results results;
028c2976
MH
839 struct buffer out_buffer = { STRBUF_INIT, 0 };
840 struct strbuf in_buffer = STRBUF_INIT;
58e60dd2 841 char *url;
0772b9a6 842 char *ep;
58e60dd2 843 char timeout_header[25];
512d632c 844 struct remote_lock *lock = NULL;
8cb01e2f 845 struct curl_slist *dav_headers = http_copy_default_headers();
acf59575 846 struct xml_ctx ctx;
519d05be 847 char *escaped;
58e60dd2 848
28310186 849 url = xstrfmt("%s%s", repo->url, path);
aa1dbc98 850
0772b9a6 851 /* Make sure leading directories exist for the remote ref */
7b5201a6 852 ep = strchr(url + strlen(repo->url) + 1, '/');
0772b9a6 853 while (ep) {
466ddf90
JS
854 char saved_character = ep[1];
855 ep[1] = '\0';
0772b9a6 856 slot = get_active_slot();
baa7b67d 857 slot->results = &results;
ebaaf316 858 curl_setup_http_get(slot->curl, url, DAV_MKCOL);
0772b9a6
NH
859 if (start_active_slot(slot)) {
860 run_active_slot(slot);
baa7b67d
NH
861 if (results.curl_result != CURLE_OK &&
862 results.http_code != 405) {
0772b9a6
NH
863 fprintf(stderr,
864 "Unable to create branch path %s\n",
865 url);
866 free(url);
867 return NULL;
868 }
869 } else {
1a703cba 870 fprintf(stderr, "Unable to start MKCOL request\n");
0772b9a6
NH
871 free(url);
872 return NULL;
873 }
466ddf90 874 ep[1] = saved_character;
0772b9a6
NH
875 ep = strchr(ep + 1, '/');
876 }
877
5cb2194a 878 escaped = xml_entities(ident_default_email());
519d05be
MH
879 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
880 free(escaped);
26349b2e 881
5096d490 882 xsnprintf(timeout_header, sizeof(timeout_header), "Timeout: Second-%ld", timeout);
58e60dd2
NH
883 dav_headers = curl_slist_append(dav_headers, timeout_header);
884 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
885
886 slot = get_active_slot();
baa7b67d 887 slot->results = &results;
ebaaf316 888 curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
58e60dd2 889 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
ebaaf316 890 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
58e60dd2 891
aa1dbc98 892 lock = xcalloc(1, sizeof(*lock));
aa1dbc98 893 lock->timeout = -1;
acf59575 894
58e60dd2
NH
895 if (start_active_slot(slot)) {
896 run_active_slot(slot);
baa7b67d 897 if (results.curl_result == CURLE_OK) {
472b2570
MH
898 XML_Parser parser = XML_ParserCreate(NULL);
899 enum XML_Status result;
acf59575
NH
900 ctx.name = xcalloc(10, 1);
901 ctx.len = 0;
902 ctx.cdata = NULL;
903 ctx.userFunc = handle_new_lock_ctx;
aa1dbc98 904 ctx.userData = lock;
acf59575
NH
905 XML_SetUserData(parser, &ctx);
906 XML_SetElementHandler(parser, xml_start_tag,
907 xml_end_tag);
908 XML_SetCharacterDataHandler(parser, xml_cdata);
028c2976
MH
909 result = XML_Parse(parser, in_buffer.buf,
910 in_buffer.len, 1);
acf59575
NH
911 free(ctx.name);
912 if (result != XML_STATUS_OK) {
913 fprintf(stderr, "XML error: %s\n",
914 XML_ErrorString(
915 XML_GetErrorCode(parser)));
aa1dbc98 916 lock->timeout = -1;
acf59575 917 }
472b2570 918 XML_ParserFree(parser);
a2b9820c
PO
919 } else {
920 fprintf(stderr,
921 "error: curl result=%d, HTTP code=%ld\n",
922 results.curl_result, results.http_code);
58e60dd2
NH
923 }
924 } else {
1a703cba 925 fprintf(stderr, "Unable to start LOCK request\n");
58e60dd2
NH
926 }
927
acf59575 928 curl_slist_free_all(dav_headers);
028c2976
MH
929 strbuf_release(&out_buffer.buf);
930 strbuf_release(&in_buffer);
26349b2e 931
aa1dbc98 932 if (lock->token == NULL || lock->timeout <= 0) {
8e0f7003
JM
933 free(lock->token);
934 free(lock->owner);
75187c9d 935 free(url);
6a83d902 936 FREE_AND_NULL(lock);
acf59575 937 } else {
aa1dbc98 938 lock->url = url;
aa1dbc98 939 lock->start_time = time(NULL);
7b5201a6
AK
940 lock->next = repo->locks;
941 repo->locks = lock;
26349b2e
NH
942 }
943
aa1dbc98 944 return lock;
58e60dd2
NH
945}
946
aa1dbc98 947static int unlock_remote(struct remote_lock *lock)
58e60dd2
NH
948{
949 struct active_request_slot *slot;
baa7b67d 950 struct slot_results results;
7b5201a6 951 struct remote_lock *prev = repo->locks;
b1c7d4aa 952 struct curl_slist *dav_headers;
58e60dd2
NH
953 int rc = 0;
954
b1c7d4aa 955 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
58e60dd2
NH
956
957 slot = get_active_slot();
baa7b67d 958 slot->results = &results;
ebaaf316 959 curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
58e60dd2
NH
960 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
961
962 if (start_active_slot(slot)) {
963 run_active_slot(slot);
baa7b67d 964 if (results.curl_result == CURLE_OK)
58e60dd2
NH
965 rc = 1;
966 else
512d632c 967 fprintf(stderr, "UNLOCK HTTP error %ld\n",
baa7b67d 968 results.http_code);
58e60dd2 969 } else {
512d632c 970 fprintf(stderr, "Unable to start UNLOCK request\n");
58e60dd2
NH
971 }
972
973 curl_slist_free_all(dav_headers);
75187c9d 974
7b5201a6
AK
975 if (repo->locks == lock) {
976 repo->locks = lock->next;
512d632c
NH
977 } else {
978 while (prev && prev->next != lock)
979 prev = prev->next;
980 if (prev)
981 prev->next = prev->next->next;
982 }
983
8e0f7003 984 free(lock->owner);
512d632c
NH
985 free(lock->url);
986 free(lock->token);
987 free(lock);
58e60dd2
NH
988
989 return rc;
990}
991
6a491a17
CB
992static void remove_locks(void)
993{
7b5201a6 994 struct remote_lock *lock = repo->locks;
6a491a17
CB
995
996 fprintf(stderr, "Removing remote locks...\n");
997 while (lock) {
6589ebf1 998 struct remote_lock *next = lock->next;
6a491a17 999 unlock_remote(lock);
6589ebf1 1000 lock = next;
6a491a17
CB
1001 }
1002}
1003
1004static void remove_locks_on_signal(int signo)
1005{
1006 remove_locks();
4a16d072 1007 sigchain_pop(signo);
6a491a17
CB
1008 raise(signo);
1009}
1010
3030baa7
NH
1011static void remote_ls(const char *path, int flags,
1012 void (*userFunc)(struct remote_ls_ctx *ls),
1013 void *userData);
aa1dbc98 1014
c3bdc4e7 1015/* extract hex from sharded "xx/x{38}" filename */
1aa40df6 1016static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
67a31f61 1017{
1aa40df6 1018 if (strlen(path) != GIT_SHA1_HEXSZ + 1)
67a31f61
JK
1019 return -1;
1020
c3bdc4e7
RS
1021 if (hex_to_bytes(oid->hash, path, 1))
1022 return -1;
67a31f61
JK
1023 path += 2;
1024 path++; /* skip '/' */
67a31f61 1025
c3bdc4e7 1026 return hex_to_bytes(oid->hash + 1, path, GIT_SHA1_RAWSZ - 1);
67a31f61
JK
1027}
1028
3030baa7
NH
1029static void process_ls_object(struct remote_ls_ctx *ls)
1030{
1031 unsigned int *parent = (unsigned int *)ls->userData;
67a31f61 1032 const char *path = ls->dentry_name;
1aa40df6 1033 struct object_id oid;
aa1dbc98 1034
3030baa7
NH
1035 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1036 remote_dir_exists[*parent] = 1;
1037 return;
1038 }
aa1dbc98 1039
67a31f61 1040 if (!skip_prefix(path, "objects/", &path) ||
1aa40df6 1041 get_oid_hex_from_objpath(path, &oid))
3030baa7 1042 return;
67a31f61 1043
1aa40df6 1044 one_remote_object(&oid);
3030baa7 1045}
aa1dbc98 1046
3030baa7
NH
1047static void process_ls_ref(struct remote_ls_ctx *ls)
1048{
1049 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1050 fprintf(stderr, " %s\n", ls->dentry_name);
1051 return;
1052 }
aa1dbc98 1053
3030baa7
NH
1054 if (!(ls->dentry_flags & IS_DIR))
1055 one_remote_ref(ls->dentry_name);
1056}
aa1dbc98 1057
3030baa7
NH
1058static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1059{
1060 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
aa1dbc98 1061
3030baa7
NH
1062 if (tag_closed) {
1063 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1064 if (ls->dentry_flags & IS_DIR) {
0fdadc50
TRC
1065
1066 /* ensure collection names end with slash */
1067 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1068
3030baa7
NH
1069 if (ls->flags & PROCESS_DIRS) {
1070 ls->userFunc(ls);
1071 }
1072 if (strcmp(ls->dentry_name, ls->path) &&
1073 ls->flags & RECURSIVE) {
1074 remote_ls(ls->dentry_name,
1075 ls->flags,
1076 ls->userFunc,
1077 ls->userData);
1078 }
1079 } else if (ls->flags & PROCESS_FILES) {
1080 ls->userFunc(ls);
aa1dbc98 1081 }
3030baa7 1082 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
e1f33efe
KK
1083 char *path = ctx->cdata;
1084 if (*ctx->cdata == 'h') {
1085 path = strstr(path, "//");
1086 if (path) {
1087 path = strchr(path+2, '/');
1088 }
1089 }
1090 if (path) {
dfc2dcd9
TRC
1091 const char *url = repo->url;
1092 if (repo->path)
1093 url = repo->path;
1094 if (strncmp(path, url, repo->path_len))
82247e9b 1095 error("Parsed path '%s' does not match url: '%s'",
dfc2dcd9
TRC
1096 path, url);
1097 else {
1098 path += repo->path_len;
1099 ls->dentry_name = xstrdup(path);
1100 }
e1f33efe 1101 }
3030baa7
NH
1102 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1103 ls->dentry_flags |= IS_DIR;
aa1dbc98 1104 }
3030baa7 1105 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
6a83d902 1106 FREE_AND_NULL(ls->dentry_name);
3030baa7 1107 ls->dentry_flags = 0;
aa1dbc98 1108 }
aa1dbc98
NH
1109}
1110
20642801
JS
1111/*
1112 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1113 * should _only_ heed the information from that file, instead of trying to
1114 * determine the refs from the remote file system (badly: it does not even
1115 * know about packed-refs).
1116 */
3030baa7
NH
1117static void remote_ls(const char *path, int flags,
1118 void (*userFunc)(struct remote_ls_ctx *ls),
1119 void *userData)
aa1dbc98 1120{
28310186 1121 char *url = xstrfmt("%s%s", repo->url, path);
aa1dbc98 1122 struct active_request_slot *slot;
baa7b67d 1123 struct slot_results results;
028c2976
MH
1124 struct strbuf in_buffer = STRBUF_INIT;
1125 struct buffer out_buffer = { STRBUF_INIT, 0 };
8cb01e2f 1126 struct curl_slist *dav_headers = http_copy_default_headers();
aa1dbc98 1127 struct xml_ctx ctx;
3030baa7
NH
1128 struct remote_ls_ctx ls;
1129
1130 ls.flags = flags;
9befac47 1131 ls.path = xstrdup(path);
3030baa7
NH
1132 ls.dentry_name = NULL;
1133 ls.dentry_flags = 0;
1134 ls.userData = userData;
1135 ls.userFunc = userFunc;
aa1dbc98 1136
02962d36 1137 strbuf_addstr(&out_buffer.buf, PROPFIND_ALL_REQUEST);
aa1dbc98
NH
1138
1139 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1140 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1141
1142 slot = get_active_slot();
baa7b67d 1143 slot->results = &results;
ebaaf316
DM
1144 curl_setup_http(slot->curl, url, DAV_PROPFIND,
1145 &out_buffer, fwrite_buffer);
aa1dbc98 1146 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
ebaaf316 1147 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
aa1dbc98
NH
1148
1149 if (start_active_slot(slot)) {
1150 run_active_slot(slot);
baa7b67d 1151 if (results.curl_result == CURLE_OK) {
472b2570
MH
1152 XML_Parser parser = XML_ParserCreate(NULL);
1153 enum XML_Status result;
aa1dbc98
NH
1154 ctx.name = xcalloc(10, 1);
1155 ctx.len = 0;
1156 ctx.cdata = NULL;
3030baa7
NH
1157 ctx.userFunc = handle_remote_ls_ctx;
1158 ctx.userData = &ls;
aa1dbc98
NH
1159 XML_SetUserData(parser, &ctx);
1160 XML_SetElementHandler(parser, xml_start_tag,
1161 xml_end_tag);
1162 XML_SetCharacterDataHandler(parser, xml_cdata);
028c2976
MH
1163 result = XML_Parse(parser, in_buffer.buf,
1164 in_buffer.len, 1);
aa1dbc98
NH
1165 free(ctx.name);
1166
1167 if (result != XML_STATUS_OK) {
1168 fprintf(stderr, "XML error: %s\n",
1169 XML_ErrorString(
1170 XML_GetErrorCode(parser)));
1171 }
472b2570 1172 XML_ParserFree(parser);
aa1dbc98
NH
1173 }
1174 } else {
3030baa7 1175 fprintf(stderr, "Unable to start PROPFIND request\n");
aa1dbc98
NH
1176 }
1177
3030baa7 1178 free(ls.path);
aa1dbc98 1179 free(url);
028c2976
MH
1180 strbuf_release(&out_buffer.buf);
1181 strbuf_release(&in_buffer);
aa1dbc98
NH
1182 curl_slist_free_all(dav_headers);
1183}
1184
3030baa7
NH
1185static void get_remote_object_list(unsigned char parent)
1186{
1187 char path[] = "objects/XX/";
1188 static const char hex[] = "0123456789abcdef";
1189 unsigned int val = parent;
1190
1191 path[8] = hex[val >> 4];
1192 path[9] = hex[val & 0xf];
1193 remote_dir_exists[val] = 0;
1194 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1195 process_ls_object, &val);
1196}
1197
acf59575 1198static int locking_available(void)
58e60dd2
NH
1199{
1200 struct active_request_slot *slot;
baa7b67d 1201 struct slot_results results;
028c2976
MH
1202 struct strbuf in_buffer = STRBUF_INIT;
1203 struct buffer out_buffer = { STRBUF_INIT, 0 };
8cb01e2f 1204 struct curl_slist *dav_headers = http_copy_default_headers();
acf59575
NH
1205 struct xml_ctx ctx;
1206 int lock_flags = 0;
519d05be 1207 char *escaped;
58e60dd2 1208
519d05be
MH
1209 escaped = xml_entities(repo->url);
1210 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1211 free(escaped);
58e60dd2
NH
1212
1213 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1214 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
baa7b67d 1215
58e60dd2 1216 slot = get_active_slot();
baa7b67d 1217 slot->results = &results;
ebaaf316
DM
1218 curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
1219 &out_buffer, fwrite_buffer);
58e60dd2 1220 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
ebaaf316 1221 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
58e60dd2
NH
1222
1223 if (start_active_slot(slot)) {
1224 run_active_slot(slot);
baa7b67d 1225 if (results.curl_result == CURLE_OK) {
472b2570
MH
1226 XML_Parser parser = XML_ParserCreate(NULL);
1227 enum XML_Status result;
acf59575
NH
1228 ctx.name = xcalloc(10, 1);
1229 ctx.len = 0;
1230 ctx.cdata = NULL;
1231 ctx.userFunc = handle_lockprop_ctx;
1232 ctx.userData = &lock_flags;
1233 XML_SetUserData(parser, &ctx);
1234 XML_SetElementHandler(parser, xml_start_tag,
1235 xml_end_tag);
028c2976
MH
1236 result = XML_Parse(parser, in_buffer.buf,
1237 in_buffer.len, 1);
acf59575
NH
1238 free(ctx.name);
1239
1240 if (result != XML_STATUS_OK) {
1241 fprintf(stderr, "XML error: %s\n",
1242 XML_ErrorString(
1243 XML_GetErrorCode(parser)));
1244 lock_flags = 0;
1245 }
472b2570 1246 XML_ParserFree(parser);
325ce395 1247 if (!lock_flags)
d5c87cb4 1248 error("no DAV locking support on %s",
7b5201a6 1249 repo->url);
325ce395
JH
1250
1251 } else {
1252 error("Cannot access URL %s, return code %d",
7b5201a6 1253 repo->url, results.curl_result);
325ce395 1254 lock_flags = 0;
58e60dd2 1255 }
58e60dd2 1256 } else {
7b5201a6 1257 error("Unable to start PROPFIND request on %s", repo->url);
58e60dd2
NH
1258 }
1259
028c2976
MH
1260 strbuf_release(&out_buffer.buf);
1261 strbuf_release(&in_buffer);
acf59575
NH
1262 curl_slist_free_all(dav_headers);
1263
1264 return lock_flags;
58e60dd2
NH
1265}
1266
b5bf7cd6 1267static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1f1e895f
LT
1268{
1269 struct object_list *entry = xmalloc(sizeof(struct object_list));
1270 entry->item = obj;
1271 entry->next = *p;
1272 *p = entry;
1273 return &entry->next;
1274}
1275
aa1dbc98 1276static struct object_list **process_blob(struct blob *blob,
41595938 1277 struct object_list **p)
58e60dd2 1278{
aa1dbc98 1279 struct object *obj = &blob->object;
58e60dd2 1280
aa1dbc98
NH
1281 obj->flags |= LOCAL;
1282
1283 if (obj->flags & (UNINTERESTING | SEEN))
1284 return p;
1285
1286 obj->flags |= SEEN;
1f1e895f 1287 return add_one_object(obj, p);
aa1dbc98
NH
1288}
1289
1290static struct object_list **process_tree(struct tree *tree,
41595938 1291 struct object_list **p)
aa1dbc98
NH
1292{
1293 struct object *obj = &tree->object;
2d9c58c6 1294 struct tree_desc desc;
4c068a98 1295 struct name_entry entry;
aa1dbc98
NH
1296
1297 obj->flags |= LOCAL;
1298
1299 if (obj->flags & (UNINTERESTING | SEEN))
1300 return p;
1301 if (parse_tree(tree) < 0)
f2fd0760 1302 die("bad tree object %s", oid_to_hex(&obj->oid));
aa1dbc98
NH
1303
1304 obj->flags |= SEEN;
1f1e895f 1305 p = add_one_object(obj, p);
2d9c58c6 1306
6fda5e51 1307 init_tree_desc(&desc, tree->buffer, tree->size);
2d9c58c6 1308
1bbeb4c6
JS
1309 while (tree_entry(&desc, &entry))
1310 switch (object_type(entry.mode)) {
1311 case OBJ_TREE:
740ee055 1312 p = process_tree(lookup_tree(entry.oid), p);
1bbeb4c6
JS
1313 break;
1314 case OBJ_BLOB:
3aca1fc6 1315 p = process_blob(lookup_blob(entry.oid), 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) {
41595938 1334 p = process_tree(commit->tree, 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
aa1dbc98 1372static int update_remote(unsigned char *sha1, 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
028c2976 1381 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
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)) {
1430 obj = lookup_unknown_object(ref->old_oid.hash);
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
c251c83d 1462 o = parse_object(&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) {
197e8951 1476 o = deref_tag(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
TRC
1673{
1674#ifdef USE_CURL_MULTI
1675 is_running_queue = 1;
1676 fill_active_slots();
1677 add_fill_function(NULL, fill_active_slot);
1678#endif
1679 do {
1680 finish_all_active_slots();
1681#ifdef USE_CURL_MULTI
1682 fill_active_slots();
1683#endif
1684 } while (request_queue_head && !aborted);
1685
1686#ifdef USE_CURL_MULTI
1687 is_running_queue = 0;
1688#endif
1689}
1690
3f2e2297 1691int cmd_main(int argc, const char **argv)
58e60dd2 1692{
58e60dd2
NH
1693 struct transfer_request *request;
1694 struct transfer_request *next_request;
1695 int nr_refspec = 0;
3f2e2297 1696 const char **refspec = NULL;
512d632c 1697 struct remote_lock *ref_lock = NULL;
197e8951 1698 struct remote_lock *info_ref_lock = NULL;
aa1dbc98 1699 struct rev_info revs;
3dfaf7bc
NH
1700 int delete_branch = 0;
1701 int force_delete = 0;
1a703cba 1702 int objects_to_send;
58e60dd2
NH
1703 int rc = 0;
1704 int i;
8c9e7947 1705 int new_refs;
454e2025 1706 struct ref *ref, *local_refs;
58e60dd2 1707
f3d51ffd 1708 repo = xcalloc(1, sizeof(*repo));
58e60dd2
NH
1709
1710 argv++;
1711 for (i = 1; i < argc; i++, argv++) {
3f2e2297 1712 const char *arg = *argv;
58e60dd2
NH
1713
1714 if (*arg == '-') {
aa1dbc98 1715 if (!strcmp(arg, "--all")) {
28b9d6e5 1716 push_all = MATCH_REFS_ALL;
58e60dd2
NH
1717 continue;
1718 }
1719 if (!strcmp(arg, "--force")) {
1720 force_all = 1;
1721 continue;
1722 }
fe5d1d3e
SP
1723 if (!strcmp(arg, "--dry-run")) {
1724 dry_run = 1;
1725 continue;
1726 }
ae4efe19
SP
1727 if (!strcmp(arg, "--helper-status")) {
1728 helper_status = 1;
1729 continue;
1730 }
58e60dd2
NH
1731 if (!strcmp(arg, "--verbose")) {
1732 push_verbosely = 1;
e9176745 1733 http_is_verbose = 1;
58e60dd2
NH
1734 continue;
1735 }
3dfaf7bc
NH
1736 if (!strcmp(arg, "-d")) {
1737 delete_branch = 1;
1738 continue;
1739 }
1740 if (!strcmp(arg, "-D")) {
1741 delete_branch = 1;
1742 force_delete = 1;
1743 continue;
1744 }
548d3464
JN
1745 if (!strcmp(arg, "-h"))
1746 usage(http_push_usage);
58e60dd2 1747 }
7b5201a6 1748 if (!repo->url) {
aa1dbc98 1749 char *path = strstr(arg, "//");
1462d1af
TRC
1750 str_end_url_with_slash(arg, &repo->url);
1751 repo->path_len = strlen(repo->url);
aa1dbc98 1752 if (path) {
7b5201a6
AK
1753 repo->path = strchr(path+2, '/');
1754 if (repo->path)
1755 repo->path_len = strlen(repo->path);
aa1dbc98 1756 }
58e60dd2
NH
1757 continue;
1758 }
1759 refspec = argv;
1760 nr_refspec = argc - i;
1761 break;
1762 }
1763
f854824b
GB
1764#ifndef USE_CURL_MULTI
1765 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1766#endif
1767
7b5201a6 1768 if (!repo->url)
3e9fabc8
NH
1769 usage(http_push_usage);
1770
3dfaf7bc
NH
1771 if (delete_branch && nr_refspec != 1)
1772 die("You must specify only one branch name when deleting a remote branch");
1773
548d3464
JN
1774 setup_git_directory();
1775
aa1dbc98 1776 memset(remote_dir_exists, -1, 256);
0dd276b8 1777
a4ddbc33 1778 http_init(NULL, repo->url, 1);
58e60dd2 1779
68862a31 1780#ifdef USE_CURL_MULTI
4f66250d 1781 is_running_queue = 0;
68862a31 1782#endif
4f66250d 1783
58e60dd2 1784 /* Verify DAV compliance/lock support */
acf59575 1785 if (!locking_available()) {
58e60dd2
NH
1786 rc = 1;
1787 goto cleanup;
1788 }
1789
57b235a4 1790 sigchain_push_common(remove_locks_on_signal);
6a491a17 1791
197e8951 1792 /* Check whether the remote has server info files */
7b5201a6
AK
1793 repo->can_update_info_refs = 0;
1794 repo->has_info_refs = remote_exists("info/refs");
1795 repo->has_info_packs = remote_exists("objects/info/packs");
1796 if (repo->has_info_refs) {
197e8951
NH
1797 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1798 if (info_ref_lock)
7b5201a6 1799 repo->can_update_info_refs = 1;
9bdbabad 1800 else {
d5c87cb4 1801 error("cannot lock existing info/refs");
9bdbabad
GB
1802 rc = 1;
1803 goto cleanup;
1804 }
197e8951 1805 }
7b5201a6 1806 if (repo->has_info_packs)
197e8951
NH
1807 fetch_indices();
1808
aa1dbc98 1809 /* Get a list of all local and remote heads to validate refspecs */
454e2025 1810 local_refs = get_local_heads();
aa1dbc98
NH
1811 fprintf(stderr, "Fetching remote heads...\n");
1812 get_dav_remote_heads();
4f66250d 1813 run_request_queue();
aa1dbc98 1814
3dfaf7bc
NH
1815 /* Remove a remote branch if -d or -D was specified */
1816 if (delete_branch) {
ae4efe19 1817 if (delete_remote_branch(refspec[0], force_delete) == -1) {
3dfaf7bc
NH
1818 fprintf(stderr, "Unable to delete remote branch %s\n",
1819 refspec[0]);
ae4efe19
SP
1820 if (helper_status)
1821 printf("error %s cannot remove\n", refspec[0]);
1822 }
3dfaf7bc
NH
1823 goto cleanup;
1824 }
1825
aa1dbc98 1826 /* match them up */
29753cdd
JH
1827 if (match_push_refs(local_refs, &remote_refs,
1828 nr_refspec, (const char **) refspec, push_all)) {
9116de59
GB
1829 rc = -1;
1830 goto cleanup;
1831 }
aa1dbc98
NH
1832 if (!remote_refs) {
1833 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
ae4efe19
SP
1834 if (helper_status)
1835 printf("error null no match\n");
9116de59
GB
1836 rc = 0;
1837 goto cleanup;
aa1dbc98
NH
1838 }
1839
8c9e7947 1840 new_refs = 0;
aa1dbc98 1841 for (ref = remote_refs; ref; ref = ref->next) {
a0355f6b 1842 struct argv_array commit_argv = ARGV_ARRAY_INIT;
8c9e7947 1843
aa1dbc98
NH
1844 if (!ref->peer_ref)
1845 continue;
6eaf4060 1846
f4e54d02 1847 if (is_null_oid(&ref->peer_ref->new_oid)) {
6eaf4060
CB
1848 if (delete_remote_branch(ref->name, 1) == -1) {
1849 error("Could not remove %s", ref->name);
ae4efe19
SP
1850 if (helper_status)
1851 printf("error %s cannot remove\n", ref->name);
6eaf4060
CB
1852 rc = -4;
1853 }
ae4efe19
SP
1854 else if (helper_status)
1855 printf("ok %s\n", ref->name);
6eaf4060
CB
1856 new_refs++;
1857 continue;
1858 }
1859
f4e54d02 1860 if (!oidcmp(&ref->old_oid, &ref->peer_ref->new_oid)) {
b5e59989 1861 if (push_verbosely)
aa1dbc98 1862 fprintf(stderr, "'%s': up-to-date\n", ref->name);
ae4efe19
SP
1863 if (helper_status)
1864 printf("ok %s up to date\n", ref->name);
aa1dbc98
NH
1865 continue;
1866 }
1867
1868 if (!force_all &&
f4e54d02 1869 !is_null_oid(&ref->old_oid) &&
aa1dbc98 1870 !ref->force) {
f4e54d02 1871 if (!has_object_file(&ref->old_oid) ||
6f3d57b6 1872 !ref_newer(&ref->peer_ref->new_oid,
1873 &ref->old_oid)) {
00ae8289
BF
1874 /*
1875 * We do not have the remote ref, or
aa1dbc98
NH
1876 * we know that the remote ref is not
1877 * an ancestor of what we are trying to
1878 * push. Either way this can be losing
1879 * commits at the remote end and likely
1880 * we were not up to date to begin with.
1881 */
00ae8289
BF
1882 error("remote '%s' is not an ancestor of\n"
1883 "local '%s'.\n"
1884 "Maybe you are not up-to-date and "
aa1dbc98
NH
1885 "need to pull first?",
1886 ref->name,
1887 ref->peer_ref->name);
ae4efe19
SP
1888 if (helper_status)
1889 printf("error %s non-fast forward\n", ref->name);
1a703cba 1890 rc = -2;
aa1dbc98
NH
1891 continue;
1892 }
58e60dd2 1893 }
f4e54d02 1894 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
aa1dbc98 1895 new_refs++;
aa1dbc98
NH
1896
1897 fprintf(stderr, "updating '%s'", ref->name);
1898 if (strcmp(ref->name, ref->peer_ref->name))
1899 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2b87d3a8 1900 fprintf(stderr, "\n from %s\n to %s\n",
f4e54d02 1901 oid_to_hex(&ref->old_oid), oid_to_hex(&ref->new_oid));
ae4efe19
SP
1902 if (dry_run) {
1903 if (helper_status)
1904 printf("ok %s\n", ref->name);
fe5d1d3e 1905 continue;
ae4efe19 1906 }
58e60dd2
NH
1907
1908 /* Lock remote branch ref */
aa1dbc98
NH
1909 ref_lock = lock_remote(ref->name, LOCK_TIME);
1910 if (ref_lock == NULL) {
58e60dd2 1911 fprintf(stderr, "Unable to lock remote branch %s\n",
aa1dbc98 1912 ref->name);
ae4efe19
SP
1913 if (helper_status)
1914 printf("error %s lock error\n", ref->name);
58e60dd2
NH
1915 rc = 1;
1916 continue;
1917 }
1918
aa1dbc98 1919 /* Set up revision info for this refspec */
a0355f6b
JK
1920 argv_array_push(&commit_argv, ""); /* ignored */
1921 argv_array_push(&commit_argv, "--objects");
f4e54d02 1922 argv_array_push(&commit_argv, oid_to_hex(&ref->new_oid));
1923 if (!push_all && !is_null_oid(&ref->old_oid))
a0355f6b 1924 argv_array_pushf(&commit_argv, "^%s",
f4e54d02 1925 oid_to_hex(&ref->old_oid));
db6296a5 1926 init_revisions(&revs, setup_git_directory());
a0355f6b 1927 setup_revisions(commit_argv.argc, commit_argv.argv, &revs, NULL);
d633c882 1928 revs.edge_hint = 0; /* just in case */
58e60dd2 1929
aa1dbc98 1930 /* Generate a list of objects that need to be pushed */
58e60dd2 1931 pushing = 0;
3d51e1b5
MK
1932 if (prepare_revision_walk(&revs))
1933 die("revision walk setup failed");
e76a5fb4 1934 mark_edges_uninteresting(&revs, NULL);
1a703cba 1935 objects_to_send = get_delta(&revs, ref_lock);
29508e1e 1936 finish_all_active_slots();
58e60dd2
NH
1937
1938 /* Push missing objects to remote, this would be a
1939 convenient time to pack them first if appropriate. */
1940 pushing = 1;
1a703cba
NH
1941 if (objects_to_send)
1942 fprintf(stderr, " sending %d objects\n",
1943 objects_to_send);
4f66250d
TRC
1944
1945 run_request_queue();
58e60dd2
NH
1946
1947 /* Update the remote branch if all went well */
f4e54d02 1948 if (aborted || !update_remote(ref->new_oid.hash, ref_lock))
aa1dbc98 1949 rc = 1;
58e60dd2 1950
aa1dbc98
NH
1951 if (!rc)
1952 fprintf(stderr, " done\n");
ae4efe19
SP
1953 if (helper_status)
1954 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
aa1dbc98 1955 unlock_remote(ref_lock);
512d632c 1956 check_locks();
a0355f6b 1957 argv_array_clear(&commit_argv);
58e60dd2
NH
1958 }
1959
197e8951 1960 /* Update remote server info if appropriate */
7b5201a6
AK
1961 if (repo->has_info_refs && new_refs) {
1962 if (info_ref_lock && repo->can_update_info_refs) {
197e8951 1963 fprintf(stderr, "Updating remote server info\n");
fe5d1d3e
SP
1964 if (!dry_run)
1965 update_remote_info_refs(info_ref_lock);
197e8951
NH
1966 } else {
1967 fprintf(stderr, "Unable to update server info\n");
1968 }
1969 }
197e8951 1970
58e60dd2 1971 cleanup:
9116de59
GB
1972 if (info_ref_lock)
1973 unlock_remote(info_ref_lock);
7b5201a6 1974 free(repo);
58e60dd2 1975
29508e1e 1976 http_cleanup();
58e60dd2
NH
1977
1978 request = request_queue_head;
1979 while (request != NULL) {
1980 next_request = request->next;
1981 release_request(request);
58e60dd2
NH
1982 request = next_request;
1983 }
1984
58e60dd2
NH
1985 return rc;
1986}