lock_ref_for_update(): don't re-read non-symbolic references

Before the previous patch, our first read of the reference happened before the reference was locked, so we couldn't trust its value and had to read it again. But now that our first read of the reference happens after acquiring the lock, there is no need to read it a second time. So move the read_ref_full() call into the (update->type & REF_ISSYMREF) block. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>

Michael Haggerty committed Apr 25, 2016 at 17:38 UTC 8169d0d06ad721aa54d95f044f4b097d79151ea2
1 file changed +30 -18
refs/files-backend.c
+30 -18
@@ -3436,33 +3436,45 @@ static int lock_ref_for_update(struct ref_update *update,
3436
3437 lock = update->lock;
3438
3439 - if (read_ref_full(update->refname,
3440 - mustexist ? RESOLVE_REF_READING : 0,
3441 - lock->old_oid.hash, NULL)) {
3442 - if (update->flags & REF_HAVE_OLD) {
3443 - strbuf_addf(err, "cannot lock ref '%s': can't resolve old value",
3444 - update->refname);
3439 + if (update->type & REF_ISSYMREF) {
3440 + if (read_ref_full(update->refname,
3441 + mustexist ? RESOLVE_REF_READING : 0,
3442 + lock->old_oid.hash, NULL)) {
3443 + if (update->flags & REF_HAVE_OLD) {
3444 + strbuf_addf(err, "cannot lock ref '%s': can't resolve old value",
3445 + update->refname);
3446 + return TRANSACTION_GENERIC_ERROR;
3447 + } else {
3448 + hashclr(lock->old_oid.hash);
3449 + }
3450 + }
3451 + if ((update->flags & REF_HAVE_OLD) &&
3452 + hashcmp(lock->old_oid.hash, update->old_sha1)) {
3453 + strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
3454 + update->refname,
3455 + sha1_to_hex(lock->old_oid.hash),
3456 + sha1_to_hex(update->old_sha1));
3457 return TRANSACTION_GENERIC_ERROR;
3446 - } else {
3447 - hashclr(lock->old_oid.hash);
3458 }
3449 - }
3450 - if ((update->flags & REF_HAVE_OLD) &&
3451 - hashcmp(lock->old_oid.hash, update->old_sha1)) {
3452 - strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
3453 - update->refname,
3454 - sha1_to_hex(lock->old_oid.hash),
3455 - sha1_to_hex(update->old_sha1));
3456 - return TRANSACTION_GENERIC_ERROR;
3457 - }
3459
3459 - if (update->type & REF_ISSYMREF) {
3460 if (!(update->flags & REF_NODEREF)) {
3461 ret = split_symref_update(update, referent.buf, transaction,
3462 affected_refnames, err);
3463 if (ret)
3464 return ret;
3465 }
3466 + } else if ((update->flags & REF_HAVE_OLD) &&
3467 + hashcmp(lock->old_oid.hash, update->old_sha1)) {
3468 + if (is_null_sha1(update->old_sha1))
3469 + strbuf_addf(err, "cannot lock ref '%s': reference already exists",
3470 + update->refname);
3471 + else
3472 + strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
3473 + update->refname,
3474 + sha1_to_hex(lock->old_oid.hash),
3475 + sha1_to_hex(update->old_sha1));
3476 +
3477 + return TRANSACTION_GENERIC_ERROR;
3478 }
3479
3480 if ((update->flags & REF_HAVE_NEW) &&