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

[PATCH v2 1/2] branch: forbid refs/heads/HEAD

From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Date
Nov 14, 2017, 11:42 UTC
Message-ID
<20171114114259.8937-1-kaartic.sivaraam@gmail.com>
In-Reply-To
<20171013051132.3973-1-gitster@pobox.com>
From: Junio C Hamano <gitster@pobox.com>

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

Use it to also reject "HEAD" as a branch name.
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
  Changes in v2:
  * Fixed the issue of .git/HEAD being renamed when trying to do,
      $ git branch -m HEAD head
   This also allows to rename a branch named HEAD that was created by accident.
   cf. <1509209933.2256.4.camel@gmail.com>
  * Added a test to ensure that it is possible to rename a branch named HEAD.
 sha1_name.c             |  8 +++++++-
 t/t1430-bad-ref-name.sh | 29 +++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/sha1_name.c b/sha1_name.c
index 9a2d5caf3..657a060cb 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -1438,9 +1438,15 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
 		strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
 	else
 		strbuf_addstr(sb, name);
-	if (name[0] == '-')
+	if (*name == '-')
 		return -1;
+
 	strbuf_splice(sb, 0, 0, "refs/heads/", 11);
+
+	/* HEAD is not to be used as a branch name */
+	if(!strcmp(sb->buf, "refs/heads/HEAD"))
+		return -1;
+
 	return check_refname_format(sb->buf, 0);
 }
 
diff --git a/t/t1430-bad-ref-name.sh b/t/t1430-bad-ref-name.sh
index e88349c8a..421e80a7a 100755
--- a/t/t1430-bad-ref-name.sh
+++ b/t/t1430-bad-ref-name.sh
@@ -331,4 +331,33 @@ test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
 	grep "fatal: invalid ref format: ~a" err
 '
 
+test_expect_success 'branch rejects HEAD as a branch name' '
+	test_must_fail git branch HEAD HEAD^ &&
+	test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'checkout -b rejects HEAD as a branch name' '
+	test_must_fail git checkout -B HEAD HEAD^ &&
+	test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'update-ref can operate on refs/heads/HEAD' '
+	git update-ref refs/heads/HEAD HEAD^ &&
+	git show-ref refs/heads/HEAD &&
+	git update-ref -d refs/heads/HEAD &&
+	test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'branch -d can remove refs/heads/HEAD' '
+	git update-ref refs/heads/HEAD HEAD^ &&
+	git branch -d HEAD &&
+	test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'branch -m can rename refs/heads/HEAD' '
+	git update-ref refs/heads/HEAD HEAD^ &&
+	git branch -m HEAD head &&
+	test_must_fail git show-ref refs/heads/HEAD
+'
+
 test_done
-- 
2.15.0.rc2.397.geff0134c7.dirty
Previous: Kaartic SivaraamNext: Kaartic Sivaraam
Message 18 of 26 in “a small branch API clean-up”
  1. 0/3 a small branch API clean-upJunio C Hamano, Oct 13, 2017
  2. 2/3 branch: split validate_new_branchname() into twoJunio C Hamano, Oct 13, 2017
  3. Kaartic SivaraamOct 21, 2017
  4. Junio C HamanoOct 21, 2017
  5. 1/3 branch: streamline "attr_only" handling in validate_new_branchname()Junio C Hamano, Oct 13, 2017
  6. Eric SunshineOct 13, 2017
  7. 3/3 branch: forbid refs/heads/HEADJunio C Hamano, Oct 13, 2017
  8. Jeff KingOct 13, 2017
  9. Junio C HamanoOct 14, 2017
  10. Junio C HamanoOct 14, 2017
  11. Jeff KingOct 16, 2017
  12. Kaartic SivaraamOct 21, 2017
  13. Junio C HamanoOct 21, 2017
  14. Kaartic SivaraamOct 22, 2017
  15. Kaartic SivaraamOct 21, 2017
  16. Junio C HamanoOct 21, 2017
  17. Kaartic SivaraamOct 22, 2017
  18. 1/2 branch: forbid refs/heads/HEADKaartic Sivaraam, Nov 14, 2017
  19. 2/2 builtin/branch: remove redundant check for HEADKaartic Sivaraam, Nov 14, 2017
  20. Kaartic SivaraamNov 14, 2017
  21. Junio C HamanoNov 14, 2017
  22. Kaartic SivaraamNov 15, 2017
  23. 3/4 branch: correctly reject refs/heads/{-dash,HEAD}Junio C Hamano, Nov 15, 2017
  24. Kaartic SivaraamNov 16, 2017
  25. Junio C HamanoNov 16, 2017
  26. Kaartic SivaraamNov 16, 2017

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.