git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Ingo Rohloff <ingo.rohloff@lauterbach.com>
To: git@vger.kernel.org
Cc: Ingo Rohloff <ingo.rohloff@lauterbach.com>
Subject: [PATCH v2 1/4] refs: new function newname_has_bad_prefix
Date: Thu,  7 Nov 2019 20:07:47 +0100	[thread overview]
Message-ID: <20191107190750.26674-2-ingo.rohloff@lauterbach.com> (raw)
In-Reply-To: <20191107190750.26674-1-ingo.rohloff@lauterbach.com>

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 --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);
-- 
2.24.0


  reply	other threads:[~2019-11-07 19:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-06 16:56 [PATCH] branch: Forbid to create local branches with confusing names Ingo Rohloff
2019-11-06 22:15 ` Johannes Schindelin
2019-11-07 19:04   ` Ingo Rohloff
2019-11-07  6:05 ` Junio C Hamano
2019-11-07 12:54   ` Ingo Rohloff
2019-11-08  2:54     ` Junio C Hamano
2019-11-08 12:45       ` Ingo Rohloff
2019-11-07 19:07 ` [PATCH v2 0/4] Do not create new refnames "refs" or "refs/*" Ingo Rohloff
2019-11-07 19:07   ` Ingo Rohloff [this message]
2019-11-07 19:07   ` [PATCH v2 2/4] branch: Prevent creation of local branches named " Ingo Rohloff
2019-11-07 19:07   ` [PATCH v2 3/4] remote: Prevent users from creating remotes " Ingo Rohloff
2019-11-07 19:07   ` [PATCH v2 4/4] tag: Prevent users from creating tags " Ingo Rohloff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191107190750.26674-2-ingo.rohloff@lauterbach.com \
    --to=ingo.rohloff@lauterbach.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).