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