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