Merge branch 'fc/at-head'

Instead of typing four capital letters "HEAD", you can say "@" now,
e.g. "git log @".

* fc/at-head:
  Add new @ shortcut for HEAD
  sha1-name: pass len argument to interpret_branch_name()
This commit is contained in:
Junio C Hamano 2013-09-20 12:38:10 -07:00
commit f406140baa
7 changed files with 54 additions and 7 deletions

View File

@ -54,6 +54,8 @@ Git imposes the following rules on how references are named:
. They cannot contain a sequence `@{`.
. They cannot be the single character `@`.
. They cannot contain a `\`.
These rules make it easy for shell script based tools to parse

View File

@ -58,6 +58,9 @@ the '$GIT_DIR/refs' directory or from the '$GIT_DIR/packed-refs' file.
While the ref name encoding is unspecified, UTF-8 is preferred as
some output processing may assume ref names in UTF-8.
'@'::
'@' alone is a shortcut for 'HEAD'.
'<refname>@\{<date>\}', e.g. 'master@\{yesterday\}', 'HEAD@\{5 minutes ago\}'::
A ref followed by the suffix '@' with a date specification
enclosed in a brace

View File

@ -880,7 +880,7 @@ extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, i
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
extern int interpret_branch_name(const char *str, struct strbuf *);
extern int interpret_branch_name(const char *str, int len, struct strbuf *);
extern int get_sha1_mb(const char *str, unsigned char *sha1);
extern int refname_match(const char *abbrev_name, const char *full_name, const char **rules);

6
refs.c
View File

@ -72,6 +72,10 @@ int check_refname_format(const char *refname, int flags)
{
int component_len, component_count = 0;
if (!strcmp(refname, "@"))
/* Refname is a single character '@'. */
return -1;
while (1) {
/* We are at the start of a path component. */
component_len = check_refname_component(refname, flags);
@ -1951,7 +1955,7 @@ static int remove_empty_directories(const char *file)
static char *substitute_branch_name(const char **string, int *len)
{
struct strbuf buf = STRBUF_INIT;
int ret = interpret_branch_name(*string, &buf);
int ret = interpret_branch_name(*string, *len, &buf);
if (ret == *len) {
size_t size;

View File

@ -200,7 +200,7 @@ static void add_pending_object_with_mode(struct rev_info *revs,
revs->no_walk = 0;
if (revs->reflog_info && obj->type == OBJ_COMMIT) {
struct strbuf buf = STRBUF_INIT;
int len = interpret_branch_name(name, &buf);
int len = interpret_branch_name(name, 0, &buf);
int st;
if (0 < len && name[len] && buf.len)

View File

@ -1006,6 +1006,28 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
return st;
}
/* parse @something syntax, when 'something' is not {.*} */
static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
{
const char *next;
if (len || name[1] == '{')
return -1;
/* make sure it's a single @, or @@{.*}, not @foo */
next = strchr(name + len + 1, '@');
if (next && next[1] != '{')
return -1;
if (!next)
next = name + namelen;
if (next != name + 1)
return -1;
strbuf_reset(buf);
strbuf_add(buf, "HEAD", 4);
return 1;
}
static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf)
{
/* we have extra data, which might need further processing */
@ -1014,7 +1036,7 @@ static int reinterpret(const char *name, int namelen, int len, struct strbuf *bu
int ret;
strbuf_add(buf, name + len, namelen - len);
ret = interpret_branch_name(buf->buf, &tmp);
ret = interpret_branch_name(buf->buf, buf->len, &tmp);
/* that data was not interpreted, remove our cruft */
if (ret < 0) {
strbuf_setlen(buf, used);
@ -1048,14 +1070,16 @@ static int reinterpret(const char *name, int namelen, int len, struct strbuf *bu
* If the input was ok but there are not N branch switches in the
* reflog, it returns 0.
*/
int interpret_branch_name(const char *name, struct strbuf *buf)
int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
{
char *cp;
struct branch *upstream;
int namelen = strlen(name);
int len = interpret_nth_prior_checkout(name, buf);
int tmp_len;
if (!namelen)
namelen = strlen(name);
if (!len) {
return len; /* syntax Ok, not enough switches */
} else if (len > 0) {
@ -1068,9 +1092,15 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
cp = strchr(name, '@');
if (!cp)
return -1;
len = interpret_empty_at(name, namelen, cp - name, buf);
if (len > 0)
return reinterpret(name, namelen, len, buf);
tmp_len = upstream_mark(cp, namelen - (cp - name));
if (!tmp_len)
return -1;
len = cp + tmp_len - name;
cp = xstrndup(name, cp - name);
upstream = branch_get(*cp ? cp : NULL);
@ -1102,7 +1132,7 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
int strbuf_branchname(struct strbuf *sb, const char *name)
{
int len = strlen(name);
int used = interpret_branch_name(name, sb);
int used = interpret_branch_name(name, len, sb);
if (used == len)
return 0;

View File

@ -32,6 +32,9 @@ test_expect_success 'setup' '
git checkout -b upstream-branch &&
test_commit upstream-one &&
test_commit upstream-two &&
git checkout -b @/at-test &&
git checkout -b @@/at-test &&
git checkout -b @at-test &&
git checkout -b old-branch &&
test_commit old-one &&
test_commit old-two &&
@ -55,6 +58,11 @@ check "HEAD@{u}" ref refs/heads/upstream-branch
check "@{u}@{1}" commit upstream-one
check "@{-1}@{u}" ref refs/heads/master
check "@{-1}@{u}@{1}" commit master-one
check "@" commit new-two
check "@@{u}" ref refs/heads/upstream-branch
check "@@/at-test" ref refs/heads/@@/at-test
check "@/at-test" ref refs/heads/@/at-test
check "@at-test" ref refs/heads/@at-test
nonsense "@{u}@{-1}"
nonsense "@{0}@{0}"
nonsense "@{1}@{u}"