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

Re: [PATCH] refs.c: interpret @ as HEAD

From
Duy Nguyen <pclouds@gmail.com>
Date
Apr 30, 2013, 15:04 UTC
Message-ID
<20130430150430.GA13398@lanh>
In-Reply-To
<87zjwguq8t.fsf@linux-k42r.v.cablecom.net>
On Tue, Apr 30, 2013 at 03:01:22PM +0200, Thomas Rast wrote:
Show 6 quoted lines
> > This patch has the exact same effect as:
> >
> >     $ git symbolic-ref @ HEAD
> 
> But then why don't you just 'git symbolic-ref H HEAD' for a sort of
> "local alias"?

For me, it's because I don't want to do that on every repo. One day if I change my mind and make P the new alias, i'll need to update every repo again.

Show 7 quoted lines
> 
> What annoys me more is that there's no way to say
> 
>   git symbolic-ref U @{u}
> 
> so that I can avoid that -- it's really clumsy to type on a Swiss German
> keyboard.  We'd need some sort of ref-alias feature for that to work.

It's not hard to do. The below patch makes "." equivalent to HEAD and ".U" -> "@{u}". Refs are not supposed to have '.' at the beginning, so it's easy to figure somebody is using alias from what they send you. Supporting refalias.* config should be easy.

-- 8< --
diff --git a/sha1_name.c b/sha1_name.c
index 3820f28..7842147 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -1239,6 +1239,27 @@ static char *resolve_relative_path(const char *rel)
 			   rel);
 }
 
+static int get_sha1_with_alias(const char *name, int namelen,
+			       unsigned char *sha1, int flags)
+{
+	int ret;
+	struct strbuf sb = STRBUF_INIT;
+	int alias_len = strcspn(name, "@^~:");
+	if (alias_len > namelen)
+		alias_len = namelen;
+
+	if (alias_len == 0)	/* '.' is equal to HEAD */
+		strbuf_addstr(&sb, "HEAD");
+	else if (alias_len == 1 && !strncmp(name, "U", 1))
+		strbuf_addstr(&sb, "@{u}");
+	else
+		return error("unknown alias %.*s", alias_len, name);
+	strbuf_addstr(&sb, name + alias_len);
+	ret = get_sha1_1(sb.buf, sb.len, sha1, flags);
+	strbuf_release(&sb);
+	return ret;
+}
+
 static int get_sha1_with_context_1(const char *name,
 				   unsigned flags,
 				   const char *prefix,
@@ -1252,6 +1273,8 @@ static int get_sha1_with_context_1(const char *name,
 
 	memset(oc, 0, sizeof(*oc));
 	oc->mode = S_IFINVALID;
+	if (name[0] == '.')
+		return get_sha1_with_alias(name + 1, namelen - 1, sha1, flags);
 	ret = get_sha1_1(name, namelen, sha1, flags);
 	if (!ret)
 		return ret;
-- 8< --
Previous: Ramkumar RamachandraNext: Michael Haggerty
Message 5 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.