Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 9892d5d

Browse files
peffgitster
authored andcommitted
interpret_branch_name: find all possible @-marks
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f6eb30 commit 9892d5d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

sha1_name.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ static int interpret_upstream_mark(const char *name, int namelen,
11261126
int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
11271127
{
11281128
char *at;
1129+
const char *start;
11291130
int len = interpret_nth_prior_checkout(name, namelen, buf);
11301131

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

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

1147-
len = interpret_empty_at(name, namelen, at - name, buf);
1148-
if (len > 0)
1149-
return reinterpret(name, namelen, len, buf);
1148+
len = interpret_empty_at(name, namelen, at - name, buf);
1149+
if (len > 0)
1150+
return reinterpret(name, namelen, len, buf);
11501151

1151-
len = interpret_upstream_mark(name, namelen, at - name, buf);
1152-
if (len > 0)
1153-
return len;
1152+
len = interpret_upstream_mark(name, namelen, at - name, buf);
1153+
if (len > 0)
1154+
return len;
1155+
}
11541156

11551157
return -1;
11561158
}

t/t1507-rev-parse-upstream.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ test_expect_success 'setup' '
1717
test_commit 4 &&
1818
git branch --track my-side origin/side &&
1919
git branch --track local-master master &&
20+
git branch --track fun@ny origin/side &&
21+
git branch --track @funny origin/side &&
22+
git branch --track funny@ origin/side &&
2023
git remote add -t master master-only .. &&
2124
git fetch master-only &&
2225
git branch bad-upstream &&
@@ -54,6 +57,24 @@ test_expect_success 'my-side@{upstream} resolves to correct full name' '
5457
test refs/remotes/origin/side = "$(full_name my-side@{u})"
5558
'
5659

60+
test_expect_success 'upstream of branch with @ in middle' '
61+
full_name fun@ny@{u} >actual &&
62+
echo refs/remotes/origin/side >expect &&
63+
test_cmp expect actual
64+
'
65+
66+
test_expect_success 'upstream of branch with @ at start' '
67+
full_name @funny@{u} >actual &&
68+
echo refs/remotes/origin/side >expect &&
69+
test_cmp expect actual
70+
'
71+
72+
test_expect_success 'upstream of branch with @ at end' '
73+
full_name funny@@{u} >actual &&
74+
echo refs/remotes/origin/side >expect &&
75+
test_cmp expect actual
76+
'
77+
5778
test_expect_success 'refs/heads/my-side@{upstream} does not resolve to my-side{upstream}' '
5879
test_must_fail full_name refs/heads/my-side@{upstream}
5980
'

0 commit comments

Comments
 (0)