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

[PATCH] Add new @ shortcut for HEAD

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 29, 2013, 19:35 UTC
Message-ID
<1367264106-2351-1-git-send-email-felipe.contreras@gmail.com>

So HEAD@{0}~0^0 is too much to type, but we can remove '^0', and we can remove '~0', and we can remove 'HEAD', but we can't remove '{0}'?

This patch allows 'HEAD@' to be the same as 'HEAD@{0}', and similarly with 'master@'.

So we can type '@' instead of 'HEAD@', or rather 'HEAD'. So now we can use 'git show @~1', and all that goody goodness.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---

People have complained about the lack of a HEAD shortcut before, and some proposals have been made, but I think this one is a clean trick in accordance with our current syntax.

 Documentation/revisions.txt | 3 +++
 sha1_name.c                 | 8 +++++++-
 t/t1503-rev-parse-verify.sh | 9 +++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/Documentation/revisions.txt b/Documentation/revisions.txt
index d477b3f..6315ce5 100644
--- a/Documentation/revisions.txt
+++ b/Documentation/revisions.txt
@@ -58,6 +58,9 @@ the '$GIT_DIR/refs' directory or from the '$GIT_DIR/packed-refs' file.
 While the ref name encoding is unspecified, UTF-8 is preferred as
 some output processing may assume ref names in UTF-8.
 
+'@'::
+  '@' alone is a shortcut for 'HEAD'
+
 '<refname>@\{<date>\}', e.g. 'master@\{yesterday\}', 'HEAD@\{5 minutes ago\}'::
   A ref followed by the suffix '@' with a date specification
   enclosed in a brace
diff --git a/sha1_name.c b/sha1_name.c
index 3820f28..d7c3d6f 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -437,11 +437,15 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 	static const char *warn_msg = "refname '%.*s' is ambiguous.";
 	char *real_ref = NULL;
 	int refs_found = 0;
-	int at, reflog_len;
+	int at, reflog_len, empty_at;
 
 	if (len == 40 && !get_sha1_hex(str, sha1))
 		return 0;
 
+	empty_at = len && str[len-1] == '@';
+	if (empty_at)
+		len = len-1;
+
 	/* basic@{time or number or -number} format to query ref-log */
 	reflog_len = at = 0;
 	if (len && str[len-1] == '}') {
@@ -475,6 +479,8 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 		refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
 	} else if (reflog_len)
 		refs_found = dwim_log(str, len, sha1, &real_ref);
+	else if (!len && empty_at)
+		refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
 	else
 		refs_found = dwim_ref(str, len, sha1, &real_ref);
 
diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh
index 813cc1b..426c63f 100755
--- a/t/t1503-rev-parse-verify.sh
+++ b/t/t1503-rev-parse-verify.sh
@@ -115,4 +115,13 @@ test_expect_success 'master@{n} for various n' '
 	test_must_fail git rev-parse --verify master@{$Np1}
 '
 
+test_expect_success 'empty @' '
+	rev_hash=$(git rev-parse --verify @) &&
+	test "$rev_hash" = "$HASH4" &&
+	rev_hash=$(git rev-parse --verify HEAD@) &&
+	test "$rev_hash" = "$HASH4" &&
+	rev_hash=$(git rev-parse --verify master@) &&
+	test "$rev_hash" = "$HASH4"
+'
+
 test_done
-- 
1.8.2.1.1031.g2ee5873
Next: Ramkumar Ramachandra
Message 1 of 56 in “Add new @ shortcut for HEAD”
  1. Add new @ shortcut for HEADFelipe Contreras, Apr 29, 2013
  2. Ramkumar RamachandraApr 29, 2013
  3. Junio C HamanoApr 29, 2013
  4. Felipe ContrerasApr 29, 2013
  5. Junio C HamanoApr 29, 2013
  6. Felipe ContrerasApr 29, 2013
  7. Junio C HamanoApr 29, 2013
  8. Junio C HamanoApr 29, 2013
  9. Felipe ContrerasApr 29, 2013
  10. Felipe ContrerasApr 29, 2013
  11. Junio C HamanoApr 29, 2013
  12. Jonathan NiederApr 30, 2013
  13. Junio C HamanoApr 30, 2013
  14. Felipe ContrerasApr 30, 2013
  15. Junio C HamanoApr 29, 2013
  16. Felipe ContrerasApr 29, 2013
  17. Junio C HamanoApr 29, 2013
  18. Felipe ContrerasApr 29, 2013
  19. Duy NguyenApr 30, 2013
  20. Felipe ContrerasApr 30, 2013
  21. Duy NguyenApr 30, 2013
  22. Felipe ContrerasApr 30, 2013
  23. Duy NguyenApr 30, 2013
  24. Felipe ContrerasApr 30, 2013
  25. Michael HaggertyApr 30, 2013
  26. Ramkumar RamachandraApr 30, 2013
  27. Duy NguyenApr 30, 2013
  28. Ramkumar RamachandraApr 30, 2013
  29. Ramkumar RamachandraApr 30, 2013
  30. Duy NguyenApr 30, 2013
  31. Junio C HamanoApr 30, 2013
  32. Ramkumar RamachandraApr 30, 2013
  33. Ramkumar RamachandraApr 30, 2013
  34. Junio C HamanoApr 30, 2013
  35. Ramkumar RamachandraApr 30, 2013
  36. Felipe ContrerasApr 30, 2013
  37. Ramkumar RamachandraApr 30, 2013
  38. Felipe ContrerasApr 30, 2013
  39. Felipe ContrerasApr 30, 2013
  40. Jeff KingApr 30, 2013
  41. Felipe ContrerasApr 30, 2013
  42. Junio C HamanoApr 30, 2013
  43. Ramkumar RamachandraApr 30, 2013
  44. Junio C HamanoApr 30, 2013
  45. Ramkumar RamachandraApr 30, 2013
  46. Junio C HamanoApr 30, 2013
  47. Felipe ContrerasApr 30, 2013
  48. Junio C HamanoApr 30, 2013
  49. Felipe ContrerasApr 30, 2013
  50. Junio C HamanoApr 30, 2013
  51. Felipe ContrerasApr 30, 2013
  52. Felipe ContrerasApr 30, 2013
  53. Felipe ContrerasApr 30, 2013
  54. Junio C HamanoApr 30, 2013
  55. Duy NguyenApr 30, 2013
  56. Felipe ContrerasApr 30, 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.