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

[PATCH] refs.c: interpret @ as HEAD

From
Ramkumar Ramachandra <artagnon@gmail.com>
Date
Apr 30, 2013, 12:24 UTC
Message-ID
<1367324685-22788-1-git-send-email-artagnon@gmail.com>

The rev spec forms @{}, .., ... fill in HEAD as the missing argument automatically. Unfortunately, HEAD~<n> is a very common idiom and there is no way to make HEAD implicit here (due the shell expansion of ~<n>).

However, there is an alternative solution to the issue: overload the character @ to mean HEAD. Do this at the lowest possible layer of abstraction: in dwim_ref(), substitute @ with HEAD just before calling resolve_ref_unsafe(). The program will only reach this point after the other specs like ~, ^ and @{} have been resolved; therefore, it is safe to do it here.

This patch has the exact same effect as:
    $ git symbolic-ref @ HEAD

It means that you can now do @~1, @^2, and even topic..@. However, since the @-parsing happens before we ever reach the symref resolution, @@{u} is invalid. But this is okay, since @{u} already has an implicit HEAD in it.

Inspired-by: Felipe Contreras <felipe.contreras@gmail.com>
Inspired-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 I haven't included documentation/ tests because I want feedback on
 this two-liner first.
 refs.c | 2 ++
 1 file changed, 2 insertions(+)
diff --git a/refs.c b/refs.c
index de2d8eb..cb67b73 100644
--- a/refs.c
+++ b/refs.c
@@ -1604,6 +1604,8 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
 
 		this_result = refs_found ? sha1_from_ref : sha1;
 		mksnpath(fullref, sizeof(fullref), *p, len, str);
+		if (!strcmp(fullref, "@"))
+			mksnpath(fullref, sizeof(fullref), *p, 4, "HEAD");
 		r = resolve_ref_unsafe(fullref, this_result, 1, &flag);
 		if (r) {
 			if (!refs_found++)
-- 
1.8.2.1.628.gcd33b41.dirty
Next: Duy Nguyen
Message 1 of 21 in “refs.c: interpret @ as HEAD”
  1. refs.c: interpret @ as HEADRamkumar Ramachandra, Apr 30, 2013
  2. Duy NguyenApr 30, 2013
  3. Thomas RastApr 30, 2013
  4. Ramkumar RamachandraApr 30, 2013
  5. Duy NguyenApr 30, 2013
  6. Michael HaggertyApr 30, 2013
  7. Junio C HamanoApr 30, 2013
  8. Duy NguyenApr 30, 2013
  9. Ramkumar RamachandraApr 30, 2013
  10. Duy NguyenMay 1, 2013
  11. Thomas RastMay 1, 2013
  12. Ramkumar RamachandraApr 30, 2013
  13. Ramkumar RamachandraApr 30, 2013
  14. Felipe ContrerasApr 30, 2013
  15. Ramkumar RamachandraApr 30, 2013
  16. Junio C HamanoApr 30, 2013
  17. Ramkumar RamachandraApr 30, 2013
  18. Ramkumar RamachandraApr 30, 2013
  19. Ramkumar RamachandraApr 30, 2013
  20. Ramkumar RamachandraApr 30, 2013
  21. 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.