Skip to content

Commit

Permalink
interpret_branch_name: find all possible @-marks
Browse files Browse the repository at this point in the history
When we parse a string like "foo@{upstream}", we look for
the first "@"-sign, and check to see if it is an upstream
mark. However, since branch names can contain an @, we may
also see "@foo@{upstream}". In this case, we check only the
first @, and ignore the second. As a result, we do not find
the upstream.

We can solve this by iterating through all @-marks in the
string, and seeing if any is a legitimate upstream or
empty-at mark.

Another strategy would be to parse from the right-hand side
of the string. However, that does not work for the
"empty_at" case, which allows "@@{upstream}". We need to
find the left-most one in this case (and we then recurse as
"HEAD@{upstream}").

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Jan 15, 2014
1 parent 3f6eb30 commit 9892d5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
20 changes: 11 additions & 9 deletions sha1_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ static int interpret_upstream_mark(const char *name, int namelen,
int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
{
char *at;
const char *start;
int len = interpret_nth_prior_checkout(name, namelen, buf);

if (!namelen)
Expand All @@ -1140,17 +1141,18 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
return reinterpret(name, namelen, len, buf);
}

at = memchr(name, '@', namelen);
if (!at)
return -1;
for (start = name;
(at = memchr(start, '@', namelen - (start - name)));
start = at + 1) {

len = interpret_empty_at(name, namelen, at - name, buf);
if (len > 0)
return reinterpret(name, namelen, len, buf);
len = interpret_empty_at(name, namelen, at - name, buf);
if (len > 0)
return reinterpret(name, namelen, len, buf);

len = interpret_upstream_mark(name, namelen, at - name, buf);
if (len > 0)
return len;
len = interpret_upstream_mark(name, namelen, at - name, buf);
if (len > 0)
return len;
}

return -1;
}
Expand Down
21 changes: 21 additions & 0 deletions t/t1507-rev-parse-upstream.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ test_expect_success 'setup' '
test_commit 4 &&
git branch --track my-side origin/side &&
git branch --track local-master master &&
git branch --track fun@ny origin/side &&
git branch --track @funny origin/side &&
git branch --track funny@ origin/side &&
git remote add -t master master-only .. &&
git fetch master-only &&
git branch bad-upstream &&
Expand Down Expand Up @@ -54,6 +57,24 @@ test_expect_success 'my-side@{upstream} resolves to correct full name' '
test refs/remotes/origin/side = "$(full_name my-side@{u})"
'

test_expect_success 'upstream of branch with @ in middle' '
full_name fun@ny@{u} >actual &&
echo refs/remotes/origin/side >expect &&
test_cmp expect actual
'

test_expect_success 'upstream of branch with @ at start' '
full_name @funny@{u} >actual &&
echo refs/remotes/origin/side >expect &&
test_cmp expect actual
'

test_expect_success 'upstream of branch with @ at end' '
full_name funny@@{u} >actual &&
echo refs/remotes/origin/side >expect &&
test_cmp expect actual
'

test_expect_success 'refs/heads/my-side@{upstream} does not resolve to my-side{upstream}' '
test_must_fail full_name refs/heads/my-side@{upstream}
'
Expand Down

0 comments on commit 9892d5d

Please sign in to comment.