aboutsummaryrefslogtreecommitdiff
path: root/refs.c
diff options
context:
space:
mode:
authorMichael Haggerty <mhagger@alum.mit.edu>2012-04-10 07:30:19 +0200
committerJunio C Hamano <gitster@pobox.com>2012-04-10 15:54:54 -0700
commit5a4d4947318d638f39a47be253d83460a10e65ef (patch)
tree9c1af5452518c5e50e914d6eeed82dea2e2da6fc /refs.c
parentd66da478f26df3114af1177fa5614035439178ed (diff)
downloadgit-5a4d4947318d638f39a47be253d83460a10e65ef.tar.gz
git-5a4d4947318d638f39a47be253d83460a10e65ef.tar.xz
names_conflict(): simplify implementation
Save a bunch of lines of code and a couple of strlen() calls. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c62
1 files changed, 37 insertions, 25 deletions
diff --git a/refs.c b/refs.c
index d40ab63e9..4b94b08db 100644
--- a/refs.c
+++ b/refs.c
@@ -342,41 +342,53 @@ static int do_for_each_ref_in_arrays(struct ref_array *array1,
*/
static int names_conflict(const char *refname1, const char *refname2)
{
- int len1 = strlen(refname1);
- int len2 = strlen(refname2);
- int cmplen;
- const char *lead;
-
- if (len1 < len2) {
- cmplen = len1;
- lead = refname2;
- } else {
- cmplen = len2;
- lead = refname1;
+ for (; *refname1 && *refname1 == *refname2; refname1++, refname2++)
+ ;
+ return (*refname1 == '\0' && *refname2 == '/')
+ || (*refname1 == '/' && *refname2 == '\0');
+}
+
+struct name_conflict_cb {
+ const char *refname;
+ const char *oldrefname;
+ const char *conflicting_refname;
+};
+
+static int name_conflict_fn(const char *existingrefname, const unsigned char *sha1,
+ int flags, void *cb_data)
+{
+ struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data;
+ if (data->oldrefname && !strcmp(data->oldrefname, existingrefname))
+ return 0;
+ if (names_conflict(data->refname, existingrefname)) {
+ data->conflicting_refname = existingrefname;
+ return 1;
}
- return !strncmp(refname1, refname2, cmplen) && lead[cmplen] == '/';
+ return 0;
}
/*
* Return true iff a reference named refname could be created without
- * conflicting with the name of an existing reference. If oldrefname
- * is non-NULL, ignore potential conflicts with oldrefname (e.g.,
- * because oldrefname is scheduled for deletion in the same
+ * conflicting with the name of an existing reference in array. If
+ * oldrefname is non-NULL, ignore potential conflicts with oldrefname
+ * (e.g., because oldrefname is scheduled for deletion in the same
* operation).
*/
static int is_refname_available(const char *refname, const char *oldrefname,
struct ref_array *array)
{
- int i;
- for (i = 0; i < array->nr; i++) {
- struct ref_entry *entry = array->refs[i];
- if (oldrefname && !strcmp(oldrefname, entry->name))
- continue;
- if (names_conflict(refname, entry->name)) {
- error("'%s' exists; cannot create '%s'",
- entry->name, refname);
- return 0;
- }
+ struct name_conflict_cb data;
+ data.refname = refname;
+ data.oldrefname = oldrefname;
+ data.conflicting_refname = NULL;
+
+ sort_ref_array(array);
+ if (do_for_each_ref_in_array(array, 0, "", name_conflict_fn,
+ 0, DO_FOR_EACH_INCLUDE_BROKEN,
+ &data)) {
+ error("'%s' exists; cannot create '%s'",
+ data.conflicting_refname, refname);
+ return 0;
}
return 1;
}