Skip to content

Commit 6348624

Browse files
committed
disallow branch names that start with a hyphen
The current command line parser is 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 does not like to parse anything that begins with a dash as a short-hand refname. "git checkout -foo" won't work, nor will "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 codepaths that create 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 a branch that begins with a hyphen. You can still get rid of historical mistakes with $ git update-ref -d refs/heads/-foo and third-party Porcelains are free to keep using update-ref to create refs with a path component that begins with "-". Issue originally raised by Clemens Buchacher. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5879b6b commit 6348624

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

strbuf.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ int strbuf_branchname(struct strbuf *sb, const char *name)
399399
int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
400400
{
401401
strbuf_branchname(sb, name);
402+
if (name[0] == '-')
403+
return CHECK_REF_FORMAT_ERROR;
402404
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
403405
return check_ref_format(sb->buf);
404406
}

0 commit comments

Comments
 (0)