From: David Turner <dtur...@twopensource.com>

Create new function verify_no_descendants, to hold one of the ref
conflict checks used in verify_refname_available. Multiple backends
will need this function, so move it to the common code.

Also move rename_ref_available to the common code, because alternate
backends might need it and it has no files-backend-specific code.

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/files-backend.c | 49 +++++++------------------------------------------
 refs/refs-internal.h | 16 ++++++++++++++++
 refs/refs.c          | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 42 deletions(-)

diff --git a/refs/files-backend.c b/refs/files-backend.c
index e0ae769..baa13bb 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -729,6 +729,7 @@ static int verify_refname_available_dir(const char *refname,
                                        struct strbuf *err)
 {
        const char *slash;
+       const char *extra_refname;
        int pos;
        struct strbuf dirname = STRBUF_INIT;
        int ret = -1;
@@ -834,32 +835,12 @@ static int verify_refname_available_dir(const char 
*refname,
                }
        }
 
-       if (extras) {
-               /*
-                * Check for entries in extras that start with
-                * "$refname/". We do that by looking for the place
-                * where "$refname/" would be inserted in extras. If
-                * there is an entry at that position that starts with
-                * "$refname/" and is not in skip, then we have a
-                * conflict.
-                */
-               for (pos = string_list_find_insert_index(extras, dirname.buf, 
0);
-                    pos < extras->nr; pos++) {
-                       const char *extra_refname = extras->items[pos].string;
-
-                       if (!starts_with(extra_refname, dirname.buf))
-                               break;
-
-                       if (!skip || !string_list_has_string(skip, 
extra_refname)) {
-                               strbuf_addf(err, "cannot process '%s' and '%s' 
at the same time",
-                                           refname, extra_refname);
-                               goto cleanup;
-                       }
-               }
-       }
-
-       /* No conflicts were found */
-       ret = 0;
+       extra_refname = find_descendant_ref(dirname.buf, extras, skip);
+       if (extra_refname)
+               strbuf_addf(err, "cannot process '%s' and '%s' at the same 
time",
+                           refname, extra_refname);
+       else
+               ret = 0;
 
 cleanup:
        strbuf_release(&dirname);
@@ -2461,22 +2442,6 @@ out:
        return ret;
 }
 
-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 = !verify_refname_available(newname, NULL, &skip, &err);
-       if (!ret)
-               error("%s", err.buf);
-
-       string_list_clear(&skip, 0);
-       strbuf_release(&err);
-       return ret;
-}
-
 static int write_ref_to_lockfile(struct ref_lock *lock,
                                 const unsigned char *sha1, struct strbuf *err);
 static int commit_ref_update(struct ref_lock *lock,
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index f6a79b9..500b708 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -25,6 +25,22 @@
 int refname_is_safe(const char *refname);
 
 /*
+ * Check for entries in extras that are within the specified
+ * directory, where dirname is a reference directory name including
+ * the trailing slash (e.g., "refs/heads/foo/"). Ignore any
+ * conflicting references that are found in skip. If there is a
+ * conflicting reference, return its name.
+ *
+ * extras and skip must be sorted lists of reference names. Either one
+ * can be NULL, signifying the empty list.
+ */
+const char *find_descendant_ref(const char *dirname,
+                               const struct string_list *extras,
+                               const struct string_list *skip);
+
+int rename_ref_available(const char *oldname, const char *newname);
+
+/*
  * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
  * refs (i.e., because the reference is about to be deleted anyway).
  */
diff --git a/refs/refs.c b/refs/refs.c
index 25d4e22..1c1c6cb 100644
--- a/refs/refs.c
+++ b/refs/refs.c
@@ -1027,3 +1027,47 @@ enum peel_status peel_object(const unsigned char *name, 
unsigned char *sha1)
        hashcpy(sha1, o->sha1);
        return PEEL_PEELED;
 }
+
+const char *find_descendant_ref(const char *dirname,
+                               const struct string_list *extras,
+                               const struct string_list *skip)
+{
+       int pos;
+
+       if (!extras)
+               return NULL;
+
+       /*
+        * Look at the place where dirname would be inserted into
+        * extras. If there is an entry at that position that starts
+        * with dirname (remember, dirname includes the trailing
+        * slash) and is not in skip, then we have a conflict.
+        */
+       for (pos = string_list_find_insert_index(extras, dirname, 0);
+            pos < extras->nr; pos++) {
+               const char *extra_refname = extras->items[pos].string;
+
+               if (!starts_with(extra_refname, dirname))
+                       break;
+
+               if (!skip || !string_list_has_string(skip, extra_refname))
+                       return extra_refname;
+       }
+       return NULL;
+}
+
+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 = !verify_refname_available(newname, NULL, &skip, &err);
+       if (!ret)
+               error("%s", err.buf);
+
+       string_list_clear(&skip, 0);
+       strbuf_release(&err);
+       return ret;
+}
-- 
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