verify_refname_available(): new function
authorRonnie Sahlberg <sahlberg@google.com>
Tue, 10 Nov 2015 11:42:32 +0000 (12:42 +0100)
committerJeff King <peff@peff.net>
Fri, 20 Nov 2015 09:52:01 +0000 (04:52 -0500)
Add a new verify_refname_available() function, which checks whether the
refname is available for use, taking all references (both packed and
loose) into account. This function, unlike the old
verify_refname_available(), has semantics independent of the choice of
reference storage, and can therefore be implemented by alternative
reference backends.

Use the new function in a couple of places.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Jeff King <peff@peff.net>
refs.c
diff --git a/refs.c b/refs.c
index 0617e0c6b9f62b578627c4f5543509cc419968e3..ddcdf81fab9b86303241c3d292c9c81a8169863b 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -279,7 +279,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 verify_refname_available_dir().)
+ * 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
@@ -897,19 +897,7 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
 /*
  * 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
- * leading components of the other; e.g., "refs/foo/bar" conflicts
- * with both "refs/foo" and with "refs/foo/bar/baz" but not with
- * "refs/foo/bar" or "refs/foo/barbados".
- *
- * extras and skip must be sorted.
+ * See verify_refname_available for more information.
  */
 static int verify_refname_available_dir(const char *refname,
                                        const struct string_list *extras,
@@ -3120,6 +3108,40 @@ static int rename_tmp_log(const char *newrefname)
        return ret;
 }
 
+/*
+ * Return 0 if a reference named refname could be created without
+ * conflicting with the name of an existing reference. 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
+ * leading components of the other; e.g., "foo/bar" conflicts with
+ * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
+ * "foo/barbados".
+ *
+ * extras and skip must be sorted.
+ */
+static int verify_refname_available(const char *newname,
+                                   struct string_list *extras,
+                                   struct string_list *skip,
+                                   struct strbuf *err)
+{
+       struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
+       struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
+
+       if (verify_refname_available_dir(newname, extras, skip,
+                                        packed_refs, err) ||
+           verify_refname_available_dir(newname, extras, skip,
+                                        loose_refs, err))
+               return -1;
+
+       return 0;
+}
+
 static int rename_ref_available(const char *oldname, const char *newname)
 {
        struct string_list skip = STRING_LIST_INIT_NODUP;
@@ -3127,10 +3149,7 @@ static int rename_ref_available(const char *oldname, const char *newname)
        int ret;
 
        string_list_insert(&skip, oldname);
-       ret = !verify_refname_available_dir(newname, NULL, &skip,
-                                           get_packed_refs(&ref_cache), &err)
-               && !verify_refname_available_dir(newname, NULL, &skip,
-                                                get_loose_refs(&ref_cache), &err);
+       ret = !verify_refname_available(newname, NULL, &skip, &err);
        if (!ret)
                error("%s", err.buf);
 
@@ -4334,8 +4353,6 @@ static int ref_present(const char *refname,
 int initial_ref_transaction_commit(struct ref_transaction *transaction,
                                   struct strbuf *err)
 {
-       struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
-       struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
        int ret = 0, i;
        int n = transaction->nr;
        struct ref_update **updates = transaction->updates;
@@ -4376,12 +4393,9 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
                if ((update->flags & REF_HAVE_OLD) &&
                    !is_null_sha1(update->old_sha1))
                        die("BUG: initial ref transaction with old_sha1 set");
-               if (verify_refname_available_dir(update->refname,
-                                                &affected_refnames, NULL,
-                                                loose_refs, err) ||
-                   verify_refname_available_dir(update->refname,
-                                                &affected_refnames, NULL,
-                                                packed_refs, err)) {
+               if (verify_refname_available(update->refname,
+                                            &affected_refnames, NULL,
+                                            err)) {
                        ret = TRANSACTION_NAME_CONFLICT;
                        goto cleanup;
                }