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

Commit 3f6eb30

Browse files
peffgitster
authored andcommitted
interpret_branch_name: avoid @{upstream} past colon
get_sha1() cannot currently parse a valid object name like "HEAD:@{upstream}" (assuming that such an oddly named file exists in the HEAD commit). It takes two passes to parse the string: 1. It first considers the whole thing as a ref, which results in looking for the upstream of "HEAD:". 2. It finds the colon, parses "HEAD" as a tree-ish, and then finds the path "@{upstream}" in the tree. For a path that looks like a normal reflog (e.g., "HEAD:@{yesterday}"), the first pass is a no-op. We try to dwim_ref("HEAD:"), that returns zero refs, and we proceed with colon-parsing. For "HEAD:@{upstream}", though, the first pass ends up in interpret_upstream_mark, which tries to find the branch "HEAD:". When it sees that the branch does not exist, it actually dies rather than returning an error to the caller. As a result, we never make it to the second pass. One obvious way of fixing this would be to teach interpret_upstream_mark to simply report "no, this isn't an upstream" in such a case. However, that would make the error-reporting for legitimate upstream cases significantly worse. Something like "bogus@{upstream}" would simply report "unknown revision: bogus@{upstream}", while the current code diagnoses a wide variety of possible misconfigurations (no such branch, branch exists but does not have upstream, etc). However, we can take advantage of the fact that a branch name cannot contain a colon. Therefore even if we find an upstream mark, any prefix with a colon must mean that the upstream mark we found is actually a pathname, and should be disregarded completely. This patch implements that logic. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8cd4249 commit 3f6eb30

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

sha1_name.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,9 @@ static int interpret_upstream_mark(const char *name, int namelen,
10951095
if (!len)
10961096
return -1;
10971097

1098+
if (memchr(name, ':', at))
1099+
return -1;
1100+
10981101
set_shortened_ref(buf, get_upstream_branch(name, at));
10991102
return len + at;
11001103
}

t/t1507-rev-parse-upstream.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,20 @@ test_expect_success 'log -g other@{u}@{now}' '
210210
test_cmp expect actual
211211
'
212212

213+
test_expect_success '@{reflog}-parsing does not look beyond colon' '
214+
echo content >@{yesterday} &&
215+
git add @{yesterday} &&
216+
git commit -m "funny reflog file" &&
217+
git hash-object @{yesterday} >expect &&
218+
git rev-parse HEAD:@{yesterday} >actual
219+
'
220+
221+
test_expect_success '@{upstream}-parsing does not look beyond colon' '
222+
echo content >@{upstream} &&
223+
git add @{upstream} &&
224+
git commit -m "funny upstream file" &&
225+
git hash-object @{upstream} >expect &&
226+
git rev-parse HEAD:@{upstream} >actual
227+
'
228+
213229
test_done

0 commit comments

Comments
 (0)