Skip to content

Commit 33ffc17

Browse files
mhaggergitster
authored andcommitted
verify_lock(): report errors via a strbuf
Instead of writing error messages directly to stderr, write them to a "strbuf *err". The caller, lock_ref_sha1_basic(), uses this error reporting convention with all the other callees, and reports its error this way to its callers. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f41d632 commit 33ffc17

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

refs.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,23 +2221,28 @@ static void unlock_ref(struct ref_lock *lock)
22212221
/*
22222222
* Verify that the reference locked by lock has the value old_sha1.
22232223
* Fail if the reference doesn't exist and mustexist is set. Return 0
2224-
* on success or a negative value on error. This function should make
2225-
* sure errno is meaningful on error.
2224+
* on success. On error, write an error message to err, set errno, and
2225+
* return a negative value.
22262226
*/
22272227
static int verify_lock(struct ref_lock *lock,
2228-
const unsigned char *old_sha1, int mustexist)
2228+
const unsigned char *old_sha1, int mustexist,
2229+
struct strbuf *err)
22292230
{
2231+
assert(err);
2232+
22302233
if (read_ref_full(lock->ref_name,
22312234
mustexist ? RESOLVE_REF_READING : 0,
22322235
lock->old_sha1, NULL)) {
22332236
int save_errno = errno;
2234-
error("Can't verify ref %s", lock->ref_name);
2237+
strbuf_addf(err, "Can't verify ref %s", lock->ref_name);
22352238
errno = save_errno;
22362239
return -1;
22372240
}
22382241
if (hashcmp(lock->old_sha1, old_sha1)) {
2239-
error("Ref %s is at %s but expected %s", lock->ref_name,
2240-
sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
2242+
strbuf_addf(err, "Ref %s is at %s but expected %s",
2243+
lock->ref_name,
2244+
sha1_to_hex(lock->old_sha1),
2245+
sha1_to_hex(old_sha1));
22412246
errno = EBUSY;
22422247
return -1;
22432248
}
@@ -2469,7 +2474,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24692474
goto error_return;
24702475
}
24712476
}
2472-
if (old_sha1 && verify_lock(lock, old_sha1, mustexist)) {
2477+
if (old_sha1 && verify_lock(lock, old_sha1, mustexist, err)) {
24732478
last_errno = errno;
24742479
goto error_return;
24752480
}

0 commit comments

Comments
 (0)