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