git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / builtin / receive-pack.c
| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "builtin.h" |
| 5 | #include "abspath.h" |
| 6 | #include "commit.h" |
| 7 | #include "commit-reach.h" |
| 8 | #include "config.h" |
| 9 | #include "connect.h" |
| 10 | #include "connected.h" |
| 11 | #include "environment.h" |
| 12 | #include "exec-cmd.h" |
| 13 | #include "fsck.h" |
| 14 | #include "gettext.h" |
| 15 | #include "gpg-interface.h" |
| 16 | #include "hex.h" |
| 17 | #include "hook.h" |
| 18 | #include "object.h" |
| 19 | #include "object-file.h" |
| 20 | #include "object-name.h" |
| 21 | #include "odb.h" |
| 22 | #include "oid-array.h" |
| 23 | #include "oidset.h" |
| 24 | #include "pack.h" |
| 25 | #include "parse-options.h" |
| 26 | #include "pkt-line.h" |
| 27 | #include "protocol.h" |
| 28 | #include "refs.h" |
| 29 | #include "remote.h" |
| 30 | #include "run-command.h" |
| 31 | #include "server-info.h" |
| 32 | #include "setup.h" |
| 33 | #include "shallow.h" |
| 34 | #include "sideband.h" |
| 35 | #include "sigchain.h" |
| 36 | #include "string-list.h" |
| 37 | #include "strvec.h" |
| 38 | #include "trace.h" |
| 39 | #include "trace2.h" |
| 40 | #include "version.h" |
| 41 | #include "worktree.h" |
| 42 | |
| 43 | static const char * const receive_pack_usage[] = { |
| 44 | N_("git receive-pack <git-dir>"), |
| 45 | NULL |
| 46 | }; |
| 47 | |
| 48 | enum deny_action { |
| 49 | DENY_UNCONFIGURED, |
| 50 | DENY_IGNORE, |
| 51 | DENY_WARN, |
| 52 | DENY_REFUSE, |
| 53 | DENY_UPDATE_INSTEAD |
| 54 | }; |
| 55 | |
| 56 | enum report_status_version { |
| 57 | REPORT_STATUS_UNKNOWN = 0, |
| 58 | REPORT_STATUS_V0, |
| 59 | REPORT_STATUS_V2, |
| 60 | }; |
| 61 | |
| 62 | static int deny_deletes; |
| 63 | static int deny_non_fast_forwards; |
| 64 | static enum deny_action deny_current_branch = DENY_UNCONFIGURED; |
| 65 | static enum deny_action deny_delete_current = DENY_UNCONFIGURED; |
| 66 | static int receive_fsck_objects = -1; |
| 67 | static int transfer_fsck_objects = -1; |
| 68 | static struct strbuf fsck_msg_types = STRBUF_INIT; |
| 69 | static int advertise_atomic_push = 1; |
| 70 | static int advertise_push_options; |
| 71 | static int advertise_sid; |
| 72 | static off_t max_input_size; |
| 73 | static int use_sideband; |
| 74 | static int use_atomic; |
| 75 | static int use_push_options; |
| 76 | static int quiet; |
| 77 | static int prefer_ofs_delta = 1; |
| 78 | static int auto_update_server_info; |
| 79 | static int auto_gc = 1; |
| 80 | static int reject_thin; |
| 81 | static int skip_connectivity_check; |
| 82 | static int stateless_rpc; |
| 83 | static const char *service_dir; |
| 84 | static const char *head_name; |
| 85 | static void *head_name_to_free; |
| 86 | static int sent_capabilities; |
| 87 | static int shallow_update; |
| 88 | static struct strbuf push_cert = STRBUF_INIT; |
| 89 | static struct object_id push_cert_oid; |
| 90 | static struct signature_check sigcheck; |
| 91 | static const char *push_cert_nonce; |
| 92 | static char *cert_nonce_seed; |
| 93 | static struct strvec hidden_refs = STRVEC_INIT; |
| 94 | |
| 95 | static const char *NONCE_UNSOLICITED = "UNSOLICITED"; |
| 96 | static const char *NONCE_BAD = "BAD"; |
| 97 | static const char *NONCE_MISSING = "MISSING"; |
| 98 | static const char *NONCE_OK = "OK"; |
| 99 | static const char *NONCE_SLOP = "SLOP"; |
| 100 | static const char *nonce_status; |
| 101 | static long nonce_stamp_slop; |
| 102 | static timestamp_t nonce_stamp_slop_limit; |
| 103 | static struct ref_transaction *transaction; |
| 104 | |
| 105 | static enum { |
| 106 | KEEPALIVE_NEVER = 0, |
| 107 | KEEPALIVE_AFTER_NUL, |
| 108 | KEEPALIVE_ALWAYS |
| 109 | } use_keepalive; |
| 110 | static int keepalive_in_sec = 5; |
| 111 | |
| 112 | static struct proc_receive_ref { |
| 113 | unsigned int want_add:1, |
| 114 | want_delete:1, |
| 115 | want_modify:1, |
| 116 | negative_ref:1; |
| 117 | char *ref_prefix; |
| 118 | struct proc_receive_ref *next; |
| 119 | } *proc_receive_ref; |
| 120 | |
| 121 | static void proc_receive_ref_append(const char *prefix); |
| 122 | |
| 123 | static enum deny_action parse_deny_action(const char *var, const char *value) |
| 124 | { |
| 125 | if (value) { |
| 126 | if (!strcasecmp(value, "ignore")) |
| 127 | return DENY_IGNORE; |
| 128 | if (!strcasecmp(value, "warn")) |
| 129 | return DENY_WARN; |
| 130 | if (!strcasecmp(value, "refuse")) |
| 131 | return DENY_REFUSE; |
| 132 | if (!strcasecmp(value, "updateinstead")) |
| 133 | return DENY_UPDATE_INSTEAD; |
| 134 | } |
| 135 | if (git_config_bool(var, value)) |
| 136 | return DENY_REFUSE; |
| 137 | return DENY_IGNORE; |
| 138 | } |
| 139 | |
| 140 | static int receive_pack_config(const char *var, const char *value, |
| 141 | const struct config_context *ctx, void *cb) |
| 142 | { |
| 143 | const char *msg_id; |
| 144 | int status = parse_hide_refs_config(var, value, "receive", &hidden_refs); |
| 145 | |
| 146 | if (status) |
| 147 | return status; |
| 148 | |
| 149 | if (strcmp(var, "receive.denydeletes") == 0) { |
| 150 | deny_deletes = git_config_bool(var, value); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | if (strcmp(var, "receive.denynonfastforwards") == 0) { |
| 155 | deny_non_fast_forwards = git_config_bool(var, value); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | if (strcmp(var, "receive.fsck.skiplist") == 0) { |
| 160 | char *path; |
| 161 | |
| 162 | if (git_config_pathname(&path, var, value)) |
| 163 | return -1; |
| 164 | if (path) |
| 165 | strbuf_addf(&fsck_msg_types, "%cskiplist=%s", |
| 166 | fsck_msg_types.len ? ',' : '=', path); |
| 167 | free(path); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | if (skip_prefix(var, "receive.fsck.", &msg_id)) { |
| 172 | if (!value) |
| 173 | return config_error_nonbool(var); |
| 174 | if (is_valid_msg_type(msg_id, value)) |
| 175 | strbuf_addf(&fsck_msg_types, "%c%s=%s", |
| 176 | fsck_msg_types.len ? ',' : '=', msg_id, value); |
| 177 | else |
| 178 | warning("skipping unknown msg id '%s'", msg_id); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | if (strcmp(var, "receive.fsckobjects") == 0) { |
| 183 | receive_fsck_objects = git_config_bool(var, value); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | if (strcmp(var, "transfer.fsckobjects") == 0) { |
| 188 | transfer_fsck_objects = git_config_bool(var, value); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | if (!strcmp(var, "receive.denycurrentbranch")) { |
| 193 | deny_current_branch = parse_deny_action(var, value); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | if (strcmp(var, "receive.denydeletecurrent") == 0) { |
| 198 | deny_delete_current = parse_deny_action(var, value); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | if (strcmp(var, "repack.usedeltabaseoffset") == 0) { |
| 203 | prefer_ofs_delta = git_config_bool(var, value); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | if (strcmp(var, "receive.updateserverinfo") == 0) { |
| 208 | auto_update_server_info = git_config_bool(var, value); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | if (strcmp(var, "receive.autogc") == 0) { |
| 213 | auto_gc = git_config_bool(var, value); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | if (strcmp(var, "receive.shallowupdate") == 0) { |
| 218 | shallow_update = git_config_bool(var, value); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | if (strcmp(var, "receive.certnonceseed") == 0) |
| 223 | return git_config_string(&cert_nonce_seed, var, value); |
| 224 | |
| 225 | if (strcmp(var, "receive.certnonceslop") == 0) { |
| 226 | nonce_stamp_slop_limit = git_config_ulong(var, value, ctx->kvi); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | if (strcmp(var, "receive.advertiseatomic") == 0) { |
| 231 | advertise_atomic_push = git_config_bool(var, value); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | if (strcmp(var, "receive.advertisepushoptions") == 0) { |
| 236 | advertise_push_options = git_config_bool(var, value); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | if (strcmp(var, "receive.keepalive") == 0) { |
| 241 | keepalive_in_sec = git_config_int(var, value, ctx->kvi); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | if (strcmp(var, "receive.maxinputsize") == 0) { |
| 246 | max_input_size = git_config_int64(var, value, ctx->kvi); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | if (strcmp(var, "receive.procreceiverefs") == 0) { |
| 251 | if (!value) |
| 252 | return config_error_nonbool(var); |
| 253 | proc_receive_ref_append(value); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | if (strcmp(var, "transfer.advertisesid") == 0) { |
| 258 | advertise_sid = git_config_bool(var, value); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | return git_default_config(var, value, ctx, cb); |
| 263 | } |
| 264 | |
| 265 | static void show_ref(const char *path, const struct object_id *oid) |
| 266 | { |
| 267 | if (sent_capabilities) { |
| 268 | packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), path); |
| 269 | } else { |
| 270 | struct strbuf cap = STRBUF_INIT; |
| 271 | |
| 272 | strbuf_addstr(&cap, |
| 273 | "report-status report-status-v2 delete-refs side-band-64k quiet"); |
| 274 | if (advertise_atomic_push) |
| 275 | strbuf_addstr(&cap, " atomic"); |
| 276 | if (prefer_ofs_delta) |
| 277 | strbuf_addstr(&cap, " ofs-delta"); |
| 278 | if (push_cert_nonce) |
| 279 | strbuf_addf(&cap, " push-cert=%s", push_cert_nonce); |
| 280 | if (advertise_push_options) |
| 281 | strbuf_addstr(&cap, " push-options"); |
| 282 | if (advertise_sid) |
| 283 | strbuf_addf(&cap, " session-id=%s", trace2_session_id()); |
| 284 | strbuf_addf(&cap, " object-format=%s", the_hash_algo->name); |
| 285 | strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized()); |
| 286 | packet_write_fmt(1, "%s %s%c%s\n", |
| 287 | oid_to_hex(oid), path, 0, cap.buf); |
| 288 | strbuf_release(&cap); |
| 289 | sent_capabilities = 1; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static int show_ref_cb(const struct reference *ref, void *data) |
| 294 | { |
| 295 | struct oidset *seen = data; |
| 296 | const char *path = strip_namespace(ref->name); |
| 297 | |
| 298 | if (ref_is_hidden(path, ref->name, &hidden_refs)) |
| 299 | return 0; |
| 300 | |
| 301 | /* |
| 302 | * Advertise refs outside our current namespace as ".have" |
| 303 | * refs, so that the client can use them to minimize data |
| 304 | * transfer but will otherwise ignore them. |
| 305 | */ |
| 306 | if (!path) { |
| 307 | if (oidset_insert(seen, ref->oid)) |
| 308 | return 0; |
| 309 | path = ".have"; |
| 310 | } else { |
| 311 | oidset_insert(seen, ref->oid); |
| 312 | } |
| 313 | show_ref(path, ref->oid); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static void show_one_alternate_ref(const struct object_id *oid, |
| 318 | void *data) |
| 319 | { |
| 320 | struct oidset *seen = data; |
| 321 | |
| 322 | if (oidset_insert(seen, oid)) |
| 323 | return; |
| 324 | |
| 325 | show_ref(".have", oid); |
| 326 | } |
| 327 | |
| 328 | static void write_head_info(void) |
| 329 | { |
| 330 | struct refs_for_each_ref_options opts = { 0 }; |
| 331 | static struct oidset seen = OIDSET_INIT; |
| 332 | struct strvec excludes_vector = STRVEC_INIT; |
| 333 | |
| 334 | /* |
| 335 | * We need access to the reference names both with and without their |
| 336 | * namespace and thus cannot use `refs_for_each_namespaced_ref()`. We |
| 337 | * thus have to adapt exclude patterns to carry the namespace prefix |
| 338 | * ourselves. |
| 339 | */ |
| 340 | opts.exclude_patterns = get_namespaced_exclude_patterns( |
| 341 | hidden_refs_to_excludes(&hidden_refs), |
| 342 | get_git_namespace(), &excludes_vector); |
| 343 | |
| 344 | refs_for_each_ref_ext(get_main_ref_store(the_repository), |
| 345 | show_ref_cb, &seen, &opts); |
| 346 | odb_for_each_alternate_ref(the_repository->objects, |
| 347 | show_one_alternate_ref, &seen); |
| 348 | |
| 349 | oidset_clear(&seen); |
| 350 | strvec_clear(&excludes_vector); |
| 351 | |
| 352 | if (!sent_capabilities) |
| 353 | show_ref("capabilities^{}", null_oid(the_hash_algo)); |
| 354 | |
| 355 | advertise_shallow_grafts(1); |
| 356 | |
| 357 | /* EOF */ |
| 358 | packet_flush(1); |
| 359 | } |
| 360 | |
| 361 | #define RUN_PROC_RECEIVE_SCHEDULED 1 |
| 362 | #define RUN_PROC_RECEIVE_RETURNED 2 |
| 363 | struct command { |
| 364 | struct command *next; |
| 365 | const char *error_string; |
| 366 | char *error_string_owned; |
| 367 | struct ref_push_report *report; |
| 368 | unsigned int skip_update:1, |
| 369 | did_not_exist:1, |
| 370 | run_proc_receive:2; |
| 371 | int index; |
| 372 | struct object_id old_oid; |
| 373 | struct object_id new_oid; |
| 374 | char ref_name[FLEX_ARRAY]; /* more */ |
| 375 | }; |
| 376 | |
| 377 | static void proc_receive_ref_append(const char *prefix) |
| 378 | { |
| 379 | struct proc_receive_ref *ref_pattern; |
| 380 | const char *p; |
| 381 | int len; |
| 382 | |
| 383 | CALLOC_ARRAY(ref_pattern, 1); |
| 384 | p = strchr(prefix, ':'); |
| 385 | if (p) { |
| 386 | while (prefix < p) { |
| 387 | if (*prefix == 'a') |
| 388 | ref_pattern->want_add = 1; |
| 389 | else if (*prefix == 'd') |
| 390 | ref_pattern->want_delete = 1; |
| 391 | else if (*prefix == 'm') |
| 392 | ref_pattern->want_modify = 1; |
| 393 | else if (*prefix == '!') |
| 394 | ref_pattern->negative_ref = 1; |
| 395 | prefix++; |
| 396 | } |
| 397 | prefix++; |
| 398 | } else { |
| 399 | ref_pattern->want_add = 1; |
| 400 | ref_pattern->want_delete = 1; |
| 401 | ref_pattern->want_modify = 1; |
| 402 | } |
| 403 | len = strlen(prefix); |
| 404 | while (len && prefix[len - 1] == '/') |
| 405 | len--; |
| 406 | ref_pattern->ref_prefix = xmemdupz(prefix, len); |
| 407 | if (!proc_receive_ref) { |
| 408 | proc_receive_ref = ref_pattern; |
| 409 | } else { |
| 410 | struct proc_receive_ref *end; |
| 411 | |
| 412 | end = proc_receive_ref; |
| 413 | while (end->next) |
| 414 | end = end->next; |
| 415 | end->next = ref_pattern; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static int proc_receive_ref_matches(struct command *cmd) |
| 420 | { |
| 421 | struct proc_receive_ref *p; |
| 422 | |
| 423 | if (!proc_receive_ref) |
| 424 | return 0; |
| 425 | |
| 426 | for (p = proc_receive_ref; p; p = p->next) { |
| 427 | const char *match = p->ref_prefix; |
| 428 | const char *remains; |
| 429 | |
| 430 | if (!p->want_add && is_null_oid(&cmd->old_oid)) |
| 431 | continue; |
| 432 | else if (!p->want_delete && is_null_oid(&cmd->new_oid)) |
| 433 | continue; |
| 434 | else if (!p->want_modify && |
| 435 | !is_null_oid(&cmd->old_oid) && |
| 436 | !is_null_oid(&cmd->new_oid)) |
| 437 | continue; |
| 438 | |
| 439 | if (skip_prefix(cmd->ref_name, match, &remains) && |
| 440 | (!*remains || *remains == '/')) { |
| 441 | if (!p->negative_ref) |
| 442 | return 1; |
| 443 | } else if (p->negative_ref) { |
| 444 | return 1; |
| 445 | } |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static void report_message(const char *prefix, const char *err, va_list params) |
| 451 | { |
| 452 | int sz; |
| 453 | char msg[4096]; |
| 454 | |
| 455 | sz = xsnprintf(msg, sizeof(msg), "%s", prefix); |
| 456 | sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params); |
| 457 | if (sz > (sizeof(msg) - 1)) |
| 458 | sz = sizeof(msg) - 1; |
| 459 | msg[sz++] = '\n'; |
| 460 | |
| 461 | if (use_sideband) |
| 462 | send_sideband(1, 2, msg, sz, use_sideband); |
| 463 | else |
| 464 | xwrite(2, msg, sz); |
| 465 | } |
| 466 | |
| 467 | __attribute__((format (printf, 1, 2))) |
| 468 | static void rp_warning(const char *err, ...) |
| 469 | { |
| 470 | va_list params; |
| 471 | va_start(params, err); |
| 472 | report_message("warning: ", err, params); |
| 473 | va_end(params); |
| 474 | } |
| 475 | |
| 476 | __attribute__((format (printf, 1, 2))) |
| 477 | static void rp_error(const char *err, ...) |
| 478 | { |
| 479 | va_list params; |
| 480 | va_start(params, err); |
| 481 | report_message("error: ", err, params); |
| 482 | va_end(params); |
| 483 | } |
| 484 | |
| 485 | static int copy_to_sideband(int in, int out UNUSED, void *arg UNUSED) |
| 486 | { |
| 487 | char data[128]; |
| 488 | int keepalive_active = 0; |
| 489 | |
| 490 | if (keepalive_in_sec <= 0) |
| 491 | use_keepalive = KEEPALIVE_NEVER; |
| 492 | if (use_keepalive == KEEPALIVE_ALWAYS) |
| 493 | keepalive_active = 1; |
| 494 | |
| 495 | while (1) { |
| 496 | ssize_t sz; |
| 497 | |
| 498 | if (keepalive_active) { |
| 499 | struct pollfd pfd; |
| 500 | int ret; |
| 501 | |
| 502 | pfd.fd = in; |
| 503 | pfd.events = POLLIN; |
| 504 | ret = poll(&pfd, 1, 1000 * keepalive_in_sec); |
| 505 | |
| 506 | if (ret < 0) { |
| 507 | if (errno == EINTR) |
| 508 | continue; |
| 509 | else |
| 510 | break; |
| 511 | } else if (ret == 0) { |
| 512 | /* no data; send a keepalive packet */ |
| 513 | static const char buf[] = "0005\1"; |
| 514 | write_or_die(1, buf, sizeof(buf) - 1); |
| 515 | continue; |
| 516 | } /* else there is actual data to read */ |
| 517 | } |
| 518 | |
| 519 | sz = xread(in, data, sizeof(data)); |
| 520 | if (sz <= 0) |
| 521 | break; |
| 522 | |
| 523 | if (use_keepalive == KEEPALIVE_AFTER_NUL && !keepalive_active) { |
| 524 | const char *p = memchr(data, '\0', sz); |
| 525 | if (p) { |
| 526 | /* |
| 527 | * The NUL tells us to start sending keepalives. Make |
| 528 | * sure we send any other data we read along |
| 529 | * with it. |
| 530 | */ |
| 531 | keepalive_active = 1; |
| 532 | send_sideband(1, 2, data, p - data, use_sideband); |
| 533 | send_sideband(1, 2, p + 1, sz - (p - data + 1), use_sideband); |
| 534 | continue; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Either we're not looking for a NUL signal, or we didn't see |
| 540 | * it yet; just pass along the data. |
| 541 | */ |
| 542 | send_sideband(1, 2, data, sz, use_sideband); |
| 543 | } |
| 544 | close(in); |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Start an async thread which redirects hook stderr over the sideband. |
| 550 | * The original stderr fd is saved to `saved_stderr` and STDERR_FILENO is |
| 551 | * redirected to the async's input pipe. |
| 552 | */ |
| 553 | static void prepare_sideband_async(struct async *sideband_async, int *saved_stderr, int *started) |
| 554 | { |
| 555 | *started = 0; |
| 556 | |
| 557 | if (!use_sideband) |
| 558 | return; |
| 559 | |
| 560 | memset(sideband_async, 0, sizeof(*sideband_async)); |
| 561 | sideband_async->proc = copy_to_sideband; |
| 562 | sideband_async->in = -1; |
| 563 | |
| 564 | if (!start_async(sideband_async)) { |
| 565 | *started = 1; |
| 566 | *saved_stderr = dup(STDERR_FILENO); |
| 567 | if (*saved_stderr >= 0) |
| 568 | dup2(sideband_async->in, STDERR_FILENO); |
| 569 | close(sideband_async->in); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Restore the original stderr and wait for the async sideband thread to finish. |
| 575 | */ |
| 576 | static void finish_sideband_async(struct async *sideband_async, int saved_stderr, int started) |
| 577 | { |
| 578 | if (!use_sideband) |
| 579 | return; |
| 580 | |
| 581 | if (saved_stderr >= 0) { |
| 582 | dup2(saved_stderr, STDERR_FILENO); |
| 583 | close(saved_stderr); |
| 584 | } |
| 585 | |
| 586 | if (started) |
| 587 | finish_async(sideband_async); |
| 588 | } |
| 589 | |
| 590 | static void hmac_hash(unsigned char *out, |
| 591 | const char *key_in, size_t key_len, |
| 592 | const char *text, size_t text_len) |
| 593 | { |
| 594 | unsigned char key[GIT_MAX_BLKSZ]; |
| 595 | unsigned char k_ipad[GIT_MAX_BLKSZ]; |
| 596 | unsigned char k_opad[GIT_MAX_BLKSZ]; |
| 597 | int i; |
| 598 | struct git_hash_ctx ctx; |
| 599 | |
| 600 | /* RFC 2104 2. (1) */ |
| 601 | memset(key, '\0', GIT_MAX_BLKSZ); |
| 602 | if (the_hash_algo->blksz < key_len) { |
| 603 | git_hash_init(&ctx, the_hash_algo); |
| 604 | git_hash_update(&ctx, key_in, key_len); |
| 605 | git_hash_final(key, &ctx); |
| 606 | } else { |
| 607 | memcpy(key, key_in, key_len); |
| 608 | } |
| 609 | |
| 610 | /* RFC 2104 2. (2) & (5) */ |
| 611 | for (i = 0; i < sizeof(key); i++) { |
| 612 | k_ipad[i] = key[i] ^ 0x36; |
| 613 | k_opad[i] = key[i] ^ 0x5c; |
| 614 | } |
| 615 | |
| 616 | /* RFC 2104 2. (3) & (4) */ |
| 617 | git_hash_init(&ctx, the_hash_algo); |
| 618 | git_hash_update(&ctx, k_ipad, sizeof(k_ipad)); |
| 619 | git_hash_update(&ctx, text, text_len); |
| 620 | git_hash_final(out, &ctx); |
| 621 | |
| 622 | /* RFC 2104 2. (6) & (7) */ |
| 623 | git_hash_init(&ctx, the_hash_algo); |
| 624 | git_hash_update(&ctx, k_opad, sizeof(k_opad)); |
| 625 | git_hash_update(&ctx, out, the_hash_algo->rawsz); |
| 626 | git_hash_final(out, &ctx); |
| 627 | } |
| 628 | |
| 629 | static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp) |
| 630 | { |
| 631 | struct strbuf buf = STRBUF_INIT; |
| 632 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 633 | |
| 634 | strbuf_addf(&buf, "%s:%"PRItime, path, stamp); |
| 635 | hmac_hash(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed)); |
| 636 | strbuf_release(&buf); |
| 637 | |
| 638 | /* RFC 2104 5. HMAC-SHA1 or HMAC-SHA256 */ |
| 639 | strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, hash_to_hex(hash)); |
| 640 | return strbuf_detach(&buf, NULL); |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Return zero if a and b are equal up to n bytes and nonzero if they are not. |
| 645 | * This operation is guaranteed to run in constant time to avoid leaking data. |
| 646 | */ |
| 647 | static int constant_memequal(const char *a, const char *b, size_t n) |
| 648 | { |
| 649 | int res = 0; |
| 650 | size_t i; |
| 651 | |
| 652 | for (i = 0; i < n; i++) |
| 653 | res |= a[i] ^ b[i]; |
| 654 | return res; |
| 655 | } |
| 656 | |
| 657 | static const char *check_nonce(const char *buf) |
| 658 | { |
| 659 | size_t noncelen; |
| 660 | const char *found = find_commit_header(buf, "nonce", &noncelen); |
| 661 | char *nonce = found ? xmemdupz(found, noncelen) : NULL; |
| 662 | timestamp_t stamp, ostamp; |
| 663 | char *bohmac, *expect = NULL; |
| 664 | const char *retval = NONCE_BAD; |
| 665 | |
| 666 | if (!nonce) { |
| 667 | retval = NONCE_MISSING; |
| 668 | goto leave; |
| 669 | } else if (!push_cert_nonce) { |
| 670 | retval = NONCE_UNSOLICITED; |
| 671 | goto leave; |
| 672 | } else if (!strcmp(push_cert_nonce, nonce)) { |
| 673 | retval = NONCE_OK; |
| 674 | goto leave; |
| 675 | } |
| 676 | |
| 677 | if (!stateless_rpc) { |
| 678 | /* returned nonce MUST match what we gave out earlier */ |
| 679 | retval = NONCE_BAD; |
| 680 | goto leave; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * In stateless mode, we may be receiving a nonce issued by |
| 685 | * another instance of the server that serving the same |
| 686 | * repository, and the timestamps may not match, but the |
| 687 | * nonce-seed and dir should match, so we can recompute and |
| 688 | * report the time slop. |
| 689 | * |
| 690 | * In addition, when a nonce issued by another instance has |
| 691 | * timestamp within receive.certnonceslop seconds, we pretend |
| 692 | * as if we issued that nonce when reporting to the hook. |
| 693 | */ |
| 694 | |
| 695 | /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */ |
| 696 | if (*nonce <= '0' || '9' < *nonce) { |
| 697 | retval = NONCE_BAD; |
| 698 | goto leave; |
| 699 | } |
| 700 | stamp = parse_timestamp(nonce, &bohmac, 10); |
| 701 | if (bohmac == nonce || bohmac[0] != '-') { |
| 702 | retval = NONCE_BAD; |
| 703 | goto leave; |
| 704 | } |
| 705 | |
| 706 | expect = prepare_push_cert_nonce(service_dir, stamp); |
| 707 | if (noncelen != strlen(expect)) { |
| 708 | /* This is not even the right size. */ |
| 709 | retval = NONCE_BAD; |
| 710 | goto leave; |
| 711 | } |
| 712 | if (constant_memequal(expect, nonce, noncelen)) { |
| 713 | /* Not what we would have signed earlier */ |
| 714 | retval = NONCE_BAD; |
| 715 | goto leave; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * By how many seconds is this nonce stale? Negative value |
| 720 | * would mean it was issued by another server with its clock |
| 721 | * skewed in the future. |
| 722 | */ |
| 723 | ostamp = parse_timestamp(push_cert_nonce, NULL, 10); |
| 724 | nonce_stamp_slop = (long)ostamp - (long)stamp; |
| 725 | |
| 726 | if (nonce_stamp_slop_limit && |
| 727 | labs(nonce_stamp_slop) <= nonce_stamp_slop_limit) { |
| 728 | /* |
| 729 | * Pretend as if the received nonce (which passes the |
| 730 | * HMAC check, so it is not a forged by third-party) |
| 731 | * is what we issued. |
| 732 | */ |
| 733 | free((void *)push_cert_nonce); |
| 734 | push_cert_nonce = xstrdup(nonce); |
| 735 | retval = NONCE_OK; |
| 736 | } else { |
| 737 | retval = NONCE_SLOP; |
| 738 | } |
| 739 | |
| 740 | leave: |
| 741 | free(nonce); |
| 742 | free(expect); |
| 743 | return retval; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Return 1 if there is no push_cert or if the push options in push_cert are |
| 748 | * the same as those in the argument; 0 otherwise. |
| 749 | */ |
| 750 | static int check_cert_push_options(const struct string_list *push_options) |
| 751 | { |
| 752 | const char *buf = push_cert.buf; |
| 753 | |
| 754 | const char *option; |
| 755 | size_t optionlen; |
| 756 | int options_seen = 0; |
| 757 | |
| 758 | int retval = 1; |
| 759 | |
| 760 | if (!*buf) |
| 761 | return 1; |
| 762 | |
| 763 | while ((option = find_commit_header(buf, "push-option", &optionlen))) { |
| 764 | buf = option + optionlen + 1; |
| 765 | options_seen++; |
| 766 | if (options_seen > push_options->nr |
| 767 | || xstrncmpz(push_options->items[options_seen - 1].string, |
| 768 | option, optionlen)) |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | if (options_seen != push_options->nr) |
| 773 | retval = 0; |
| 774 | |
| 775 | return retval; |
| 776 | } |
| 777 | |
| 778 | static void prepare_push_cert_sha1(struct run_hooks_opt *opt) |
| 779 | { |
| 780 | static int already_done; |
| 781 | |
| 782 | if (!push_cert.len) |
| 783 | return; |
| 784 | |
| 785 | if (!already_done) { |
| 786 | int bogs /* beginning_of_gpg_sig */; |
| 787 | |
| 788 | already_done = 1; |
| 789 | if (odb_write_object(the_repository->objects, push_cert.buf, |
| 790 | push_cert.len, OBJ_BLOB, &push_cert_oid)) |
| 791 | oidclr(&push_cert_oid, the_repository->hash_algo); |
| 792 | |
| 793 | memset(&sigcheck, '\0', sizeof(sigcheck)); |
| 794 | |
| 795 | bogs = parse_signed_buffer(push_cert.buf, push_cert.len); |
| 796 | sigcheck.payload = xmemdupz(push_cert.buf, bogs); |
| 797 | sigcheck.payload_len = bogs; |
| 798 | check_signature(&sigcheck, push_cert.buf + bogs, |
| 799 | push_cert.len - bogs); |
| 800 | |
| 801 | nonce_status = check_nonce(sigcheck.payload); |
| 802 | } |
| 803 | if (!is_null_oid(&push_cert_oid)) { |
| 804 | strvec_pushf(&opt->env, "GIT_PUSH_CERT=%s", |
| 805 | oid_to_hex(&push_cert_oid)); |
| 806 | strvec_pushf(&opt->env, "GIT_PUSH_CERT_SIGNER=%s", |
| 807 | sigcheck.signer ? sigcheck.signer : ""); |
| 808 | strvec_pushf(&opt->env, "GIT_PUSH_CERT_KEY=%s", |
| 809 | sigcheck.key ? sigcheck.key : ""); |
| 810 | strvec_pushf(&opt->env, "GIT_PUSH_CERT_STATUS=%c", |
| 811 | sigcheck.result); |
| 812 | if (push_cert_nonce) { |
| 813 | strvec_pushf(&opt->env, |
| 814 | "GIT_PUSH_CERT_NONCE=%s", |
| 815 | push_cert_nonce); |
| 816 | strvec_pushf(&opt->env, |
| 817 | "GIT_PUSH_CERT_NONCE_STATUS=%s", |
| 818 | nonce_status); |
| 819 | if (nonce_status == NONCE_SLOP) |
| 820 | strvec_pushf(&opt->env, |
| 821 | "GIT_PUSH_CERT_NONCE_SLOP=%ld", |
| 822 | nonce_stamp_slop); |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | struct receive_hook_feed_state { |
| 828 | struct command *cmd; |
| 829 | struct ref_push_report *report; |
| 830 | int skip_broken; |
| 831 | struct strbuf buf; |
| 832 | }; |
| 833 | |
| 834 | static int feed_receive_hook_cb(int hook_stdin_fd, void *pp_cb UNUSED, void *pp_task_cb) |
| 835 | { |
| 836 | struct receive_hook_feed_state *state = pp_task_cb; |
| 837 | struct command *cmd = state->cmd; |
| 838 | |
| 839 | strbuf_reset(&state->buf); |
| 840 | |
| 841 | while (cmd && |
| 842 | state->skip_broken && (cmd->error_string || cmd->did_not_exist)) |
| 843 | cmd = cmd->next; |
| 844 | |
| 845 | if (!cmd) |
| 846 | return 1; /* no more commands left */ |
| 847 | |
| 848 | if (!state->report) |
| 849 | state->report = cmd->report; |
| 850 | |
| 851 | if (state->report) { |
| 852 | struct object_id *old_oid; |
| 853 | struct object_id *new_oid; |
| 854 | const char *ref_name; |
| 855 | |
| 856 | old_oid = state->report->old_oid ? state->report->old_oid : &cmd->old_oid; |
| 857 | new_oid = state->report->new_oid ? state->report->new_oid : &cmd->new_oid; |
| 858 | ref_name = state->report->ref_name ? state->report->ref_name : cmd->ref_name; |
| 859 | |
| 860 | strbuf_addf(&state->buf, "%s %s %s\n", |
| 861 | oid_to_hex(old_oid), oid_to_hex(new_oid), |
| 862 | ref_name); |
| 863 | |
| 864 | state->report = state->report->next; |
| 865 | if (!state->report) |
| 866 | cmd = cmd->next; |
| 867 | } else { |
| 868 | strbuf_addf(&state->buf, "%s %s %s\n", |
| 869 | oid_to_hex(&cmd->old_oid), oid_to_hex(&cmd->new_oid), |
| 870 | cmd->ref_name); |
| 871 | cmd = cmd->next; |
| 872 | } |
| 873 | |
| 874 | state->cmd = cmd; |
| 875 | |
| 876 | if (state->buf.len > 0) { |
| 877 | int ret = write_in_full(hook_stdin_fd, state->buf.buf, state->buf.len); |
| 878 | if (ret < 0) { |
| 879 | if (errno == EPIPE) |
| 880 | return 1; /* child closed pipe */ |
| 881 | return ret; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return state->cmd ? 0 : 1; /* 0 = more to come, 1 = EOF */ |
| 886 | } |
| 887 | |
| 888 | static void *receive_hook_feed_state_alloc(void *feed_pipe_ctx) |
| 889 | { |
| 890 | struct receive_hook_feed_state *init_state = feed_pipe_ctx; |
| 891 | struct receive_hook_feed_state *data; |
| 892 | |
| 893 | CALLOC_ARRAY(data, 1); |
| 894 | data->report = init_state->report; |
| 895 | data->cmd = init_state->cmd; |
| 896 | data->skip_broken = init_state->skip_broken; |
| 897 | strbuf_init(&data->buf, 0); |
| 898 | |
| 899 | return data; |
| 900 | } |
| 901 | |
| 902 | static void receive_hook_feed_state_free(void *data) |
| 903 | { |
| 904 | struct receive_hook_feed_state *d = data; |
| 905 | if (!d) |
| 906 | return; |
| 907 | strbuf_release(&d->buf); |
| 908 | free(d); |
| 909 | } |
| 910 | |
| 911 | static int run_receive_hook(struct command *commands, |
| 912 | const char *hook_name, |
| 913 | int skip_broken, |
| 914 | struct odb_transaction *transaction, |
| 915 | const struct string_list *push_options) |
| 916 | { |
| 917 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; |
| 918 | struct command *iter = commands; |
| 919 | struct receive_hook_feed_state feed_init_state = { |
| 920 | .cmd = commands, |
| 921 | .skip_broken = skip_broken, |
| 922 | .buf = STRBUF_INIT, |
| 923 | }; |
| 924 | struct async sideband_async; |
| 925 | int sideband_async_started = 0; |
| 926 | int saved_stderr = -1; |
| 927 | int ret; |
| 928 | |
| 929 | if (!hook_exists(the_repository, hook_name)) |
| 930 | return 0; |
| 931 | |
| 932 | /* if there are no valid commands, don't invoke the hook at all. */ |
| 933 | while (iter && skip_broken && (iter->error_string || iter->did_not_exist)) |
| 934 | iter = iter->next; |
| 935 | if (!iter) |
| 936 | return 0; |
| 937 | |
| 938 | if (push_options) { |
| 939 | for (int i = 0; i < push_options->nr; i++) |
| 940 | strvec_pushf(&opt.env, "GIT_PUSH_OPTION_%d=%s", i, |
| 941 | push_options->items[i].string); |
| 942 | strvec_pushf(&opt.env, "GIT_PUSH_OPTION_COUNT=%"PRIuMAX"", |
| 943 | (uintmax_t)push_options->nr); |
| 944 | } else { |
| 945 | strvec_push(&opt.env, "GIT_PUSH_OPTION_COUNT"); |
| 946 | } |
| 947 | |
| 948 | if (transaction) |
| 949 | odb_transaction_env(transaction, &opt.env); |
| 950 | |
| 951 | prepare_push_cert_sha1(&opt); |
| 952 | |
| 953 | prepare_sideband_async(&sideband_async, &saved_stderr, &sideband_async_started); |
| 954 | |
| 955 | /* set up stdin callback */ |
| 956 | opt.feed_pipe_ctx = &feed_init_state; |
| 957 | opt.feed_pipe = feed_receive_hook_cb; |
| 958 | opt.feed_pipe_cb_data_alloc = receive_hook_feed_state_alloc; |
| 959 | opt.feed_pipe_cb_data_free = receive_hook_feed_state_free; |
| 960 | |
| 961 | ret = run_hooks_opt(the_repository, hook_name, &opt); |
| 962 | |
| 963 | finish_sideband_async(&sideband_async, saved_stderr, sideband_async_started); |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | static int run_update_hook(struct command *cmd) |
| 969 | { |
| 970 | static const char hook_name[] = "update"; |
| 971 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; |
| 972 | struct async sideband_async; |
| 973 | int sideband_async_started = 0; |
| 974 | int saved_stderr = -1; |
| 975 | int code; |
| 976 | |
| 977 | if (!hook_exists(the_repository, hook_name)) |
| 978 | return 0; |
| 979 | |
| 980 | strvec_pushl(&opt.args, |
| 981 | cmd->ref_name, |
| 982 | oid_to_hex(&cmd->old_oid), |
| 983 | oid_to_hex(&cmd->new_oid), |
| 984 | NULL); |
| 985 | |
| 986 | prepare_sideband_async(&sideband_async, &saved_stderr, &sideband_async_started); |
| 987 | |
| 988 | code = run_hooks_opt(the_repository, hook_name, &opt); |
| 989 | |
| 990 | finish_sideband_async(&sideband_async, saved_stderr, sideband_async_started); |
| 991 | |
| 992 | return code; |
| 993 | } |
| 994 | |
| 995 | static int run_receive_report_hook(struct strbuf *report) |
| 996 | { |
| 997 | struct child_process proc = CHILD_PROCESS_INIT; |
| 998 | struct async sideband_async; |
| 999 | int sideband_async_started = 0; |
| 1000 | int saved_stderr = -1; |
| 1001 | struct strbuf out = STRBUF_INIT; |
| 1002 | const char *hook_path; |
| 1003 | int ret; |
| 1004 | |
| 1005 | hook_path = find_hook(the_repository, "receive-report"); |
| 1006 | if (!hook_path) |
| 1007 | return 0; |
| 1008 | |
| 1009 | strvec_push(&proc.args, hook_path); |
| 1010 | proc.trace2_hook_name = "receive-report"; |
| 1011 | |
| 1012 | prepare_sideband_async(&sideband_async, &saved_stderr, |
| 1013 | &sideband_async_started); |
| 1014 | |
| 1015 | sigchain_push(SIGPIPE, SIG_IGN); |
| 1016 | ret = pipe_command(&proc, report->buf, report->len, &out, |
| 1017 | report->len, NULL, 0); |
| 1018 | sigchain_pop(SIGPIPE); |
| 1019 | |
| 1020 | finish_sideband_async(&sideband_async, saved_stderr, |
| 1021 | sideband_async_started); |
| 1022 | |
| 1023 | if (!ret) |
| 1024 | strbuf_swap(&out, report); |
| 1025 | |
| 1026 | strbuf_release(&out); |
| 1027 | return ret; |
| 1028 | } |
| 1029 | |
| 1030 | static struct command *find_command_by_refname(struct command *list, |
| 1031 | const char *refname) |
| 1032 | { |
| 1033 | for (; list; list = list->next) |
| 1034 | if (!strcmp(list->ref_name, refname)) |
| 1035 | return list; |
| 1036 | return NULL; |
| 1037 | } |
| 1038 | |
| 1039 | static int read_proc_receive_report(struct packet_reader *reader, |
| 1040 | struct command *commands, |
| 1041 | struct strbuf *errmsg) |
| 1042 | { |
| 1043 | struct command *cmd; |
| 1044 | struct command *hint = NULL; |
| 1045 | struct ref_push_report *report = NULL; |
| 1046 | int new_report = 0; |
| 1047 | int code = 0; |
| 1048 | int once = 0; |
| 1049 | int response = 0; |
| 1050 | |
| 1051 | for (;;) { |
| 1052 | struct object_id old_oid, new_oid; |
| 1053 | char *head; |
| 1054 | char *refname; |
| 1055 | char *p; |
| 1056 | enum packet_read_status status; |
| 1057 | |
| 1058 | status = packet_reader_read(reader); |
| 1059 | if (status != PACKET_READ_NORMAL) { |
| 1060 | /* Check whether proc-receive exited abnormally */ |
| 1061 | if (status == PACKET_READ_EOF && !response) { |
| 1062 | strbuf_addstr(errmsg, "proc-receive exited abnormally"); |
| 1063 | return -1; |
| 1064 | } |
| 1065 | break; |
| 1066 | } |
| 1067 | response++; |
| 1068 | |
| 1069 | head = reader->line; |
| 1070 | p = strchr(head, ' '); |
| 1071 | if (!p) { |
| 1072 | strbuf_addf(errmsg, "proc-receive reported incomplete status line: '%s'\n", head); |
| 1073 | code = -1; |
| 1074 | continue; |
| 1075 | } |
| 1076 | *p++ = '\0'; |
| 1077 | if (!strcmp(head, "option")) { |
| 1078 | char *key; |
| 1079 | const char *val; |
| 1080 | |
| 1081 | if (!hint || !(report || new_report)) { |
| 1082 | if (!once++) |
| 1083 | strbuf_addstr(errmsg, "proc-receive reported 'option' without a matching 'ok/ng' directive\n"); |
| 1084 | code = -1; |
| 1085 | continue; |
| 1086 | } |
| 1087 | if (new_report) { |
| 1088 | if (!hint->report) { |
| 1089 | CALLOC_ARRAY(hint->report, 1); |
| 1090 | report = hint->report; |
| 1091 | } else { |
| 1092 | report = hint->report; |
| 1093 | while (report->next) |
| 1094 | report = report->next; |
| 1095 | report->next = xcalloc(1, sizeof(struct ref_push_report)); |
| 1096 | report = report->next; |
| 1097 | } |
| 1098 | new_report = 0; |
| 1099 | } |
| 1100 | key = p; |
| 1101 | p = strchr(key, ' '); |
| 1102 | if (p) |
| 1103 | *p++ = '\0'; |
| 1104 | val = p; |
| 1105 | if (!strcmp(key, "refname")) |
| 1106 | report->ref_name = xstrdup_or_null(val); |
| 1107 | else if (!strcmp(key, "old-oid") && val && |
| 1108 | !parse_oid_hex(val, &old_oid, &val)) |
| 1109 | report->old_oid = oiddup(&old_oid); |
| 1110 | else if (!strcmp(key, "new-oid") && val && |
| 1111 | !parse_oid_hex(val, &new_oid, &val)) |
| 1112 | report->new_oid = oiddup(&new_oid); |
| 1113 | else if (!strcmp(key, "forced-update")) |
| 1114 | report->forced_update = 1; |
| 1115 | else if (!strcmp(key, "fall-through")) |
| 1116 | /* Fall through, let 'receive-pack' to execute it. */ |
| 1117 | hint->run_proc_receive = 0; |
| 1118 | continue; |
| 1119 | } |
| 1120 | |
| 1121 | report = NULL; |
| 1122 | new_report = 0; |
| 1123 | refname = p; |
| 1124 | p = strchr(refname, ' '); |
| 1125 | if (p) |
| 1126 | *p++ = '\0'; |
| 1127 | if (strcmp(head, "ok") && strcmp(head, "ng")) { |
| 1128 | strbuf_addf(errmsg, "proc-receive reported bad status '%s' on ref '%s'\n", |
| 1129 | head, refname); |
| 1130 | code = -1; |
| 1131 | continue; |
| 1132 | } |
| 1133 | |
| 1134 | /* first try searching at our hint, falling back to all refs */ |
| 1135 | if (hint) |
| 1136 | hint = find_command_by_refname(hint, refname); |
| 1137 | if (!hint) |
| 1138 | hint = find_command_by_refname(commands, refname); |
| 1139 | if (!hint) { |
| 1140 | strbuf_addf(errmsg, "proc-receive reported status on unknown ref: %s\n", |
| 1141 | refname); |
| 1142 | code = -1; |
| 1143 | continue; |
| 1144 | } |
| 1145 | if (!hint->run_proc_receive) { |
| 1146 | strbuf_addf(errmsg, "proc-receive reported status on unexpected ref: %s\n", |
| 1147 | refname); |
| 1148 | code = -1; |
| 1149 | continue; |
| 1150 | } |
| 1151 | hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED; |
| 1152 | if (!strcmp(head, "ng")) { |
| 1153 | if (p) |
| 1154 | hint->error_string = hint->error_string_owned = xstrdup(p); |
| 1155 | else |
| 1156 | hint->error_string = "failed"; |
| 1157 | code = -1; |
| 1158 | continue; |
| 1159 | } |
| 1160 | new_report = 1; |
| 1161 | } |
| 1162 | |
| 1163 | for (cmd = commands; cmd; cmd = cmd->next) |
| 1164 | if (cmd->run_proc_receive && !cmd->error_string && |
| 1165 | !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) { |
| 1166 | cmd->error_string = "proc-receive failed to report status"; |
| 1167 | code = -1; |
| 1168 | } |
| 1169 | return code; |
| 1170 | } |
| 1171 | |
| 1172 | static int run_proc_receive_hook(struct command *commands, |
| 1173 | const struct string_list *push_options) |
| 1174 | { |
| 1175 | struct child_process proc = CHILD_PROCESS_INIT; |
| 1176 | struct async muxer; |
| 1177 | struct command *cmd; |
| 1178 | struct packet_reader reader; |
| 1179 | struct strbuf cap = STRBUF_INIT; |
| 1180 | struct strbuf errmsg = STRBUF_INIT; |
| 1181 | int hook_use_push_options = 0; |
| 1182 | int version = 0; |
| 1183 | int code; |
| 1184 | const char *hook_path = find_hook(the_repository, "proc-receive"); |
| 1185 | |
| 1186 | if (!hook_path) { |
| 1187 | rp_error("cannot find hook 'proc-receive'"); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
| 1191 | strvec_push(&proc.args, hook_path); |
| 1192 | proc.in = -1; |
| 1193 | proc.out = -1; |
| 1194 | proc.trace2_hook_name = "proc-receive"; |
| 1195 | |
| 1196 | if (use_sideband) { |
| 1197 | memset(&muxer, 0, sizeof(muxer)); |
| 1198 | muxer.proc = copy_to_sideband; |
| 1199 | muxer.in = -1; |
| 1200 | code = start_async(&muxer); |
| 1201 | if (code) |
| 1202 | return code; |
| 1203 | proc.err = muxer.in; |
| 1204 | } else { |
| 1205 | proc.err = 0; |
| 1206 | } |
| 1207 | |
| 1208 | code = start_command(&proc); |
| 1209 | if (code) { |
| 1210 | if (use_sideband) |
| 1211 | finish_async(&muxer); |
| 1212 | return code; |
| 1213 | } |
| 1214 | |
| 1215 | sigchain_push(SIGPIPE, SIG_IGN); |
| 1216 | |
| 1217 | /* Version negotiaton */ |
| 1218 | packet_reader_init(&reader, proc.out, NULL, 0, |
| 1219 | PACKET_READ_CHOMP_NEWLINE | |
| 1220 | PACKET_READ_GENTLE_ON_EOF); |
| 1221 | if (use_atomic) |
| 1222 | strbuf_addstr(&cap, " atomic"); |
| 1223 | if (use_push_options) |
| 1224 | strbuf_addstr(&cap, " push-options"); |
| 1225 | if (cap.len) { |
| 1226 | code = packet_write_fmt_gently(proc.in, "version=1%c%s\n", '\0', cap.buf + 1); |
| 1227 | strbuf_release(&cap); |
| 1228 | } else { |
| 1229 | code = packet_write_fmt_gently(proc.in, "version=1\n"); |
| 1230 | } |
| 1231 | if (!code) |
| 1232 | code = packet_flush_gently(proc.in); |
| 1233 | |
| 1234 | if (!code) |
| 1235 | for (;;) { |
| 1236 | int linelen; |
| 1237 | enum packet_read_status status; |
| 1238 | |
| 1239 | status = packet_reader_read(&reader); |
| 1240 | if (status != PACKET_READ_NORMAL) { |
| 1241 | /* Check whether proc-receive exited abnormally */ |
| 1242 | if (status == PACKET_READ_EOF) |
| 1243 | code = -1; |
| 1244 | break; |
| 1245 | } |
| 1246 | |
| 1247 | if (reader.pktlen > 8 && starts_with(reader.line, "version=")) { |
| 1248 | version = atoi(reader.line + 8); |
| 1249 | linelen = strlen(reader.line); |
| 1250 | if (linelen < reader.pktlen) { |
| 1251 | const char *feature_list = reader.line + linelen + 1; |
| 1252 | if (parse_feature_request(feature_list, "push-options")) |
| 1253 | hook_use_push_options = 1; |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | if (code) { |
| 1259 | strbuf_addstr(&errmsg, "fail to negotiate version with proc-receive hook"); |
| 1260 | goto cleanup; |
| 1261 | } |
| 1262 | |
| 1263 | switch (version) { |
| 1264 | case 0: |
| 1265 | /* fallthrough */ |
| 1266 | case 1: |
| 1267 | break; |
| 1268 | default: |
| 1269 | strbuf_addf(&errmsg, "proc-receive version '%d' is not supported", |
| 1270 | version); |
| 1271 | code = -1; |
| 1272 | goto cleanup; |
| 1273 | } |
| 1274 | |
| 1275 | /* Send commands */ |
| 1276 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1277 | if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string) |
| 1278 | continue; |
| 1279 | code = packet_write_fmt_gently(proc.in, "%s %s %s", |
| 1280 | oid_to_hex(&cmd->old_oid), |
| 1281 | oid_to_hex(&cmd->new_oid), |
| 1282 | cmd->ref_name); |
| 1283 | if (code) |
| 1284 | break; |
| 1285 | } |
| 1286 | if (!code) |
| 1287 | code = packet_flush_gently(proc.in); |
| 1288 | if (code) { |
| 1289 | strbuf_addstr(&errmsg, "fail to write commands to proc-receive hook"); |
| 1290 | goto cleanup; |
| 1291 | } |
| 1292 | |
| 1293 | /* Send push options */ |
| 1294 | if (hook_use_push_options) { |
| 1295 | struct string_list_item *item; |
| 1296 | |
| 1297 | for_each_string_list_item(item, push_options) { |
| 1298 | code = packet_write_fmt_gently(proc.in, "%s", item->string); |
| 1299 | if (code) |
| 1300 | break; |
| 1301 | } |
| 1302 | if (!code) |
| 1303 | code = packet_flush_gently(proc.in); |
| 1304 | if (code) { |
| 1305 | strbuf_addstr(&errmsg, |
| 1306 | "fail to write push-options to proc-receive hook"); |
| 1307 | goto cleanup; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /* Read result from proc-receive */ |
| 1312 | code = read_proc_receive_report(&reader, commands, &errmsg); |
| 1313 | |
| 1314 | cleanup: |
| 1315 | close(proc.in); |
| 1316 | close(proc.out); |
| 1317 | if (use_sideband) |
| 1318 | finish_async(&muxer); |
| 1319 | if (finish_command(&proc)) |
| 1320 | code = -1; |
| 1321 | if (errmsg.len >0) { |
| 1322 | char *p = errmsg.buf; |
| 1323 | |
| 1324 | p += errmsg.len - 1; |
| 1325 | if (*p == '\n') |
| 1326 | *p = '\0'; |
| 1327 | rp_error("%s", errmsg.buf); |
| 1328 | strbuf_release(&errmsg); |
| 1329 | } |
| 1330 | sigchain_pop(SIGPIPE); |
| 1331 | |
| 1332 | return code; |
| 1333 | } |
| 1334 | |
| 1335 | static const char *refuse_unconfigured_deny_msg = |
| 1336 | N_("By default, updating the current branch in a non-bare repository\n" |
| 1337 | "is denied, because it will make the index and work tree inconsistent\n" |
| 1338 | "with what you pushed, and will require 'git reset --hard' to match\n" |
| 1339 | "the work tree to HEAD.\n" |
| 1340 | "\n" |
| 1341 | "You can set the 'receive.denyCurrentBranch' configuration variable\n" |
| 1342 | "to 'ignore' or 'warn' in the remote repository to allow pushing into\n" |
| 1343 | "its current branch; however, this is not recommended unless you\n" |
| 1344 | "arranged to update its work tree to match what you pushed in some\n" |
| 1345 | "other way.\n" |
| 1346 | "\n" |
| 1347 | "To squelch this message and still keep the default behaviour, set\n" |
| 1348 | "'receive.denyCurrentBranch' configuration variable to 'refuse'."); |
| 1349 | |
| 1350 | static void refuse_unconfigured_deny(void) |
| 1351 | { |
| 1352 | rp_error("%s", _(refuse_unconfigured_deny_msg)); |
| 1353 | } |
| 1354 | |
| 1355 | static const char *refuse_unconfigured_deny_delete_current_msg = |
| 1356 | N_("By default, deleting the current branch is denied, because the next\n" |
| 1357 | "'git clone' won't result in any file checked out, causing confusion.\n" |
| 1358 | "\n" |
| 1359 | "You can set 'receive.denyDeleteCurrent' configuration variable to\n" |
| 1360 | "'warn' or 'ignore' in the remote repository to allow deleting the\n" |
| 1361 | "current branch, with or without a warning message.\n" |
| 1362 | "\n" |
| 1363 | "To squelch this message, you can set it to 'refuse'."); |
| 1364 | |
| 1365 | static void refuse_unconfigured_deny_delete_current(void) |
| 1366 | { |
| 1367 | rp_error("%s", _(refuse_unconfigured_deny_delete_current_msg)); |
| 1368 | } |
| 1369 | |
| 1370 | static const struct object_id *command_singleton_iterator(void *cb_data); |
| 1371 | static int update_shallow_ref(struct command *cmd, struct shallow_info *si) |
| 1372 | { |
| 1373 | struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT; |
| 1374 | struct oid_array extra = OID_ARRAY_INIT; |
| 1375 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
| 1376 | uint32_t mask = 1 << (cmd->index % 32); |
| 1377 | int i; |
| 1378 | |
| 1379 | trace_printf_key(&trace_shallow, |
| 1380 | "shallow: update_shallow_ref %s\n", cmd->ref_name); |
| 1381 | for (i = 0; i < si->shallow->nr; i++) |
| 1382 | if (si->used_shallow[i] && |
| 1383 | (si->used_shallow[i][cmd->index / 32] & mask) && |
| 1384 | !delayed_reachability_test(si, i)) |
| 1385 | oid_array_append(&extra, &si->shallow->oid[i]); |
| 1386 | |
| 1387 | setup_alternate_shallow(&shallow_lock, &opt.shallow_file, &extra); |
| 1388 | if (check_connected(the_repository, command_singleton_iterator, |
| 1389 | cmd, &opt)) { |
| 1390 | rollback_shallow_file(the_repository, &shallow_lock); |
| 1391 | oid_array_clear(&extra); |
| 1392 | return -1; |
| 1393 | } |
| 1394 | |
| 1395 | commit_shallow_file(the_repository, &shallow_lock); |
| 1396 | |
| 1397 | /* |
| 1398 | * Make sure setup_alternate_shallow() for the next ref does |
| 1399 | * not lose these new roots.. |
| 1400 | */ |
| 1401 | for (i = 0; i < extra.nr; i++) |
| 1402 | register_shallow(the_repository, &extra.oid[i]); |
| 1403 | |
| 1404 | si->shallow_ref[cmd->index] = 0; |
| 1405 | oid_array_clear(&extra); |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | static const char *push_to_deploy(unsigned char *sha1, |
| 1410 | struct strvec *env, |
| 1411 | const struct worktree *worktree) |
| 1412 | { |
| 1413 | struct child_process child = CHILD_PROCESS_INIT; |
| 1414 | |
| 1415 | strvec_pushl(&child.args, "update-index", "-q", "--ignore-submodules", |
| 1416 | "--refresh", NULL); |
| 1417 | strvec_pushv(&child.env, env->v); |
| 1418 | child.dir = worktree->path; |
| 1419 | child.no_stdin = 1; |
| 1420 | child.stdout_to_stderr = 1; |
| 1421 | child.git_cmd = 1; |
| 1422 | if (run_command(&child)) |
| 1423 | return "Up-to-date check failed"; |
| 1424 | |
| 1425 | /* run_command() does not clean up completely; reinitialize */ |
| 1426 | child_process_init(&child); |
| 1427 | strvec_pushl(&child.args, "diff-files", "--quiet", |
| 1428 | "--ignore-submodules", "--", NULL); |
| 1429 | strvec_pushv(&child.env, env->v); |
| 1430 | child.dir = worktree->path; |
| 1431 | child.no_stdin = 1; |
| 1432 | child.stdout_to_stderr = 1; |
| 1433 | child.git_cmd = 1; |
| 1434 | if (run_command(&child)) |
| 1435 | return "Working directory has unstaged changes"; |
| 1436 | |
| 1437 | child_process_init(&child); |
| 1438 | strvec_pushl(&child.args, "diff-index", "--quiet", "--cached", |
| 1439 | "--ignore-submodules", |
| 1440 | /* |
| 1441 | * diff-index with either HEAD or an empty tree |
| 1442 | * |
| 1443 | * NEEDSWORK: is_null_oid() cannot know whether it's an |
| 1444 | * unborn HEAD or a corrupt ref. It works for now because |
| 1445 | * it's only needed to know if we are comparing HEAD or an |
| 1446 | * empty tree. |
| 1447 | */ |
| 1448 | !is_null_oid(&worktree->head_oid) ? "HEAD" : |
| 1449 | empty_tree_oid_hex(the_repository->hash_algo), "--", NULL); |
| 1450 | strvec_pushv(&child.env, env->v); |
| 1451 | child.no_stdin = 1; |
| 1452 | child.no_stdout = 1; |
| 1453 | child.stdout_to_stderr = 0; |
| 1454 | child.git_cmd = 1; |
| 1455 | if (run_command(&child)) |
| 1456 | return "Working directory has staged changes"; |
| 1457 | |
| 1458 | child_process_init(&child); |
| 1459 | strvec_pushl(&child.args, "read-tree", "-u", "-m", hash_to_hex(sha1), |
| 1460 | NULL); |
| 1461 | strvec_pushv(&child.env, env->v); |
| 1462 | child.dir = worktree->path; |
| 1463 | child.no_stdin = 1; |
| 1464 | child.no_stdout = 1; |
| 1465 | child.stdout_to_stderr = 0; |
| 1466 | child.git_cmd = 1; |
| 1467 | if (run_command(&child)) |
| 1468 | return "Could not update working tree to new HEAD"; |
| 1469 | |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | |
| 1473 | static const char *push_to_checkout_hook = "push-to-checkout"; |
| 1474 | |
| 1475 | static const char *push_to_checkout(unsigned char *hash, |
| 1476 | int *invoked_hook, |
| 1477 | struct strvec *env, |
| 1478 | const char *work_tree) |
| 1479 | { |
| 1480 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT_FORCE_SERIAL; |
| 1481 | |
| 1482 | opt.invoked_hook = invoked_hook; |
| 1483 | |
| 1484 | strvec_pushv(&opt.env, env->v); |
| 1485 | strvec_pushf(&opt.env, "GIT_WORK_TREE=%s", absolute_path(work_tree)); |
| 1486 | strvec_push(&opt.args, hash_to_hex(hash)); |
| 1487 | if (run_hooks_opt(the_repository, push_to_checkout_hook, &opt)) |
| 1488 | return "push-to-checkout hook declined"; |
| 1489 | else |
| 1490 | return NULL; |
| 1491 | } |
| 1492 | |
| 1493 | static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree) |
| 1494 | { |
| 1495 | const char *retval; |
| 1496 | char *git_dir; |
| 1497 | struct strvec env = STRVEC_INIT; |
| 1498 | int invoked_hook; |
| 1499 | |
| 1500 | if (!worktree || !worktree->path) |
| 1501 | BUG("worktree->path must be non-NULL"); |
| 1502 | |
| 1503 | if (worktree->is_bare) |
| 1504 | return "denyCurrentBranch = updateInstead needs a worktree"; |
| 1505 | git_dir = get_worktree_git_dir(worktree); |
| 1506 | |
| 1507 | strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir)); |
| 1508 | |
| 1509 | retval = push_to_checkout(sha1, &invoked_hook, &env, worktree->path); |
| 1510 | if (!invoked_hook) |
| 1511 | retval = push_to_deploy(sha1, &env, worktree); |
| 1512 | |
| 1513 | strvec_clear(&env); |
| 1514 | free(git_dir); |
| 1515 | return retval; |
| 1516 | } |
| 1517 | |
| 1518 | static const char *update(struct command *cmd, struct shallow_info *si) |
| 1519 | { |
| 1520 | const char *name = cmd->ref_name; |
| 1521 | struct strbuf namespaced_name_buf = STRBUF_INIT; |
| 1522 | static char *namespaced_name; |
| 1523 | const char *ret; |
| 1524 | struct object_id *old_oid = &cmd->old_oid; |
| 1525 | struct object_id *new_oid = &cmd->new_oid; |
| 1526 | int do_update_worktree = 0; |
| 1527 | struct worktree **worktrees = get_worktrees(the_repository); |
| 1528 | const struct worktree *worktree = |
| 1529 | find_shared_symref(worktrees, "HEAD", name); |
| 1530 | |
| 1531 | /* only refs/... are allowed */ |
| 1532 | if (!starts_with(name, "refs/") || |
| 1533 | check_refname_format(name + 5, is_null_oid(new_oid) ? |
| 1534 | REFNAME_ALLOW_ONELEVEL : 0)) { |
| 1535 | rp_error("refusing to update funny ref '%s' remotely", name); |
| 1536 | ret = "funny refname"; |
| 1537 | goto out; |
| 1538 | } |
| 1539 | |
| 1540 | strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name); |
| 1541 | free(namespaced_name); |
| 1542 | namespaced_name = strbuf_detach(&namespaced_name_buf, NULL); |
| 1543 | |
| 1544 | if (worktree && !worktree->is_bare) { |
| 1545 | switch (deny_current_branch) { |
| 1546 | case DENY_IGNORE: |
| 1547 | break; |
| 1548 | case DENY_WARN: |
| 1549 | rp_warning("updating the current branch"); |
| 1550 | break; |
| 1551 | case DENY_REFUSE: |
| 1552 | case DENY_UNCONFIGURED: |
| 1553 | rp_error("refusing to update checked out branch: %s", name); |
| 1554 | if (deny_current_branch == DENY_UNCONFIGURED) |
| 1555 | refuse_unconfigured_deny(); |
| 1556 | ret = "branch is currently checked out"; |
| 1557 | goto out; |
| 1558 | case DENY_UPDATE_INSTEAD: |
| 1559 | /* pass -- let other checks intervene first */ |
| 1560 | do_update_worktree = 1; |
| 1561 | break; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | if (!is_null_oid(new_oid) && |
| 1566 | !odb_has_object(the_repository->objects, new_oid, |
| 1567 | ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) { |
| 1568 | error("unpack should have generated %s, " |
| 1569 | "but I can't find it!", oid_to_hex(new_oid)); |
| 1570 | ret = "bad pack"; |
| 1571 | goto out; |
| 1572 | } |
| 1573 | |
| 1574 | if (!is_null_oid(old_oid) && is_null_oid(new_oid)) { |
| 1575 | if (deny_deletes && starts_with(name, "refs/heads/")) { |
| 1576 | rp_error("denying ref deletion for %s", name); |
| 1577 | ret = "deletion prohibited"; |
| 1578 | goto out; |
| 1579 | } |
| 1580 | |
| 1581 | if (worktree || (head_name && !strcmp(namespaced_name, head_name))) { |
| 1582 | switch (deny_delete_current) { |
| 1583 | case DENY_IGNORE: |
| 1584 | break; |
| 1585 | case DENY_WARN: |
| 1586 | rp_warning("deleting the current branch"); |
| 1587 | break; |
| 1588 | case DENY_REFUSE: |
| 1589 | case DENY_UNCONFIGURED: |
| 1590 | case DENY_UPDATE_INSTEAD: |
| 1591 | if (deny_delete_current == DENY_UNCONFIGURED) |
| 1592 | refuse_unconfigured_deny_delete_current(); |
| 1593 | rp_error("refusing to delete the current branch: %s", name); |
| 1594 | ret = "deletion of the current branch prohibited"; |
| 1595 | goto out; |
| 1596 | default: |
| 1597 | ret = "Invalid denyDeleteCurrent setting"; |
| 1598 | goto out; |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | if (deny_non_fast_forwards && !is_null_oid(new_oid) && |
| 1604 | !is_null_oid(old_oid) && |
| 1605 | starts_with(name, "refs/heads/")) { |
| 1606 | struct object *old_object, *new_object; |
| 1607 | struct commit *old_commit, *new_commit; |
| 1608 | int ret2; |
| 1609 | |
| 1610 | old_object = parse_object(the_repository, old_oid); |
| 1611 | new_object = parse_object(the_repository, new_oid); |
| 1612 | |
| 1613 | if (!old_object || !new_object || |
| 1614 | old_object->type != OBJ_COMMIT || |
| 1615 | new_object->type != OBJ_COMMIT) { |
| 1616 | error("bad sha1 objects for %s", name); |
| 1617 | ret = "bad ref"; |
| 1618 | goto out; |
| 1619 | } |
| 1620 | old_commit = (struct commit *)old_object; |
| 1621 | new_commit = (struct commit *)new_object; |
| 1622 | ret2 = repo_in_merge_bases(the_repository, old_commit, new_commit); |
| 1623 | if (ret2 < 0) |
| 1624 | exit(128); |
| 1625 | if (!ret2) { |
| 1626 | rp_error("denying non-fast-forward %s" |
| 1627 | " (you should pull first)", name); |
| 1628 | ret = "non-fast-forward"; |
| 1629 | goto out; |
| 1630 | } |
| 1631 | } |
| 1632 | if (run_update_hook(cmd)) { |
| 1633 | rp_error("hook declined to update %s", name); |
| 1634 | ret = "hook declined"; |
| 1635 | goto out; |
| 1636 | } |
| 1637 | |
| 1638 | if (do_update_worktree) { |
| 1639 | ret = update_worktree(new_oid->hash, worktree); |
| 1640 | if (ret) |
| 1641 | goto out; |
| 1642 | } |
| 1643 | |
| 1644 | if (is_null_oid(new_oid)) { |
| 1645 | struct strbuf err = STRBUF_INIT; |
| 1646 | if (!parse_object(the_repository, old_oid)) { |
| 1647 | old_oid = NULL; |
| 1648 | if (refs_ref_exists(get_main_ref_store(the_repository), name)) { |
| 1649 | rp_warning("allowing deletion of corrupt ref"); |
| 1650 | } else { |
| 1651 | rp_warning("deleting a non-existent ref"); |
| 1652 | cmd->did_not_exist = 1; |
| 1653 | } |
| 1654 | } |
| 1655 | if (ref_transaction_delete(transaction, |
| 1656 | namespaced_name, |
| 1657 | old_oid, |
| 1658 | NULL, 0, |
| 1659 | "push", &err)) { |
| 1660 | rp_error("%s", err.buf); |
| 1661 | ret = "failed to delete"; |
| 1662 | } else { |
| 1663 | ret = NULL; /* good */ |
| 1664 | } |
| 1665 | strbuf_release(&err); |
| 1666 | } else { |
| 1667 | enum ref_transaction_error tx_err; |
| 1668 | struct strbuf err = STRBUF_INIT; |
| 1669 | if (shallow_update && si->shallow_ref[cmd->index] && |
| 1670 | update_shallow_ref(cmd, si)) { |
| 1671 | ret = "shallow error"; |
| 1672 | goto out; |
| 1673 | } |
| 1674 | |
| 1675 | tx_err = ref_transaction_update(transaction, |
| 1676 | namespaced_name, |
| 1677 | new_oid, old_oid, |
| 1678 | NULL, NULL, |
| 1679 | 0, "push", |
| 1680 | &err); |
| 1681 | if (tx_err) { |
| 1682 | rp_error("%s", err.buf); |
| 1683 | if (tx_err == REF_TRANSACTION_ERROR_GENERIC) |
| 1684 | ret = "failed to update ref"; |
| 1685 | else |
| 1686 | ret = ref_transaction_error_msg(tx_err); |
| 1687 | } else { |
| 1688 | ret = NULL; /* good */ |
| 1689 | } |
| 1690 | strbuf_release(&err); |
| 1691 | } |
| 1692 | |
| 1693 | out: |
| 1694 | free_worktrees(worktrees); |
| 1695 | return ret; |
| 1696 | } |
| 1697 | |
| 1698 | static void run_update_post_hook(struct command *commands) |
| 1699 | { |
| 1700 | static const char hook_name[] = "post-update"; |
| 1701 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; |
| 1702 | struct async sideband_async; |
| 1703 | struct command *cmd; |
| 1704 | int sideband_async_started = 0; |
| 1705 | int saved_stderr = -1; |
| 1706 | |
| 1707 | if (!hook_exists(the_repository, hook_name)) |
| 1708 | return; |
| 1709 | |
| 1710 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1711 | if (cmd->error_string || cmd->did_not_exist) |
| 1712 | continue; |
| 1713 | strvec_push(&opt.args, cmd->ref_name); |
| 1714 | } |
| 1715 | if (!opt.args.nr) |
| 1716 | return; |
| 1717 | |
| 1718 | prepare_sideband_async(&sideband_async, &saved_stderr, &sideband_async_started); |
| 1719 | |
| 1720 | run_hooks_opt(the_repository, hook_name, &opt); |
| 1721 | |
| 1722 | finish_sideband_async(&sideband_async, saved_stderr, sideband_async_started); |
| 1723 | } |
| 1724 | |
| 1725 | static void check_aliased_update_internal(struct command *cmd, |
| 1726 | struct string_list *list, |
| 1727 | const char *dst_name, int flag) |
| 1728 | { |
| 1729 | struct string_list_item *item; |
| 1730 | struct command *dst_cmd; |
| 1731 | |
| 1732 | if (!(flag & REF_ISSYMREF)) |
| 1733 | return; |
| 1734 | |
| 1735 | if (!dst_name) { |
| 1736 | rp_error("refusing update to broken symref '%s'", cmd->ref_name); |
| 1737 | cmd->skip_update = 1; |
| 1738 | cmd->error_string = "broken symref"; |
| 1739 | return; |
| 1740 | } |
| 1741 | dst_name = strip_namespace(dst_name); |
| 1742 | |
| 1743 | if (!(item = string_list_lookup(list, dst_name))) |
| 1744 | return; |
| 1745 | |
| 1746 | cmd->skip_update = 1; |
| 1747 | |
| 1748 | dst_cmd = (struct command *) item->util; |
| 1749 | |
| 1750 | if (oideq(&cmd->old_oid, &dst_cmd->old_oid) && |
| 1751 | oideq(&cmd->new_oid, &dst_cmd->new_oid)) |
| 1752 | return; |
| 1753 | |
| 1754 | dst_cmd->skip_update = 1; |
| 1755 | |
| 1756 | rp_error("refusing inconsistent update between symref '%s' (%s..%s) and" |
| 1757 | " its target '%s' (%s..%s)", |
| 1758 | cmd->ref_name, |
| 1759 | repo_find_unique_abbrev(the_repository, &cmd->old_oid, DEFAULT_ABBREV), |
| 1760 | repo_find_unique_abbrev(the_repository, &cmd->new_oid, DEFAULT_ABBREV), |
| 1761 | dst_cmd->ref_name, |
| 1762 | repo_find_unique_abbrev(the_repository, &dst_cmd->old_oid, DEFAULT_ABBREV), |
| 1763 | repo_find_unique_abbrev(the_repository, &dst_cmd->new_oid, DEFAULT_ABBREV)); |
| 1764 | |
| 1765 | cmd->error_string = dst_cmd->error_string = |
| 1766 | "inconsistent aliased update"; |
| 1767 | } |
| 1768 | |
| 1769 | static void check_aliased_update(struct command *cmd, struct string_list *list) |
| 1770 | { |
| 1771 | struct strbuf buf = STRBUF_INIT; |
| 1772 | const char *dst_name; |
| 1773 | int flag; |
| 1774 | |
| 1775 | strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name); |
| 1776 | dst_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
| 1777 | buf.buf, 0, NULL, &flag); |
| 1778 | check_aliased_update_internal(cmd, list, dst_name, flag); |
| 1779 | strbuf_release(&buf); |
| 1780 | } |
| 1781 | |
| 1782 | static void check_aliased_updates(struct command *commands) |
| 1783 | { |
| 1784 | struct command *cmd; |
| 1785 | struct string_list ref_list = STRING_LIST_INIT_NODUP; |
| 1786 | |
| 1787 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1788 | struct string_list_item *item = |
| 1789 | string_list_append(&ref_list, cmd->ref_name); |
| 1790 | item->util = (void *)cmd; |
| 1791 | } |
| 1792 | string_list_sort(&ref_list); |
| 1793 | |
| 1794 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1795 | if (!cmd->error_string) |
| 1796 | check_aliased_update(cmd, &ref_list); |
| 1797 | } |
| 1798 | |
| 1799 | string_list_clear(&ref_list, 0); |
| 1800 | } |
| 1801 | |
| 1802 | static const struct object_id *command_singleton_iterator(void *cb_data) |
| 1803 | { |
| 1804 | struct command **cmd_list = cb_data; |
| 1805 | struct command *cmd = *cmd_list; |
| 1806 | |
| 1807 | if (!cmd || is_null_oid(&cmd->new_oid)) |
| 1808 | return NULL; |
| 1809 | *cmd_list = NULL; /* this returns only one */ |
| 1810 | return &cmd->new_oid; |
| 1811 | } |
| 1812 | |
| 1813 | static void set_connectivity_errors(struct command *commands, |
| 1814 | struct shallow_info *si, |
| 1815 | struct odb_transaction *transaction) |
| 1816 | { |
| 1817 | struct command *cmd; |
| 1818 | |
| 1819 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1820 | struct command *singleton = cmd; |
| 1821 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
| 1822 | struct strvec env = STRVEC_INIT; |
| 1823 | |
| 1824 | if (shallow_update && si->shallow_ref[cmd->index]) |
| 1825 | /* to be checked in update_shallow_ref() */ |
| 1826 | continue; |
| 1827 | |
| 1828 | odb_transaction_env(transaction, &env); |
| 1829 | opt.env = env.v; |
| 1830 | |
| 1831 | if (!check_connected(the_repository, |
| 1832 | command_singleton_iterator, |
| 1833 | &singleton, &opt)) |
| 1834 | continue; |
| 1835 | |
| 1836 | cmd->error_string = "missing necessary objects"; |
| 1837 | |
| 1838 | strvec_clear(&env); |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | struct iterate_data { |
| 1843 | struct command *cmds; |
| 1844 | struct shallow_info *si; |
| 1845 | }; |
| 1846 | |
| 1847 | static const struct object_id *iterate_receive_command_list(void *cb_data) |
| 1848 | { |
| 1849 | struct iterate_data *data = cb_data; |
| 1850 | struct command **cmd_list = &data->cmds; |
| 1851 | struct command *cmd = *cmd_list; |
| 1852 | |
| 1853 | for (; cmd; cmd = cmd->next) { |
| 1854 | if (shallow_update && data->si->shallow_ref[cmd->index]) |
| 1855 | /* to be checked in update_shallow_ref() */ |
| 1856 | continue; |
| 1857 | if (!is_null_oid(&cmd->new_oid) && !cmd->skip_update) { |
| 1858 | *cmd_list = cmd->next; |
| 1859 | return &cmd->new_oid; |
| 1860 | } |
| 1861 | } |
| 1862 | return NULL; |
| 1863 | } |
| 1864 | |
| 1865 | static void reject_updates_to_hidden(struct command *commands) |
| 1866 | { |
| 1867 | struct strbuf refname_full = STRBUF_INIT; |
| 1868 | size_t prefix_len; |
| 1869 | struct command *cmd; |
| 1870 | |
| 1871 | strbuf_addstr(&refname_full, get_git_namespace()); |
| 1872 | prefix_len = refname_full.len; |
| 1873 | |
| 1874 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1875 | if (cmd->error_string) |
| 1876 | continue; |
| 1877 | |
| 1878 | strbuf_setlen(&refname_full, prefix_len); |
| 1879 | strbuf_addstr(&refname_full, cmd->ref_name); |
| 1880 | |
| 1881 | if (!ref_is_hidden(cmd->ref_name, refname_full.buf, &hidden_refs)) |
| 1882 | continue; |
| 1883 | if (is_null_oid(&cmd->new_oid)) |
| 1884 | cmd->error_string = "deny deleting a hidden ref"; |
| 1885 | else |
| 1886 | cmd->error_string = "deny updating a hidden ref"; |
| 1887 | } |
| 1888 | |
| 1889 | strbuf_release(&refname_full); |
| 1890 | } |
| 1891 | |
| 1892 | static int should_process_cmd(struct command *cmd) |
| 1893 | { |
| 1894 | return !cmd->error_string && !cmd->skip_update; |
| 1895 | } |
| 1896 | |
| 1897 | static void BUG_if_skipped_connectivity_check(struct command *commands, |
| 1898 | struct shallow_info *si) |
| 1899 | { |
| 1900 | struct command *cmd; |
| 1901 | |
| 1902 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1903 | if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) |
| 1904 | bug("connectivity check has not been run on ref %s", |
| 1905 | cmd->ref_name); |
| 1906 | } |
| 1907 | BUG_if_bug("connectivity check skipped???"); |
| 1908 | } |
| 1909 | |
| 1910 | static void ref_transaction_rejection_handler(const char *refname, |
| 1911 | const struct object_id *old_oid UNUSED, |
| 1912 | const struct object_id *new_oid UNUSED, |
| 1913 | const char *old_target UNUSED, |
| 1914 | const char *new_target UNUSED, |
| 1915 | enum ref_transaction_error err, |
| 1916 | const char *details, |
| 1917 | void *cb_data) |
| 1918 | { |
| 1919 | struct strmap *failed_refs = cb_data; |
| 1920 | |
| 1921 | if (details) |
| 1922 | rp_error("%s", details); |
| 1923 | |
| 1924 | strmap_put(failed_refs, refname, (char *)ref_transaction_error_msg(err)); |
| 1925 | } |
| 1926 | |
| 1927 | static void execute_commands_non_atomic(struct command *commands, |
| 1928 | struct shallow_info *si) |
| 1929 | { |
| 1930 | struct command *cmd; |
| 1931 | struct strbuf err = STRBUF_INIT; |
| 1932 | const char *reported_error = NULL; |
| 1933 | struct strmap failed_refs = STRMAP_INIT; |
| 1934 | |
| 1935 | /* |
| 1936 | * Reference updates, where D/F conflicts shouldn't arise due to |
| 1937 | * one reference being deleted, while the other being created |
| 1938 | * are treated as conflicts in batched updates. This is because |
| 1939 | * we don't do conflict resolution inside a transaction. To |
| 1940 | * mitigate this, delete references in a separate batch. |
| 1941 | * |
| 1942 | * NEEDSWORK: Add conflict resolution between deletion and creation |
| 1943 | * of reference updates within a transaction. With that, we can |
| 1944 | * combine the two phases. |
| 1945 | */ |
| 1946 | enum processing_phase { |
| 1947 | PHASE_DELETIONS, |
| 1948 | PHASE_OTHERS |
| 1949 | }; |
| 1950 | |
| 1951 | for (enum processing_phase phase = PHASE_DELETIONS; phase <= PHASE_OTHERS; phase++) { |
| 1952 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1953 | if (!should_process_cmd(cmd) || cmd->run_proc_receive) |
| 1954 | continue; |
| 1955 | |
| 1956 | if (phase == PHASE_DELETIONS && !is_null_oid(&cmd->new_oid)) |
| 1957 | continue; |
| 1958 | else if (phase == PHASE_OTHERS && is_null_oid(&cmd->new_oid)) |
| 1959 | continue; |
| 1960 | |
| 1961 | /* |
| 1962 | * Lazily create a transaction only when we know there are |
| 1963 | * updates to be added. |
| 1964 | */ |
| 1965 | if (!transaction) { |
| 1966 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
| 1967 | REF_TRANSACTION_ALLOW_FAILURE, &err); |
| 1968 | if (!transaction) { |
| 1969 | rp_error("%s", err.buf); |
| 1970 | strbuf_reset(&err); |
| 1971 | reported_error = "transaction failed to start"; |
| 1972 | goto failure; |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | cmd->error_string = update(cmd, si); |
| 1977 | } |
| 1978 | |
| 1979 | /* No transaction, so nothing to commit */ |
| 1980 | if (!transaction) |
| 1981 | goto cleanup; |
| 1982 | |
| 1983 | if (ref_transaction_commit(transaction, &err)) { |
| 1984 | rp_error("%s", err.buf); |
| 1985 | reported_error = "failed to update refs"; |
| 1986 | goto failure; |
| 1987 | } |
| 1988 | |
| 1989 | ref_transaction_for_each_rejected_update(transaction, |
| 1990 | |
| 1991 | ref_transaction_rejection_handler, |
| 1992 | &failed_refs); |
| 1993 | |
| 1994 | if (strmap_empty(&failed_refs)) |
| 1995 | goto cleanup; |
| 1996 | |
| 1997 | failure: |
| 1998 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 1999 | if (reported_error) |
| 2000 | cmd->error_string = reported_error; |
| 2001 | else if (strmap_contains(&failed_refs, cmd->ref_name)) |
| 2002 | cmd->error_string = cmd->error_string_owned = xstrdup(strmap_get(&failed_refs, cmd->ref_name)); |
| 2003 | } |
| 2004 | |
| 2005 | cleanup: |
| 2006 | ref_transaction_free(transaction); |
| 2007 | transaction = NULL; |
| 2008 | strmap_clear(&failed_refs, 0); |
| 2009 | strbuf_release(&err); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | static void execute_commands_atomic(struct command *commands, |
| 2014 | struct shallow_info *si) |
| 2015 | { |
| 2016 | struct command *cmd; |
| 2017 | struct strbuf err = STRBUF_INIT; |
| 2018 | const char *reported_error = "atomic push failure"; |
| 2019 | |
| 2020 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
| 2021 | 0, &err); |
| 2022 | if (!transaction) { |
| 2023 | rp_error("%s", err.buf); |
| 2024 | strbuf_reset(&err); |
| 2025 | reported_error = "transaction failed to start"; |
| 2026 | goto failure; |
| 2027 | } |
| 2028 | |
| 2029 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2030 | if (!should_process_cmd(cmd) || cmd->run_proc_receive) |
| 2031 | continue; |
| 2032 | |
| 2033 | cmd->error_string = update(cmd, si); |
| 2034 | |
| 2035 | if (cmd->error_string) |
| 2036 | goto failure; |
| 2037 | } |
| 2038 | |
| 2039 | if (ref_transaction_commit(transaction, &err)) { |
| 2040 | rp_error("%s", err.buf); |
| 2041 | reported_error = "atomic transaction failed"; |
| 2042 | goto failure; |
| 2043 | } |
| 2044 | goto cleanup; |
| 2045 | |
| 2046 | failure: |
| 2047 | for (cmd = commands; cmd; cmd = cmd->next) |
| 2048 | if (!cmd->error_string) |
| 2049 | cmd->error_string = reported_error; |
| 2050 | |
| 2051 | cleanup: |
| 2052 | ref_transaction_free(transaction); |
| 2053 | strbuf_release(&err); |
| 2054 | } |
| 2055 | |
| 2056 | static void execute_commands(struct command *commands, |
| 2057 | int unpacker_error, |
| 2058 | struct shallow_info *si, |
| 2059 | struct odb_transaction *transaction, |
| 2060 | const struct string_list *push_options) |
| 2061 | { |
| 2062 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
| 2063 | struct command *cmd; |
| 2064 | struct iterate_data data; |
| 2065 | struct async muxer; |
| 2066 | int err_fd = 0; |
| 2067 | int run_proc_receive = 0; |
| 2068 | |
| 2069 | if (unpacker_error) { |
| 2070 | for (cmd = commands; cmd; cmd = cmd->next) |
| 2071 | cmd->error_string = "unpacker error"; |
| 2072 | return; |
| 2073 | } |
| 2074 | |
| 2075 | if (!skip_connectivity_check) { |
| 2076 | struct strvec env = STRVEC_INIT; |
| 2077 | |
| 2078 | if (use_sideband) { |
| 2079 | memset(&muxer, 0, sizeof(muxer)); |
| 2080 | muxer.proc = copy_to_sideband; |
| 2081 | muxer.in = -1; |
| 2082 | if (!start_async(&muxer)) |
| 2083 | err_fd = muxer.in; |
| 2084 | /* ...else, continue without relaying sideband */ |
| 2085 | } |
| 2086 | |
| 2087 | data.cmds = commands; |
| 2088 | data.si = si; |
| 2089 | opt.err_fd = err_fd; |
| 2090 | opt.progress = err_fd && !quiet; |
| 2091 | odb_transaction_env(transaction, &env); |
| 2092 | opt.env = env.v; |
| 2093 | opt.exclude_hidden_refs_section = "receive"; |
| 2094 | |
| 2095 | if (check_connected(the_repository, |
| 2096 | iterate_receive_command_list, |
| 2097 | &data, &opt)) |
| 2098 | set_connectivity_errors(commands, si, transaction); |
| 2099 | |
| 2100 | if (use_sideband) |
| 2101 | finish_async(&muxer); |
| 2102 | |
| 2103 | strvec_clear(&env); |
| 2104 | } |
| 2105 | |
| 2106 | reject_updates_to_hidden(commands); |
| 2107 | |
| 2108 | /* |
| 2109 | * Try to find commands that have special prefix in their reference names, |
| 2110 | * and mark them to run an external "proc-receive" hook later. |
| 2111 | */ |
| 2112 | if (proc_receive_ref) { |
| 2113 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2114 | if (!should_process_cmd(cmd)) |
| 2115 | continue; |
| 2116 | |
| 2117 | if (proc_receive_ref_matches(cmd)) { |
| 2118 | cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED; |
| 2119 | run_proc_receive = 1; |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | if (run_receive_hook(commands, "pre-receive", 0, transaction, push_options)) { |
| 2125 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2126 | if (!cmd->error_string) |
| 2127 | cmd->error_string = "pre-receive hook declined"; |
| 2128 | } |
| 2129 | return; |
| 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * If there is no command ready to run, should return directly to destroy |
| 2134 | * temporary data in the quarantine area. |
| 2135 | */ |
| 2136 | for (cmd = commands; cmd && cmd->error_string; cmd = cmd->next) |
| 2137 | ; /* nothing */ |
| 2138 | if (!cmd) |
| 2139 | return; |
| 2140 | |
| 2141 | /* |
| 2142 | * Now we'll start writing out refs, which means the objects need |
| 2143 | * to be in their final positions so that other processes can see them. |
| 2144 | */ |
| 2145 | if (odb_transaction_commit(transaction)) { |
| 2146 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2147 | if (!cmd->error_string) |
| 2148 | cmd->error_string = "unable to migrate objects to permanent storage"; |
| 2149 | } |
| 2150 | return; |
| 2151 | } |
| 2152 | |
| 2153 | check_aliased_updates(commands); |
| 2154 | |
| 2155 | free(head_name_to_free); |
| 2156 | head_name = head_name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository), |
| 2157 | "HEAD", 0, NULL, |
| 2158 | NULL); |
| 2159 | |
| 2160 | if (run_proc_receive && |
| 2161 | run_proc_receive_hook(commands, push_options)) |
| 2162 | for (cmd = commands; cmd; cmd = cmd->next) |
| 2163 | if (!cmd->error_string && |
| 2164 | !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED) && |
| 2165 | (cmd->run_proc_receive || use_atomic)) |
| 2166 | cmd->error_string = "fail to run proc-receive hook"; |
| 2167 | |
| 2168 | if (use_atomic) |
| 2169 | execute_commands_atomic(commands, si); |
| 2170 | else |
| 2171 | execute_commands_non_atomic(commands, si); |
| 2172 | |
| 2173 | if (shallow_update) |
| 2174 | BUG_if_skipped_connectivity_check(commands, si); |
| 2175 | } |
| 2176 | |
| 2177 | static struct command **queue_command(struct command **tail, |
| 2178 | const char *line, |
| 2179 | int linelen) |
| 2180 | { |
| 2181 | struct object_id old_oid, new_oid; |
| 2182 | struct command *cmd; |
| 2183 | const char *refname; |
| 2184 | int reflen; |
| 2185 | const char *p; |
| 2186 | |
| 2187 | if (parse_oid_hex(line, &old_oid, &p) || |
| 2188 | *p++ != ' ' || |
| 2189 | parse_oid_hex(p, &new_oid, &p) || |
| 2190 | *p++ != ' ') |
| 2191 | die("protocol error: expected old/new/ref, got '%s'", line); |
| 2192 | |
| 2193 | refname = p; |
| 2194 | reflen = linelen - (p - line); |
| 2195 | FLEX_ALLOC_MEM(cmd, ref_name, refname, reflen); |
| 2196 | oidcpy(&cmd->old_oid, &old_oid); |
| 2197 | oidcpy(&cmd->new_oid, &new_oid); |
| 2198 | *tail = cmd; |
| 2199 | return &cmd->next; |
| 2200 | } |
| 2201 | |
| 2202 | static void free_commands(struct command *commands) |
| 2203 | { |
| 2204 | while (commands) { |
| 2205 | struct command *next = commands->next; |
| 2206 | |
| 2207 | ref_push_report_free(commands->report); |
| 2208 | free(commands->error_string_owned); |
| 2209 | free(commands); |
| 2210 | commands = next; |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | static void queue_commands_from_cert(struct command **tail, |
| 2215 | struct strbuf *push_cert) |
| 2216 | { |
| 2217 | const char *boc, *eoc; |
| 2218 | |
| 2219 | if (*tail) |
| 2220 | die("protocol error: got both push certificate and unsigned commands"); |
| 2221 | |
| 2222 | boc = strstr(push_cert->buf, "\n\n"); |
| 2223 | if (!boc) |
| 2224 | die("malformed push certificate %.*s", 100, push_cert->buf); |
| 2225 | else |
| 2226 | boc += 2; |
| 2227 | eoc = push_cert->buf + parse_signed_buffer(push_cert->buf, push_cert->len); |
| 2228 | |
| 2229 | while (boc < eoc) { |
| 2230 | const char *eol = memchr(boc, '\n', eoc - boc); |
| 2231 | tail = queue_command(tail, boc, eol ? eol - boc : eoc - boc); |
| 2232 | boc = eol ? eol + 1 : eoc; |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | static struct command *read_head_info(struct packet_reader *reader, |
| 2237 | struct oid_array *shallow, |
| 2238 | enum report_status_version *version) |
| 2239 | { |
| 2240 | struct command *commands = NULL; |
| 2241 | struct command **p = &commands; |
| 2242 | for (;;) { |
| 2243 | int linelen; |
| 2244 | |
| 2245 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
| 2246 | break; |
| 2247 | |
| 2248 | if (reader->pktlen > 8 && starts_with(reader->line, "shallow ")) { |
| 2249 | struct object_id oid; |
| 2250 | if (get_oid_hex(reader->line + 8, &oid)) |
| 2251 | die("protocol error: expected shallow sha, got '%s'", |
| 2252 | reader->line + 8); |
| 2253 | oid_array_append(shallow, &oid); |
| 2254 | continue; |
| 2255 | } |
| 2256 | |
| 2257 | linelen = strlen(reader->line); |
| 2258 | if (linelen < reader->pktlen) { |
| 2259 | const char *feature_list = reader->line + linelen + 1; |
| 2260 | const char *hash = NULL; |
| 2261 | const char *client_sid; |
| 2262 | size_t len = 0; |
| 2263 | if (parse_feature_request(feature_list, "report-status")) |
| 2264 | *version = REPORT_STATUS_V0; |
| 2265 | if (parse_feature_request(feature_list, "report-status-v2")) |
| 2266 | *version = REPORT_STATUS_V2; |
| 2267 | if (parse_feature_request(feature_list, "side-band-64k")) |
| 2268 | use_sideband = LARGE_PACKET_MAX; |
| 2269 | if (parse_feature_request(feature_list, "quiet")) |
| 2270 | quiet = 1; |
| 2271 | if (advertise_atomic_push |
| 2272 | && parse_feature_request(feature_list, "atomic")) |
| 2273 | use_atomic = 1; |
| 2274 | if (advertise_push_options |
| 2275 | && parse_feature_request(feature_list, "push-options")) |
| 2276 | use_push_options = 1; |
| 2277 | hash = parse_feature_value(feature_list, "object-format", &len, NULL); |
| 2278 | if (!hash) { |
| 2279 | hash = hash_algos[GIT_HASH_SHA1_LEGACY].name; |
| 2280 | len = strlen(hash); |
| 2281 | } |
| 2282 | if (xstrncmpz(the_hash_algo->name, hash, len)) |
| 2283 | die("error: unsupported object format '%s'", hash); |
| 2284 | client_sid = parse_feature_value(feature_list, "session-id", &len, NULL); |
| 2285 | if (client_sid) { |
| 2286 | char *sid = xstrndup(client_sid, len); |
| 2287 | trace2_data_string("transfer", NULL, "client-sid", client_sid); |
| 2288 | free(sid); |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | if (!strcmp(reader->line, "push-cert")) { |
| 2293 | int true_flush = 0; |
| 2294 | int saved_options = reader->options; |
| 2295 | reader->options &= ~PACKET_READ_CHOMP_NEWLINE; |
| 2296 | |
| 2297 | for (;;) { |
| 2298 | packet_reader_read(reader); |
| 2299 | if (reader->status == PACKET_READ_FLUSH) { |
| 2300 | true_flush = 1; |
| 2301 | break; |
| 2302 | } |
| 2303 | if (reader->status != PACKET_READ_NORMAL) { |
| 2304 | die("protocol error: got an unexpected packet"); |
| 2305 | } |
| 2306 | if (!strcmp(reader->line, "push-cert-end\n")) |
| 2307 | break; /* end of cert */ |
| 2308 | strbuf_addstr(&push_cert, reader->line); |
| 2309 | } |
| 2310 | reader->options = saved_options; |
| 2311 | |
| 2312 | if (true_flush) |
| 2313 | break; |
| 2314 | continue; |
| 2315 | } |
| 2316 | |
| 2317 | p = queue_command(p, reader->line, linelen); |
| 2318 | } |
| 2319 | |
| 2320 | if (push_cert.len) |
| 2321 | queue_commands_from_cert(p, &push_cert); |
| 2322 | |
| 2323 | return commands; |
| 2324 | } |
| 2325 | |
| 2326 | static void read_push_options(struct packet_reader *reader, |
| 2327 | struct string_list *options) |
| 2328 | { |
| 2329 | while (1) { |
| 2330 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
| 2331 | break; |
| 2332 | |
| 2333 | string_list_append(options, reader->line); |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | static int unpack_with_sideband(struct odb_transaction *transaction, |
| 2338 | const char *shallow_file, |
| 2339 | struct strbuf *err_msg) |
| 2340 | { |
| 2341 | struct odb_transaction_write_pack_opts opts = { |
| 2342 | .fsck_objects = (receive_fsck_objects >= 0 |
| 2343 | ? receive_fsck_objects |
| 2344 | : transfer_fsck_objects >= 0 |
| 2345 | ? transfer_fsck_objects |
| 2346 | : 0), |
| 2347 | .fsck_msg_types = fsck_msg_types.buf, |
| 2348 | .max_input_size = max_input_size, |
| 2349 | .shallow_file = shallow_file, |
| 2350 | .reject_thin = reject_thin, |
| 2351 | .quiet = quiet, |
| 2352 | }; |
| 2353 | struct async muxer; |
| 2354 | int ret; |
| 2355 | |
| 2356 | if (!use_sideband) |
| 2357 | return odb_transaction_write_pack(transaction, 0, err_msg, &opts); |
| 2358 | |
| 2359 | use_keepalive = KEEPALIVE_AFTER_NUL; |
| 2360 | memset(&muxer, 0, sizeof(muxer)); |
| 2361 | muxer.proc = copy_to_sideband; |
| 2362 | muxer.in = -1; |
| 2363 | if (start_async(&muxer)) |
| 2364 | return 0; |
| 2365 | |
| 2366 | opts.err_fd = muxer.in; |
| 2367 | ret = odb_transaction_write_pack(transaction, 0, err_msg, &opts); |
| 2368 | |
| 2369 | finish_async(&muxer); |
| 2370 | return ret; |
| 2371 | } |
| 2372 | |
| 2373 | static void prepare_shallow_update(struct shallow_info *si, |
| 2374 | const char *shallow_file) |
| 2375 | { |
| 2376 | int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32); |
| 2377 | |
| 2378 | ALLOC_ARRAY(si->used_shallow, si->shallow->nr); |
| 2379 | assign_shallow_commits_to_refs(si, si->used_shallow, NULL); |
| 2380 | |
| 2381 | CALLOC_ARRAY(si->need_reachability_test, si->shallow->nr); |
| 2382 | CALLOC_ARRAY(si->reachable, si->shallow->nr); |
| 2383 | CALLOC_ARRAY(si->shallow_ref, si->ref->nr); |
| 2384 | |
| 2385 | for (i = 0; i < si->nr_ours; i++) |
| 2386 | si->need_reachability_test[si->ours[i]] = 1; |
| 2387 | |
| 2388 | for (i = 0; i < si->shallow->nr; i++) { |
| 2389 | if (!si->used_shallow[i]) |
| 2390 | continue; |
| 2391 | for (j = 0; j < bitmap_size; j++) { |
| 2392 | if (!si->used_shallow[i][j]) |
| 2393 | continue; |
| 2394 | si->need_reachability_test[i]++; |
| 2395 | for (k = 0; k < 32; k++) |
| 2396 | if (si->used_shallow[i][j] & (1U << k)) |
| 2397 | si->shallow_ref[j * 32 + k]++; |
| 2398 | } |
| 2399 | |
| 2400 | /* |
| 2401 | * true for those associated with some refs and belong |
| 2402 | * in "ours" list aka "step 7 not done yet" |
| 2403 | */ |
| 2404 | si->need_reachability_test[i] = |
| 2405 | si->need_reachability_test[i] > 1; |
| 2406 | } |
| 2407 | |
| 2408 | /* |
| 2409 | * keep hooks happy by forcing a temporary shallow file via |
| 2410 | * env variable because we can't add --shallow-file to every |
| 2411 | * command. check_connected() will be done with |
| 2412 | * true .git/shallow though. |
| 2413 | */ |
| 2414 | setenv(GIT_SHALLOW_FILE_ENVIRONMENT, shallow_file, 1); |
| 2415 | } |
| 2416 | |
| 2417 | static void update_shallow_info(struct command *commands, |
| 2418 | struct shallow_info *si, |
| 2419 | struct oid_array *ref, |
| 2420 | const char *shallow_file) |
| 2421 | { |
| 2422 | struct command *cmd; |
| 2423 | int *ref_status; |
| 2424 | remove_nonexistent_theirs_shallow(si); |
| 2425 | if (!si->nr_ours && !si->nr_theirs) { |
| 2426 | shallow_update = 0; |
| 2427 | return; |
| 2428 | } |
| 2429 | |
| 2430 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2431 | if (is_null_oid(&cmd->new_oid)) |
| 2432 | continue; |
| 2433 | oid_array_append(ref, &cmd->new_oid); |
| 2434 | cmd->index = ref->nr - 1; |
| 2435 | } |
| 2436 | si->ref = ref; |
| 2437 | |
| 2438 | if (shallow_update) { |
| 2439 | prepare_shallow_update(si, shallow_file); |
| 2440 | return; |
| 2441 | } |
| 2442 | |
| 2443 | ALLOC_ARRAY(ref_status, ref->nr); |
| 2444 | assign_shallow_commits_to_refs(si, NULL, ref_status); |
| 2445 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2446 | if (is_null_oid(&cmd->new_oid)) |
| 2447 | continue; |
| 2448 | if (ref_status[cmd->index]) { |
| 2449 | cmd->error_string = "shallow update not allowed"; |
| 2450 | cmd->skip_update = 1; |
| 2451 | } |
| 2452 | } |
| 2453 | free(ref_status); |
| 2454 | } |
| 2455 | |
| 2456 | static void override_cmds_error(struct command *commands, const char *err) |
| 2457 | { |
| 2458 | for (struct command *cmd = commands; cmd; cmd = cmd->next) { |
| 2459 | FREE_AND_NULL(cmd->error_string_owned); |
| 2460 | cmd->error_string = err; |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | /* |
| 2465 | * Generate the response to be sent to the client invoking 'git-receive-pack(1)'. |
| 2466 | */ |
| 2467 | static void generate_report(struct strbuf *buf, struct command *commands, |
| 2468 | const struct strbuf *unpack_status, |
| 2469 | enum report_status_version version) |
| 2470 | { |
| 2471 | struct command *cmd; |
| 2472 | |
| 2473 | packet_buf_write(buf, "unpack %s\n", |
| 2474 | unpack_status->len ? unpack_status->buf : "ok"); |
| 2475 | |
| 2476 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2477 | struct ref_push_report *report; |
| 2478 | int count = 0; |
| 2479 | |
| 2480 | if (cmd->error_string) |
| 2481 | packet_buf_write(buf, "ng %s %s\n", |
| 2482 | cmd->ref_name, cmd->error_string); |
| 2483 | else |
| 2484 | packet_buf_write(buf, "ok %s\n", cmd->ref_name); |
| 2485 | |
| 2486 | if (version != REPORT_STATUS_V2 || cmd->error_string) |
| 2487 | continue; |
| 2488 | |
| 2489 | for (report = cmd->report; report; report = report->next) { |
| 2490 | if (count++ > 0) |
| 2491 | packet_buf_write(buf, "ok %s\n", |
| 2492 | cmd->ref_name); |
| 2493 | if (report->ref_name) |
| 2494 | packet_buf_write(buf, "option refname %s\n", |
| 2495 | report->ref_name); |
| 2496 | if (report->old_oid) |
| 2497 | packet_buf_write(buf, "option old-oid %s\n", |
| 2498 | oid_to_hex(report->old_oid)); |
| 2499 | if (report->new_oid) |
| 2500 | packet_buf_write(buf, "option new-oid %s\n", |
| 2501 | oid_to_hex(report->new_oid)); |
| 2502 | if (report->forced_update) |
| 2503 | packet_buf_write(buf, "option forced-update\n"); |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | packet_buf_flush(buf); |
| 2508 | } |
| 2509 | |
| 2510 | static void report(struct command *commands, const struct strbuf *unpack_status, |
| 2511 | enum report_status_version version) |
| 2512 | { |
| 2513 | struct strbuf buf = STRBUF_INIT; |
| 2514 | |
| 2515 | generate_report(&buf, commands, unpack_status, version); |
| 2516 | |
| 2517 | if (run_receive_report_hook(&buf)) { |
| 2518 | strbuf_reset(&buf); |
| 2519 | override_cmds_error(commands, "receive-report hook failed"); |
| 2520 | generate_report(&buf, commands, unpack_status, version); |
| 2521 | } |
| 2522 | |
| 2523 | if (use_sideband) |
| 2524 | send_sideband(1, 1, buf.buf, buf.len, use_sideband); |
| 2525 | else |
| 2526 | write_or_die(1, buf.buf, buf.len); |
| 2527 | strbuf_release(&buf); |
| 2528 | } |
| 2529 | |
| 2530 | static int delete_only(struct command *commands) |
| 2531 | { |
| 2532 | struct command *cmd; |
| 2533 | for (cmd = commands; cmd; cmd = cmd->next) { |
| 2534 | if (!is_null_oid(&cmd->new_oid)) |
| 2535 | return 0; |
| 2536 | } |
| 2537 | return 1; |
| 2538 | } |
| 2539 | |
| 2540 | int cmd_receive_pack(int argc, |
| 2541 | const char **argv, |
| 2542 | const char *prefix, |
| 2543 | struct repository *repo UNUSED) |
| 2544 | { |
| 2545 | int advertise_refs = 0; |
| 2546 | struct command *commands; |
| 2547 | struct oid_array shallow = OID_ARRAY_INIT; |
| 2548 | struct oid_array ref = OID_ARRAY_INIT; |
| 2549 | struct shallow_info si; |
| 2550 | struct packet_reader reader; |
| 2551 | struct odb_transaction *transaction = NULL; |
| 2552 | enum report_status_version version = REPORT_STATUS_UNKNOWN; |
| 2553 | |
| 2554 | struct option options[] = { |
| 2555 | OPT__QUIET(&quiet, N_("quiet")), |
| 2556 | OPT_HIDDEN_BOOL(0, "skip-connectivity-check", &skip_connectivity_check, NULL), |
| 2557 | OPT_HIDDEN_BOOL(0, "stateless-rpc", &stateless_rpc, NULL), |
| 2558 | OPT_HIDDEN_BOOL(0, "http-backend-info-refs", &advertise_refs, NULL), |
| 2559 | OPT_ALIAS(0, "advertise-refs", "http-backend-info-refs"), |
| 2560 | OPT_HIDDEN_BOOL(0, "reject-thin-pack-for-testing", &reject_thin, NULL), |
| 2561 | OPT_END() |
| 2562 | }; |
| 2563 | |
| 2564 | packet_trace_identity("receive-pack"); |
| 2565 | |
| 2566 | argc = parse_options(argc, argv, prefix, options, receive_pack_usage, 0); |
| 2567 | |
| 2568 | if (argc > 1) |
| 2569 | usage_msg_opt(_("too many arguments"), receive_pack_usage, options); |
| 2570 | if (argc == 0) |
| 2571 | usage_msg_opt(_("you must specify a directory"), receive_pack_usage, options); |
| 2572 | |
| 2573 | service_dir = argv[0]; |
| 2574 | |
| 2575 | setup_path(); |
| 2576 | |
| 2577 | if (!enter_repo(the_repository, service_dir, 0)) |
| 2578 | die("'%s' does not appear to be a git repository", service_dir); |
| 2579 | |
| 2580 | repo_config(the_repository, receive_pack_config, NULL); |
| 2581 | if (cert_nonce_seed) |
| 2582 | push_cert_nonce = prepare_push_cert_nonce(service_dir, time(NULL)); |
| 2583 | |
| 2584 | switch (determine_protocol_version_server()) { |
| 2585 | case protocol_v2: |
| 2586 | /* |
| 2587 | * push support for protocol v2 has not been implemented yet, |
| 2588 | * so ignore the request to use v2 and fallback to using v0. |
| 2589 | */ |
| 2590 | break; |
| 2591 | case protocol_v1: |
| 2592 | /* |
| 2593 | * v1 is just the original protocol with a version string, |
| 2594 | * so just fall through after writing the version string. |
| 2595 | */ |
| 2596 | if (advertise_refs || !stateless_rpc) |
| 2597 | packet_write_fmt(1, "version 1\n"); |
| 2598 | |
| 2599 | /* fallthrough */ |
| 2600 | case protocol_v0: |
| 2601 | break; |
| 2602 | case protocol_unknown_version: |
| 2603 | BUG("unknown protocol version"); |
| 2604 | } |
| 2605 | |
| 2606 | if (advertise_refs || !stateless_rpc) { |
| 2607 | write_head_info(); |
| 2608 | } |
| 2609 | if (advertise_refs) |
| 2610 | return 0; |
| 2611 | |
| 2612 | packet_reader_init(&reader, 0, NULL, 0, |
| 2613 | PACKET_READ_CHOMP_NEWLINE | |
| 2614 | PACKET_READ_DIE_ON_ERR_PACKET); |
| 2615 | |
| 2616 | if ((commands = read_head_info(&reader, &shallow, &version))) { |
| 2617 | struct string_list push_options = STRING_LIST_INIT_DUP; |
| 2618 | struct strbuf unpack_status = STRBUF_INIT; |
| 2619 | |
| 2620 | if (use_push_options) |
| 2621 | read_push_options(&reader, &push_options); |
| 2622 | if (!check_cert_push_options(&push_options)) { |
| 2623 | struct command *cmd; |
| 2624 | for (cmd = commands; cmd; cmd = cmd->next) |
| 2625 | cmd->error_string = "inconsistent push options"; |
| 2626 | } |
| 2627 | |
| 2628 | prepare_shallow_info(&si, &shallow); |
| 2629 | if (!si.nr_ours && !si.nr_theirs) |
| 2630 | shallow_update = 0; |
| 2631 | if (!delete_only(commands)) { |
| 2632 | const char *alt_shallow_file = NULL; |
| 2633 | |
| 2634 | if (si.nr_ours || si.nr_theirs) |
| 2635 | alt_shallow_file = setup_temporary_shallow(si.shallow); |
| 2636 | |
| 2637 | if (odb_transaction_begin(the_repository->objects, &transaction, ODB_TRANSACTION_RECEIVE)) |
| 2638 | strbuf_addstr(&unpack_status, "unable to start object transaction"); |
| 2639 | else |
| 2640 | unpack_with_sideband(transaction, alt_shallow_file, &unpack_status); |
| 2641 | |
| 2642 | update_shallow_info(commands, &si, &ref, alt_shallow_file); |
| 2643 | } |
| 2644 | use_keepalive = KEEPALIVE_ALWAYS; |
| 2645 | execute_commands(commands, !!unpack_status.len, &si, transaction, |
| 2646 | &push_options); |
| 2647 | odb_transaction_finalize(transaction); |
| 2648 | sigchain_push(SIGPIPE, SIG_IGN); |
| 2649 | |
| 2650 | switch (version) { |
| 2651 | case REPORT_STATUS_V2: |
| 2652 | case REPORT_STATUS_V0: |
| 2653 | report(commands, &unpack_status, version); |
| 2654 | break; |
| 2655 | case REPORT_STATUS_UNKNOWN: |
| 2656 | break; |
| 2657 | } |
| 2658 | |
| 2659 | sigchain_pop(SIGPIPE); |
| 2660 | run_receive_hook(commands, "post-receive", 1, NULL, |
| 2661 | &push_options); |
| 2662 | run_update_post_hook(commands); |
| 2663 | free_commands(commands); |
| 2664 | string_list_clear(&push_options, 0); |
| 2665 | if (auto_gc) { |
| 2666 | struct child_process proc = CHILD_PROCESS_INIT; |
| 2667 | |
| 2668 | if (prepare_auto_maintenance(the_repository, 1, &proc)) { |
| 2669 | proc.no_stdin = 1; |
| 2670 | proc.stdout_to_stderr = 1; |
| 2671 | proc.err = use_sideband ? -1 : 0; |
| 2672 | |
| 2673 | if (!start_command(&proc)) { |
| 2674 | if (use_sideband) |
| 2675 | copy_to_sideband(proc.err, -1, NULL); |
| 2676 | finish_command(&proc); |
| 2677 | } |
| 2678 | } |
| 2679 | } |
| 2680 | if (auto_update_server_info) |
| 2681 | update_server_info(the_repository, 0); |
| 2682 | clear_shallow_info(&si); |
| 2683 | strbuf_release(&unpack_status); |
| 2684 | } |
| 2685 | if (use_sideband) |
| 2686 | packet_flush(1); |
| 2687 | oid_array_clear(&shallow); |
| 2688 | oid_array_clear(&ref); |
| 2689 | strvec_clear(&hidden_refs); |
| 2690 | free((void *)push_cert_nonce); |
| 2691 | return 0; |
| 2692 | } |