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

[PATCH 9/9] refs.c: make @ a pseudo-ref alias to HEAD

From
Ramkumar Ramachandra <artagnon@gmail.com>
Date
May 2, 2013, 13:39 UTC
Message-ID
<1367501974-6879-10-git-send-email-artagnon@gmail.com>
In-Reply-To
<1367501974-6879-1-git-send-email-artagnon@gmail.com>

First, make sure that check_refname_format() rejects the a refname beginning with a '@'. Add a test to t1400 (update-ref) demonstrating that update-ref forbids the user from updating a ref named "@".

Now, resolve_ref_unsafe() is built to resolve any refs that have a corresponding file inside $GITDIR. Our "@" ref is a special pseudo-ref and does not have a filesystem counterpart. So, hard-interpret "@" as "HEAD" and resolve .git/HEAD as usual. This means that we can drop the 'git symbolic-ref @ HEAD' line in t1508 (at-combinations), and everything will continue working as usual.

If the user does manage to create a '.git/@' unsafely (via symbolic-ref or otherwise), it will be ignored.

In practice, this means that you will now be able to do:
    $ git show @~1
    $ git log @^2
Advertise these features in the tests and documentation.
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 Documentation/git-check-ref-format.txt |  2 ++
 Documentation/revisions.txt            |  8 ++++++--
 refs.c                                 | 12 ++++++++++--
 t/t1400-update-ref.sh                  |  3 +++
 t/t1508-at-combinations.sh             |  4 +---
 5 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-check-ref-format.txt b/Documentation/git-check-ref-format.txt
index ec1739a..3de9adc 100644
--- a/Documentation/git-check-ref-format.txt
+++ b/Documentation/git-check-ref-format.txt
@@ -52,6 +52,8 @@ Git imposes the following rules on how references are named:
 
 . They cannot end with a dot `.`.
 
+. They cannot be the single character `@`.
+
 . They cannot contain a sequence `@{`.
 
 . They cannot contain a `\`.
diff --git a/Documentation/revisions.txt b/Documentation/revisions.txt
index d477b3f..02bfc90 100644
--- a/Documentation/revisions.txt
+++ b/Documentation/revisions.txt
@@ -27,6 +27,10 @@ blobs contained in a commit.
   When ambiguous, a '<refname>' is disambiguated by taking the
   first match in the following rules:
 
+  . '@' is a synonym for 'HEAD'.  An '@' followed by '\{' has no
+    relationship to this and means something entirely different (see
+    below).
+
   . If '$GIT_DIR/<refname>' exists, that is what you mean (this is usually
     useful only for 'HEAD', 'FETCH_HEAD', 'ORIG_HEAD', 'MERGE_HEAD'
     and 'CHERRY_PICK_HEAD');
@@ -93,7 +97,7 @@ some output processing may assume ref names in UTF-8.
   refers to the branch that the branch specified by branchname is set to build on
   top of.  A missing branchname defaults to the current one.
 
-'<rev>{caret}', e.g. 'HEAD{caret}, v1.5.1{caret}0'::
+'<rev>{caret}', e.g. '@{caret}2', 'HEAD{caret}', 'v1.5.1{caret}0'::
   A suffix '{caret}' to a revision parameter means the first parent of
   that commit object.  '{caret}<n>' means the <n>th parent (i.e.
   '<rev>{caret}'
@@ -101,7 +105,7 @@ some output processing may assume ref names in UTF-8.
   '<rev>{caret}0' means the commit itself and is used when '<rev>' is the
   object name of a tag object that refers to a commit object.
 
-'<rev>{tilde}<n>', e.g. 'master{tilde}3'::
+'<rev>{tilde}<n>', e.g. '@{tilde}1', 'master{tilde}3'::
   A suffix '{tilde}<n>' to a revision parameter means the commit
   object that is the <n>th generation ancestor of the named
   commit object, following only the first parents.  I.e. '<rev>{tilde}3' is
diff --git a/refs.c b/refs.c
index de2d8eb..6a75f77 100644
--- a/refs.c
+++ b/refs.c
@@ -72,6 +72,9 @@ int check_refname_format(const char *refname, int flags)
 {
 	int component_len, component_count = 0;
 
+	if (!strcmp(refname, "@"))
+		return -1;
+
 	while (1) {
 		/* We are at the start of a path component. */
 		component_len = check_refname_component(refname, flags);
@@ -1093,8 +1096,13 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
 	if (flag)
 		*flag = 0;
 
-	if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
-		return NULL;
+	if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) < 0) {
+		/* Handle the pseudo-ref @ */
+		if (!strcmp(refname, "@"))
+			refname = "HEAD";
+		else
+			return NULL;
+	}
 
 	for (;;) {
 		char path[PATH_MAX];
diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
index e415ee0..ee93979 100755
--- a/t/t1400-update-ref.sh
+++ b/t/t1400-update-ref.sh
@@ -127,6 +127,9 @@ test_expect_success '(not) change HEAD with wrong SHA1' "
 test_expect_success "(not) changed .git/$m" "
 	! test $B"' = $(cat .git/'"$m"')
 '
+test_expect_success 'disallow creating a ref with name @' '
+	test_must_fail git update-ref @ HEAD
+'
 rm -f .git/$m
 
 : a repository with working tree always has reflog these days...
diff --git a/t/t1508-at-combinations.sh b/t/t1508-at-combinations.sh
index 6a69f9e..c50c3f7 100755
--- a/t/t1508-at-combinations.sh
+++ b/t/t1508-at-combinations.sh
@@ -65,9 +65,7 @@ check "H@{1}" commit new-one
 check "H@{now}" commit new-two
 check "H@{u}" refs/heads/upstream-branch
 
-# To make sure that the @-parser isn't buggy, check things with the
-# symbolic-ref @
-git symbolic-ref @ HEAD
+# Check everything with pseudo-ref @
 check "@@{1}" commit new-one
 check "@@{now}" commit new-two
 check "@@{u}" refs/heads/upstream-branch
-- 
1.8.3.rc0.40.g09a0447
Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 21 of 24 in “A natural solution to the @ -> HEAD problem”
  1. 0/9 A natural solution to the @ -> HEAD problemRamkumar Ramachandra, May 2, 2013
  2. 1/9 t1508 (at-combinations): simplify setupRamkumar Ramachandra, May 2, 2013
  3. 2/9 t1508 (at-combinations): test branches separatelyRamkumar Ramachandra, May 2, 2013
  4. Felipe ContrerasMay 2, 2013
  5. Ramkumar RamachandraMay 2, 2013
  6. Felipe ContrerasMay 2, 2013
  7. 3/9 t1508 (at-combinations): improve nonsense()Ramkumar Ramachandra, May 2, 2013
  8. 4/9 t1508 (at-combinations): increase coverageRamkumar Ramachandra, May 2, 2013
  9. Eric SunshineMay 2, 2013
  10. 5/9 t1508 (at-combinations): document @{N} versus HEAD@{N}Ramkumar Ramachandra, May 2, 2013
  11. Felipe ContrerasMay 2, 2013
  12. Ramkumar RamachandraMay 2, 2013
  13. Felipe ContrerasMay 2, 2013
  14. Ramkumar RamachandraMay 2, 2013
  15. 6/9 t1508 (at-combinations): test with symbolic refsRamkumar Ramachandra, May 2, 2013
  16. 7/9 remote.c: teach branch_get() to treat symrefs other than HEADRamkumar Ramachandra, May 2, 2013
  17. Ramkumar RamachandraMay 2, 2013
  18. Ramkumar RamachandraMay 2, 2013
  19. Felipe ContrerasMay 2, 2013
  20. 8/9 sha1_name.c: fix @-parsing bug in interpret_branch_name()Ramkumar Ramachandra, May 2, 2013
  21. 9/9 refs.c: make @ a pseudo-ref alias to HEADRamkumar Ramachandra, May 2, 2013
  22. Ramkumar RamachandraMay 2, 2013
  23. Felipe ContrerasMay 2, 2013
  24. Ramkumar RamachandraMay 2, 2013

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.