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