Skip to content

Commit 9ba89f4

Browse files
felipecgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf99a76 commit 9ba89f4

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

Documentation/git-check-ref-format.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Git imposes the following rules on how references are named:
5454

5555
. They cannot contain a sequence `@{`.
5656

57+
. They cannot be the single character `@`.
58+
5759
. They cannot contain a `\`.
5860

5961
These rules make it easy for shell script based tools to parse

Documentation/revisions.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ the '$GIT_DIR/refs' directory or from the '$GIT_DIR/packed-refs' file.
5858
While the ref name encoding is unspecified, UTF-8 is preferred as
5959
some output processing may assume ref names in UTF-8.
6060

61+
'@'::
62+
'@' alone is a shortcut for 'HEAD'.
63+
6164
'<refname>@\{<date>\}', e.g. 'master@\{yesterday\}', 'HEAD@\{5 minutes ago\}'::
6265
A ref followed by the suffix '@' with a date specification
6366
enclosed in a brace

refs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ int check_refname_format(const char *refname, int flags)
7272
{
7373
int component_len, component_count = 0;
7474

75+
if (!strcmp(refname, "@"))
76+
/* Refname is a single character '@'. */
77+
return -1;
78+
7579
while (1) {
7680
/* We are at the start of a path component. */
7781
component_len = check_refname_component(refname, flags);

sha1_name.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,28 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
10041004
return st;
10051005
}
10061006

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+
10071029
static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf)
10081030
{
10091031
/* 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)
10681090
cp = strchr(name, '@');
10691091
if (!cp)
10701092
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+
10711098
tmp_len = upstream_mark(cp, namelen - (cp - name));
10721099
if (!tmp_len)
10731100
return -1;
1101+
10741102
len = cp + tmp_len - name;
10751103
cp = xstrndup(name, cp - name);
10761104
upstream = branch_get(*cp ? cp : NULL);

t/t1508-at-combinations.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ test_expect_success 'setup' '
3232
git checkout -b upstream-branch &&
3333
test_commit upstream-one &&
3434
test_commit upstream-two &&
35+
git checkout -b @/at-test &&
36+
git checkout -b @@/at-test &&
37+
git checkout -b @at-test &&
3538
git checkout -b old-branch &&
3639
test_commit old-one &&
3740
test_commit old-two &&
@@ -55,6 +58,11 @@ check "HEAD@{u}" ref refs/heads/upstream-branch
5558
check "@{u}@{1}" commit upstream-one
5659
check "@{-1}@{u}" ref refs/heads/master
5760
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
5866
nonsense "@{u}@{-1}"
5967
nonsense "@{0}@{0}"
6068
nonsense "@{1}@{u}"

0 commit comments

Comments
 (0)