git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] We do not like "HEAD" as a new branch name

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Dec 15, 2005, 11:46 UTC
Message-ID
<Pine.LNX.4.63.0512151244230.22400@wbgn013.biozentrum.uni-wuerzburg.de>

This makes git-check-ref-format fail for "HEAD". Since the check is only executed when creating refs, the existing symbolic ref is safe.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---
	This patch was triggered by Alex' mail. Even if it should be a 
	rare case, it could catch some bad errors: Just try to run
		git checkout -b HEAD
	and imagine what a regular git user makes of the error 
	message...
 refs.c                 |    5 +++++
 t/t3300-funny-names.sh |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
04d60f0bf743f91d03e03acebf123db527cb7507
diff --git a/refs.c b/refs.c
index bda45f7..293bfe7 100644
--- a/refs.c
+++ b/refs.c
@@ -332,6 +332,11 @@ int check_ref_format(const char *ref)
 		if (ch == '.' || bad_ref_char(ch))
 			return -1;
 
+		/* do not allow "HEAD" as ref name */
+		if (ch == 'H' && (!strcmp(cp, "EAD") ||
+					!strncmp(cp, "EAD/", 4)))
+			return -1;
+
 		/* scan the rest of the path component */
 		while ((ch = *cp++) != 0) {
 			if (bad_ref_char(ch))
diff --git a/t/t3300-funny-names.sh b/t/t3300-funny-names.sh
index 897c378..8ebd896 100755
--- a/t/t3300-funny-names.sh
+++ b/t/t3300-funny-names.sh
@@ -139,4 +139,6 @@ test_expect_success 'git-apply non-git d
 	 git-apply --stat | sed -e "s/|.*//" -e "s/ *\$//" >current &&
 	 diff -u expected current'
 
+test_expect_failure 'HEAD is special' 'git checkout -b HEAD'
+
 test_done
-- 
0.99.9.GIT
Next: H. Peter Anvin
Message 1 of 10 in “We do not like "HEAD" as a new branch name”
  1. We do not like "HEAD" as a new branch nameJohannes Schindelin, Dec 15, 2005
  2. H. Peter AnvinDec 15, 2005
  3. Johannes SchindelinDec 15, 2005
  4. Alex RiesenDec 15, 2005
  5. Junio C HamanoDec 15, 2005
  6. Johannes SchindelinDec 15, 2005
  7. Junio C HamanoDec 15, 2005
  8. Johannes SchindelinDec 16, 2005
  9. Junio C HamanoDec 16, 2005
  10. Johannes SchindelinDec 16, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.