branch: correctly reject refs/heads/{-dash,HEAD}
commita625b092cc59940521789fe8a3ff69c8d6b14eb2
authorJunio C Hamano <gitster@pobox.com>
Tue, 14 Nov 2017 11:42:58 +0000 (14 17:12 +0530)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Nov 2017 02:41:53 +0000 (15 11:41 +0900)
tree3df0f71f1729090dd80b2b4e80e8c95e8a691410
parentbc1c9c0e674bdd293c29ae84365915848ed01d7a
branch: correctly reject refs/heads/{-dash,HEAD}

strbuf_check_branch_ref() is the central place where many codepaths
see if a proposed name is suitable for the name of a branch.  It was
designed to allow us to get stricter than the check_refname_format()
check used for refnames in general, and we already use it to reject
a branch whose name begins with a '-'.  The function gets a strbuf
and a string "name", and returns non-zero if the name is not
appropriate as the name for a branch.  When the name is good, it
places the full refname for the branch with the proposed name in the
strbuf before it returns.

However, it turns out that one caller looks at what is in the strbuf
even when the function returns an error.  Make the function populate
the strbuf even when it returns an error.  That way, when "-dash" is
given as name, "refs/heads/-dash" is placed in the strbuf when
returning an error to copy_or_rename_branch(), which notices that
the user is trying to recover with "git branch -m -- -dash dash" to
rename "-dash" to "dash".

While at it, use the same mechanism to also reject "HEAD" as a
branch name.

Helped-by: Jeff King <peff@peff.net>
Helped-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sha1_name.c
t/t1430-bad-ref-name.sh