reflog_expire(): integrate lock_ref_sha1_basic() errors into ours
[gitweb.git] / refs.c
diff --git a/refs.c b/refs.c
index ff9b9cf92a7696dcb4d009f1c511a42641aad4cc..97043fd2ef88fd50dadf05d673bb80018d3caf06 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2369,8 +2369,12 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                ref_file = git_path("%s", orig_refname);
                if (remove_empty_directories(ref_file)) {
                        last_errno = errno;
-                       strbuf_addf(err, "there are still refs under '%s'",
-                                   orig_refname);
+
+                       if (!verify_refname_available(orig_refname, extras, skip,
+                                                     get_loose_refs(&ref_cache), err))
+                               strbuf_addf(err, "there are still refs under '%s'",
+                                           orig_refname);
+
                        goto error_return;
                }
                refname = resolve_ref_unsafe(orig_refname, resolve_flags,
@@ -2380,8 +2384,12 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
            *type_p = type;
        if (!refname) {
                last_errno = errno;
-               strbuf_addf(err, "unable to resolve reference %s: %s",
-                           orig_refname, strerror(errno));
+               if (last_errno != ENOTDIR ||
+                   !verify_refname_available(orig_refname, extras, skip,
+                                             get_loose_refs(&ref_cache), err))
+                       strbuf_addf(err, "unable to resolve reference %s: %s",
+                                   orig_refname, strerror(last_errno));
+
                goto error_return;
        }
        /*
@@ -2899,9 +2907,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 
        lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL, &err);
        if (!lock) {
-               error("%s", err.buf);
+               error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
                strbuf_release(&err);
-               error("unable to lock %s for update", newrefname);
                goto rollback;
        }
        hashcpy(lock->old_sha1, orig_sha1);
@@ -2915,9 +2922,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
  rollback:
        lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL, &err);
        if (!lock) {
-               error("%s", err.buf);
+               error("unable to lock %s for rollback: %s", oldrefname, err.buf);
                strbuf_release(&err);
-               error("unable to lock %s for rollback", oldrefname);
                goto rollbacklog;
        }
 
@@ -3832,13 +3838,15 @@ int ref_transaction_commit(struct ref_transaction *transaction,
                                &update->type,
                                err);
                if (!update->lock) {
+                       char *reason;
+
                        ret = (errno == ENOTDIR)
                                ? TRANSACTION_NAME_CONFLICT
                                : TRANSACTION_GENERIC_ERROR;
-                       error("%s", err->buf);
-                       strbuf_reset(err);
-                       strbuf_addf(err, "Cannot lock the ref '%s'.",
-                                   update->refname);
+                       reason = strbuf_detach(err, NULL);
+                       strbuf_addf(err, "Cannot lock ref '%s': %s",
+                                   update->refname, reason);
+                       free(reason);
                        goto cleanup;
                }
        }
@@ -4110,9 +4118,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
         */
        lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type, &err);
        if (!lock) {
-               error("%s", err.buf);
+               error("cannot lock ref '%s': %s", refname, err.buf);
                strbuf_release(&err);
-               return error("cannot lock ref '%s'", refname);
+               return -1;
        }
        if (!reflog_exists(refname)) {
                unlock_ref(lock);