From: Ronnie Sahlberg <sahlb...@google.com>

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 <sahlb...@google.com>
Signed-off-by: David Turner <dtur...@twopensource.com>
Signed-off-by: Junio C Hamano <gits...@pobox.com>
Signed-off-by: Michael Haggerty <mhag...@alum.mit.edu>
---
 refs.c | 66 ++++++++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 40 insertions(+), 26 deletions(-)

diff --git a/refs.c b/refs.c
index 0617e0c..ddcdf81 100644
--- 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 @@ out:
        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;
                }
-- 
2.6.2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to