disallow branch names that start with a hyphen
PATCH 1/1 disallow branch names that start with a hyphen
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.
--- refs.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
refs.c +2 -2
@@ -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) {
Signature · 1 line
1.7.2.1.1.g202c
Quoted 5 lines · Clemens Buchacher
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.
Quoted 7 lines
In order to avoid mistakes, do not allow such
branch names.Signed-off-by Clemens Buchacher <[email protected]>--- 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?
Quoted 2 lines · Junio C Hamano
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
Quoted 6 lines · Clemens Buchacher
Quoted 2 lines · Junio C Hamano
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.
---
strbuf.c +2 -0
@@ -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); }
Quoted 5 lines · Junio C Hamano
Quoted 3 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
Quoted 32 lines · Junio C Hamano
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/-fooand 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 <[email protected]>---
strbuf.c +0
-0
strbuf.c int strbuf_branchname(struct strbuf *sb, const char *name) · line 406
Quoted 8 lines
strbuf.c +2
-0@@ -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?
Signature · 3 lines
Jakub Narebski Poland ShadeHawk on #git