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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 15, 2014 at 03:40 UTC 9892d5d4541d93a8a6a4fd9ac4178d71d0d308e3
2 files changed +32 -9
sha1_name.c
+11 -9
@@ -1126,6 +1126,7 @@ static int interpret_upstream_mark(const char *name, int namelen,
1126 int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
1127 {
1128 char *at;
1129 + const char *start;
1130 int len = interpret_nth_prior_checkout(name, namelen, buf);
1131
1132 if (!namelen)
@@ -1140,17 +1141,18 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
1141 return reinterpret(name, namelen, len, buf);
1142 }
1143
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) {
1147
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);
1151
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 + }
1156
1157 return -1;
1158 }
t/t1507-rev-parse-upstream.sh
+21
@@ -17,6 +17,9 @@ test_expect_success 'setup' '
17 test_commit 4 &&
18 git branch --track my-side origin/side &&
19 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 &&
23 git remote add -t master master-only .. &&
24 git fetch master-only &&
25 git branch bad-upstream &&
@@ -54,6 +57,24 @@ test_expect_success 'my-side@{upstream} resolves to correct full name' '
57 test refs/remotes/origin/side = "$(full_name my-side@{u})"
58 '
59
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 +
78 test_expect_success 'refs/heads/my-side@{upstream} does not resolve to my-side{upstream}' '
79 test_must_fail full_name refs/heads/my-side@{upstream}
80 '