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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
May 23, 2015 at 01:34 UTC
33ffc176d65e1e98e1eea262dc7827830c33c56d
1 file changed
+12
-7
refs.c
+12
-7
@@ -2221,23 +2221,28 @@ static void unlock_ref(struct ref_lock *lock)
2221
/*
2222
* Verify that the reference locked by lock has the value old_sha1.
2223
* 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.
2226
*/
2227
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)
2230
{
2231
+ assert(err);
2232
+
2233
if (read_ref_full(lock->ref_name,
2234
mustexist ? RESOLVE_REF_READING : 0,
2235
lock->old_sha1, NULL)) {
2236
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);
2238
errno = save_errno;
2239
return -1;
2240
}
2241
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));
2246
errno = EBUSY;
2247
return -1;
2248
}
@@ -2469,7 +2474,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2474
goto error_return;
2475
}
2476
}
2472
- if (old_sha1 && verify_lock(lock, old_sha1, mustexist)) {
2477
+ if (old_sha1 && verify_lock(lock, old_sha1, mustexist, err)) {
2478
last_errno = errno;
2479
goto error_return;
2480
}