threads / patch / 24828

patchdisallow branch names that start with a hyphen

Subject: [PATCH] disallow branch names that start with a hyphen

## tl;dr

6 messages between Aug 22, 2010 and Sep 15, 2010. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Clemens Buchacher· Aug 22, 2010, 14:08 UTC · lore

Branch names such as "-", "--" or even "---" do not work with git checkout. Anything that starts with a hyphen is also potentially ambiguous with a command option.

In order to avoid mistakes, do not allow such branch names.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
 refs.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
Show changes to refs.c +2 −2
diff --git a/refs.c b/refs.c
index b540067..6884dff 100644
--- a/refs.c
+++ b/refs.c
@@ -742,7 +742,7 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
  * Make sure "ref" is something reasonable to have under ".git/refs/";
  * We do not like it if:
  *
- * - any path component of it begins with ".", or
+ * - any path component of it begins with "." or "-", or
  * - it has double dots "..", or
  * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
  * - it ends with a "/".
@@ -778,7 +778,7 @@ int check_ref_format(const char *ref)
 			return CHECK_REF_FORMAT_ERROR;
 
 		/* we are at the beginning of the path component */
-		if (ch == '.')
+		if (ch == '.' || ch == '-')
 			return CHECK_REF_FORMAT_ERROR;
 		bad_type = bad_ref_char(ch);
 		if (bad_type) {
-- 
1.7.2.1.1.g202c
Junio C Hamano· Aug 22, 2010, 21:20 UTC · re: Clemens Buchacher · lore

Re: [PATCH] disallow branch names that start with a hyphen

Clemens Buchacher <drizzd@aon.at> writes:
Show 12 quoted lines
> Branch names such as "-", "--" or even "---" do
> not work with git checkout. Anything that starts
> with a hyphen is also potentially ambiguous with a
> command option.
>
> In order to avoid mistakes, do not allow such
> branch names.
>
> Signed-off-by: Clemens Buchacher <drizzd@aon.at>
> ---
>  refs.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)

While the inconsistency between "git branch -- -foo" (which succeeds and creates refs/heads/-foo) and "git checkout -foo" (whcih does not work) is a valid issue to address, I think this patch does so at a wrong level of abstraction. The abstractions implemented by refs.c and friends are at the plumbing layer that is accessible by Porcelain scripts other people write, and we should try not to break them with policy decisions the Porcelains (e.g. "git checkout") we ship as part of the core git (e.g. we do not like a branch whose name begins with a dash).

Currently these succeed:
    git update-ref refs/mine/-foo HEAD
    git update-ref -d refs/mine/-foo

so it is reasonable to expect that somebody has a Porcelain that uses a convention to use its own refs namespace "mine" with leading dash to mean something to it. Your patch would break such a Porcelain rather badly, but I do not think we have to break this to fix your problem.

Two valid approaches would be: (1) teach a syntax to checkout to allow checking out such a branch; or (2) forbid "checkout -b" and "branch" from creating such a branch.

I do not think in the context of the core git Porcelain it is necessary to start allowing checking out "-foo" which we didn't before, so how about fixing this by tightening the command line parsing rules for Porcelain commands that create a new branch?

Note that either way update-ref should be kept as is, to allow third party Porcelains to keep doing what they have been doing, _and_ to allow unfortunate users who ran a broken "git branch" command with "-- -foo" as its arguments to recover from it.

Hmm?
Clemens Buchacher· Aug 23, 2010, 04:37 UTC · re: Junio C Hamano · lore

Re: [PATCH] disallow branch names that start with a hyphen

On Sun, Aug 22, 2010 at 02:20:17PM -0700, Junio C Hamano wrote:
> 
> forbid "checkout -b" and "branch" from creating such a branch.

Sounds good to me. I will have limited access to email this week. I'll revisit this when I am back.

Clemens
Junio C Hamano· Sep 14, 2010, 20:09 UTC · re: Clemens Buchacher · lore

Re: [PATCH] disallow branch names that start with a hyphen

Clemens Buchacher <drizzd@aon.at> writes:
Show 6 quoted lines
> On Sun, Aug 22, 2010 at 02:20:17PM -0700, Junio C Hamano wrote:
>> 
>> forbid "checkout -b" and "branch" from creating such a branch.
>
> Sounds good to me. I will have limited access to email this week.
> I'll revisit this when I am back.
Heh, it turns out that we have a perfect place to hook this into.
-- >8 --
Subject: disallow branch names that start with a hyphen

The current command line parser overly lax in places and allows a branch whose name begins with a hyphen e.g. "-foo" to be created, but the parseopt infrastructure in general do not like to parse anything that begin with a dash as a short-hand refname. "git checkout -foo" won't work, nor "git branch -d -foo" (even though "git branch -d -- -foo" works, it does so by mistake; we should not be taking anything but pathspecs after double-dash).

All the codepath that creates a new branch ref, including the destination of "branch -m src dst", use strbuf_check_branch_ref() to validate if the given name is suitable as a branch name. Tighten it to disallow such a name.

You can still get rid of historical mistake with
  $ git update-ref -d refs/heads/-foo

and third-party Porcelains are free to keep using update-ref to create refs with path component that begins with "-".

Issue originally raised by Clemens Buchacher.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
Show changes to strbuf.c +2 −0
diff --git a/strbuf.c b/strbuf.c
index bc3a080..65b4cf4 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -399,6 +399,8 @@ int strbuf_branchname(struct strbuf *sb, const char *name)
 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
 {
 	strbuf_branchname(sb, name);
+	if (name[0] == '-')
+		return CHECK_REF_FORMAT_ERROR;
 	strbuf_splice(sb, 0, 0, "refs/heads/", 11);
 	return check_ref_format(sb->buf);
 }
Jakub Narebski· Sep 14, 2010, 20:38 UTC · re: Junio C Hamano · lore

Re: [PATCH] disallow branch names that start with a hyphen

Junio C Hamano <gitster@pobox.com> writes:
Show 40 quoted lines
> Subject: disallow branch names that start with a hyphen
> 
> The current command line parser overly lax in places and allows a branch
> whose name begins with a hyphen e.g. "-foo" to be created, but the
> parseopt infrastructure in general do not like to parse anything that
> begin with a dash as a short-hand refname.  "git checkout -foo" won't
> work, nor "git branch -d -foo" (even though "git branch -d -- -foo" works,
> it does so by mistake; we should not be taking anything but pathspecs
> after double-dash).
> 
> All the codepath that creates a new branch ref, including the destination
> of "branch -m src dst", use strbuf_check_branch_ref() to validate if the
> given name is suitable as a branch name.  Tighten it to disallow such a
> name.
> 
> You can still get rid of historical mistake with
> 
>   $ git update-ref -d refs/heads/-foo
> 
> and third-party Porcelains are free to keep using update-ref to create
> refs with path component that begins with "-".
> 
> Issue originally raised by Clemens Buchacher.
> 
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
> 
> diff --git a/strbuf.c b/strbuf.c
> index bc3a080..65b4cf4 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -399,6 +399,8 @@ int strbuf_branchname(struct strbuf *sb, const char *name)
>  int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
>  {
>  	strbuf_branchname(sb, name);
> +	if (name[0] == '-')
> +		return CHECK_REF_FORMAT_ERROR;
>  	strbuf_splice(sb, 0, 0, "refs/heads/", 11);
>  	return check_ref_format(sb->buf);
>  }
Shouldn't it include update to Documentation/git-check-ref-format.txt?
-- 
Jakub Narebski
Poland
ShadeHawk on #git
Clemens Buchacher· Sep 15, 2010, 07:34 UTC · re: Junio C Hamano · lore

Re: [PATCH] disallow branch names that start with a hyphen

On Tue, Sep 14, 2010 at 01:09:20PM -0700, Junio C Hamano wrote:
Show 5 quoted lines
> >
> > Sounds good to me. I will have limited access to email this week.
> > I'll revisit this when I am back.
> 
> Heh, it turns out that we have a perfect place to hook this into.

Thank you for taking care of this! Unfortunately, I haven't had the time to do any work at all.

Clemens

← back to recent threads