summaryrefslogtreecommitdiff
path: root/refs.c
diff options
context:
space:
mode:
authorDavid Turner <dturner@twopensource.com>2015-11-10 11:42:40 (GMT)
committerJeff King <peff@peff.net>2015-11-20 09:52:01 (GMT)
commit0845122c39c415fa50904ee94c6b60e4e722466b (patch)
tree7fcb6eb01d6f13d272f346079a1e57eb8cbe32f8 /refs.c
parent5f3c3a4e6f11dedad93d9f5bb2caa85c684db54a (diff)
downloadgit-0845122c39c415fa50904ee94c6b60e4e722466b.zip
git-0845122c39c415fa50904ee94c6b60e4e722466b.tar.gz
git-0845122c39c415fa50904ee94c6b60e4e722466b.tar.bz2
refs: break out ref conflict checks
Create new function find_descendant_ref, 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 <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>
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/refs.c b/refs.c
index 1620a53..ad883ec 100644
--- a/refs.c
+++ b/refs.c
@@ -1029,3 +1029,47 @@ int ref_is_hidden(const char *refname)
}
return 0;
}
+
+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;
+}