Skip to content

Commit a356e8e

Browse files
peffgitster
authored andcommitted
t3204: test git-branch @-expansion corner cases
git-branch feeds the branch names from the command line to strbuf_branchname(), but we do not yet tell that function which kinds of expansions should be allowed. Let's create a set of tests that cover both the allowed and disallowed cases. That shows off some breakages where we currently create or delete the wrong ref (and will make sure that we do not break any cases that _should_ be working when we do add more restrictions). Note that we check branch creation and deletion, but do not bother with renames. Those follow the same code path as creation. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0e9f62d commit a356e8e

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

t/t3204-branch-name-interpretation.sh

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/bin/sh
2+
3+
test_description='interpreting exotic branch name arguments
4+
5+
Branch name arguments are usually names which are taken to be inside of
6+
refs/heads/, but we interpret some magic syntax like @{-1}, @{upstream}, etc.
7+
This script aims to check the behavior of those corner cases.
8+
'
9+
. ./test-lib.sh
10+
11+
expect_branch() {
12+
git log -1 --format=%s "$1" >actual &&
13+
echo "$2" >expect &&
14+
test_cmp expect actual
15+
}
16+
17+
expect_deleted() {
18+
test_must_fail git rev-parse --verify "$1"
19+
}
20+
21+
test_expect_success 'set up repo' '
22+
test_commit one &&
23+
test_commit two &&
24+
git remote add origin foo.git
25+
'
26+
27+
test_expect_success 'update branch via @{-1}' '
28+
git branch previous one &&
29+
30+
git checkout previous &&
31+
git checkout master &&
32+
33+
git branch -f @{-1} two &&
34+
expect_branch previous two
35+
'
36+
37+
test_expect_success 'update branch via local @{upstream}' '
38+
git branch local one &&
39+
git branch --set-upstream-to=local &&
40+
41+
git branch -f @{upstream} two &&
42+
expect_branch local two
43+
'
44+
45+
test_expect_failure 'disallow updating branch via remote @{upstream}' '
46+
git update-ref refs/remotes/origin/remote one &&
47+
git branch --set-upstream-to=origin/remote &&
48+
49+
test_must_fail git branch -f @{upstream} two
50+
'
51+
52+
test_expect_success 'create branch with pseudo-qualified name' '
53+
git branch refs/heads/qualified two &&
54+
expect_branch refs/heads/refs/heads/qualified two
55+
'
56+
57+
test_expect_success 'delete branch via @{-1}' '
58+
git branch previous-del &&
59+
60+
git checkout previous-del &&
61+
git checkout master &&
62+
63+
git branch -D @{-1} &&
64+
expect_deleted previous-del
65+
'
66+
67+
test_expect_success 'delete branch via local @{upstream}' '
68+
git branch local-del &&
69+
git branch --set-upstream-to=local-del &&
70+
71+
git branch -D @{upstream} &&
72+
expect_deleted local-del
73+
'
74+
75+
test_expect_success 'delete branch via remote @{upstream}' '
76+
git update-ref refs/remotes/origin/remote-del two &&
77+
git branch --set-upstream-to=origin/remote-del &&
78+
79+
git branch -r -D @{upstream} &&
80+
expect_deleted origin/remote-del
81+
'
82+
83+
# Note that we create two oddly named local branches here. We want to make
84+
# sure that we do not accidentally delete either of them, even if
85+
# shorten_unambiguous_ref() tweaks the name to avoid ambiguity.
86+
test_expect_failure 'delete @{upstream} expansion matches -r option' '
87+
git update-ref refs/remotes/origin/remote-del two &&
88+
git branch --set-upstream-to=origin/remote-del &&
89+
git update-ref refs/heads/origin/remote-del two &&
90+
git update-ref refs/heads/remotes/origin/remote-del two &&
91+
92+
test_must_fail git branch -D @{upstream} &&
93+
expect_branch refs/heads/origin/remote-del two &&
94+
expect_branch refs/heads/remotes/origin/remote-del two
95+
'
96+
97+
test_expect_failure 'disallow deleting remote branch via @{-1}' '
98+
git update-ref refs/remotes/origin/previous one &&
99+
100+
git checkout -b origin/previous two &&
101+
git checkout master &&
102+
103+
test_must_fail git branch -r -D @{-1} &&
104+
expect_branch refs/remotes/origin/previous one &&
105+
expect_branch refs/heads/origin/previous two
106+
'
107+
108+
# The thing we are testing here is that "@" is the real branch refs/heads/@,
109+
# and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a
110+
# sane thing, but it _is_ technically allowed for now. If we disallow it, these
111+
# can be switched to test_must_fail.
112+
test_expect_failure 'create branch named "@"' '
113+
git branch -f @ one &&
114+
expect_branch refs/heads/@ one
115+
'
116+
117+
test_expect_failure 'delete branch named "@"' '
118+
git update-ref refs/heads/@ two &&
119+
git branch -D @ &&
120+
expect_deleted refs/heads/@
121+
'
122+
123+
test_done

0 commit comments

Comments
 (0)