Skip to content

Commit cdfd948

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 7a0a49a commit cdfd948

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,17 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
972972
return st;
973973
}
974974

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+
975986
static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf)
976987
{
977988
/* we have extra data, which might need further processing */
@@ -1032,9 +1043,15 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
10321043
cp = strchr(name, '@');
10331044
if (!cp)
10341045
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+
10351051
tmp_len = upstream_mark(cp, namelen - (cp - name));
10361052
if (!tmp_len)
10371053
return -1;
1054+
10381055
len = cp + tmp_len - name;
10391056
cp = xstrndup(name, cp - name);
10401057
upstream = branch_get(*cp ? cp : NULL);

t/t1508-at-combinations.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ check "HEAD@{u}" ref refs/heads/upstream-branch
5555
check "@{u}@{1}" commit upstream-one
5656
check "@{-1}@{u}" ref refs/heads/master
5757
check "@{-1}@{u}@{1}" commit master-one
58+
check "@" commit new-two
59+
check "@@{u}" ref refs/heads/upstream-branch
5860
nonsense "@{u}@{-1}"
5961
nonsense "@{0}@{0}"
6062
nonsense "@{1}@{u}"

0 commit comments

Comments
 (0)