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