git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] disallow branch names that start with a hyphen
@ 2010-08-22 14:08 Clemens Buchacher
  2010-08-22 21:20 ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Clemens Buchacher @ 2010-08-22 14:08 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Thomas Rast

[-- Attachment #1: Type: text/plain, Size: 1195 bytes --]


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(-)

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


[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] disallow branch names that start with a hyphen
  2010-08-22 14:08 [PATCH] disallow branch names that start with a hyphen Clemens Buchacher
@ 2010-08-22 21:20 ` Junio C Hamano
  2010-08-23  4:37   ` Clemens Buchacher
  0 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2010-08-22 21:20 UTC (permalink / raw)
  To: Clemens Buchacher; +Cc: git, Thomas Rast

Clemens Buchacher <drizzd@aon.at> writes:

> 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?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] disallow branch names that start with a hyphen
  2010-08-22 21:20 ` Junio C Hamano
@ 2010-08-23  4:37   ` Clemens Buchacher
  2010-09-14 20:09     ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Clemens Buchacher @ 2010-08-23  4:37 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Thomas Rast

[-- Attachment #1: Type: text/plain, Size: 250 bytes --]

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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] disallow branch names that start with a hyphen
  2010-08-23  4:37   ` Clemens Buchacher
@ 2010-09-14 20:09     ` Junio C Hamano
  2010-09-14 20:38       ` Jakub Narebski
  2010-09-15  7:34       ` Clemens Buchacher
  0 siblings, 2 replies; 6+ messages in thread
From: Junio C Hamano @ 2010-09-14 20:09 UTC (permalink / raw)
  To: Clemens Buchacher; +Cc: git, Thomas Rast

Clemens Buchacher <drizzd@aon.at> writes:

> 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>
---

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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] disallow branch names that start with a hyphen
  2010-09-14 20:09     ` Junio C Hamano
@ 2010-09-14 20:38       ` Jakub Narebski
  2010-09-15  7:34       ` Clemens Buchacher
  1 sibling, 0 replies; 6+ messages in thread
From: Jakub Narebski @ 2010-09-14 20:38 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Clemens Buchacher, git, Thomas Rast

Junio C Hamano <gitster@pobox.com> writes:

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] disallow branch names that start with a hyphen
  2010-09-14 20:09     ` Junio C Hamano
  2010-09-14 20:38       ` Jakub Narebski
@ 2010-09-15  7:34       ` Clemens Buchacher
  1 sibling, 0 replies; 6+ messages in thread
From: Clemens Buchacher @ 2010-09-15  7:34 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Thomas Rast

[-- Attachment #1: Type: text/plain, Size: 364 bytes --]

On Tue, Sep 14, 2010 at 01:09:20PM -0700, Junio C Hamano wrote:
> >
> > 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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-09-15  7:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-22 14:08 [PATCH] disallow branch names that start with a hyphen Clemens Buchacher
2010-08-22 21:20 ` Junio C Hamano
2010-08-23  4:37   ` Clemens Buchacher
2010-09-14 20:09     ` Junio C Hamano
2010-09-14 20:38       ` Jakub Narebski
2010-09-15  7:34       ` Clemens Buchacher

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).