diff mbox series

[v2,1/4] refs: new function newname_has_bad_prefix

Message ID 20191107190750.26674-2-ingo.rohloff@lauterbach.com (mailing list archive)
State New, archived
Headers show
Series Do not create new refnames "refs" or "refs/*" | expand

Commit Message

Ingo Rohloff Nov. 7, 2019, 7:07 p.m. UTC
The function is intended to check if a user specified name
for a new reference (for remotes, branches or tags) has
a bad prefix.

Intention: Prevent users from creating tags/remotes/branches
which are named "refs" or "refs/..."

Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
---
 refs.c | 30 ++++++++++++++++++++++++++++++
 refs.h |  2 ++
 2 files changed, 32 insertions(+)
diff mbox series

Patch

diff --git a/refs.c b/refs.c
index 1ab0bb54d3..49e4f662df 100644
--- a/refs.c
+++ b/refs.c
@@ -321,6 +321,36 @@  int ref_exists(const char *refname)
 	return refs_ref_exists(get_main_ref_store(the_repository), refname);
 }
 
+
+static int starts_with_component(const char *str, const char *prefix)
+{
+	for (; ; str++, prefix++) {
+		if (!*prefix) {
+			if (!*str || *str == '/')
+				return 1;
+			return 0;
+		} else if (*str != *prefix)
+			return 0;
+	}
+}
+
+static const char *newname_bad_prefixes[] = {
+	"refs",
+	NULL
+};
+
+int newname_has_bad_prefix(const char *refname)
+{
+	const char **p;
+	p = newname_bad_prefixes;
+	while (*p) {
+		if (starts_with_component(refname, *p))
+			return 1;
+		p++;
+	}
+	return 0;
+}
+
 static int match_ref_pattern(const char *refname,
 			     const struct string_list_item *item)
 {
diff --git a/refs.h b/refs.h
index 730d05ad91..00c11ec589 100644
--- a/refs.h
+++ b/refs.h
@@ -107,6 +107,8 @@  int refs_verify_refname_available(struct ref_store *refs,
 
 int ref_exists(const char *refname);
 
+int newname_has_bad_prefix(const char *refname);
+
 int should_autocreate_reflog(const char *refname);
 
 int is_branch(const char *refname);