lock_ref_sha1_basic(): report errors via a "struct strbuf *err"

For now, change the callers to spew the error to stderr like before. But soon we will change them to incorporate the reason for the failure into their own error messages. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>

Michael Haggerty committed May 11, 2015 at 17:25 UTC 4a32b2e08be8daf965949956e8ea16718797a031
1 file changed +28 -16
refs.c
+28 -16
@@ -2333,7 +2333,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2333 const unsigned char *old_sha1,
2334 const struct string_list *extras,
2335 const struct string_list *skip,
2336 - unsigned int flags, int *type_p)
2336 + unsigned int flags, int *type_p,
2337 + struct strbuf *err)
2338 {
2339 char *ref_file;
2340 const char *orig_refname = refname;
@@ -2343,7 +2344,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2344 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
2345 int resolve_flags = 0;
2346 int attempts_remaining = 3;
2346 - struct strbuf err = STRBUF_INIT;
2347 +
2348 + assert(err);
2349
2350 lock = xcalloc(1, sizeof(struct ref_lock));
2351 lock->lock_fd = -1;
@@ -2367,7 +2369,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2369 ref_file = git_path("%s", orig_refname);
2370 if (remove_empty_directories(ref_file)) {
2371 last_errno = errno;
2370 - error("there are still refs under '%s'", orig_refname);
2372 + strbuf_addf(err, "there are still refs under '%s'",
2373 + orig_refname);
2374 goto error_return;
2375 }
2376 refname = resolve_ref_unsafe(orig_refname, resolve_flags,
@@ -2377,8 +2380,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2380 *type_p = type;
2381 if (!refname) {
2382 last_errno = errno;
2380 - error("unable to resolve reference %s: %s",
2381 - orig_refname, strerror(errno));
2383 + strbuf_addf(err, "unable to resolve reference %s: %s",
2384 + orig_refname, strerror(errno));
2385 goto error_return;
2386 }
2387 /*
@@ -2389,8 +2392,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2392 */
2393 if (is_null_sha1(lock->old_sha1) &&
2394 verify_refname_available(refname, extras, skip,
2392 - get_packed_refs(&ref_cache), &err)) {
2393 - error("%s", err.buf);
2395 + get_packed_refs(&ref_cache), err)) {
2396 last_errno = ENOTDIR;
2397 goto error_return;
2398 }
@@ -2416,7 +2418,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2418 /* fall through */
2419 default:
2420 last_errno = errno;
2419 - error("unable to create directory for %s", ref_file);
2421 + strbuf_addf(err, "unable to create directory for %s", ref_file);
2422 goto error_return;
2423 }
2424
@@ -2431,8 +2433,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2433 */
2434 goto retry;
2435 else {
2434 - unable_to_lock_message(ref_file, errno, &err);
2435 - error("%s", err.buf);
2436 + unable_to_lock_message(ref_file, errno, err);
2437 goto error_return;
2438 }
2439 }
@@ -2440,7 +2441,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2441
2442 error_return:
2443 unlock_ref(lock);
2443 - strbuf_release(&err);
2444 errno = last_errno;
2445 return NULL;
2446 }
@@ -2854,6 +2854,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2854 struct stat loginfo;
2855 int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
2856 const char *symref = NULL;
2857 + struct strbuf err = STRBUF_INIT;
2858
2859 if (log && S_ISLNK(loginfo.st_mode))
2860 return error("reflog for %s is a symlink", oldrefname);
@@ -2896,8 +2897,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2897
2898 logmoved = log;
2899
2899 - lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL);
2900 + lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL, &err);
2901 if (!lock) {
2902 + error("%s", err.buf);
2903 + strbuf_release(&err);
2904 error("unable to lock %s for update", newrefname);
2905 goto rollback;
2906 }
@@ -2910,8 +2913,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2913 return 0;
2914
2915 rollback:
2913 - lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL);
2916 + lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL, &err);
2917 if (!lock) {
2918 + error("%s", err.buf);
2919 + strbuf_release(&err);
2920 error("unable to lock %s for rollback", oldrefname);
2921 goto rollbacklog;
2922 }
@@ -3824,11 +3829,14 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3829 update->old_sha1 : NULL),
3830 &affected_refnames, NULL,
3831 flags,
3827 - &update->type);
3832 + &update->type,
3833 + err);
3834 if (!update->lock) {
3835 ret = (errno == ENOTDIR)
3836 ? TRANSACTION_NAME_CONFLICT
3837 : TRANSACTION_GENERIC_ERROR;
3838 + error("%s", err->buf);
3839 + strbuf_reset(err);
3840 strbuf_addf(err, "Cannot lock the ref '%s'.",
3841 update->refname);
3842 goto cleanup;
@@ -4088,6 +4096,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
4096 char *log_file;
4097 int status = 0;
4098 int type;
4099 + struct strbuf err = STRBUF_INIT;
4100
4101 memset(&cb, 0, sizeof(cb));
4102 cb.flags = flags;
@@ -4099,9 +4108,12 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
4108 * reference itself, plus we might need to update the
4109 * reference if --updateref was specified:
4110 */
4102 - lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type);
4103 - if (!lock)
4111 + lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type, &err);
4112 + if (!lock) {
4113 + error("%s", err.buf);
4114 + strbuf_release(&err);
4115 return error("cannot lock ref '%s'", refname);
4116 + }
4117 if (!reflog_exists(refname)) {
4118 unlock_ref(lock);
4119 return 0;