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
Sep 2, 2013 at 01:34 UTC
9ba89f484e023827eca6ad44baf69af37dac4db3
5 files changed
+45
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
+28
@@ -1004,6 +1004,28 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
1004
return st;
1005
}
1006
1007
+/* parse @something syntax, when 'something' is not {.*} */
1008
+static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1009
+{
1010
+ const char *next;
1011
+
1012
+ if (len || name[1] == '{')
1013
+ return -1;
1014
+
1015
+ /* make sure it's a single @, or @@{.*}, not @foo */
1016
+ next = strchr(name + len + 1, '@');
1017
+ if (next && next[1] != '{')
1018
+ return -1;
1019
+ if (!next)
1020
+ next = name + namelen;
1021
+ if (next != name + 1)
1022
+ return -1;
1023
+
1024
+ strbuf_reset(buf);
1025
+ strbuf_add(buf, "HEAD", 4);
1026
+ return 1;
1027
+}
1028
+
1029
static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf)
1030
{
1031
/* we have extra data, which might need further processing */
@@ -1068,9 +1090,15 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
1090
cp = strchr(name, '@');
1091
if (!cp)
1092
return -1;
1093
+
1094
+ len = interpret_empty_at(name, namelen, cp - name, buf);
1095
+ if (len > 0)
1096
+ return reinterpret(name, namelen, len, buf);
1097
+
1098
tmp_len = upstream_mark(cp, namelen - (cp - name));
1099
if (!tmp_len)
1100
return -1;
1101
+
1102
len = cp + tmp_len - name;
1103
cp = xstrndup(name, cp - name);
1104
upstream = branch_get(*cp ? cp : NULL);
t/t1508-at-combinations.sh
+8
@@ -32,6 +32,9 @@ test_expect_success 'setup' '
32
git checkout -b upstream-branch &&
33
test_commit upstream-one &&
34
test_commit upstream-two &&
35
+ git checkout -b @/at-test &&
36
+ git checkout -b @@/at-test &&
37
+ git checkout -b @at-test &&
38
git checkout -b old-branch &&
39
test_commit old-one &&
40
test_commit old-two &&
@@ -55,6 +58,11 @@ check "HEAD@{u}" ref refs/heads/upstream-branch
58
check "@{u}@{1}" commit upstream-one
59
check "@{-1}@{u}" ref refs/heads/master
60
check "@{-1}@{u}@{1}" commit master-one
61
+check "@" commit new-two
62
+check "@@{u}" ref refs/heads/upstream-branch
63
+check "@@/at-test" ref refs/heads/@@/at-test
64
+check "@/at-test" ref refs/heads/@/at-test
65
+check "@at-test" ref refs/heads/@at-test
66
nonsense "@{u}@{-1}"
67
nonsense "@{0}@{0}"
68
nonsense "@{1}@{u}"