Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/update-ref.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / update-ref.c` 905 lines ยท 25633 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "builtin.h" 5 #include "config.h" 6 #include "environment.h" 7 #include "gettext.h" 8 #include "hash.h" 9 #include "hex.h" 10 #include "refs.h" 11 #include "object-name.h" 12 #include "parse-options.h" 13 #include "quote.h" 14 15 static const char * const git_update_ref_usage[] = { 16 N_("git update-ref [] -d []"), 17 N_("git update-ref [] []"), 18 N_("git update-ref [] --stdin [-z] [--batch-updates]"), 19 NULL 20 }; 21 22 static char line_termination = '\n'; 23 static unsigned int update_flags; 24 static unsigned int default_flags; 25 static unsigned create_reflog_flag; 26 static const char *msg; 27 28 struct command_options { 29 /* 30 * Individual updates are allowed to fail without causing 31 * update-ref to exit. This is set when using the 32 * '--batch-updates' flag. 33 */ 34 bool allow_update_failures; 35 }; 36 37 /* 38 * Parse one whitespace- or NUL-terminated, possibly C-quoted argument 39 * and append the result to arg. Return a pointer to the terminator. 40 * Die if there is an error in how the argument is C-quoted. This 41 * function is only used if not -z. 42 */ 43 static const char *parse_arg(const char *next, struct strbuf *arg) 44 { 45 if (*next == '"') { 46 const char *orig = next; 47 48 if (unquote_c_style(arg, next, &next)) 49 die("badly quoted argument: %s", orig); 50 if (*next && !isspace(*next)) 51 die("unexpected character after quoted argument: %s", orig); 52 } else { 53 while (*next && !isspace(*next)) 54 strbuf_addch(arg, *next++); 55 } 56 57 return next; 58 } 59 60 /* 61 * Parse the reference name immediately after "command SP". If not 62 * -z, then handle C-quoting. Return a pointer to a newly allocated 63 * string containing the name of the reference, or NULL if there was 64 * an error. Update *next to point at the character that terminates 65 * the argument. Die if C-quoting is malformed or the reference name 66 * is invalid. 67 */ 68 static char *parse_refname(const char **next) 69 { 70 struct strbuf ref = STRBUF_INIT; 71 72 if (line_termination) { 73 /* Without -z, use the next argument */ 74 *next = parse_arg(*next, &ref); 75 } else { 76 /* With -z, use everything up to the next NUL */ 77 strbuf_addstr(&ref, *next); 78 *next += ref.len; 79 } 80 81 if (!ref.len) { 82 strbuf_release(&ref); 83 return NULL; 84 } 85 86 if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL)) 87 die("invalid ref format: %s", ref.buf); 88 89 return strbuf_detach(&ref, NULL); 90 } 91 92 /* 93 * Wrapper around parse_refname which skips the next delimiter. 94 */ 95 static char *parse_next_refname(const char **next) 96 { 97 if (line_termination) { 98 /* Without -z, consume SP and use next argument */ 99 if (!**next || **next == line_termination) 100 return NULL; 101 if (**next != ' ') 102 die("expected SP but got: %s", *next); 103 } else { 104 /* With -z, read the next NUL-terminated line */ 105 if (**next) 106 return NULL; 107 } 108 /* Skip the delimiter */ 109 (*next)++; 110 111 return parse_refname(next); 112 } 113 114 /* 115 * Wrapper around parse_arg which skips the next delimiter. 116 */ 117 static char *parse_next_arg(const char **next) 118 { 119 struct strbuf arg = STRBUF_INIT; 120 121 if (line_termination) { 122 /* Without -z, consume SP and use next argument */ 123 if (!**next || **next == line_termination) 124 return NULL; 125 if (**next != ' ') 126 die("expected SP but got: %s", *next); 127 } else { 128 /* With -z, read the next NUL-terminated line */ 129 if (**next) 130 return NULL; 131 } 132 /* Skip the delimiter */ 133 (*next)++; 134 135 if (line_termination) { 136 /* Without -z, use the next argument */ 137 *next = parse_arg(*next, &arg); 138 } else { 139 /* With -z, use everything up to the next NUL */ 140 strbuf_addstr(&arg, *next); 141 *next += arg.len; 142 } 143 144 if (arg.len) 145 return strbuf_detach(&arg, NULL); 146 147 strbuf_release(&arg); 148 return NULL; 149 } 150 151 /* 152 * The value being parsed is (as opposed to ; the 153 * difference affects which error messages are generated): 154 */ 155 #define PARSE_SHA1_OLD 0x01 156 157 /* 158 * For backwards compatibility, accept an empty string for update's 159 * in binary mode to be equivalent to specifying zeros. 160 */ 161 #define PARSE_SHA1_ALLOW_EMPTY 0x02 162 163 /* 164 * Parse an argument separator followed by the next argument, if any. 165 * If there is an argument, convert it to a SHA-1, write it to sha1, 166 * set *next to point at the character terminating the argument, and 167 * return 0. If there is no argument at all (not even the empty 168 * string), return 1 and leave *next unchanged. If the value is 169 * provided but cannot be converted to a SHA-1, die. flags can 170 * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY. 171 */ 172 static int parse_next_oid(const char **next, const char *end, 173 struct object_id *oid, 174 const char *command, const char *refname, 175 int flags) 176 { 177 struct strbuf arg = STRBUF_INIT; 178 int ret = 0; 179 180 if (*next == end) 181 goto eof; 182 183 if (line_termination) { 184 /* Without -z, consume SP and use next argument */ 185 if (!**next || **next == line_termination) 186 return 1; 187 if (**next != ' ') 188 die("%s %s: expected SP but got: %s", 189 command, refname, *next); 190 (*next)++; 191 *next = parse_arg(*next, &arg); 192 if (arg.len) { 193 if (repo_get_oid_with_flags(the_repository, arg.buf, oid, 194 GET_OID_SKIP_AMBIGUITY_CHECK)) 195 goto invalid; 196 } else { 197 /* Without -z, an empty value means all zeros: */ 198 oidclr(oid, the_repository->hash_algo); 199 } 200 } else { 201 /* With -z, read the next NUL-terminated line */ 202 if (**next) 203 die("%s %s: expected NUL but got: %s", 204 command, refname, *next); 205 (*next)++; 206 if (*next == end) 207 goto eof; 208 strbuf_addstr(&arg, *next); 209 *next += arg.len; 210 211 if (arg.len) { 212 if (repo_get_oid_with_flags(the_repository, arg.buf, oid, 213 GET_OID_SKIP_AMBIGUITY_CHECK)) 214 goto invalid; 215 } else if (flags & PARSE_SHA1_ALLOW_EMPTY) { 216 /* With -z, treat an empty value as all zeros: */ 217 warning("%s %s: missing , treating as zero", 218 command, refname); 219 oidclr(oid, the_repository->hash_algo); 220 } else { 221 /* 222 * With -z, an empty non-required value means 223 * unspecified: 224 */ 225 ret = 1; 226 } 227 } 228 229 strbuf_release(&arg); 230 231 return ret; 232 233 invalid: 234 die(flags & PARSE_SHA1_OLD ? 235 "%s %s: invalid : %s" : 236 "%s %s: invalid : %s", 237 command, refname, arg.buf); 238 239 eof: 240 die(flags & PARSE_SHA1_OLD ? 241 "%s %s: unexpected end of input when reading " : 242 "%s %s: unexpected end of input when reading ", 243 command, refname); 244 } 245 246 static void print_rejected_refs(const char *refname, 247 const struct object_id *old_oid, 248 const struct object_id *new_oid, 249 const char *old_target, 250 const char *new_target, 251 enum ref_transaction_error err, 252 const char *details, 253 void *cb_data UNUSED) 254 { 255 struct strbuf sb = STRBUF_INIT; 256 257 if (details && *details) 258 error("%s", details); 259 260 strbuf_addf(&sb, "rejected %s %s %s %s\n", refname, 261 new_oid ? oid_to_hex(new_oid) : new_target, 262 old_oid ? oid_to_hex(old_oid) : old_target, 263 ref_transaction_error_msg(err)); 264 265 fwrite(sb.buf, sb.len, 1, stdout); 266 strbuf_release(&sb); 267 } 268 269 /* 270 * Handle transaction errors. If we're using batches updates, we want to only 271 * die for generic errors and print the remaining to the user. 272 */ 273 static void handle_ref_transaction_error(const char *refname, 274 struct object_id *new_oid, 275 struct object_id *old_oid, 276 const char *new_target, 277 const char *old_target, 278 enum ref_transaction_error tx_err, 279 struct strbuf *err, 280 struct command_options *opts) 281 { 282 if (!tx_err) 283 return; 284 285 if (tx_err != REF_TRANSACTION_ERROR_GENERIC && opts->allow_update_failures) { 286 print_rejected_refs(refname, old_oid, new_oid, old_target, 287 new_target, tx_err, err->buf, NULL); 288 return; 289 } 290 291 die("%s", err->buf); 292 } 293 294 /* 295 * The following five parse_cmd_*() functions parse the corresponding 296 * command. In each case, next points at the character following the 297 * command name and the following space. They each return a pointer 298 * to the character terminating the command, and die with an 299 * explanatory message if there are any parsing problems. All of 300 * these functions handle either text or binary format input, 301 * depending on how line_termination is set. 302 */ 303 304 static void parse_cmd_update(struct ref_transaction *transaction, 305 const char *next, const char *end, 306 struct command_options *opts) 307 { 308 struct strbuf err = STRBUF_INIT; 309 char *refname; 310 struct object_id new_oid, old_oid; 311 enum ref_transaction_error tx_err; 312 int have_old; 313 314 refname = parse_refname(&next); 315 if (!refname) 316 die("update: missing "); 317 318 if (parse_next_oid(&next, end, &new_oid, "update", refname, 319 PARSE_SHA1_ALLOW_EMPTY)) 320 die("update %s: missing ", refname); 321 322 have_old = !parse_next_oid(&next, end, &old_oid, "update", refname, 323 PARSE_SHA1_OLD); 324 325 if (*next != line_termination) 326 die("update %s: extra input: %s", refname, next); 327 328 tx_err = ref_transaction_update(transaction, refname, 329 &new_oid, have_old ? &old_oid : NULL, 330 NULL, NULL, 331 update_flags | create_reflog_flag, 332 msg, &err); 333 handle_ref_transaction_error(refname, &new_oid, have_old ? &old_oid : NULL, 334 NULL, NULL, tx_err, &err, opts); 335 336 337 update_flags = default_flags; 338 free(refname); 339 strbuf_release(&err); 340 } 341 342 static void parse_cmd_symref_update(struct ref_transaction *transaction, 343 const char *next, const char *end UNUSED, 344 struct command_options *opts) 345 { 346 char *refname, *new_target, *old_arg; 347 enum ref_transaction_error tx_err; 348 char *old_target = NULL; 349 struct strbuf err = STRBUF_INIT; 350 struct object_id old_oid; 351 int have_old_oid = 0; 352 353 refname = parse_refname(&next); 354 if (!refname) 355 die("symref-update: missing "); 356 357 new_target = parse_next_refname(&next); 358 if (!new_target) 359 die("symref-update %s: missing ", refname); 360 361 old_arg = parse_next_arg(&next); 362 if (old_arg) { 363 old_target = parse_next_arg(&next); 364 if (!old_target) 365 die("symref-update %s: expected old value", refname); 366 367 if (!strcmp(old_arg, "oid")) { 368 if (repo_get_oid_with_flags(the_repository, old_target, &old_oid, 369 GET_OID_SKIP_AMBIGUITY_CHECK)) 370 die("symref-update %s: invalid oid: %s", refname, old_target); 371 372 have_old_oid = 1; 373 } else if (!strcmp(old_arg, "ref")) { 374 if (check_refname_format(old_target, REFNAME_ALLOW_ONELEVEL)) 375 die("symref-update %s: invalid ref: %s", refname, old_target); 376 } else { 377 die("symref-update %s: invalid arg '%s' for old value", refname, old_arg); 378 } 379 } 380 381 if (*next != line_termination) 382 die("symref-update %s: extra input: %s", refname, next); 383 384 tx_err = ref_transaction_update(transaction, refname, NULL, 385 have_old_oid ? &old_oid : NULL, 386 new_target, 387 have_old_oid ? NULL : old_target, 388 update_flags | create_reflog_flag, 389 msg, &err); 390 handle_ref_transaction_error(refname, NULL, have_old_oid ? &old_oid : NULL, 391 new_target, have_old_oid ? NULL : old_target, 392 tx_err, &err, opts); 393 394 update_flags = default_flags; 395 free(refname); 396 free(old_arg); 397 free(old_target); 398 free(new_target); 399 strbuf_release(&err); 400 } 401 402 static void parse_cmd_create(struct ref_transaction *transaction, 403 const char *next, const char *end, 404 struct command_options *opts) 405 { 406 struct strbuf err = STRBUF_INIT; 407 char *refname; 408 struct object_id new_oid; 409 enum ref_transaction_error tx_err; 410 411 refname = parse_refname(&next); 412 if (!refname) 413 die("create: missing "); 414 415 if (parse_next_oid(&next, end, &new_oid, "create", refname, 0)) 416 die("create %s: missing ", refname); 417 418 if (is_null_oid(&new_oid)) 419 die("create %s: zero ", refname); 420 421 if (*next != line_termination) 422 die("create %s: extra input: %s", refname, next); 423 424 tx_err = ref_transaction_create(transaction, refname, &new_oid, NULL, 425 update_flags | create_reflog_flag, 426 msg, &err); 427 handle_ref_transaction_error(refname, &new_oid, NULL, NULL, NULL, tx_err, 428 &err, opts); 429 430 update_flags = default_flags; 431 free(refname); 432 strbuf_release(&err); 433 } 434 435 static void parse_cmd_symref_create(struct ref_transaction *transaction, 436 const char *next, const char *end UNUSED, 437 struct command_options *opts) 438 { 439 struct strbuf err = STRBUF_INIT; 440 char *refname, *new_target; 441 enum ref_transaction_error tx_err; 442 443 refname = parse_refname(&next); 444 if (!refname) 445 die("symref-create: missing "); 446 447 new_target = parse_next_refname(&next); 448 if (!new_target) 449 die("symref-create %s: missing ", refname); 450 451 if (*next != line_termination) 452 die("symref-create %s: extra input: %s", refname, next); 453 454 tx_err = ref_transaction_create(transaction, refname, NULL, new_target, 455 update_flags | create_reflog_flag, 456 msg, &err); 457 handle_ref_transaction_error(refname, NULL, NULL, new_target, NULL, 458 tx_err, &err, opts); 459 460 update_flags = default_flags; 461 free(refname); 462 free(new_target); 463 strbuf_release(&err); 464 } 465 466 static void parse_cmd_delete(struct ref_transaction *transaction, 467 const char *next, const char *end, 468 struct command_options *opts UNUSED) 469 { 470 struct strbuf err = STRBUF_INIT; 471 char *refname; 472 struct object_id old_oid; 473 int have_old; 474 475 refname = parse_refname(&next); 476 if (!refname) 477 die("delete: missing "); 478 479 if (parse_next_oid(&next, end, &old_oid, "delete", refname, 480 PARSE_SHA1_OLD)) { 481 have_old = 0; 482 } else { 483 if (is_null_oid(&old_oid)) 484 die("delete %s: zero ", refname); 485 have_old = 1; 486 } 487 488 if (*next != line_termination) 489 die("delete %s: extra input: %s", refname, next); 490 491 if (ref_transaction_delete(transaction, refname, 492 have_old ? &old_oid : NULL, 493 NULL, update_flags, msg, &err)) 494 die("%s", err.buf); 495 496 update_flags = default_flags; 497 free(refname); 498 strbuf_release(&err); 499 } 500 501 static void parse_cmd_symref_delete(struct ref_transaction *transaction, 502 const char *next, const char *end UNUSED, 503 struct command_options *opts UNUSED) 504 { 505 struct strbuf err = STRBUF_INIT; 506 char *refname, *old_target; 507 508 if (!(update_flags & REF_NO_DEREF)) 509 die("symref-delete: cannot operate with deref mode"); 510 511 refname = parse_refname(&next); 512 if (!refname) 513 die("symref-delete: missing "); 514 515 old_target = parse_next_refname(&next); 516 517 if (*next != line_termination) 518 die("symref-delete %s: extra input: %s", refname, next); 519 520 if (ref_transaction_delete(transaction, refname, NULL, 521 old_target, update_flags, msg, &err)) 522 die("%s", err.buf); 523 524 update_flags = default_flags; 525 free(refname); 526 free(old_target); 527 strbuf_release(&err); 528 } 529 530 static void parse_cmd_verify(struct ref_transaction *transaction, 531 const char *next, const char *end, 532 struct command_options *opts UNUSED) 533 { 534 struct strbuf err = STRBUF_INIT; 535 char *refname; 536 struct object_id old_oid; 537 538 refname = parse_refname(&next); 539 if (!refname) 540 die("verify: missing "); 541 542 if (parse_next_oid(&next, end, &old_oid, "verify", refname, 543 PARSE_SHA1_OLD)) 544 oidclr(&old_oid, the_repository->hash_algo); 545 546 if (*next != line_termination) 547 die("verify %s: extra input: %s", refname, next); 548 549 if (ref_transaction_verify(transaction, refname, &old_oid, 550 NULL, update_flags, &err)) 551 die("%s", err.buf); 552 553 update_flags = default_flags; 554 free(refname); 555 strbuf_release(&err); 556 } 557 558 static void parse_cmd_symref_verify(struct ref_transaction *transaction, 559 const char *next, const char *end UNUSED, 560 struct command_options *opts UNUSED) 561 { 562 struct strbuf err = STRBUF_INIT; 563 struct object_id old_oid; 564 char *refname, *old_target; 565 566 if (!(update_flags & REF_NO_DEREF)) 567 die("symref-verify: cannot operate with deref mode"); 568 569 refname = parse_refname(&next); 570 if (!refname) 571 die("symref-verify: missing "); 572 573 /* 574 * old_ref is optional, if not provided, we need to ensure that the 575 * ref doesn't exist. 576 */ 577 old_target = parse_next_refname(&next); 578 if (!old_target) 579 oidcpy(&old_oid, null_oid(the_hash_algo)); 580 581 if (*next != line_termination) 582 die("symref-verify %s: extra input: %s", refname, next); 583 584 if (ref_transaction_verify(transaction, refname, 585 old_target ? NULL : &old_oid, 586 old_target, update_flags, &err)) 587 die("%s", err.buf); 588 589 update_flags = default_flags; 590 free(refname); 591 free(old_target); 592 strbuf_release(&err); 593 } 594 595 static void report_ok(const char *command) 596 { 597 fprintf(stdout, "%s: ok\n", command); 598 fflush(stdout); 599 } 600 601 static void parse_cmd_option(struct ref_transaction *transaction UNUSED, 602 const char *next, const char *end UNUSED, 603 struct command_options *opts UNUSED) 604 { 605 const char *rest; 606 if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination) 607 update_flags |= REF_NO_DEREF; 608 else 609 die("option unknown: %s", next); 610 } 611 612 static void parse_cmd_start(struct ref_transaction *transaction UNUSED, 613 const char *next, const char *end UNUSED, 614 struct command_options *opts UNUSED) 615 { 616 if (*next != line_termination) 617 die("start: extra input: %s", next); 618 report_ok("start"); 619 } 620 621 static void parse_cmd_prepare(struct ref_transaction *transaction, 622 const char *next, const char *end UNUSED, 623 struct command_options *opts UNUSED) 624 { 625 struct strbuf error = STRBUF_INIT; 626 if (*next != line_termination) 627 die("prepare: extra input: %s", next); 628 if (ref_transaction_prepare(transaction, &error)) 629 die("prepare: %s", error.buf); 630 report_ok("prepare"); 631 } 632 633 static void parse_cmd_abort(struct ref_transaction *transaction, 634 const char *next, const char *end UNUSED, 635 struct command_options *opts UNUSED) 636 { 637 struct strbuf error = STRBUF_INIT; 638 if (*next != line_termination) 639 die("abort: extra input: %s", next); 640 if (ref_transaction_abort(transaction, &error)) 641 die("abort: %s", error.buf); 642 report_ok("abort"); 643 } 644 645 static void parse_cmd_commit(struct ref_transaction *transaction, 646 const char *next, const char *end UNUSED, 647 struct command_options *opts UNUSED) 648 { 649 struct strbuf error = STRBUF_INIT; 650 if (*next != line_termination) 651 die("commit: extra input: %s", next); 652 if (ref_transaction_commit(transaction, &error)) 653 die("commit: %s", error.buf); 654 655 ref_transaction_for_each_rejected_update(transaction, 656 print_rejected_refs, NULL); 657 658 report_ok("commit"); 659 ref_transaction_free(transaction); 660 } 661 662 enum update_refs_state { 663 /* Non-transactional state open for updates. */ 664 UPDATE_REFS_OPEN, 665 /* A transaction has been started. */ 666 UPDATE_REFS_STARTED, 667 /* References are locked and ready for commit */ 668 UPDATE_REFS_PREPARED, 669 /* Transaction has been committed or closed. */ 670 UPDATE_REFS_CLOSED, 671 }; 672 673 static const struct parse_cmd { 674 const char *prefix; 675 void (*fn)(struct ref_transaction *, const char *, const char *, 676 struct command_options *); 677 unsigned args; 678 enum update_refs_state state; 679 } command[] = { 680 { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN }, 681 { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, 682 { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, 683 { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, 684 { "symref-update", parse_cmd_symref_update, 4, UPDATE_REFS_OPEN }, 685 { "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN }, 686 { "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN }, 687 { "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN }, 688 { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, 689 { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED }, 690 { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED }, 691 { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED }, 692 { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED }, 693 }; 694 695 static void update_refs_stdin(unsigned int flags) 696 { 697 struct strbuf input = STRBUF_INIT, err = STRBUF_INIT; 698 enum update_refs_state state = UPDATE_REFS_OPEN; 699 struct ref_transaction *transaction; 700 int i, j; 701 702 struct command_options opts = { 703 .allow_update_failures = flags & REF_TRANSACTION_ALLOW_FAILURE, 704 }; 705 706 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 707 flags, &err); 708 if (!transaction) 709 die("%s", err.buf); 710 711 /* Read each line dispatch its command */ 712 while (!strbuf_getwholeline(&input, stdin, line_termination)) { 713 const struct parse_cmd *cmd = NULL; 714 715 if (*input.buf == line_termination) 716 die("empty command in input"); 717 else if (isspace(*input.buf)) 718 die("whitespace before command: %s", input.buf); 719 720 for (i = 0; i < ARRAY_SIZE(command); i++) { 721 const char *prefix = command[i].prefix; 722 char c; 723 724 if (!starts_with(input.buf, prefix)) 725 continue; 726 727 /* 728 * If the command has arguments, verify that it's 729 * followed by a space. Otherwise, it shall be followed 730 * by a line terminator. 731 */ 732 c = command[i].args ? ' ' : line_termination; 733 if (input.buf[strlen(prefix)] != c) 734 continue; 735 736 cmd = &command[i]; 737 break; 738 } 739 if (!cmd) 740 die("unknown command: %s", input.buf); 741 742 /* 743 * Read additional arguments if NUL-terminated. Do not raise an 744 * error in case there is an early EOF to let the command 745 * handle missing arguments with a proper error message. 746 */ 747 for (j = 1; line_termination == '\0' && j < cmd->args; j++) 748 if (strbuf_appendwholeline(&input, stdin, line_termination)) 749 break; 750 751 switch (state) { 752 case UPDATE_REFS_OPEN: 753 case UPDATE_REFS_STARTED: 754 if (state == UPDATE_REFS_STARTED && cmd->state == UPDATE_REFS_STARTED) 755 die("cannot restart ongoing transaction"); 756 /* Do not downgrade a transaction to a non-transaction. */ 757 if (cmd->state >= state) 758 state = cmd->state; 759 break; 760 case UPDATE_REFS_PREPARED: 761 if (cmd->state != UPDATE_REFS_CLOSED) 762 die("prepared transactions can only be closed"); 763 state = cmd->state; 764 break; 765 case UPDATE_REFS_CLOSED: 766 if (cmd->state != UPDATE_REFS_STARTED) 767 die("transaction is closed"); 768 769 /* 770 * Open a new transaction if we're currently closed and 771 * get a "start". 772 */ 773 state = cmd->state; 774 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 775 flags, &err); 776 if (!transaction) 777 die("%s", err.buf); 778 779 break; 780 } 781 782 cmd->fn(transaction, input.buf + strlen(cmd->prefix) + !!cmd->args, 783 input.buf + input.len, &opts); 784 } 785 786 switch (state) { 787 case UPDATE_REFS_OPEN: 788 /* Commit by default if no transaction was requested. */ 789 if (ref_transaction_commit(transaction, &err)) 790 die("%s", err.buf); 791 ref_transaction_for_each_rejected_update(transaction, 792 print_rejected_refs, NULL); 793 ref_transaction_free(transaction); 794 break; 795 case UPDATE_REFS_STARTED: 796 case UPDATE_REFS_PREPARED: 797 /* If using a transaction, we want to abort it. */ 798 if (ref_transaction_abort(transaction, &err)) 799 die("%s", err.buf); 800 break; 801 case UPDATE_REFS_CLOSED: 802 /* Otherwise no need to do anything, the transaction was closed already. */ 803 break; 804 } 805 806 strbuf_release(&err); 807 strbuf_release(&input); 808 } 809 810 int cmd_update_ref(int argc, 811 const char **argv, 812 const char *prefix, 813 struct repository *repo UNUSED) 814 { 815 const char *refname, *oldval; 816 struct object_id oid, oldoid; 817 int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0; 818 int create_reflog = 0; 819 unsigned int flags = 0; 820 821 struct option options[] = { 822 OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")), 823 OPT_BOOL('d', NULL, &delete, N_("delete the reference")), 824 OPT_BOOL( 0 , "no-deref", &no_deref, 825 N_("update not the one it points to")), 826 OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")), 827 OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")), 828 OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create a reflog")), 829 OPT_BIT('0', "batch-updates", &flags, N_("batch reference updates"), 830 REF_TRANSACTION_ALLOW_FAILURE), 831 OPT_END(), 832 }; 833 834 repo_config(the_repository, git_default_config, NULL); 835 argc = parse_options(argc, argv, prefix, options, git_update_ref_usage, 836 0); 837 if (msg && !*msg) 838 die("Refusing to perform update with empty message."); 839 840 create_reflog_flag = create_reflog ? REF_FORCE_CREATE_REFLOG : 0; 841 842 if (no_deref) { 843 default_flags = REF_NO_DEREF; 844 update_flags = default_flags; 845 } 846 847 if (read_stdin) { 848 if (delete || argc > 0) 849 usage_with_options(git_update_ref_usage, options); 850 if (end_null) 851 line_termination = '\0'; 852 update_refs_stdin(flags); 853 return 0; 854 } else if (flags & REF_TRANSACTION_ALLOW_FAILURE) { 855 die("--batch-updates can only be used with --stdin"); 856 } 857 858 if (end_null) 859 usage_with_options(git_update_ref_usage, options); 860 861 if (delete) { 862 if (argc < 1 || argc > 2) 863 usage_with_options(git_update_ref_usage, options); 864 refname = argv[0]; 865 oldval = argv[1]; 866 } else { 867 const char *value; 868 if (argc < 2 || argc > 3) 869 usage_with_options(git_update_ref_usage, options); 870 refname = argv[0]; 871 value = argv[1]; 872 oldval = argv[2]; 873 if (repo_get_oid_with_flags(the_repository, value, &oid, 874 GET_OID_SKIP_AMBIGUITY_CHECK)) 875 die("%s: not a valid SHA1", value); 876 } 877 878 if (oldval) { 879 if (!*oldval) 880 /* 881 * The empty string implies that the reference 882 * must not already exist: 883 */ 884 oidclr(&oldoid, the_repository->hash_algo); 885 else if (repo_get_oid_with_flags(the_repository, oldval, &oldoid, 886 GET_OID_SKIP_AMBIGUITY_CHECK)) 887 die("%s: not a valid old SHA1", oldval); 888 } 889 890 if (delete) 891 /* 892 * For purposes of backwards compatibility, we treat 893 * NULL_SHA1 as "don't care" here: 894 */ 895 return refs_delete_ref(get_main_ref_store(the_repository), 896 msg, refname, 897 (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL, 898 default_flags); 899 else 900 return refs_update_ref(get_main_ref_store(the_repository), 901 msg, refname, &oid, 902 oldval ? &oldoid : NULL, 903 default_flags | create_reflog_flag, 904 UPDATE_REFS_DIE_ON_ERR); 905 }