Skip to content

Commit 829f03e

Browse files
committed
Merge branch 'mh/verify-lock-error-report'
Bring consistency to error reporting mechanism used in "refs" API. * mh/verify-lock-error-report: ref_transaction_commit(): do not capitalize error messages verify_lock(): do not capitalize error messages verify_lock(): report errors via a strbuf verify_lock(): on errors, let the caller unlock the lock verify_lock(): return 0/-1 rather than struct ref_lock *
2 parents db65170 + c2e0a71 commit 829f03e

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

refs.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,27 +2219,35 @@ static void unlock_ref(struct ref_lock *lock)
22192219
free(lock);
22202220
}
22212221

2222-
/* This function should make sure errno is meaningful on error */
2223-
static struct ref_lock *verify_lock(struct ref_lock *lock,
2224-
const unsigned char *old_sha1, int mustexist)
2222+
/*
2223+
* Verify that the reference locked by lock has the value old_sha1.
2224+
* Fail if the reference doesn't exist and mustexist is set. Return 0
2225+
* on success. On error, write an error message to err, set errno, and
2226+
* return a negative value.
2227+
*/
2228+
static int verify_lock(struct ref_lock *lock,
2229+
const unsigned char *old_sha1, int mustexist,
2230+
struct strbuf *err)
22252231
{
2232+
assert(err);
2233+
22262234
if (read_ref_full(lock->ref_name,
22272235
mustexist ? RESOLVE_REF_READING : 0,
22282236
lock->old_oid.hash, NULL)) {
22292237
int save_errno = errno;
2230-
error("Can't verify ref %s", lock->ref_name);
2231-
unlock_ref(lock);
2238+
strbuf_addf(err, "can't verify ref %s", lock->ref_name);
22322239
errno = save_errno;
2233-
return NULL;
2240+
return -1;
22342241
}
22352242
if (hashcmp(lock->old_oid.hash, old_sha1)) {
2236-
error("Ref %s is at %s but expected %s", lock->ref_name,
2237-
oid_to_hex(&lock->old_oid), sha1_to_hex(old_sha1));
2238-
unlock_ref(lock);
2243+
strbuf_addf(err, "ref %s is at %s but expected %s",
2244+
lock->ref_name,
2245+
sha1_to_hex(lock->old_oid.hash),
2246+
sha1_to_hex(old_sha1));
22392247
errno = EBUSY;
2240-
return NULL;
2248+
return -1;
22412249
}
2242-
return lock;
2250+
return 0;
22432251
}
22442252

22452253
static int remove_empty_directories(const char *file)
@@ -2467,7 +2475,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24672475
goto error_return;
24682476
}
24692477
}
2470-
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
2478+
if (old_sha1 && verify_lock(lock, old_sha1, mustexist, err)) {
2479+
last_errno = errno;
2480+
goto error_return;
2481+
}
2482+
return lock;
24712483

24722484
error_return:
24732485
unlock_ref(lock);
@@ -3912,7 +3924,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
39123924
? TRANSACTION_NAME_CONFLICT
39133925
: TRANSACTION_GENERIC_ERROR;
39143926
reason = strbuf_detach(err, NULL);
3915-
strbuf_addf(err, "Cannot lock ref '%s': %s",
3927+
strbuf_addf(err, "cannot lock ref '%s': %s",
39163928
update->refname, reason);
39173929
free(reason);
39183930
goto cleanup;
@@ -3935,7 +3947,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
39353947
* write_ref_to_lockfile():
39363948
*/
39373949
update->lock = NULL;
3938-
strbuf_addf(err, "Cannot update the ref '%s'.",
3950+
strbuf_addf(err, "cannot update the ref '%s'.",
39393951
update->refname);
39403952
ret = TRANSACTION_GENERIC_ERROR;
39413953
goto cleanup;

t/t1400-update-ref.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ test_expect_success 'stdin create ref works with path with space to blob' '
519519
test_expect_success 'stdin update ref fails with wrong old value' '
520520
echo "update $c $m $m~1" >stdin &&
521521
test_must_fail git update-ref --stdin <stdin 2>err &&
522-
grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
522+
grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
523523
test_must_fail git rev-parse --verify -q $c
524524
'
525525

@@ -555,7 +555,7 @@ test_expect_success 'stdin update ref works with right old value' '
555555
test_expect_success 'stdin delete ref fails with wrong old value' '
556556
echo "delete $a $m~1" >stdin &&
557557
test_must_fail git update-ref --stdin <stdin 2>err &&
558-
grep "fatal: Cannot lock ref '"'"'$a'"'"'" err &&
558+
grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
559559
git rev-parse $m >expect &&
560560
git rev-parse $a >actual &&
561561
test_cmp expect actual
@@ -688,7 +688,7 @@ test_expect_success 'stdin update refs fails with wrong old value' '
688688
update $c ''
689689
EOF
690690
test_must_fail git update-ref --stdin <stdin 2>err &&
691-
grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
691+
grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
692692
git rev-parse $m >expect &&
693693
git rev-parse $a >actual &&
694694
test_cmp expect actual &&
@@ -883,7 +883,7 @@ test_expect_success 'stdin -z create ref works with path with space to blob' '
883883
test_expect_success 'stdin -z update ref fails with wrong old value' '
884884
printf $F "update $c" "$m" "$m~1" >stdin &&
885885
test_must_fail git update-ref -z --stdin <stdin 2>err &&
886-
grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
886+
grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
887887
test_must_fail git rev-parse --verify -q $c
888888
'
889889

@@ -899,7 +899,7 @@ test_expect_success 'stdin -z create ref fails when ref exists' '
899899
git rev-parse "$c" >expect &&
900900
printf $F "create $c" "$m~1" >stdin &&
901901
test_must_fail git update-ref -z --stdin <stdin 2>err &&
902-
grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
902+
grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
903903
git rev-parse "$c" >actual &&
904904
test_cmp expect actual
905905
'
@@ -930,7 +930,7 @@ test_expect_success 'stdin -z update ref works with right old value' '
930930
test_expect_success 'stdin -z delete ref fails with wrong old value' '
931931
printf $F "delete $a" "$m~1" >stdin &&
932932
test_must_fail git update-ref -z --stdin <stdin 2>err &&
933-
grep "fatal: Cannot lock ref '"'"'$a'"'"'" err &&
933+
grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
934934
git rev-parse $m >expect &&
935935
git rev-parse $a >actual &&
936936
test_cmp expect actual
@@ -1045,7 +1045,7 @@ test_expect_success 'stdin -z update refs fails with wrong old value' '
10451045
git update-ref $c $m &&
10461046
printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
10471047
test_must_fail git update-ref -z --stdin <stdin 2>err &&
1048-
grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
1048+
grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
10491049
git rev-parse $m >expect &&
10501050
git rev-parse $a >actual &&
10511051
test_cmp expect actual &&

0 commit comments

Comments
 (0)