Add new @ shortcut for HEAD

Typing 'HEAD' is tedious, especially when we can use '@' instead. The reason for choosing '@' is that it follows naturally from the ref@op syntax (e.g. HEAD@{u}), except we have no ref, and no operation, and when we don't have those, it makes sens to assume 'HEAD'. So now we can use 'git show @~1', and all that goody goodness. Until now '@' was a valid name, but it conflicts with this idea, so let's make it invalid. Probably very few people, if any, used this name. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed May 7, 2013 at 17:04 UTC cdfd94837b27c220f70f032b596ea993d195488f
5 files changed +28
Documentation/git-check-ref-format.txt
+2
@@ -54,6 +54,8 @@ Git imposes the following rules on how references are named:
54
55 . They cannot contain a sequence `@{`.
56
57 +. They cannot be the single character `@`.
58 +
59 . They cannot contain a `\`.
60
61 These rules make it easy for shell script based tools to parse
Documentation/revisions.txt
+3
@@ -58,6 +58,9 @@ the '$GIT_DIR/refs' directory or from the '$GIT_DIR/packed-refs' file.
58 While the ref name encoding is unspecified, UTF-8 is preferred as
59 some output processing may assume ref names in UTF-8.
60
61 +'@'::
62 + '@' alone is a shortcut for 'HEAD'.
63 +
64 '<refname>@\{<date>\}', e.g. 'master@\{yesterday\}', 'HEAD@\{5 minutes ago\}'::
65 A ref followed by the suffix '@' with a date specification
66 enclosed in a brace
refs.c
+4
@@ -72,6 +72,10 @@ int check_refname_format(const char *refname, int flags)
72 {
73 int component_len, component_count = 0;
74
75 + if (!strcmp(refname, "@"))
76 + /* Refname is a single character '@'. */
77 + return -1;
78 +
79 while (1) {
80 /* We are at the start of a path component. */
81 component_len = check_refname_component(refname, flags);
sha1_name.c
+17
@@ -972,6 +972,17 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
972 return st;
973 }
974
975 +/* parse @something syntax, when 'something' is not {.*} */
976 +static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
977 +{
978 + if (len || name[1] == '{')
979 + return -1;
980 +
981 + strbuf_reset(buf);
982 + strbuf_add(buf, "HEAD", 4);
983 + return 1;
984 +}
985 +
986 static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf)
987 {
988 /* we have extra data, which might need further processing */
@@ -1032,9 +1043,15 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
1043 cp = strchr(name, '@');
1044 if (!cp)
1045 return -1;
1046 +
1047 + len = interpret_empty_at(name, namelen, cp - name, buf);
1048 + if (len > 0)
1049 + return reinterpret(name, namelen, len, buf);
1050 +
1051 tmp_len = upstream_mark(cp, namelen - (cp - name));
1052 if (!tmp_len)
1053 return -1;
1054 +
1055 len = cp + tmp_len - name;
1056 cp = xstrndup(name, cp - name);
1057 upstream = branch_get(*cp ? cp : NULL);
t/t1508-at-combinations.sh
+2
@@ -55,6 +55,8 @@ check "HEAD@{u}" ref refs/heads/upstream-branch
55 check "@{u}@{1}" commit upstream-one
56 check "@{-1}@{u}" ref refs/heads/master
57 check "@{-1}@{u}@{1}" commit master-one
58 +check "@" commit new-two
59 +check "@@{u}" ref refs/heads/upstream-branch
60 nonsense "@{u}@{-1}"
61 nonsense "@{0}@{0}"
62 nonsense "@{1}@{u}"