lock_ref_sha1_basic(): report errors via a "struct strbuf *err"
[gitweb.git] / refs.c
diff --git a/refs.c b/refs.c
index 0fa70fb5fb6527875e2f379ba2444a78a2930113..ff9b9cf92a7696dcb4d009f1c511a42641aad4cc 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -263,7 +263,7 @@ struct ref_dir {
  * presence of an empty subdirectory does not block the creation of a
  * similarly-named reference.  (The fact that reference names with the
  * same leading components can conflict *with each other* is a
- * separate issue that is regulated by is_refname_available().)
+ * separate issue that is regulated by verify_refname_available().)
  *
  * Please note that the name field contains the fully-qualified
  * reference (or subdirectory) name.  Space could be saved by only
@@ -858,12 +858,13 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
 }
 
 /*
- * Return true iff a reference named refname could be created without
- * conflicting with the name of an existing reference in dir. If
- * extras is non-NULL, it is a list of additional refnames with which
- * refname is not allowed to conflict. If skip is non-NULL, ignore
- * potential conflicts with refs in skip (e.g., because they are
- * scheduled for deletion in the same operation). Behavior is
+ * Return 0 if a reference named refname could be created without
+ * conflicting with the name of an existing reference in dir.
+ * Otherwise, return a negative value and write an explanation to err.
+ * If extras is non-NULL, it is a list of additional refnames with
+ * which refname is not allowed to conflict. If skip is non-NULL,
+ * ignore potential conflicts with refs in skip (e.g., because they
+ * are scheduled for deletion in the same operation). Behavior is
  * undefined if the same name is listed in both extras and skip.
  *
  * Two reference names conflict if one of them exactly matches the
@@ -873,21 +874,24 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
  *
  * extras and skip must be sorted.
  */
-static int is_refname_available(const char *refname,
-                               const struct string_list *extras,
-                               const struct string_list *skip,
-                               struct ref_dir *dir)
+static int verify_refname_available(const char *refname,
+                                   const struct string_list *extras,
+                                   const struct string_list *skip,
+                                   struct ref_dir *dir,
+                                   struct strbuf *err)
 {
        const char *slash;
        int pos;
        struct strbuf dirname = STRBUF_INIT;
-       int ret = 0;
+       int ret = -1;
 
        /*
         * For the sake of comments in this function, suppose that
         * refname is "refs/foo/bar".
         */
 
+       assert(err);
+
        strbuf_grow(&dirname, strlen(refname) + 1);
        for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
                /* Expand dirname to the new prefix, not including the trailing slash: */
@@ -907,16 +911,16 @@ static int is_refname_available(const char *refname,
                                 * a proper prefix of refname; e.g.,
                                 * "refs/foo", and is not in skip.
                                 */
-                               error("'%s' exists; cannot create '%s'",
-                                     dirname.buf, refname);
+                               strbuf_addf(err, "'%s' exists; cannot create '%s'",
+                                           dirname.buf, refname);
                                goto cleanup;
                        }
                }
 
                if (extras && string_list_has_string(extras, dirname.buf) &&
                    (!skip || !string_list_has_string(skip, dirname.buf))) {
-                       error("cannot process '%s' and '%s' at the same time",
-                             refname, dirname.buf);
+                       strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
+                                   refname, dirname.buf);
                        goto cleanup;
                }
 
@@ -975,8 +979,8 @@ static int is_refname_available(const char *refname,
                        dir = get_ref_dir(dir->entries[pos]);
                        sort_ref_dir(dir);
                        if (do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) {
-                               error("'%s' exists; cannot create '%s'",
-                                     data.conflicting_refname, refname);
+                               strbuf_addf(err, "'%s' exists; cannot create '%s'",
+                                           data.conflicting_refname, refname);
                                goto cleanup;
                        }
                }
@@ -999,15 +1003,15 @@ static int is_refname_available(const char *refname,
                                break;
 
                        if (!skip || !string_list_has_string(skip, extra_refname)) {
-                               error("cannot process '%s' and '%s' at the same time",
-                                     refname, extra_refname);
+                               strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
+                                           refname, extra_refname);
                                goto cleanup;
                        }
                }
        }
 
        /* No conflicts were found */
-       ret = 1;
+       ret = 0;
 
 cleanup:
        strbuf_release(&dirname);
@@ -2329,7 +2333,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                                            const unsigned char *old_sha1,
                                            const struct string_list *extras,
                                            const struct string_list *skip,
-                                           unsigned int flags, int *type_p)
+                                           unsigned int flags, int *type_p,
+                                           struct strbuf *err)
 {
        char *ref_file;
        const char *orig_refname = refname;
@@ -2340,6 +2345,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
        int resolve_flags = 0;
        int attempts_remaining = 3;
 
+       assert(err);
+
        lock = xcalloc(1, sizeof(struct ref_lock));
        lock->lock_fd = -1;
 
@@ -2362,7 +2369,8 @@ 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;
-                       error("there are still refs under '%s'", orig_refname);
+                       strbuf_addf(err, "there are still refs under '%s'",
+                                   orig_refname);
                        goto error_return;
                }
                refname = resolve_ref_unsafe(orig_refname, resolve_flags,
@@ -2372,8 +2380,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
            *type_p = type;
        if (!refname) {
                last_errno = errno;
-               error("unable to resolve reference %s: %s",
-                       orig_refname, strerror(errno));
+               strbuf_addf(err, "unable to resolve reference %s: %s",
+                           orig_refname, strerror(errno));
                goto error_return;
        }
        /*
@@ -2383,7 +2391,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
         * our refname.
         */
        if (is_null_sha1(lock->old_sha1) &&
-           !is_refname_available(refname, extras, skip, get_packed_refs(&ref_cache))) {
+           verify_refname_available(refname, extras, skip,
+                                    get_packed_refs(&ref_cache), err)) {
                last_errno = ENOTDIR;
                goto error_return;
        }
@@ -2409,7 +2418,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                /* fall through */
        default:
                last_errno = errno;
-               error("unable to create directory for %s", ref_file);
+               strbuf_addf(err, "unable to create directory for %s", ref_file);
                goto error_return;
        }
 
@@ -2424,10 +2433,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                         */
                        goto retry;
                else {
-                       struct strbuf err = STRBUF_INIT;
-                       unable_to_lock_message(ref_file, errno, &err);
-                       error("%s", err.buf);
-                       strbuf_release(&err);
+                       unable_to_lock_message(ref_file, errno, err);
                        goto error_return;
                }
        }
@@ -2821,12 +2827,19 @@ static int rename_tmp_log(const char *newrefname)
 static int rename_ref_available(const char *oldname, const char *newname)
 {
        struct string_list skip = STRING_LIST_INIT_NODUP;
+       struct strbuf err = STRBUF_INIT;
        int ret;
 
        string_list_insert(&skip, oldname);
-       ret = is_refname_available(newname, NULL, &skip, get_packed_refs(&ref_cache))
-               && is_refname_available(newname, NULL, &skip, get_loose_refs(&ref_cache));
+       ret = !verify_refname_available(newname, NULL, &skip,
+                                       get_packed_refs(&ref_cache), &err)
+               && !verify_refname_available(newname, NULL, &skip,
+                                            get_loose_refs(&ref_cache), &err);
+       if (!ret)
+               error("%s", err.buf);
+
        string_list_clear(&skip, 0);
+       strbuf_release(&err);
        return ret;
 }
 
@@ -2841,6 +2854,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        struct stat loginfo;
        int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
        const char *symref = NULL;
+       struct strbuf err = STRBUF_INIT;
 
        if (log && S_ISLNK(loginfo.st_mode))
                return error("reflog for %s is a symlink", oldrefname);
@@ -2883,8 +2897,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 
        logmoved = log;
 
-       lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL);
+       lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL, &err);
        if (!lock) {
+               error("%s", err.buf);
+               strbuf_release(&err);
                error("unable to lock %s for update", newrefname);
                goto rollback;
        }
@@ -2897,8 +2913,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        return 0;
 
  rollback:
-       lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL);
+       lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL, &err);
        if (!lock) {
+               error("%s", err.buf);
+               strbuf_release(&err);
                error("unable to lock %s for rollback", oldrefname);
                goto rollbacklog;
        }
@@ -3811,11 +3829,14 @@ int ref_transaction_commit(struct ref_transaction *transaction,
                                 update->old_sha1 : NULL),
                                &affected_refnames, NULL,
                                flags,
-                               &update->type);
+                               &update->type,
+                               err);
                if (!update->lock) {
                        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);
                        goto cleanup;
@@ -4075,6 +4096,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
        char *log_file;
        int status = 0;
        int type;
+       struct strbuf err = STRBUF_INIT;
 
        memset(&cb, 0, sizeof(cb));
        cb.flags = flags;
@@ -4086,9 +4108,12 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
         * reference itself, plus we might need to update the
         * reference if --updateref was specified:
         */
-       lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type);
-       if (!lock)
+       lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type, &err);
+       if (!lock) {
+               error("%s", err.buf);
+               strbuf_release(&err);
                return error("cannot lock ref '%s'", refname);
+       }
        if (!reflog_exists(refname)) {
                unlock_ref(lock);
                return 0;