* Lock refname, without following symrefs, and set *lock_p to point * at a newly-allocated lock object. Fill in lock->old_oid, referent, * and type similarly to read_raw_ref(). * * The caller must verify that refname is a "safe" reference name (in * the sense of refname_is_safe()) before calling this function. * * If the reference doesn't already exist, verify that refname doesn't * have a
| 705 | * - Generate informative error messages in the case of failure |
| 706 | */ |
| 707 | static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs, |
| 708 | struct ref_transaction *transaction, |
| 709 | size_t update_idx, |
| 710 | int mustexist, |
| 711 | struct string_list *refnames_to_check, |
| 712 | struct ref_lock **lock_p, |
| 713 | struct strbuf *referent, |
| 714 | struct strbuf *err) |
| 715 | { |
| 716 | enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC; |
| 717 | struct ref_update *update = transaction->updates[update_idx]; |
| 718 | const struct string_list *extras = &transaction->refnames; |
| 719 | const char *refname = update->refname; |
| 720 | unsigned int *type = &update->type; |
| 721 | struct ref_lock *lock; |
| 722 | struct strbuf ref_file = STRBUF_INIT; |
| 723 | int attempts_remaining = 3; |
| 724 | int failure_errno; |
| 725 | |
| 726 | assert(err); |
| 727 | files_assert_main_repository(refs, "lock_raw_ref"); |
| 728 | |
| 729 | *type = 0; |
| 730 | |
| 731 | /* First lock the file so it can't change out from under us. */ |
| 732 | |
| 733 | *lock_p = CALLOC_ARRAY(lock, 1); |
| 734 | |
| 735 | lock->ref_name = xstrdup(refname); |
| 736 | lock->count = 1; |
| 737 | files_ref_path(refs, &ref_file, refname); |
| 738 | |
| 739 | retry: |
| 740 | switch (safe_create_leading_directories(the_repository, ref_file.buf)) { |
| 741 | case SCLD_OK: |
| 742 | break; /* success */ |
| 743 | case SCLD_EXISTS: |
| 744 | /* |
| 745 | * Suppose refname is "refs/foo/bar". We just failed |
| 746 | * to create the containing directory, "refs/foo", |
| 747 | * because there was a non-directory in the way. This |
| 748 | * indicates a D/F conflict, probably because of |
| 749 | * another reference such as "refs/foo". There is no |
| 750 | * reason to expect this error to be transitory. |
| 751 | */ |
| 752 | if (refs_verify_refname_available(&refs->base, refname, |
| 753 | extras, NULL, 0, err)) { |
| 754 | if (mustexist) { |
| 755 | /* |
| 756 | * To the user the relevant error is |
| 757 | * that the "mustexist" reference is |
| 758 | * missing: |
| 759 | */ |
| 760 | strbuf_reset(err); |
| 761 | strbuf_addf(err, "unable to resolve reference '%s'", |
| 762 | refname); |
| 763 | ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF; |
| 764 | } else { |
no test coverage detected